OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
10 | 10 |
11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
13 // Declare the OS-specific types ahead of defining the generic class. | 13 // Declare the OS-specific types ahead of defining the generic class. |
14 #if defined(TARGET_OS_ANDROID) | 14 #if defined(TARGET_OS_ANDROID) |
15 #include "bin/socket_android.h" | 15 #include "bin/socket_android.h" |
16 #elif defined(TARGET_OS_LINUX) | 16 #elif defined(TARGET_OS_LINUX) |
17 #include "bin/socket_linux.h" | 17 #include "bin/socket_linux.h" |
18 #elif defined(TARGET_OS_MACOS) | 18 #elif defined(TARGET_OS_MACOS) |
19 #include "bin/socket_macos.h" | 19 #include "bin/socket_macos.h" |
20 #elif defined(TARGET_OS_WINDOWS) | 20 #elif defined(TARGET_OS_WINDOWS) |
21 #include "bin/socket_win.h" | 21 #include "bin/socket_win.h" |
22 #else | 22 #else |
23 #error Unknown target os. | 23 #error Unknown target os. |
24 #endif | 24 #endif |
25 | 25 |
26 class SocketAddress { | |
27 public: | |
28 enum { | |
29 TYPE_ANY = -1, | |
30 TYPE_IPV4, | |
31 TYPE_IPV6, | |
32 }; | |
33 | |
34 explicit SocketAddress(struct addrinfo* addrinfo); | |
35 | |
36 int GetType() { | |
37 if (addr_.ss_family == AF_INET6) return TYPE_IPV6; | |
38 return TYPE_IPV4; | |
39 } | |
40 | |
41 const char* as_string() const { return as_string_; } | |
42 const sockaddr_storage& addr() const { return addr_; } | |
43 | |
44 static intptr_t GetAddrLength(const sockaddr_storage& addr) { | |
45 return addr.ss_family == AF_INET6 ? | |
46 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | |
47 } | |
48 | |
49 static int16_t FromType(int type) { | |
50 if (type == TYPE_ANY) return AF_UNSPEC; | |
51 if (type == TYPE_IPV4) return AF_INET; | |
52 ASSERT(type == TYPE_IPV6 && "Invalid type"); | |
53 return AF_INET6; | |
54 } | |
55 | |
56 static void SetAddrPort(struct sockaddr_storage* addr, intptr_t port) { | |
57 sock_addr_ *sock_addr = reinterpret_cast<sock_addr_*>(addr); | |
58 if (addr->ss_family == AF_INET) { | |
59 sock_addr->in.sin_port = htons(port); | |
60 } else { | |
61 sock_addr->in6.sin6_port = htons(port); | |
62 } | |
63 } | |
64 | |
65 static intptr_t GetAddrPort(struct sockaddr_storage* addr) { | |
66 sock_addr_ *sock_addr = reinterpret_cast<sock_addr_*>(addr); | |
67 if (addr->ss_family == AF_INET) { | |
68 return ntohs(sock_addr->in.sin_port); | |
69 } else { | |
70 return ntohs(sock_addr->in6.sin6_port); | |
71 } | |
72 } | |
73 | |
74 static struct sockaddr* GetAsSockAddr(struct sockaddr_storage* addr) { | |
75 sock_addr_ *sock_addr = reinterpret_cast<sock_addr_*>(addr); | |
76 return reinterpret_cast<sockaddr*>(sock_addr); | |
77 } | |
78 | |
79 static struct sockaddr_in* GetAsSockAddrIn(struct sockaddr* addr) { | |
80 sock_addr_ *sock_addr = reinterpret_cast<sock_addr_*>(addr); | |
81 return reinterpret_cast<sockaddr_in*>(sock_addr); | |
82 } | |
83 | |
84 static struct sockaddr_in6* GetAsSockAddrIn6(struct sockaddr* addr) { | |
85 sock_addr_ *sock_addr = reinterpret_cast<sock_addr_*>(addr); | |
86 return reinterpret_cast<sockaddr_in6*>(sock_addr); | |
87 } | |
88 | |
89 private: | |
90 union sock_addr_ { | |
91 struct sockaddr_storage storage; | |
92 struct sockaddr_in in; | |
93 struct sockaddr_in6 in6; | |
94 struct sockaddr addr; | |
95 char raw[sizeof(sockaddr_storage)]; | |
96 }; | |
97 | |
98 char as_string_[INET6_ADDRSTRLEN]; | |
99 sockaddr_storage addr_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | |
102 }; | |
103 | |
104 class SocketAddresses { | |
105 public: | |
106 explicit SocketAddresses(intptr_t count) | |
107 : count_(count), | |
108 addresses_(new SocketAddress*[count_]) {} | |
109 | |
110 ~SocketAddresses() { | |
111 for (intptr_t i = 0; i < count_; i++) { | |
112 delete addresses_[i]; | |
113 } | |
114 delete[] addresses_; | |
115 } | |
116 | |
117 intptr_t count() const { return count_; } | |
118 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; } | |
119 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; } | |
120 | |
121 private: | |
122 const intptr_t count_; | |
123 SocketAddress** addresses_; | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(SocketAddresses); | |
126 }; | |
127 | 26 |
128 class Socket { | 27 class Socket { |
129 public: | 28 public: |
130 enum SocketRequest { | 29 enum SocketRequest { |
131 kLookupRequest = 0, | 30 kLookupRequest = 0, |
132 }; | 31 }; |
133 | 32 |
134 static bool Initialize(); | 33 static bool Initialize(); |
135 static intptr_t Available(intptr_t fd); | 34 static intptr_t Available(intptr_t fd); |
136 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); | 35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); |
137 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); | 36 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); |
138 static intptr_t CreateConnect(sockaddr_storage addr, | 37 static intptr_t CreateConnect(const char* host, const intptr_t port); |
139 const intptr_t port); | |
140 static intptr_t GetPort(intptr_t fd); | 38 static intptr_t GetPort(intptr_t fd); |
141 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); | 39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); |
142 static void GetError(intptr_t fd, OSError* os_error); | 40 static void GetError(intptr_t fd, OSError* os_error); |
143 static int GetType(intptr_t fd); | 41 static int GetType(intptr_t fd); |
144 static intptr_t GetStdioHandle(int num); | 42 static intptr_t GetStdioHandle(int num); |
145 static void Close(intptr_t fd); | 43 static void Close(intptr_t fd); |
146 static bool SetNonBlocking(intptr_t fd); | 44 static bool SetNonBlocking(intptr_t fd); |
147 static bool SetBlocking(intptr_t fd); | 45 static bool SetBlocking(intptr_t fd); |
148 static bool SetNoDelay(intptr_t fd, bool enabled); | 46 static bool SetNoDelay(intptr_t fd, bool enabled); |
149 | 47 |
150 // Perform a hostname lookup. Returns the SocketAddresses. | 48 // Perform a IPv4 hostname lookup. Returns the hostname string in |
151 static SocketAddresses* LookupAddress(const char* host, | 49 // IPv4 dotted-decimal format. |
152 int type, | 50 static const char* LookupIPv4Address(char* host, OSError** os_error); |
153 OSError** os_error); | |
154 | 51 |
155 static Dart_Port GetServicePort(); | 52 static Dart_Port GetServicePort(); |
156 | 53 |
157 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); | 54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); |
158 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); | 55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); |
159 | 56 |
160 private: | 57 private: |
161 static dart::Mutex mutex_; | 58 static dart::Mutex mutex_; |
162 static int service_ports_size_; | 59 static int service_ports_size_; |
163 static Dart_Port* service_ports_; | 60 static Dart_Port* service_ports_; |
164 static int service_ports_index_; | 61 static int service_ports_index_; |
165 | 62 |
166 DISALLOW_ALLOCATION(); | 63 DISALLOW_ALLOCATION(); |
167 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); | 64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); |
168 }; | 65 }; |
169 | 66 |
170 | 67 |
171 class ServerSocket { | 68 class ServerSocket { |
172 public: | 69 public: |
173 static const intptr_t kTemporaryFailure = -2; | 70 static const intptr_t kTemporaryFailure = -2; |
174 | 71 |
175 static intptr_t Accept(intptr_t fd); | 72 static intptr_t Accept(intptr_t fd); |
176 | 73 |
177 // Returns a positive integer if the call is successful. In case of failure | 74 // Returns a positive integer if the call is successful. In case of failure |
178 // it returns: | 75 // it returns: |
179 // | 76 // |
180 // -1: system error (errno set) | 77 // -1: system error (errno set) |
181 // -5: invalid bindAddress | 78 // -5: invalid bindAddress |
182 static intptr_t CreateBindListen(sockaddr_storage addr, | 79 static intptr_t CreateBindListen(const char* bindAddress, |
183 intptr_t port, | 80 intptr_t port, |
184 intptr_t backlog); | 81 intptr_t backlog); |
185 | 82 |
186 DISALLOW_ALLOCATION(); | 83 DISALLOW_ALLOCATION(); |
187 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
188 }; | 85 }; |
189 | 86 |
190 #endif // BIN_SOCKET_H_ | 87 #endif // BIN_SOCKET_H_ |
OLD | NEW |