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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
11 #include <string.h> // NOLINT | 11 #include <string.h> // NOLINT |
12 #include <sys/stat.h> // NOLINT | 12 #include <sys/stat.h> // NOLINT |
13 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
14 #include <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
15 | 15 |
16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
17 #include "bin/file.h" | 17 #include "bin/file.h" |
18 #include "bin/log.h" | 18 #include "bin/log.h" |
19 #include "bin/socket.h" | 19 #include "bin/socket.h" |
20 | 20 |
21 | 21 |
| 22 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { |
| 23 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 24 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); |
| 25 const char* result = inet_ntop(addrinfo->ai_family, |
| 26 &raw->in.sin_addr, |
| 27 as_string_, |
| 28 INET6_ADDRSTRLEN); |
| 29 if (result == NULL) as_string_[0] = 0; |
| 30 memmove(reinterpret_cast<void *>(&addr_), |
| 31 addrinfo->ai_addr, |
| 32 addrinfo->ai_addrlen); |
| 33 } |
| 34 |
| 35 |
22 bool Socket::Initialize() { | 36 bool Socket::Initialize() { |
23 // Nothing to do on Mac OS. | 37 // Nothing to do on Mac OS. |
24 return true; | 38 return true; |
25 } | 39 } |
26 | 40 |
27 | 41 |
28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 42 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { |
29 intptr_t fd; | 43 intptr_t fd; |
30 struct hostent* server; | |
31 struct sockaddr_in server_address; | |
32 | 44 |
33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 45 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
34 if (fd < 0) { | 46 if (fd < 0) { |
35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 47 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
36 return -1; | 48 return -1; |
37 } | 49 } |
38 | 50 |
39 FDUtils::SetCloseOnExec(fd); | 51 FDUtils::SetCloseOnExec(fd); |
40 Socket::SetNonBlocking(fd); | 52 Socket::SetNonBlocking(fd); |
41 | 53 |
42 server = gethostbyname(host); | 54 SocketAddress::SetAddrPort(&addr, port); |
43 if (server == NULL) { | |
44 VOID_TEMP_FAILURE_RETRY(close(fd)); | |
45 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | |
46 return -1; | |
47 } | |
48 | |
49 server_address.sin_family = AF_INET; | |
50 server_address.sin_port = htons(port); | |
51 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); | |
52 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | |
53 intptr_t result = TEMP_FAILURE_RETRY( | 55 intptr_t result = TEMP_FAILURE_RETRY( |
54 connect(fd, | 56 connect(fd, |
55 reinterpret_cast<struct sockaddr *>(&server_address), | 57 &addr.addr, |
56 sizeof(server_address))); | 58 SocketAddress::GetAddrLength(addr))); |
57 if (result == 0 || errno == EINPROGRESS) { | 59 if (result == 0 || errno == EINPROGRESS) { |
58 return fd; | 60 return fd; |
59 } | 61 } |
| 62 TEMP_FAILURE_RETRY(close(fd)); |
60 return -1; | 63 return -1; |
61 } | 64 } |
62 | 65 |
63 | 66 |
64 intptr_t Socket::Available(intptr_t fd) { | 67 intptr_t Socket::Available(intptr_t fd) { |
65 return FDUtils::AvailableBytes(fd); | 68 return FDUtils::AvailableBytes(fd); |
66 } | 69 } |
67 | 70 |
68 | 71 |
69 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 72 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
(...skipping 17 matching lines...) Expand all Loading... |
87 // If the would block we need to retry and therefore return 0 as | 90 // If the would block we need to retry and therefore return 0 as |
88 // the number of bytes written. | 91 // the number of bytes written. |
89 written_bytes = 0; | 92 written_bytes = 0; |
90 } | 93 } |
91 return written_bytes; | 94 return written_bytes; |
92 } | 95 } |
93 | 96 |
94 | 97 |
95 intptr_t Socket::GetPort(intptr_t fd) { | 98 intptr_t Socket::GetPort(intptr_t fd) { |
96 ASSERT(fd >= 0); | 99 ASSERT(fd >= 0); |
97 struct sockaddr_in socket_address; | 100 RawAddr raw; |
98 socklen_t size = sizeof(socket_address); | 101 socklen_t size = sizeof(raw); |
99 if (TEMP_FAILURE_RETRY( | 102 if (TEMP_FAILURE_RETRY( |
100 getsockname(fd, | 103 getsockname(fd, |
101 reinterpret_cast<struct sockaddr *>(&socket_address), | 104 &raw.addr, |
102 &size))) { | 105 &size))) { |
103 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 106 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
104 return 0; | 107 return 0; |
105 } | 108 } |
106 return ntohs(socket_address.sin_port); | 109 return SocketAddress::GetAddrPort(&raw); |
107 } | 110 } |
108 | 111 |
109 | 112 |
110 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 113 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
111 ASSERT(fd >= 0); | 114 ASSERT(fd >= 0); |
112 struct sockaddr_in socket_address; | 115 RawAddr raw; |
113 socklen_t size = sizeof(socket_address); | 116 socklen_t size = sizeof(raw); |
114 if (TEMP_FAILURE_RETRY( | 117 if (TEMP_FAILURE_RETRY( |
115 getpeername(fd, | 118 getpeername(fd, |
116 reinterpret_cast<struct sockaddr *>(&socket_address), | 119 &raw.addr, |
117 &size))) { | 120 &size))) { |
118 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 121 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
119 return false; | 122 return false; |
120 } | 123 } |
121 if (inet_ntop(socket_address.sin_family, | 124 const void* src; |
122 reinterpret_cast<const void *>(&socket_address.sin_addr), | 125 if (raw.ss_family == AF_INET6) { |
| 126 src = reinterpret_cast<const void*>(&raw.in6.sin6_addr); |
| 127 } else { |
| 128 src = reinterpret_cast<const void*>(&raw.in.sin_addr); |
| 129 } |
| 130 if (inet_ntop(raw.ss_family, |
| 131 src, |
123 host, | 132 host, |
124 INET_ADDRSTRLEN) == NULL) { | 133 INET_ADDRSTRLEN) == NULL) { |
125 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 134 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
126 return false; | 135 return false; |
127 } | 136 } |
128 *port = ntohs(socket_address.sin_port); | 137 *port = SocketAddress::GetAddrPort(&raw); |
129 return true; | 138 return true; |
130 } | 139 } |
131 | 140 |
132 | 141 |
133 void Socket::GetError(intptr_t fd, OSError* os_error) { | 142 void Socket::GetError(intptr_t fd, OSError* os_error) { |
134 int len = sizeof(errno); | 143 int len = sizeof(errno); |
135 getsockopt(fd, | 144 getsockopt(fd, |
136 SOL_SOCKET, | 145 SOL_SOCKET, |
137 SO_ERROR, | 146 SO_ERROR, |
138 &errno, | 147 &errno, |
(...skipping 11 matching lines...) Expand all Loading... |
150 if (S_ISREG(buf.st_mode)) return File::kFile; | 159 if (S_ISREG(buf.st_mode)) return File::kFile; |
151 return File::kOther; | 160 return File::kOther; |
152 } | 161 } |
153 | 162 |
154 | 163 |
155 intptr_t Socket::GetStdioHandle(int num) { | 164 intptr_t Socket::GetStdioHandle(int num) { |
156 return static_cast<intptr_t>(num); | 165 return static_cast<intptr_t>(num); |
157 } | 166 } |
158 | 167 |
159 | 168 |
160 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 169 SocketAddresses* Socket::LookupAddress(const char* host, |
161 // Perform a name lookup for an IPv4 address. | 170 int type, |
| 171 OSError** os_error) { |
| 172 // Perform a name lookup for a host name. |
162 struct addrinfo hints; | 173 struct addrinfo hints; |
163 memset(&hints, 0, sizeof(hints)); | 174 memset(&hints, 0, sizeof(hints)); |
164 hints.ai_family = AF_INET; | 175 hints.ai_family = SocketAddress::FromType(type); |
165 hints.ai_socktype = SOCK_STREAM; | 176 hints.ai_socktype = SOCK_STREAM; |
| 177 hints.ai_flags = 0; |
166 hints.ai_protocol = IPPROTO_TCP; | 178 hints.ai_protocol = IPPROTO_TCP; |
167 struct addrinfo* info = NULL; | 179 struct addrinfo* info = NULL; |
168 int status = getaddrinfo(host, 0, &hints, &info); | 180 int status = getaddrinfo(host, 0, &hints, &info); |
169 if (status != 0) { | 181 if (status != 0) { |
170 ASSERT(*os_error == NULL); | 182 ASSERT(*os_error == NULL); |
171 *os_error = new OSError(status, | 183 *os_error = new OSError(status, |
172 gai_strerror(status), | 184 gai_strerror(status), |
173 OSError::kGetAddressInfo); | 185 OSError::kGetAddressInfo); |
174 return NULL; | 186 return NULL; |
175 } | 187 } |
176 // Convert the address into IPv4 dotted decimal notation. | 188 intptr_t count = 0; |
177 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 189 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
178 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 190 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
179 const char* result = inet_ntop(AF_INET, | |
180 reinterpret_cast<void *>(&sockaddr->sin_addr), | |
181 buffer, | |
182 INET_ADDRSTRLEN); | |
183 if (result == NULL) { | |
184 free(buffer); | |
185 return NULL; | |
186 } | 191 } |
187 ASSERT(result == buffer); | 192 SocketAddresses* addresses = new SocketAddresses(count); |
188 return buffer; | 193 intptr_t i = 0; |
| 194 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 195 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 196 addresses->SetAt(i, new SocketAddress(c)); |
| 197 i++; |
| 198 } |
| 199 } |
| 200 freeaddrinfo(info); |
| 201 return addresses; |
189 } | 202 } |
190 | 203 |
191 | 204 |
192 intptr_t ServerSocket::CreateBindListen(const char* host, | 205 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
193 intptr_t port, | 206 intptr_t port, |
194 intptr_t backlog) { | 207 intptr_t backlog) { |
195 intptr_t fd; | 208 intptr_t fd; |
196 struct sockaddr_in server_address; | |
197 | 209 |
198 in_addr_t s_addr = inet_addr(host); | 210 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
199 if (s_addr == INADDR_NONE) { | |
200 return -5; | |
201 } | |
202 | |
203 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | |
204 if (fd < 0) return -1; | 211 if (fd < 0) return -1; |
205 | 212 |
206 FDUtils::SetCloseOnExec(fd); | 213 FDUtils::SetCloseOnExec(fd); |
207 | 214 |
208 int optval = 1; | 215 int optval = 1; |
209 VOID_TEMP_FAILURE_RETRY( | 216 VOID_TEMP_FAILURE_RETRY( |
210 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 217 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
211 | 218 |
212 server_address.sin_family = AF_INET; | 219 if (addr.ss_family == AF_INET6) { |
213 server_address.sin_port = htons(port); | 220 optval = 0; |
214 server_address.sin_addr.s_addr = s_addr; | 221 VOID_TEMP_FAILURE_RETRY( |
215 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 222 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 223 } |
216 | 224 |
| 225 SocketAddress::SetAddrPort(&addr, port); |
217 if (TEMP_FAILURE_RETRY( | 226 if (TEMP_FAILURE_RETRY( |
218 bind(fd, | 227 bind(fd, |
219 reinterpret_cast<struct sockaddr *>(&server_address), | 228 &addr.addr, |
220 sizeof(server_address))) < 0) { | 229 SocketAddress::GetAddrLength(addr))) < 0) { |
221 VOID_TEMP_FAILURE_RETRY(close(fd)); | 230 VOID_TEMP_FAILURE_RETRY(close(fd)); |
222 return -1; | 231 return -1; |
223 } | 232 } |
224 | 233 |
225 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 234 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
226 VOID_TEMP_FAILURE_RETRY(close(fd)); | 235 VOID_TEMP_FAILURE_RETRY(close(fd)); |
227 return -1; | 236 return -1; |
228 } | 237 } |
229 | 238 |
230 Socket::SetNonBlocking(fd); | 239 Socket::SetNonBlocking(fd); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 286 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
278 int on = enabled ? 1 : 0; | 287 int on = enabled ? 1 : 0; |
279 return TEMP_FAILURE_RETRY(setsockopt(fd, | 288 return TEMP_FAILURE_RETRY(setsockopt(fd, |
280 IPPROTO_TCP, | 289 IPPROTO_TCP, |
281 TCP_NODELAY, | 290 TCP_NODELAY, |
282 reinterpret_cast<char *>(&on), | 291 reinterpret_cast<char *>(&on), |
283 sizeof(on))) == 0; | 292 sizeof(on))) == 0; |
284 } | 293 } |
285 | 294 |
286 #endif // defined(TARGET_OS_MACOS) | 295 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |