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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(TARGET_OS_MACOS) | 8 #if defined(TARGET_OS_MACOS) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
11 #include "bin/socket_macos.h" | 11 #include "bin/socket_macos.h" |
12 | 12 |
13 #include <errno.h> // NOLINT | 13 #include <errno.h> // NOLINT |
14 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
15 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
16 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
17 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
18 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
19 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
20 #include <sys/stat.h> // NOLINT | 20 #include <sys/stat.h> // NOLINT |
21 #include <unistd.h> // NOLINT | 21 #include <unistd.h> // NOLINT |
22 | 22 |
23 #include "bin/fdutils.h" | 23 #include "bin/fdutils.h" |
24 #include "bin/file.h" | 24 #include "bin/file.h" |
25 #include "platform/signal_blocker.h" | 25 #include "platform/signal_blocker.h" |
26 | 26 |
27 namespace dart { | 27 namespace dart { |
28 namespace bin { | 28 namespace bin { |
29 | 29 |
| 30 static void SaveErrorAndClose(intptr_t fd) { |
| 31 int err = errno; |
| 32 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 33 errno = err; |
| 34 } |
| 35 |
| 36 |
30 SocketAddress::SocketAddress(struct sockaddr* sa) { | 37 SocketAddress::SocketAddress(struct sockaddr* sa) { |
31 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 38 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
32 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, | 39 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
33 INET6_ADDRSTRLEN)) { | 40 INET6_ADDRSTRLEN)) { |
34 as_string_[0] = 0; | 41 as_string_[0] = 0; |
35 } | 42 } |
36 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 43 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
37 memmove(reinterpret_cast<void*>(&addr_), sa, salen); | 44 memmove(reinterpret_cast<void*>(&addr_), sa, salen); |
38 } | 45 } |
39 | 46 |
(...skipping 10 matching lines...) Expand all Loading... |
50 return true; | 57 return true; |
51 } | 58 } |
52 | 59 |
53 | 60 |
54 static intptr_t Create(const RawAddr& addr) { | 61 static intptr_t Create(const RawAddr& addr) { |
55 intptr_t fd; | 62 intptr_t fd; |
56 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 63 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
57 if (fd < 0) { | 64 if (fd < 0) { |
58 return -1; | 65 return -1; |
59 } | 66 } |
60 FDUtils::SetCloseOnExec(fd); | 67 if (!FDUtils::SetCloseOnExec(fd)) { |
61 FDUtils::SetNonBlocking(fd); | 68 SaveErrorAndClose(fd); |
| 69 return -1; |
| 70 } |
| 71 if (!FDUtils::SetNonBlocking(fd)) { |
| 72 SaveErrorAndClose(fd); |
| 73 return -1; |
| 74 } |
62 return fd; | 75 return fd; |
63 } | 76 } |
64 | 77 |
65 | 78 |
66 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 79 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
67 intptr_t result = TEMP_FAILURE_RETRY( | 80 intptr_t result = TEMP_FAILURE_RETRY( |
68 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 81 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
69 if ((result == 0) || (errno == EINPROGRESS)) { | 82 if ((result == 0) || (errno == EINPROGRESS)) { |
70 return fd; | 83 return fd; |
71 } | 84 } |
72 VOID_TEMP_FAILURE_RETRY(close(fd)); | 85 SaveErrorAndClose(fd); |
73 return -1; | 86 return -1; |
74 } | 87 } |
75 | 88 |
76 | 89 |
77 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 90 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
78 intptr_t fd = Create(addr); | 91 intptr_t fd = Create(addr); |
79 if (fd < 0) { | 92 if (fd < 0) { |
80 return fd; | 93 return fd; |
81 } | 94 } |
82 | 95 |
83 return Connect(fd, addr); | 96 return Connect(fd, addr); |
84 } | 97 } |
85 | 98 |
86 | 99 |
87 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 100 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
88 const RawAddr& source_addr) { | 101 const RawAddr& source_addr) { |
89 intptr_t fd = Create(addr); | 102 intptr_t fd = Create(addr); |
90 if (fd < 0) { | 103 if (fd < 0) { |
91 return fd; | 104 return fd; |
92 } | 105 } |
93 | 106 |
94 intptr_t result = TEMP_FAILURE_RETRY( | 107 intptr_t result = TEMP_FAILURE_RETRY( |
95 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 108 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
96 if ((result != 0) && (errno != EINPROGRESS)) { | 109 if ((result != 0) && (errno != EINPROGRESS)) { |
97 VOID_TEMP_FAILURE_RETRY(close(fd)); | 110 SaveErrorAndClose(fd); |
98 return -1; | 111 return -1; |
99 } | 112 } |
100 | 113 |
101 return Connect(fd, addr); | 114 return Connect(fd, addr); |
102 } | 115 } |
103 | 116 |
104 | 117 |
105 bool Socket::IsBindError(intptr_t error_number) { | 118 bool Socket::IsBindError(intptr_t error_number) { |
106 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 119 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
107 error_number == EINVAL; | 120 error_number == EINVAL; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 310 |
298 | 311 |
299 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 312 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
300 intptr_t fd; | 313 intptr_t fd; |
301 | 314 |
302 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 315 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
303 if (fd < 0) { | 316 if (fd < 0) { |
304 return -1; | 317 return -1; |
305 } | 318 } |
306 | 319 |
307 FDUtils::SetCloseOnExec(fd); | 320 if (!FDUtils::SetCloseOnExec(fd)) { |
| 321 SaveErrorAndClose(fd); |
| 322 return -1; |
| 323 } |
308 | 324 |
309 if (reuseAddress) { | 325 if (reuseAddress) { |
310 int optval = 1; | 326 int optval = 1; |
311 VOID_NO_RETRY_EXPECTED( | 327 VOID_NO_RETRY_EXPECTED( |
312 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 328 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
313 } | 329 } |
314 | 330 |
315 if (NO_RETRY_EXPECTED( | 331 if (NO_RETRY_EXPECTED( |
316 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 332 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
317 VOID_TEMP_FAILURE_RETRY(close(fd)); | 333 SaveErrorAndClose(fd); |
318 return -1; | 334 return -1; |
319 } | 335 } |
320 | 336 |
321 FDUtils::SetNonBlocking(fd); | 337 if (!FDUtils::SetNonBlocking(fd)) { |
| 338 SaveErrorAndClose(fd); |
| 339 return -1; |
| 340 } |
322 return fd; | 341 return fd; |
323 } | 342 } |
324 | 343 |
325 | 344 |
326 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 345 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
327 if (ifa->ifa_addr == NULL) { | 346 if (ifa->ifa_addr == NULL) { |
328 // OpenVPN's virtual device tun0. | 347 // OpenVPN's virtual device tun0. |
329 return false; | 348 return false; |
330 } | 349 } |
331 int family = ifa->ifa_addr->sa_family; | 350 int family = ifa->ifa_addr->sa_family; |
(...skipping 18 matching lines...) Expand all Loading... |
350 ASSERT(*os_error == NULL); | 369 ASSERT(*os_error == NULL); |
351 *os_error = | 370 *os_error = |
352 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 371 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
353 return NULL; | 372 return NULL; |
354 } | 373 } |
355 | 374 |
356 int lookup_family = SocketAddress::FromType(type); | 375 int lookup_family = SocketAddress::FromType(type); |
357 | 376 |
358 intptr_t count = 0; | 377 intptr_t count = 0; |
359 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 378 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
360 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++; | 379 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
| 380 count++; |
| 381 } |
361 } | 382 } |
362 | 383 |
363 AddressList<InterfaceSocketAddress>* addresses = | 384 AddressList<InterfaceSocketAddress>* addresses = |
364 new AddressList<InterfaceSocketAddress>(count); | 385 new AddressList<InterfaceSocketAddress>(count); |
365 int i = 0; | 386 int i = 0; |
366 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 387 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
367 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { | 388 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
368 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); | 389 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); |
369 addresses->SetAt( | 390 addresses->SetAt( |
370 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name, | 391 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name, |
371 if_nametoindex(ifa->ifa_name))); | 392 if_nametoindex(ifa->ifa_name))); |
372 i++; | 393 i++; |
373 } | 394 } |
374 } | 395 } |
375 freeifaddrs(ifaddr); | 396 freeifaddrs(ifaddr); |
376 return addresses; | 397 return addresses; |
377 } | 398 } |
378 | 399 |
379 | 400 |
380 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 401 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
381 intptr_t backlog, | 402 intptr_t backlog, |
382 bool v6_only) { | 403 bool v6_only) { |
383 intptr_t fd; | 404 intptr_t fd; |
384 | 405 |
385 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 406 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
386 if (fd < 0) { | 407 if (fd < 0) { |
387 return -1; | 408 return -1; |
388 } | 409 } |
389 | 410 |
390 FDUtils::SetCloseOnExec(fd); | 411 if (!FDUtils::SetCloseOnExec(fd)) { |
| 412 SaveErrorAndClose(fd); |
| 413 return -1; |
| 414 } |
391 | 415 |
392 int optval = 1; | 416 int optval = 1; |
393 VOID_NO_RETRY_EXPECTED( | 417 VOID_NO_RETRY_EXPECTED( |
394 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 418 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
395 | 419 |
396 if (addr.ss.ss_family == AF_INET6) { | 420 if (addr.ss.ss_family == AF_INET6) { |
397 optval = v6_only ? 1 : 0; | 421 optval = v6_only ? 1 : 0; |
398 VOID_NO_RETRY_EXPECTED( | 422 VOID_NO_RETRY_EXPECTED( |
399 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 423 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
400 } | 424 } |
401 | 425 |
402 if (NO_RETRY_EXPECTED( | 426 if (NO_RETRY_EXPECTED( |
403 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 427 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
404 VOID_TEMP_FAILURE_RETRY(close(fd)); | 428 SaveErrorAndClose(fd); |
405 return -1; | 429 return -1; |
406 } | 430 } |
407 | 431 |
408 // Test for invalid socket port 65535 (some browsers disallow it). | 432 // Test for invalid socket port 65535 (some browsers disallow it). |
409 if ((SocketAddress::GetAddrPort(addr) == 0) && | 433 if ((SocketAddress::GetAddrPort(addr) == 0) && |
410 (Socket::GetPort(fd) == 65535)) { | 434 (Socket::GetPort(fd) == 65535)) { |
411 // Don't close the socket until we have created a new socket, ensuring | 435 // Don't close the socket until we have created a new socket, ensuring |
412 // that we do not get the bad port number again. | 436 // that we do not get the bad port number again. |
413 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 437 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
414 int err = errno; | 438 SaveErrorAndClose(fd); |
415 VOID_TEMP_FAILURE_RETRY(close(fd)); | |
416 errno = err; | |
417 return new_fd; | 439 return new_fd; |
418 } | 440 } |
419 | 441 |
420 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 442 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
421 VOID_TEMP_FAILURE_RETRY(close(fd)); | 443 SaveErrorAndClose(fd); |
422 return -1; | 444 return -1; |
423 } | 445 } |
424 | 446 |
425 FDUtils::SetNonBlocking(fd); | 447 if (!FDUtils::SetNonBlocking(fd)) { |
| 448 SaveErrorAndClose(fd); |
| 449 return -1; |
| 450 } |
426 return fd; | 451 return fd; |
427 } | 452 } |
428 | 453 |
429 | 454 |
430 bool ServerSocket::StartAccept(intptr_t fd) { | 455 bool ServerSocket::StartAccept(intptr_t fd) { |
431 USE(fd); | 456 USE(fd); |
432 return true; | 457 return true; |
433 } | 458 } |
434 | 459 |
435 | 460 |
436 intptr_t ServerSocket::Accept(intptr_t fd) { | 461 intptr_t ServerSocket::Accept(intptr_t fd) { |
437 intptr_t socket; | 462 intptr_t socket; |
438 struct sockaddr clientaddr; | 463 struct sockaddr clientaddr; |
439 socklen_t addrlen = sizeof(clientaddr); | 464 socklen_t addrlen = sizeof(clientaddr); |
440 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 465 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
441 if (socket == -1) { | 466 if (socket == -1) { |
442 if (errno == EAGAIN) { | 467 if (errno == EAGAIN) { |
443 // We need to signal to the caller that this is actually not an | 468 // We need to signal to the caller that this is actually not an |
444 // error. We got woken up from the poll on the listening socket, | 469 // error. We got woken up from the poll on the listening socket, |
445 // but there is no connection ready to be accepted. | 470 // but there is no connection ready to be accepted. |
446 ASSERT(kTemporaryFailure != -1); | 471 ASSERT(kTemporaryFailure != -1); |
447 socket = kTemporaryFailure; | 472 socket = kTemporaryFailure; |
448 } | 473 } |
449 } else { | 474 } else { |
450 FDUtils::SetCloseOnExec(socket); | 475 if (!FDUtils::SetCloseOnExec(socket)) { |
451 FDUtils::SetNonBlocking(socket); | 476 SaveErrorAndClose(socket); |
| 477 return -1; |
| 478 } |
| 479 if (!FDUtils::SetNonBlocking(socket)) { |
| 480 SaveErrorAndClose(socket); |
| 481 return -1; |
| 482 } |
452 } | 483 } |
453 return socket; | 484 return socket; |
454 } | 485 } |
455 | 486 |
456 | 487 |
457 void Socket::Close(intptr_t fd) { | 488 void Socket::Close(intptr_t fd) { |
458 ASSERT(fd >= 0); | 489 ASSERT(fd >= 0); |
459 VOID_TEMP_FAILURE_RETRY(close(fd)); | 490 VOID_TEMP_FAILURE_RETRY(close(fd)); |
460 } | 491 } |
461 | 492 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 int interfaceIndex) { | 631 int interfaceIndex) { |
601 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); | 632 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); |
602 } | 633 } |
603 | 634 |
604 } // namespace bin | 635 } // namespace bin |
605 } // namespace dart | 636 } // namespace dart |
606 | 637 |
607 #endif // defined(TARGET_OS_MACOS) | 638 #endif // defined(TARGET_OS_MACOS) |
608 | 639 |
609 #endif // !defined(DART_IO_DISABLED) | 640 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |