| 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_common_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 SocketAddress::SocketAddress(struct sockaddr* sa) { | 30 SocketAddress::SocketAddress(struct sockaddr* sa) { |
| 31 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 31 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 32 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, | 32 if (!BaseSocket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), |
| 33 INET6_ADDRSTRLEN)) { | 33 as_string_, INET6_ADDRSTRLEN)) { |
| 34 as_string_[0] = 0; | 34 as_string_[0] = 0; |
| 35 } | 35 } |
| 36 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 36 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
| 37 memmove(reinterpret_cast<void*>(&addr_), sa, salen); | 37 memmove(reinterpret_cast<void*>(&addr_), sa, salen); |
| 38 } | 38 } |
| 39 | 39 |
| 40 | 40 |
| 41 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 41 bool BaseSocket::FormatNumericAddress(const RawAddr& addr, |
| 42 char* address, |
| 43 int len) { |
| 42 socklen_t salen = SocketAddress::GetAddrLength(addr); | 44 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 43 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, | 45 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, |
| 44 0, NI_NUMERICHOST)) == 0); | 46 0, NI_NUMERICHOST)) == 0); |
| 45 } | 47 } |
| 46 | 48 |
| 47 | 49 |
| 48 bool Socket::Initialize() { | 50 bool BaseSocket::Initialize() { |
| 49 // Nothing to do on Mac OS. | 51 // Nothing to do on Mac OS. |
| 50 return true; | 52 return true; |
| 51 } | 53 } |
| 52 | 54 |
| 53 | 55 |
| 54 static intptr_t Create(const RawAddr& addr) { | 56 static intptr_t Create(const RawAddr& addr) { |
| 55 intptr_t fd; | 57 intptr_t fd; |
| 56 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 58 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 57 if (fd < 0) { | 59 if (fd < 0) { |
| 58 return -1; | 60 return -1; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 103 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 102 if ((result != 0) && (errno != EINPROGRESS)) { | 104 if ((result != 0) && (errno != EINPROGRESS)) { |
| 103 FDUtils::SaveErrorAndClose(fd); | 105 FDUtils::SaveErrorAndClose(fd); |
| 104 return -1; | 106 return -1; |
| 105 } | 107 } |
| 106 | 108 |
| 107 return Connect(fd, addr); | 109 return Connect(fd, addr); |
| 108 } | 110 } |
| 109 | 111 |
| 110 | 112 |
| 111 bool Socket::IsBindError(intptr_t error_number) { | 113 bool BaseSocket::IsBindError(intptr_t error_number) { |
| 112 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 114 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
| 113 error_number == EINVAL; | 115 error_number == EINVAL; |
| 114 } | 116 } |
| 115 | 117 |
| 116 | 118 |
| 117 intptr_t Socket::Available(intptr_t fd) { | 119 intptr_t BaseSocket::Available(intptr_t fd) { |
| 118 return FDUtils::AvailableBytes(fd); | 120 return FDUtils::AvailableBytes(fd); |
| 119 } | 121 } |
| 120 | 122 |
| 121 | 123 |
| 122 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 124 intptr_t BaseSocket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 123 ASSERT(fd >= 0); | 125 ASSERT(fd >= 0); |
| 124 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 126 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 125 ASSERT(EAGAIN == EWOULDBLOCK); | 127 ASSERT(EAGAIN == EWOULDBLOCK); |
| 126 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 128 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 127 // 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 |
| 128 // as the number of bytes written. | 130 // as the number of bytes written. |
| 129 read_bytes = 0; | 131 read_bytes = 0; |
| 130 } | 132 } |
| 131 return read_bytes; | 133 return read_bytes; |
| 132 } | 134 } |
| 133 | 135 |
| 134 | 136 |
| 135 intptr_t Socket::RecvFrom(intptr_t fd, | 137 intptr_t BaseSocket::RecvFrom(intptr_t fd, |
| 136 void* buffer, | 138 void* buffer, |
| 137 intptr_t num_bytes, | 139 intptr_t num_bytes, |
| 138 RawAddr* addr) { | 140 RawAddr* addr) { |
| 139 ASSERT(fd >= 0); | 141 ASSERT(fd >= 0); |
| 140 socklen_t addr_len = sizeof(addr->ss); | 142 socklen_t addr_len = sizeof(addr->ss); |
| 141 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 143 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 142 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 144 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 143 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 145 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 144 // If the read would block we need to retry and therefore return 0 | 146 // If the read would block we need to retry and therefore return 0 |
| 145 // as the number of bytes written. | 147 // as the number of bytes written. |
| 146 read_bytes = 0; | 148 read_bytes = 0; |
| 147 } | 149 } |
| 148 return read_bytes; | 150 return read_bytes; |
| 149 } | 151 } |
| 150 | 152 |
| 151 | 153 |
| 152 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 154 intptr_t BaseSocket::Write(intptr_t fd, |
| 155 const void* buffer, |
| 156 intptr_t num_bytes) { |
| 153 ASSERT(fd >= 0); | 157 ASSERT(fd >= 0); |
| 154 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 158 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 155 ASSERT(EAGAIN == EWOULDBLOCK); | 159 ASSERT(EAGAIN == EWOULDBLOCK); |
| 156 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 160 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 157 // If the would block we need to retry and therefore return 0 as | 161 // If the would block we need to retry and therefore return 0 as |
| 158 // the number of bytes written. | 162 // the number of bytes written. |
| 159 written_bytes = 0; | 163 written_bytes = 0; |
| 160 } | 164 } |
| 161 return written_bytes; | 165 return written_bytes; |
| 162 } | 166 } |
| 163 | 167 |
| 164 | 168 |
| 165 intptr_t Socket::SendTo(intptr_t fd, | 169 intptr_t BaseSocket::SendTo(intptr_t fd, |
| 166 const void* buffer, | 170 const void* buffer, |
| 167 intptr_t num_bytes, | 171 intptr_t num_bytes, |
| 168 const RawAddr& addr) { | 172 const RawAddr& addr) { |
| 169 ASSERT(fd >= 0); | 173 ASSERT(fd >= 0); |
| 170 ssize_t written_bytes = | 174 ssize_t written_bytes = |
| 171 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, | 175 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
| 172 SocketAddress::GetAddrLength(addr))); | 176 SocketAddress::GetAddrLength(addr))); |
| 173 ASSERT(EAGAIN == EWOULDBLOCK); | 177 ASSERT(EAGAIN == EWOULDBLOCK); |
| 174 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 178 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 175 // If the would block we need to retry and therefore return 0 as | 179 // If the would block we need to retry and therefore return 0 as |
| 176 // the number of bytes written. | 180 // the number of bytes written. |
| 177 written_bytes = 0; | 181 written_bytes = 0; |
| 178 } | 182 } |
| 179 return written_bytes; | 183 return written_bytes; |
| 180 } | 184 } |
| 181 | 185 |
| 182 | 186 |
| 183 intptr_t Socket::GetPort(intptr_t fd) { | 187 intptr_t BaseSocket::GetPort(intptr_t fd) { |
| 184 ASSERT(fd >= 0); | 188 ASSERT(fd >= 0); |
| 185 RawAddr raw; | 189 RawAddr raw; |
| 186 socklen_t size = sizeof(raw); | 190 socklen_t size = sizeof(raw); |
| 187 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 191 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 188 return 0; | 192 return 0; |
| 189 } | 193 } |
| 190 return SocketAddress::GetAddrPort(raw); | 194 return SocketAddress::GetAddrPort(raw); |
| 191 } | 195 } |
| 192 | 196 |
| 193 | 197 |
| 194 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 198 SocketAddress* BaseSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 195 ASSERT(fd >= 0); | 199 ASSERT(fd >= 0); |
| 196 RawAddr raw; | 200 RawAddr raw; |
| 197 socklen_t size = sizeof(raw); | 201 socklen_t size = sizeof(raw); |
| 198 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 202 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
| 199 return NULL; | 203 return NULL; |
| 200 } | 204 } |
| 201 *port = SocketAddress::GetAddrPort(raw); | 205 *port = SocketAddress::GetAddrPort(raw); |
| 202 return new SocketAddress(&raw.addr); | 206 return new SocketAddress(&raw.addr); |
| 203 } | 207 } |
| 204 | 208 |
| 205 | 209 |
| 206 void Socket::GetError(intptr_t fd, OSError* os_error) { | 210 void BaseSocket::GetError(intptr_t fd, OSError* os_error) { |
| 207 int len = sizeof(errno); | 211 int len = sizeof(errno); |
| 208 getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, | 212 getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, |
| 209 reinterpret_cast<socklen_t*>(&len)); | 213 reinterpret_cast<socklen_t*>(&len)); |
| 210 os_error->SetCodeAndMessage(OSError::kSystem, errno); | 214 os_error->SetCodeAndMessage(OSError::kSystem, errno); |
| 211 } | 215 } |
| 212 | 216 |
| 213 | 217 |
| 214 int Socket::GetType(intptr_t fd) { | 218 int BaseSocket::GetType(intptr_t fd) { |
| 215 struct stat buf; | 219 struct stat buf; |
| 216 int result = fstat(fd, &buf); | 220 int result = fstat(fd, &buf); |
| 217 if (result == -1) { | 221 if (result == -1) { |
| 218 return -1; | 222 return -1; |
| 219 } | 223 } |
| 220 if (S_ISCHR(buf.st_mode)) { | 224 if (S_ISCHR(buf.st_mode)) { |
| 221 return File::kTerminal; | 225 return File::kTerminal; |
| 222 } | 226 } |
| 223 if (S_ISFIFO(buf.st_mode)) { | 227 if (S_ISFIFO(buf.st_mode)) { |
| 224 return File::kPipe; | 228 return File::kPipe; |
| 225 } | 229 } |
| 226 if (S_ISREG(buf.st_mode)) { | 230 if (S_ISREG(buf.st_mode)) { |
| 227 return File::kFile; | 231 return File::kFile; |
| 228 } | 232 } |
| 229 return File::kOther; | 233 return File::kOther; |
| 230 } | 234 } |
| 231 | 235 |
| 232 | 236 |
| 233 intptr_t Socket::GetStdioHandle(intptr_t num) { | 237 intptr_t BaseSocket::GetStdioHandle(intptr_t num) { |
| 234 return num; | 238 return num; |
| 235 } | 239 } |
| 236 | 240 |
| 237 | 241 |
| 238 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 242 AddressList<SocketAddress>* BaseSocket::LookupAddress(const char* host, |
| 239 int type, | 243 int type, |
| 240 OSError** os_error) { | 244 OSError** os_error) { |
| 241 // Perform a name lookup for a host name. | 245 // Perform a name lookup for a host name. |
| 242 struct addrinfo hints; | 246 struct addrinfo hints; |
| 243 memset(&hints, 0, sizeof(hints)); | 247 memset(&hints, 0, sizeof(hints)); |
| 244 hints.ai_family = SocketAddress::FromType(type); | 248 hints.ai_family = SocketAddress::FromType(type); |
| 245 hints.ai_socktype = SOCK_STREAM; | 249 hints.ai_socktype = SOCK_STREAM; |
| 246 hints.ai_flags = 0; | 250 hints.ai_flags = 0; |
| 247 hints.ai_protocol = IPPROTO_TCP; | 251 hints.ai_protocol = IPPROTO_TCP; |
| 248 struct addrinfo* info = NULL; | 252 struct addrinfo* info = NULL; |
| 249 int status = getaddrinfo(host, 0, &hints, &info); | 253 int status = getaddrinfo(host, 0, &hints, &info); |
| 250 if (status != 0) { | 254 if (status != 0) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 265 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 269 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 266 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 270 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 267 i++; | 271 i++; |
| 268 } | 272 } |
| 269 } | 273 } |
| 270 freeaddrinfo(info); | 274 freeaddrinfo(info); |
| 271 return addresses; | 275 return addresses; |
| 272 } | 276 } |
| 273 | 277 |
| 274 | 278 |
| 275 bool Socket::ReverseLookup(const RawAddr& addr, | 279 bool BaseSocket::ReverseLookup(const RawAddr& addr, |
| 276 char* host, | 280 char* host, |
| 277 intptr_t host_len, | 281 intptr_t host_len, |
| 278 OSError** os_error) { | 282 OSError** os_error) { |
| 279 ASSERT(host_len >= NI_MAXHOST); | 283 ASSERT(host_len >= NI_MAXHOST); |
| 280 int status = NO_RETRY_EXPECTED( | 284 int status = NO_RETRY_EXPECTED( |
| 281 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, | 285 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
| 282 host_len, NULL, 0, NI_NAMEREQD)); | 286 host_len, NULL, 0, NI_NAMEREQD)); |
| 283 if (status != 0) { | 287 if (status != 0) { |
| 284 ASSERT(*os_error == NULL); | 288 ASSERT(*os_error == NULL); |
| 285 *os_error = | 289 *os_error = |
| 286 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 290 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 287 return false; | 291 return false; |
| 288 } | 292 } |
| 289 return true; | 293 return true; |
| 290 } | 294 } |
| 291 | 295 |
| 292 | 296 |
| 293 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 297 bool BaseSocket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 294 int result; | 298 int result; |
| 295 if (type == SocketAddress::TYPE_IPV4) { | 299 if (type == SocketAddress::TYPE_IPV4) { |
| 296 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 300 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 297 } else { | 301 } else { |
| 298 ASSERT(type == SocketAddress::TYPE_IPV6); | 302 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 299 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 303 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 300 } | 304 } |
| 301 return (result == 1); | 305 return (result == 1); |
| 302 } | 306 } |
| 303 | 307 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 // OpenVPN's virtual device tun0. | 344 // OpenVPN's virtual device tun0. |
| 341 return false; | 345 return false; |
| 342 } | 346 } |
| 343 int family = ifa->ifa_addr->sa_family; | 347 int family = ifa->ifa_addr->sa_family; |
| 344 return ((lookup_family == family) || | 348 return ((lookup_family == family) || |
| 345 ((lookup_family == AF_UNSPEC) && | 349 ((lookup_family == AF_UNSPEC) && |
| 346 ((family == AF_INET) || (family == AF_INET6)))); | 350 ((family == AF_INET) || (family == AF_INET6)))); |
| 347 } | 351 } |
| 348 | 352 |
| 349 | 353 |
| 350 bool Socket::ListInterfacesSupported() { | 354 bool BaseSocket::ListInterfacesSupported() { |
| 351 return true; | 355 return true; |
| 352 } | 356 } |
| 353 | 357 |
| 354 | 358 |
| 355 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 359 AddressList<InterfaceSocketAddress>* BaseSocket::ListInterfaces( |
| 356 int type, | 360 int type, |
| 357 OSError** os_error) { | 361 OSError** os_error) { |
| 358 struct ifaddrs* ifaddr; | 362 struct ifaddrs* ifaddr; |
| 359 | 363 |
| 360 int status = getifaddrs(&ifaddr); | 364 int status = getifaddrs(&ifaddr); |
| 361 if (status != 0) { | 365 if (status != 0) { |
| 362 ASSERT(*os_error == NULL); | 366 ASSERT(*os_error == NULL); |
| 363 *os_error = | 367 *os_error = |
| 364 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 368 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 365 return NULL; | 369 return NULL; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 421 } |
| 418 | 422 |
| 419 if (NO_RETRY_EXPECTED( | 423 if (NO_RETRY_EXPECTED( |
| 420 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 424 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 421 FDUtils::SaveErrorAndClose(fd); | 425 FDUtils::SaveErrorAndClose(fd); |
| 422 return -1; | 426 return -1; |
| 423 } | 427 } |
| 424 | 428 |
| 425 // Test for invalid socket port 65535 (some browsers disallow it). | 429 // Test for invalid socket port 65535 (some browsers disallow it). |
| 426 if ((SocketAddress::GetAddrPort(addr) == 0) && | 430 if ((SocketAddress::GetAddrPort(addr) == 0) && |
| 427 (Socket::GetPort(fd) == 65535)) { | 431 (BaseSocket::GetPort(fd) == 65535)) { |
| 428 // Don't close the socket until we have created a new socket, ensuring | 432 // Don't close the socket until we have created a new socket, ensuring |
| 429 // that we do not get the bad port number again. | 433 // that we do not get the bad port number again. |
| 430 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 434 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
| 431 FDUtils::SaveErrorAndClose(fd); | 435 FDUtils::SaveErrorAndClose(fd); |
| 432 return new_fd; | 436 return new_fd; |
| 433 } | 437 } |
| 434 | 438 |
| 435 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 439 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 436 FDUtils::SaveErrorAndClose(fd); | 440 FDUtils::SaveErrorAndClose(fd); |
| 437 return -1; | 441 return -1; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 475 } |
| 472 if (!FDUtils::SetNonBlocking(socket)) { | 476 if (!FDUtils::SetNonBlocking(socket)) { |
| 473 FDUtils::SaveErrorAndClose(socket); | 477 FDUtils::SaveErrorAndClose(socket); |
| 474 return -1; | 478 return -1; |
| 475 } | 479 } |
| 476 } | 480 } |
| 477 return socket; | 481 return socket; |
| 478 } | 482 } |
| 479 | 483 |
| 480 | 484 |
| 481 void Socket::Close(intptr_t fd) { | 485 void BaseSocket::Close(intptr_t fd) { |
| 482 ASSERT(fd >= 0); | 486 ASSERT(fd >= 0); |
| 483 VOID_TEMP_FAILURE_RETRY(close(fd)); | 487 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 484 } | 488 } |
| 485 | 489 |
| 486 | 490 |
| 487 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 491 bool BaseSocket::GetNoDelay(intptr_t fd, bool* enabled) { |
| 488 int on; | 492 int on; |
| 489 socklen_t len = sizeof(on); | 493 socklen_t len = sizeof(on); |
| 490 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 494 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 491 reinterpret_cast<void*>(&on), &len)); | 495 reinterpret_cast<void*>(&on), &len)); |
| 492 if (err == 0) { | 496 if (err == 0) { |
| 493 *enabled = (on == 1); | 497 *enabled = (on == 1); |
| 494 } | 498 } |
| 495 return (err == 0); | 499 return (err == 0); |
| 496 } | 500 } |
| 497 | 501 |
| 498 | 502 |
| 499 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 503 bool BaseSocket::SetNoDelay(intptr_t fd, bool enabled) { |
| 500 int on = enabled ? 1 : 0; | 504 int on = enabled ? 1 : 0; |
| 501 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 505 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 502 reinterpret_cast<char*>(&on), | 506 reinterpret_cast<char*>(&on), |
| 503 sizeof(on))) == 0; | 507 sizeof(on))) == 0; |
| 504 } | 508 } |
| 505 | 509 |
| 506 | 510 |
| 507 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 511 bool BaseSocket::GetMulticastLoop(intptr_t fd, |
| 512 intptr_t protocol, |
| 513 bool* enabled) { |
| 508 uint8_t on; | 514 uint8_t on; |
| 509 socklen_t len = sizeof(on); | 515 socklen_t len = sizeof(on); |
| 510 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 516 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 511 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 517 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 512 : IPV6_MULTICAST_LOOP; | 518 : IPV6_MULTICAST_LOOP; |
| 513 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 519 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 514 reinterpret_cast<char*>(&on), &len)) == 0) { | 520 reinterpret_cast<char*>(&on), &len)) == 0) { |
| 515 *enabled = (on == 1); | 521 *enabled = (on == 1); |
| 516 return true; | 522 return true; |
| 517 } | 523 } |
| 518 return false; | 524 return false; |
| 519 } | 525 } |
| 520 | 526 |
| 521 | 527 |
| 522 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 528 bool BaseSocket::SetMulticastLoop(intptr_t fd, |
| 529 intptr_t protocol, |
| 530 bool enabled) { |
| 523 u_int on = enabled ? 1 : 0; | 531 u_int on = enabled ? 1 : 0; |
| 524 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 532 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 525 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 533 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 526 : IPV6_MULTICAST_LOOP; | 534 : IPV6_MULTICAST_LOOP; |
| 527 return NO_RETRY_EXPECTED(setsockopt( | 535 return NO_RETRY_EXPECTED(setsockopt( |
| 528 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == | 536 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
| 529 0; | 537 0; |
| 530 } | 538 } |
| 531 | 539 |
| 532 | 540 |
| 533 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 541 bool BaseSocket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| 534 uint8_t v; | 542 uint8_t v; |
| 535 socklen_t len = sizeof(v); | 543 socklen_t len = sizeof(v); |
| 536 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 544 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 537 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 545 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 538 : IPV6_MULTICAST_HOPS; | 546 : IPV6_MULTICAST_HOPS; |
| 539 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 547 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 540 reinterpret_cast<char*>(&v), &len)) == 0) { | 548 reinterpret_cast<char*>(&v), &len)) == 0) { |
| 541 *value = v; | 549 *value = v; |
| 542 return true; | 550 return true; |
| 543 } | 551 } |
| 544 return false; | 552 return false; |
| 545 } | 553 } |
| 546 | 554 |
| 547 | 555 |
| 548 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 556 bool BaseSocket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| 549 int v = value; | 557 int v = value; |
| 550 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 558 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 551 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 559 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 552 : IPV6_MULTICAST_HOPS; | 560 : IPV6_MULTICAST_HOPS; |
| 553 return NO_RETRY_EXPECTED(setsockopt( | 561 return NO_RETRY_EXPECTED(setsockopt( |
| 554 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; | 562 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
| 555 } | 563 } |
| 556 | 564 |
| 557 | 565 |
| 558 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 566 bool BaseSocket::GetBroadcast(intptr_t fd, bool* enabled) { |
| 559 int on; | 567 int on; |
| 560 socklen_t len = sizeof(on); | 568 socklen_t len = sizeof(on); |
| 561 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 569 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 562 reinterpret_cast<char*>(&on), &len)); | 570 reinterpret_cast<char*>(&on), &len)); |
| 563 if (err == 0) { | 571 if (err == 0) { |
| 564 *enabled = (on == 1); | 572 *enabled = (on == 1); |
| 565 } | 573 } |
| 566 return (err == 0); | 574 return (err == 0); |
| 567 } | 575 } |
| 568 | 576 |
| 569 | 577 |
| 570 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 578 bool BaseSocket::SetBroadcast(intptr_t fd, bool enabled) { |
| 571 int on = enabled ? 1 : 0; | 579 int on = enabled ? 1 : 0; |
| 572 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 580 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 573 reinterpret_cast<char*>(&on), | 581 reinterpret_cast<char*>(&on), |
| 574 sizeof(on))) == 0; | 582 sizeof(on))) == 0; |
| 575 } | 583 } |
| 576 | 584 |
| 577 | 585 |
| 578 static bool JoinOrLeaveMulticast(intptr_t fd, | 586 static bool JoinOrLeaveMulticast(intptr_t fd, |
| 579 const RawAddr& addr, | 587 const RawAddr& addr, |
| 580 const RawAddr& interface, | 588 const RawAddr& interface, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 603 if (join) { | 611 if (join) { |
| 604 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, | 612 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
| 605 &mreq, sizeof(mreq))) == 0; | 613 &mreq, sizeof(mreq))) == 0; |
| 606 } else { | 614 } else { |
| 607 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, | 615 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
| 608 &mreq, sizeof(mreq))) == 0; | 616 &mreq, sizeof(mreq))) == 0; |
| 609 } | 617 } |
| 610 } | 618 } |
| 611 } | 619 } |
| 612 | 620 |
| 613 bool Socket::JoinMulticast(intptr_t fd, | 621 bool BaseSocket::JoinMulticast(intptr_t fd, |
| 614 const RawAddr& addr, | 622 const RawAddr& addr, |
| 615 const RawAddr& interface, | 623 const RawAddr& interface, |
| 616 int interfaceIndex) { | 624 int interfaceIndex) { |
| 617 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, true); | 625 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, true); |
| 618 } | 626 } |
| 619 | 627 |
| 620 | 628 |
| 621 bool Socket::LeaveMulticast(intptr_t fd, | 629 bool BaseSocket::LeaveMulticast(intptr_t fd, |
| 622 const RawAddr& addr, | 630 const RawAddr& addr, |
| 623 const RawAddr& interface, | 631 const RawAddr& interface, |
| 624 int interfaceIndex) { | 632 int interfaceIndex) { |
| 625 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); | 633 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); |
| 626 } | 634 } |
| 627 | 635 |
| 628 } // namespace bin | 636 } // namespace bin |
| 629 } // namespace dart | 637 } // namespace dart |
| 630 | 638 |
| 631 #endif // defined(TARGET_OS_MACOS) | 639 #endif // defined(TARGET_OS_MACOS) |
| 632 | 640 |
| 633 #endif // !defined(DART_IO_DISABLED) | 641 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |