Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: runtime/bin/socket_macos.cc

Issue 16514010: Add NetworkAdaptor to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add support for win, mac and android. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 namespace dart { 22 namespace dart {
23 namespace bin { 23 namespace bin {
24 24
25 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { 25 SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
27 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); 27 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr);
28 const char* result = inet_ntop(addrinfo->ai_family, 28 const char* result = inet_ntop(sockaddr->sa_family,
29 &raw->in.sin_addr, 29 &raw->in.sin_addr,
30 as_string_, 30 as_string_,
31 INET6_ADDRSTRLEN); 31 INET6_ADDRSTRLEN);
32 if (result == NULL) as_string_[0] = 0; 32 if (result == NULL) as_string_[0] = 0;
33 memmove(reinterpret_cast<void *>(&addr_), 33 memmove(reinterpret_cast<void *>(&addr_),
34 addrinfo->ai_addr, 34 sockaddr,
35 addrinfo->ai_addrlen); 35 GetAddrLength(raw));
36 } 36 }
37 37
38 38
39 bool Socket::Initialize() { 39 bool Socket::Initialize() {
40 // Nothing to do on Mac OS. 40 // Nothing to do on Mac OS.
41 return true; 41 return true;
42 } 42 }
43 43
44 44
45 intptr_t Socket::Create(RawAddr addr) { 45 intptr_t Socket::Create(RawAddr addr) {
46 intptr_t fd; 46 intptr_t fd;
47 47
48 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 48 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
49 if (fd < 0) { 49 if (fd < 0) {
50 Log::PrintErr("Error Create: %s\n", strerror(errno)); 50 Log::PrintErr("Error Create: %s\n", strerror(errno));
51 return -1; 51 return -1;
52 } 52 }
53 53
54 FDUtils::SetCloseOnExec(fd); 54 FDUtils::SetCloseOnExec(fd);
55 return fd; 55 return fd;
56 } 56 }
57 57
58 58
59 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { 59 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) {
60 SocketAddress::SetAddrPort(&addr, port); 60 SocketAddress::SetAddrPort(&addr, port);
61 intptr_t result = TEMP_FAILURE_RETRY( 61 intptr_t result = TEMP_FAILURE_RETRY(
62 connect(fd, 62 connect(fd,
63 &addr.addr, 63 &addr.addr,
64 SocketAddress::GetAddrLength(addr))); 64 SocketAddress::GetAddrLength(&addr)));
65 if (result == 0 || errno == EINPROGRESS) { 65 if (result == 0 || errno == EINPROGRESS) {
66 return fd; 66 return fd;
67 } 67 }
68 VOID_TEMP_FAILURE_RETRY(close(fd)); 68 VOID_TEMP_FAILURE_RETRY(close(fd));
69 return -1; 69 return -1;
70 } 70 }
71 71
72 72
73 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { 73 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) {
74 intptr_t fd = Socket::Create(addr); 74 intptr_t fd = Socket::Create(addr);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { 213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) {
214 addresses->SetAt(i, new SocketAddress(c)); 214 addresses->SetAt(i, new SocketAddress(c));
215 i++; 215 i++;
216 } 216 }
217 } 217 }
218 freeaddrinfo(info); 218 freeaddrinfo(info);
219 return addresses; 219 return addresses;
220 } 220 }
221 221
222 222
223 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
224 int family = ifa->ifa_addr->sa_family;
225 if (lookup_family == family) return true;
226 if (lookup_family == AF_UNSPEC &&
227 (family == AF_INET || family == AF_INET6)) {
228 return true;
229 }
230 return false;
231 }
232
233
234 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
235 int type,
236 OSError** os_error) {
237 struct ifaddrs* ifaddr;
238
239 int status = getifaddrs(&ifaddr);
240 if (status != 0) {
241 ASSERT(*os_error == NULL);
242 *os_error = new OSError(status,
243 gai_strerror(status),
244 OSError::kGetAddressInfo);
245 return NULL;
246 }
247
248 int lookup_family = SocketAddress::FromType(type);
249
250 intptr_t count = 0;
251 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
252 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++;
253 }
254
255 AddressList<InterfaceSocketAddress>* addresses =
256 new AddressList<InterfaceSocketAddress>(count);
257 int i = 0;
258 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
259 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) {
260 addresses->SetAt(i, new InterfaceSocketAddress(
261 ifa->ifa_addr, strdup(ifa->ifa_name)));
262 i++;
263 }
264 }
265 freeifaddrs(ifaddr);
266 return addresses;
267 }
268
269
223 intptr_t ServerSocket::CreateBindListen(RawAddr addr, 270 intptr_t ServerSocket::CreateBindListen(RawAddr addr,
224 intptr_t port, 271 intptr_t port,
225 intptr_t backlog, 272 intptr_t backlog,
226 bool v6_only) { 273 bool v6_only) {
227 intptr_t fd; 274 intptr_t fd;
228 275
229 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 276 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
230 if (fd < 0) return -1; 277 if (fd < 0) return -1;
231 278
232 FDUtils::SetCloseOnExec(fd); 279 FDUtils::SetCloseOnExec(fd);
233 280
234 int optval = 1; 281 int optval = 1;
235 VOID_TEMP_FAILURE_RETRY( 282 VOID_TEMP_FAILURE_RETRY(
236 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 283 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
237 284
238 if (addr.ss.ss_family == AF_INET6) { 285 if (addr.ss.ss_family == AF_INET6) {
239 optval = v6_only ? 1 : 0; 286 optval = v6_only ? 1 : 0;
240 VOID_TEMP_FAILURE_RETRY( 287 VOID_TEMP_FAILURE_RETRY(
241 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); 288 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
242 } 289 }
243 290
244 SocketAddress::SetAddrPort(&addr, port); 291 SocketAddress::SetAddrPort(&addr, port);
245 if (TEMP_FAILURE_RETRY( 292 if (TEMP_FAILURE_RETRY(
246 bind(fd, 293 bind(fd,
247 &addr.addr, 294 &addr.addr,
248 SocketAddress::GetAddrLength(addr))) < 0) { 295 SocketAddress::GetAddrLength(&addr))) < 0) {
249 VOID_TEMP_FAILURE_RETRY(close(fd)); 296 VOID_TEMP_FAILURE_RETRY(close(fd));
250 return -1; 297 return -1;
251 } 298 }
252 299
253 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 300 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
254 VOID_TEMP_FAILURE_RETRY(close(fd)); 301 VOID_TEMP_FAILURE_RETRY(close(fd));
255 return -1; 302 return -1;
256 } 303 }
257 304
258 Socket::SetNonBlocking(fd); 305 Socket::SetNonBlocking(fd);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 IPPROTO_TCP, 355 IPPROTO_TCP,
309 TCP_NODELAY, 356 TCP_NODELAY,
310 reinterpret_cast<char *>(&on), 357 reinterpret_cast<char *>(&on),
311 sizeof(on))) == 0; 358 sizeof(on))) == 0;
312 } 359 }
313 360
314 } // namespace bin 361 } // namespace bin
315 } // namespace dart 362 } // namespace dart
316 363
317 #endif // defined(TARGET_OS_MACOS) 364 #endif // defined(TARGET_OS_MACOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698