| 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 "bin/socket.h" | 8 #include "bin/socket.h" |
| 9 #include "bin/socket_macos.h" | 9 #include "bin/socket_macos.h" |
| 10 | 10 |
| 11 #include <errno.h> // NOLINT | 11 #include <errno.h> // NOLINT |
| 12 #include <ifaddrs.h> // NOLINT |
| 13 #include <net/if.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT |
| 12 #include <stdio.h> // NOLINT | 15 #include <stdio.h> // NOLINT |
| 13 #include <stdlib.h> // NOLINT | 16 #include <stdlib.h> // NOLINT |
| 14 #include <string.h> // NOLINT | 17 #include <string.h> // NOLINT |
| 15 #include <sys/stat.h> // NOLINT | 18 #include <sys/stat.h> // NOLINT |
| 16 #include <unistd.h> // NOLINT | 19 #include <unistd.h> // NOLINT |
| 17 #include <net/if.h> // NOLINT | |
| 18 #include <netinet/tcp.h> // NOLINT | |
| 19 #include <ifaddrs.h> // NOLINT | |
| 20 | 20 |
| 21 #include "bin/fdutils.h" | 21 #include "bin/fdutils.h" |
| 22 #include "bin/file.h" | 22 #include "bin/file.h" |
| 23 #include "platform/signal_blocker.h" | 23 #include "platform/signal_blocker.h" |
| 24 | 24 |
| 25 namespace dart { | 25 namespace dart { |
| 26 namespace bin { | 26 namespace bin { |
| 27 | 27 |
| 28 SocketAddress::SocketAddress(struct sockaddr* sa) { | 28 SocketAddress::SocketAddress(struct sockaddr* sa) { |
| 29 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 29 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 30 if (!Socket::FormatNumericAddress( | 30 if (!Socket::FormatNumericAddress( |
| 31 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 31 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { |
| 32 as_string_[0] = 0; | 32 as_string_[0] = 0; |
| 33 } | 33 } |
| 34 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 34 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
| 35 memmove(reinterpret_cast<void *>(&addr_), sa, salen); | 35 memmove(reinterpret_cast<void *>(&addr_), sa, salen); |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 39 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { |
| 40 socklen_t salen = SocketAddress::GetAddrLength(addr); | 40 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 41 if (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, | 41 return (NO_RETRY_EXPECTED(getnameinfo( |
| 42 salen, | 42 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST)) == 0); |
| 43 address, | |
| 44 len, | |
| 45 NULL, | |
| 46 0, | |
| 47 NI_NUMERICHOST)) != 0) { | |
| 48 return false; | |
| 49 } | |
| 50 return true; | |
| 51 } | 43 } |
| 52 | 44 |
| 53 | 45 |
| 54 bool Socket::Initialize() { | 46 bool Socket::Initialize() { |
| 55 // Nothing to do on Mac OS. | 47 // Nothing to do on Mac OS. |
| 56 return true; | 48 return true; |
| 57 } | 49 } |
| 58 | 50 |
| 59 | 51 |
| 60 static intptr_t Create(const RawAddr& addr) { | 52 static intptr_t Create(const RawAddr& addr) { |
| 61 intptr_t fd; | 53 intptr_t fd; |
| 62 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 54 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 63 if (fd < 0) { | 55 if (fd < 0) { |
| 64 return -1; | 56 return -1; |
| 65 } | 57 } |
| 66 FDUtils::SetCloseOnExec(fd); | 58 FDUtils::SetCloseOnExec(fd); |
| 67 return fd; | 59 return fd; |
| 68 } | 60 } |
| 69 | 61 |
| 70 | 62 |
| 71 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 63 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 72 intptr_t result = TEMP_FAILURE_RETRY( | 64 intptr_t result = TEMP_FAILURE_RETRY( |
| 73 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 65 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 74 if (result == 0 || errno == EINPROGRESS) { | 66 if ((result == 0) || (errno == EINPROGRESS)) { |
| 75 return fd; | 67 return fd; |
| 76 } | 68 } |
| 77 VOID_TEMP_FAILURE_RETRY(close(fd)); | 69 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 78 return -1; | 70 return -1; |
| 79 } | 71 } |
| 80 | 72 |
| 81 | 73 |
| 82 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 74 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
| 83 intptr_t fd = Create(addr); | 75 intptr_t fd = Create(addr); |
| 84 if (fd < 0) { | 76 if (fd < 0) { |
| 85 return fd; | 77 return fd; |
| 86 } | 78 } |
| 87 | 79 |
| 88 FDUtils::SetNonBlocking(fd); | 80 FDUtils::SetNonBlocking(fd); |
| 89 | 81 |
| 90 return Connect(fd, addr); | 82 return Connect(fd, addr); |
| 91 } | 83 } |
| 92 | 84 |
| 93 | 85 |
| 94 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 86 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 95 const RawAddr& source_addr) { | 87 const RawAddr& source_addr) { |
| 96 intptr_t fd = Create(addr); | 88 intptr_t fd = Create(addr); |
| 97 if (fd < 0) { | 89 if (fd < 0) { |
| 98 return fd; | 90 return fd; |
| 99 } | 91 } |
| 100 | 92 |
| 101 intptr_t result = TEMP_FAILURE_RETRY( | 93 intptr_t result = TEMP_FAILURE_RETRY( |
| 102 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 94 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 103 if (result != 0 && errno != EINPROGRESS) { | 95 if ((result != 0) && (errno != EINPROGRESS)) { |
| 104 VOID_TEMP_FAILURE_RETRY(close(fd)); | 96 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 105 return -1; | 97 return -1; |
| 106 } | 98 } |
| 107 | 99 |
| 108 return Connect(fd, addr); | 100 return Connect(fd, addr); |
| 109 } | 101 } |
| 110 | 102 |
| 111 | 103 |
| 112 intptr_t Socket::Available(intptr_t fd) { | 104 intptr_t Socket::Available(intptr_t fd) { |
| 113 return FDUtils::AvailableBytes(fd); | 105 return FDUtils::AvailableBytes(fd); |
| 114 } | 106 } |
| 115 | 107 |
| 116 | 108 |
| 117 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 109 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 118 ASSERT(fd >= 0); | 110 ASSERT(fd >= 0); |
| 119 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 111 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 120 ASSERT(EAGAIN == EWOULDBLOCK); | 112 ASSERT(EAGAIN == EWOULDBLOCK); |
| 121 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 113 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 122 // If the read would block we need to retry and therefore return 0 | 114 // If the read would block we need to retry and therefore return 0 |
| 123 // as the number of bytes written. | 115 // as the number of bytes written. |
| 124 read_bytes = 0; | 116 read_bytes = 0; |
| 125 } | 117 } |
| 126 return read_bytes; | 118 return read_bytes; |
| 127 } | 119 } |
| 128 | 120 |
| 129 | 121 |
| 130 intptr_t Socket::RecvFrom( | 122 intptr_t Socket::RecvFrom( |
| 131 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { | 123 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { |
| 132 ASSERT(fd >= 0); | 124 ASSERT(fd >= 0); |
| 133 socklen_t addr_len = sizeof(addr->ss); | 125 socklen_t addr_len = sizeof(addr->ss); |
| 134 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 126 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 135 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 127 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 136 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 128 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 137 // If the read would block we need to retry and therefore return 0 | 129 // If the read would block we need to retry and therefore return 0 |
| 138 // as the number of bytes written. | 130 // as the number of bytes written. |
| 139 read_bytes = 0; | 131 read_bytes = 0; |
| 140 } | 132 } |
| 141 return read_bytes; | 133 return read_bytes; |
| 142 } | 134 } |
| 143 | 135 |
| 144 | 136 |
| 145 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 137 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
| 146 ASSERT(fd >= 0); | 138 ASSERT(fd >= 0); |
| 147 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 139 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 148 ASSERT(EAGAIN == EWOULDBLOCK); | 140 ASSERT(EAGAIN == EWOULDBLOCK); |
| 149 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 141 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 150 // If the would block we need to retry and therefore return 0 as | 142 // If the would block we need to retry and therefore return 0 as |
| 151 // the number of bytes written. | 143 // the number of bytes written. |
| 152 written_bytes = 0; | 144 written_bytes = 0; |
| 153 } | 145 } |
| 154 return written_bytes; | 146 return written_bytes; |
| 155 } | 147 } |
| 156 | 148 |
| 157 | 149 |
| 158 intptr_t Socket::SendTo( | 150 intptr_t Socket::SendTo( |
| 159 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { | 151 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { |
| 160 ASSERT(fd >= 0); | 152 ASSERT(fd >= 0); |
| 161 ssize_t written_bytes = TEMP_FAILURE_RETRY( | 153 ssize_t written_bytes = TEMP_FAILURE_RETRY( |
| 162 sendto(fd, buffer, num_bytes, 0, | 154 sendto(fd, buffer, num_bytes, 0, |
| 163 &addr.addr, SocketAddress::GetAddrLength(addr))); | 155 &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 164 ASSERT(EAGAIN == EWOULDBLOCK); | 156 ASSERT(EAGAIN == EWOULDBLOCK); |
| 165 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 157 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 166 // If the would block we need to retry and therefore return 0 as | 158 // If the would block we need to retry and therefore return 0 as |
| 167 // the number of bytes written. | 159 // the number of bytes written. |
| 168 written_bytes = 0; | 160 written_bytes = 0; |
| 169 } | 161 } |
| 170 return written_bytes; | 162 return written_bytes; |
| 171 } | 163 } |
| 172 | 164 |
| 173 | 165 |
| 174 intptr_t Socket::GetPort(intptr_t fd) { | 166 intptr_t Socket::GetPort(intptr_t fd) { |
| 175 ASSERT(fd >= 0); | 167 ASSERT(fd >= 0); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 201 SO_ERROR, | 193 SO_ERROR, |
| 202 &errno, | 194 &errno, |
| 203 reinterpret_cast<socklen_t*>(&len)); | 195 reinterpret_cast<socklen_t*>(&len)); |
| 204 os_error->SetCodeAndMessage(OSError::kSystem, errno); | 196 os_error->SetCodeAndMessage(OSError::kSystem, errno); |
| 205 } | 197 } |
| 206 | 198 |
| 207 | 199 |
| 208 int Socket::GetType(intptr_t fd) { | 200 int Socket::GetType(intptr_t fd) { |
| 209 struct stat buf; | 201 struct stat buf; |
| 210 int result = fstat(fd, &buf); | 202 int result = fstat(fd, &buf); |
| 211 if (result == -1) return -1; | 203 if (result == -1) { |
| 212 if (S_ISCHR(buf.st_mode)) return File::kTerminal; | 204 return -1; |
| 213 if (S_ISFIFO(buf.st_mode)) return File::kPipe; | 205 } |
| 214 if (S_ISREG(buf.st_mode)) return File::kFile; | 206 if (S_ISCHR(buf.st_mode)) { |
| 207 return File::kTerminal; |
| 208 } |
| 209 if (S_ISFIFO(buf.st_mode)) { |
| 210 return File::kPipe; |
| 211 } |
| 212 if (S_ISREG(buf.st_mode)) { |
| 213 return File::kFile; |
| 214 } |
| 215 return File::kOther; | 215 return File::kOther; |
| 216 } | 216 } |
| 217 | 217 |
| 218 | 218 |
| 219 intptr_t Socket::GetStdioHandle(intptr_t num) { | 219 intptr_t Socket::GetStdioHandle(intptr_t num) { |
| 220 return num; | 220 return num; |
| 221 } | 221 } |
| 222 | 222 |
| 223 | 223 |
| 224 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 224 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 int status = getaddrinfo(host, 0, &hints, &info); | 235 int status = getaddrinfo(host, 0, &hints, &info); |
| 236 if (status != 0) { | 236 if (status != 0) { |
| 237 ASSERT(*os_error == NULL); | 237 ASSERT(*os_error == NULL); |
| 238 *os_error = new OSError(status, | 238 *os_error = new OSError(status, |
| 239 gai_strerror(status), | 239 gai_strerror(status), |
| 240 OSError::kGetAddressInfo); | 240 OSError::kGetAddressInfo); |
| 241 return NULL; | 241 return NULL; |
| 242 } | 242 } |
| 243 intptr_t count = 0; | 243 intptr_t count = 0; |
| 244 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 244 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 245 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; | 245 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 246 count++; |
| 247 } |
| 246 } | 248 } |
| 247 intptr_t i = 0; | 249 intptr_t i = 0; |
| 248 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); | 250 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); |
| 249 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 251 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 250 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 252 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 251 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 253 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 252 i++; | 254 i++; |
| 253 } | 255 } |
| 254 } | 256 } |
| 255 freeaddrinfo(info); | 257 freeaddrinfo(info); |
| 256 return addresses; | 258 return addresses; |
| 257 } | 259 } |
| 258 | 260 |
| 259 | 261 |
| 260 bool Socket::ReverseLookup(const RawAddr& addr, | 262 bool Socket::ReverseLookup(const RawAddr& addr, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 282 | 284 |
| 283 | 285 |
| 284 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 286 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 285 int result; | 287 int result; |
| 286 if (type == SocketAddress::TYPE_IPV4) { | 288 if (type == SocketAddress::TYPE_IPV4) { |
| 287 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 289 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 288 } else { | 290 } else { |
| 289 ASSERT(type == SocketAddress::TYPE_IPV6); | 291 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 290 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 292 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 291 } | 293 } |
| 292 return result == 1; | 294 return (result == 1); |
| 293 } | 295 } |
| 294 | 296 |
| 295 | 297 |
| 296 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 298 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
| 297 intptr_t fd; | 299 intptr_t fd; |
| 298 | 300 |
| 299 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 301 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
| 300 if (fd < 0) return -1; | 302 if (fd < 0) { |
| 303 return -1; |
| 304 } |
| 301 | 305 |
| 302 FDUtils::SetCloseOnExec(fd); | 306 FDUtils::SetCloseOnExec(fd); |
| 303 | 307 |
| 304 if (reuseAddress) { | 308 if (reuseAddress) { |
| 305 int optval = 1; | 309 int optval = 1; |
| 306 VOID_NO_RETRY_EXPECTED( | 310 VOID_NO_RETRY_EXPECTED( |
| 307 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 311 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 308 } | 312 } |
| 309 | 313 |
| 310 if (NO_RETRY_EXPECTED( | 314 if (NO_RETRY_EXPECTED( |
| 311 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 315 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 312 VOID_TEMP_FAILURE_RETRY(close(fd)); | 316 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 313 return -1; | 317 return -1; |
| 314 } | 318 } |
| 315 | 319 |
| 316 FDUtils::SetNonBlocking(fd); | 320 FDUtils::SetNonBlocking(fd); |
| 317 return fd; | 321 return fd; |
| 318 } | 322 } |
| 319 | 323 |
| 320 | 324 |
| 321 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 325 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
| 322 if (ifa->ifa_addr == NULL) { | 326 if (ifa->ifa_addr == NULL) { |
| 323 // OpenVPN's virtual device tun0. | 327 // OpenVPN's virtual device tun0. |
| 324 return false; | 328 return false; |
| 325 } | 329 } |
| 326 int family = ifa->ifa_addr->sa_family; | 330 int family = ifa->ifa_addr->sa_family; |
| 327 if (lookup_family == family) return true; | 331 return ((lookup_family == family) || |
| 328 if (lookup_family == AF_UNSPEC && | 332 ((lookup_family == AF_UNSPEC) && |
| 329 (family == AF_INET || family == AF_INET6)) { | 333 ((family == AF_INET) || (family == AF_INET6)))); |
| 330 return true; | |
| 331 } | |
| 332 return false; | |
| 333 } | 334 } |
| 334 | 335 |
| 335 | 336 |
| 336 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 337 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
| 337 int type, | 338 int type, |
| 338 OSError** os_error) { | 339 OSError** os_error) { |
| 339 struct ifaddrs* ifaddr; | 340 struct ifaddrs* ifaddr; |
| 340 | 341 |
| 341 int status = getifaddrs(&ifaddr); | 342 int status = getifaddrs(&ifaddr); |
| 342 if (status != 0) { | 343 if (status != 0) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 369 return addresses; | 370 return addresses; |
| 370 } | 371 } |
| 371 | 372 |
| 372 | 373 |
| 373 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 374 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
| 374 intptr_t backlog, | 375 intptr_t backlog, |
| 375 bool v6_only) { | 376 bool v6_only) { |
| 376 intptr_t fd; | 377 intptr_t fd; |
| 377 | 378 |
| 378 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 379 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 379 if (fd < 0) return -1; | 380 if (fd < 0) { |
| 381 return -1; |
| 382 } |
| 380 | 383 |
| 381 FDUtils::SetCloseOnExec(fd); | 384 FDUtils::SetCloseOnExec(fd); |
| 382 | 385 |
| 383 int optval = 1; | 386 int optval = 1; |
| 384 VOID_NO_RETRY_EXPECTED( | 387 VOID_NO_RETRY_EXPECTED( |
| 385 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 388 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 386 | 389 |
| 387 if (addr.ss.ss_family == AF_INET6) { | 390 if (addr.ss.ss_family == AF_INET6) { |
| 388 optval = v6_only ? 1 : 0; | 391 optval = v6_only ? 1 : 0; |
| 389 VOID_NO_RETRY_EXPECTED( | 392 VOID_NO_RETRY_EXPECTED( |
| 390 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 393 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 391 } | 394 } |
| 392 | 395 |
| 393 if (NO_RETRY_EXPECTED( | 396 if (NO_RETRY_EXPECTED( |
| 394 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 397 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 395 VOID_TEMP_FAILURE_RETRY(close(fd)); | 398 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 396 return -1; | 399 return -1; |
| 397 } | 400 } |
| 398 | 401 |
| 399 // Test for invalid socket port 65535 (some browsers disallow it). | 402 // Test for invalid socket port 65535 (some browsers disallow it). |
| 400 if (SocketAddress::GetAddrPort(addr) == 0 && Socket::GetPort(fd) == 65535) { | 403 if ((SocketAddress::GetAddrPort(addr) == 0) && |
| 404 (Socket::GetPort(fd) == 65535)) { |
| 401 // Don't close the socket until we have created a new socket, ensuring | 405 // Don't close the socket until we have created a new socket, ensuring |
| 402 // that we do not get the bad port number again. | 406 // that we do not get the bad port number again. |
| 403 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 407 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
| 404 int err = errno; | 408 int err = errno; |
| 405 VOID_TEMP_FAILURE_RETRY(close(fd)); | 409 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 406 errno = err; | 410 errno = err; |
| 407 return new_fd; | 411 return new_fd; |
| 408 } | 412 } |
| 409 | 413 |
| 410 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 414 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 | 456 |
| 453 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 457 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
| 454 int on; | 458 int on; |
| 455 socklen_t len = sizeof(on); | 459 socklen_t len = sizeof(on); |
| 456 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 460 int err = NO_RETRY_EXPECTED(getsockopt(fd, |
| 457 IPPROTO_TCP, | 461 IPPROTO_TCP, |
| 458 TCP_NODELAY, | 462 TCP_NODELAY, |
| 459 reinterpret_cast<void *>(&on), | 463 reinterpret_cast<void *>(&on), |
| 460 &len)); | 464 &len)); |
| 461 if (err == 0) { | 465 if (err == 0) { |
| 462 *enabled = on == 1; | 466 *enabled = (on == 1); |
| 463 } | 467 } |
| 464 return err == 0; | 468 return (err == 0); |
| 465 } | 469 } |
| 466 | 470 |
| 467 | 471 |
| 468 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 472 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 469 int on = enabled ? 1 : 0; | 473 int on = enabled ? 1 : 0; |
| 470 return NO_RETRY_EXPECTED(setsockopt(fd, | 474 return NO_RETRY_EXPECTED(setsockopt(fd, |
| 471 IPPROTO_TCP, | 475 IPPROTO_TCP, |
| 472 TCP_NODELAY, | 476 TCP_NODELAY, |
| 473 reinterpret_cast<char *>(&on), | 477 reinterpret_cast<char *>(&on), |
| 474 sizeof(on))) == 0; | 478 sizeof(on))) == 0; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 | 543 |
| 540 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 544 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
| 541 int on; | 545 int on; |
| 542 socklen_t len = sizeof(on); | 546 socklen_t len = sizeof(on); |
| 543 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 547 int err = NO_RETRY_EXPECTED(getsockopt(fd, |
| 544 SOL_SOCKET, | 548 SOL_SOCKET, |
| 545 SO_BROADCAST, | 549 SO_BROADCAST, |
| 546 reinterpret_cast<char *>(&on), | 550 reinterpret_cast<char *>(&on), |
| 547 &len)); | 551 &len)); |
| 548 if (err == 0) { | 552 if (err == 0) { |
| 549 *enabled = on == 1; | 553 *enabled = (on == 1); |
| 550 } | 554 } |
| 551 return err == 0; | 555 return (err == 0); |
| 552 } | 556 } |
| 553 | 557 |
| 554 | 558 |
| 555 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 559 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
| 556 int on = enabled ? 1 : 0; | 560 int on = enabled ? 1 : 0; |
| 557 return NO_RETRY_EXPECTED(setsockopt(fd, | 561 return NO_RETRY_EXPECTED(setsockopt(fd, |
| 558 SOL_SOCKET, | 562 SOL_SOCKET, |
| 559 SO_BROADCAST, | 563 SO_BROADCAST, |
| 560 reinterpret_cast<char *>(&on), | 564 reinterpret_cast<char *>(&on), |
| 561 sizeof(on))) == 0; | 565 sizeof(on))) == 0; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 const RawAddr& addr, | 616 const RawAddr& addr, |
| 613 const RawAddr& interface, | 617 const RawAddr& interface, |
| 614 int interfaceIndex) { | 618 int interfaceIndex) { |
| 615 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); | 619 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); |
| 616 } | 620 } |
| 617 | 621 |
| 618 } // namespace bin | 622 } // namespace bin |
| 619 } // namespace dart | 623 } // namespace dart |
| 620 | 624 |
| 621 #endif // defined(TARGET_OS_MACOS) | 625 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |