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