| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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_ANDROID) | 8 #if defined(TARGET_OS_ANDROID) |
| 9 | 9 |
| 10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
| 11 #include "bin/socket_android.h" | 11 #include "bin/socket_android.h" |
| 12 | 12 |
| 13 #include <errno.h> // NOLINT | 13 #include <errno.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
| 15 #include <stdio.h> // NOLINT | 15 #include <stdio.h> // NOLINT |
| 16 #include <stdlib.h> // NOLINT | 16 #include <stdlib.h> // NOLINT |
| 17 #include <string.h> // NOLINT | 17 #include <string.h> // NOLINT |
| 18 #include <sys/stat.h> // NOLINT | 18 #include <sys/stat.h> // NOLINT |
| 19 #include <unistd.h> // NOLINT | 19 #include <unistd.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(*reinterpret_cast<RawAddr*>(sa), as_string_, |
| 31 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 31 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 return (NO_RETRY_EXPECTED(getnameinfo( | 41 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, |
| 42 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST)) == 0); | 42 0, NI_NUMERICHOST)) == 0); |
| 43 } | 43 } |
| 44 | 44 |
| 45 | 45 |
| 46 bool Socket::Initialize() { | 46 bool Socket::Initialize() { |
| 47 // Nothing to do on Android. | 47 // Nothing to do on Android. |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 static intptr_t Create(const RawAddr& addr) { | 52 static intptr_t Create(const RawAddr& addr) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 VOID_TEMP_FAILURE_RETRY(close(fd)); | 96 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 97 return -1; | 97 return -1; |
| 98 } | 98 } |
| 99 | 99 |
| 100 return Connect(fd, addr); | 100 return Connect(fd, addr); |
| 101 } | 101 } |
| 102 | 102 |
| 103 | 103 |
| 104 bool Socket::IsBindError(intptr_t error_number) { | 104 bool Socket::IsBindError(intptr_t error_number) { |
| 105 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 105 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
| 106 error_number == EINVAL; | 106 error_number == EINVAL; |
| 107 } | 107 } |
| 108 | 108 |
| 109 | 109 |
| 110 intptr_t Socket::Available(intptr_t fd) { | 110 intptr_t Socket::Available(intptr_t fd) { |
| 111 return FDUtils::AvailableBytes(fd); | 111 return FDUtils::AvailableBytes(fd); |
| 112 } | 112 } |
| 113 | 113 |
| 114 | 114 |
| 115 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 115 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 116 ASSERT(fd >= 0); | 116 ASSERT(fd >= 0); |
| 117 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 117 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 118 ASSERT(EAGAIN == EWOULDBLOCK); | 118 ASSERT(EAGAIN == EWOULDBLOCK); |
| 119 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 119 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 120 // If the read would block we need to retry and therefore return 0 | 120 // If the read would block we need to retry and therefore return 0 |
| 121 // as the number of bytes written. | 121 // as the number of bytes written. |
| 122 read_bytes = 0; | 122 read_bytes = 0; |
| 123 } | 123 } |
| 124 return read_bytes; | 124 return read_bytes; |
| 125 } | 125 } |
| 126 | 126 |
| 127 | 127 |
| 128 intptr_t Socket::RecvFrom( | 128 intptr_t Socket::RecvFrom(intptr_t fd, |
| 129 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { | 129 void* buffer, |
| 130 intptr_t num_bytes, |
| 131 RawAddr* addr) { |
| 130 ASSERT(fd >= 0); | 132 ASSERT(fd >= 0); |
| 131 socklen_t addr_len = sizeof(addr->ss); | 133 socklen_t addr_len = sizeof(addr->ss); |
| 132 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 134 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 133 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 135 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 134 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 136 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 135 // If the read would block we need to retry and therefore return 0 | 137 // If the read would block we need to retry and therefore return 0 |
| 136 // as the number of bytes written. | 138 // as the number of bytes written. |
| 137 read_bytes = 0; | 139 read_bytes = 0; |
| 138 } | 140 } |
| 139 return read_bytes; | 141 return read_bytes; |
| 140 } | 142 } |
| 141 | 143 |
| 142 | 144 |
| 143 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 145 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
| 144 ASSERT(fd >= 0); | 146 ASSERT(fd >= 0); |
| 145 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 147 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 146 ASSERT(EAGAIN == EWOULDBLOCK); | 148 ASSERT(EAGAIN == EWOULDBLOCK); |
| 147 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 149 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 148 // If the would block we need to retry and therefore return 0 as | 150 // If the would block we need to retry and therefore return 0 as |
| 149 // the number of bytes written. | 151 // the number of bytes written. |
| 150 written_bytes = 0; | 152 written_bytes = 0; |
| 151 } | 153 } |
| 152 return written_bytes; | 154 return written_bytes; |
| 153 } | 155 } |
| 154 | 156 |
| 155 | 157 |
| 156 intptr_t Socket::SendTo( | 158 intptr_t Socket::SendTo(intptr_t fd, |
| 157 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { | 159 const void* buffer, |
| 160 intptr_t num_bytes, |
| 161 const RawAddr& addr) { |
| 158 ASSERT(fd >= 0); | 162 ASSERT(fd >= 0); |
| 159 ssize_t written_bytes = TEMP_FAILURE_RETRY( | 163 ssize_t written_bytes = |
| 160 sendto(fd, buffer, num_bytes, 0, | 164 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
| 161 &addr.addr, SocketAddress::GetAddrLength(addr))); | 165 SocketAddress::GetAddrLength(addr))); |
| 162 ASSERT(EAGAIN == EWOULDBLOCK); | 166 ASSERT(EAGAIN == EWOULDBLOCK); |
| 163 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 167 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 164 // If the would block we need to retry and therefore return 0 as | 168 // If the would block we need to retry and therefore return 0 as |
| 165 // the number of bytes written. | 169 // the number of bytes written. |
| 166 written_bytes = 0; | 170 written_bytes = 0; |
| 167 } | 171 } |
| 168 return written_bytes; | 172 return written_bytes; |
| 169 } | 173 } |
| 170 | 174 |
| 171 | 175 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 188 return NULL; | 192 return NULL; |
| 189 } | 193 } |
| 190 *port = SocketAddress::GetAddrPort(raw); | 194 *port = SocketAddress::GetAddrPort(raw); |
| 191 return new SocketAddress(&raw.addr); | 195 return new SocketAddress(&raw.addr); |
| 192 } | 196 } |
| 193 | 197 |
| 194 | 198 |
| 195 void Socket::GetError(intptr_t fd, OSError* os_error) { | 199 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 196 int errorNumber; | 200 int errorNumber; |
| 197 socklen_t len = sizeof(errorNumber); | 201 socklen_t len = sizeof(errorNumber); |
| 198 getsockopt(fd, | 202 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), |
| 199 SOL_SOCKET, | |
| 200 SO_ERROR, | |
| 201 reinterpret_cast<void*>(&errorNumber), | |
| 202 &len); | 203 &len); |
| 203 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); | 204 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); |
| 204 } | 205 } |
| 205 | 206 |
| 206 | 207 |
| 207 int Socket::GetType(intptr_t fd) { | 208 int Socket::GetType(intptr_t fd) { |
| 208 struct stat buf; | 209 struct stat buf; |
| 209 int result = fstat(fd, &buf); | 210 int result = fstat(fd, &buf); |
| 210 if (result == -1) { | 211 if (result == -1) { |
| 211 return -1; | 212 return -1; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 240 hints.ai_protocol = IPPROTO_TCP; | 241 hints.ai_protocol = IPPROTO_TCP; |
| 241 struct addrinfo* info = NULL; | 242 struct addrinfo* info = NULL; |
| 242 int status = getaddrinfo(host, 0, &hints, &info); | 243 int status = getaddrinfo(host, 0, &hints, &info); |
| 243 if (status != 0) { | 244 if (status != 0) { |
| 244 // We failed, try without AI_ADDRCONFIG. This can happen when looking up | 245 // We failed, try without AI_ADDRCONFIG. This can happen when looking up |
| 245 // e.g. '::1', when there are no IPv6 addresses. | 246 // e.g. '::1', when there are no IPv6 addresses. |
| 246 hints.ai_flags = 0; | 247 hints.ai_flags = 0; |
| 247 status = getaddrinfo(host, 0, &hints, &info); | 248 status = getaddrinfo(host, 0, &hints, &info); |
| 248 if (status != 0) { | 249 if (status != 0) { |
| 249 ASSERT(*os_error == NULL); | 250 ASSERT(*os_error == NULL); |
| 250 *os_error = new OSError(status, | 251 *os_error = |
| 251 gai_strerror(status), | 252 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 252 OSError::kGetAddressInfo); | |
| 253 return NULL; | 253 return NULL; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 intptr_t count = 0; | 256 intptr_t count = 0; |
| 257 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 257 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 258 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 258 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 259 count++; | 259 count++; |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 intptr_t i = 0; | 262 intptr_t i = 0; |
| 263 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); | 263 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); |
| 264 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 264 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 265 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 265 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 266 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 266 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 267 i++; | 267 i++; |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 freeaddrinfo(info); | 270 freeaddrinfo(info); |
| 271 return addresses; | 271 return addresses; |
| 272 } | 272 } |
| 273 | 273 |
| 274 | 274 |
| 275 bool Socket::ReverseLookup(const RawAddr& addr, | 275 bool Socket::ReverseLookup(const RawAddr& addr, |
| 276 char* host, | 276 char* host, |
| 277 intptr_t host_len, | 277 intptr_t host_len, |
| 278 OSError** os_error) { | 278 OSError** os_error) { |
| 279 ASSERT(host_len >= NI_MAXHOST); | 279 ASSERT(host_len >= NI_MAXHOST); |
| 280 int status = NO_RETRY_EXPECTED(getnameinfo( | 280 int status = NO_RETRY_EXPECTED( |
| 281 &addr.addr, | 281 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
| 282 SocketAddress::GetAddrLength(addr), | 282 host_len, NULL, 0, NI_NAMEREQD)); |
| 283 host, | |
| 284 host_len, | |
| 285 NULL, | |
| 286 0, | |
| 287 NI_NAMEREQD)); | |
| 288 if (status != 0) { | 283 if (status != 0) { |
| 289 ASSERT(*os_error == NULL); | 284 ASSERT(*os_error == NULL); |
| 290 *os_error = new OSError(status, | 285 *os_error = |
| 291 gai_strerror(status), | 286 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 292 OSError::kGetAddressInfo); | |
| 293 return false; | 287 return false; |
| 294 } | 288 } |
| 295 return true; | 289 return true; |
| 296 } | 290 } |
| 297 | 291 |
| 298 | 292 |
| 299 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 293 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 300 int result; | 294 int result; |
| 301 if (type == SocketAddress::TYPE_IPV4) { | 295 if (type == SocketAddress::TYPE_IPV4) { |
| 302 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 296 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 318 | 312 |
| 319 FDUtils::SetCloseOnExec(fd); | 313 FDUtils::SetCloseOnExec(fd); |
| 320 | 314 |
| 321 if (reuseAddress) { | 315 if (reuseAddress) { |
| 322 int optval = 1; | 316 int optval = 1; |
| 323 VOID_NO_RETRY_EXPECTED( | 317 VOID_NO_RETRY_EXPECTED( |
| 324 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 318 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 325 } | 319 } |
| 326 | 320 |
| 327 if (NO_RETRY_EXPECTED( | 321 if (NO_RETRY_EXPECTED( |
| 328 bind(fd, | 322 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 329 &addr.addr, | |
| 330 SocketAddress::GetAddrLength(addr))) < 0) { | |
| 331 VOID_TEMP_FAILURE_RETRY(close(fd)); | 323 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 332 return -1; | 324 return -1; |
| 333 } | 325 } |
| 334 | 326 |
| 335 FDUtils::SetNonBlocking(fd); | 327 FDUtils::SetNonBlocking(fd); |
| 336 return fd; | 328 return fd; |
| 337 } | 329 } |
| 338 | 330 |
| 339 | 331 |
| 340 bool Socket::ListInterfacesSupported() { | 332 bool Socket::ListInterfacesSupported() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 VOID_NO_RETRY_EXPECTED( | 364 VOID_NO_RETRY_EXPECTED( |
| 373 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 365 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 374 | 366 |
| 375 if (addr.ss.ss_family == AF_INET6) { | 367 if (addr.ss.ss_family == AF_INET6) { |
| 376 optval = v6_only ? 1 : 0; | 368 optval = v6_only ? 1 : 0; |
| 377 VOID_NO_RETRY_EXPECTED( | 369 VOID_NO_RETRY_EXPECTED( |
| 378 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 370 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 379 } | 371 } |
| 380 | 372 |
| 381 if (NO_RETRY_EXPECTED( | 373 if (NO_RETRY_EXPECTED( |
| 382 bind(fd, | 374 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 383 &addr.addr, | |
| 384 SocketAddress::GetAddrLength(addr))) < 0) { | |
| 385 VOID_TEMP_FAILURE_RETRY(close(fd)); | 375 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 386 return -1; | 376 return -1; |
| 387 } | 377 } |
| 388 | 378 |
| 389 // Test for invalid socket port 65535 (some browsers disallow it). | 379 // Test for invalid socket port 65535 (some browsers disallow it). |
| 390 if ((SocketAddress::GetAddrPort(addr)) == 0 && | 380 if ((SocketAddress::GetAddrPort(addr)) == 0 && |
| 391 (Socket::GetPort(fd) == 65535)) { | 381 (Socket::GetPort(fd) == 65535)) { |
| 392 // Don't close the socket until we have created a new socket, ensuring | 382 // Don't close the socket until we have created a new socket, ensuring |
| 393 // that we do not get the bad port number again. | 383 // that we do not get the bad port number again. |
| 394 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 384 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 411 bool ServerSocket::StartAccept(intptr_t fd) { | 401 bool ServerSocket::StartAccept(intptr_t fd) { |
| 412 USE(fd); | 402 USE(fd); |
| 413 return true; | 403 return true; |
| 414 } | 404 } |
| 415 | 405 |
| 416 | 406 |
| 417 static bool IsTemporaryAcceptError(int error) { | 407 static bool IsTemporaryAcceptError(int error) { |
| 418 // On Android a number of protocol errors should be treated as EAGAIN. | 408 // On Android a number of protocol errors should be treated as EAGAIN. |
| 419 // These are the ones for TCP/IP. | 409 // These are the ones for TCP/IP. |
| 420 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 410 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
| 421 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 411 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
| 422 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 412 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
| 423 (error == ENETUNREACH); | 413 (error == ENETUNREACH); |
| 424 } | 414 } |
| 425 | 415 |
| 426 | 416 |
| 427 intptr_t ServerSocket::Accept(intptr_t fd) { | 417 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 428 intptr_t socket; | 418 intptr_t socket; |
| 429 struct sockaddr clientaddr; | 419 struct sockaddr clientaddr; |
| 430 socklen_t addrlen = sizeof(clientaddr); | 420 socklen_t addrlen = sizeof(clientaddr); |
| 431 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 421 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 432 if (socket == -1) { | 422 if (socket == -1) { |
| 433 if (IsTemporaryAcceptError(errno)) { | 423 if (IsTemporaryAcceptError(errno)) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 447 | 437 |
| 448 void Socket::Close(intptr_t fd) { | 438 void Socket::Close(intptr_t fd) { |
| 449 ASSERT(fd >= 0); | 439 ASSERT(fd >= 0); |
| 450 VOID_TEMP_FAILURE_RETRY(close(fd)); | 440 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 451 } | 441 } |
| 452 | 442 |
| 453 | 443 |
| 454 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 444 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
| 455 int on; | 445 int on; |
| 456 socklen_t len = sizeof(on); | 446 socklen_t len = sizeof(on); |
| 457 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 447 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 458 IPPROTO_TCP, | 448 reinterpret_cast<void*>(&on), &len)); |
| 459 TCP_NODELAY, | |
| 460 reinterpret_cast<void *>(&on), | |
| 461 &len)); | |
| 462 if (err == 0) { | 449 if (err == 0) { |
| 463 *enabled = (on == 1); | 450 *enabled = (on == 1); |
| 464 } | 451 } |
| 465 return (err == 0); | 452 return (err == 0); |
| 466 } | 453 } |
| 467 | 454 |
| 468 | 455 |
| 469 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 456 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 470 int on = enabled ? 1 : 0; | 457 int on = enabled ? 1 : 0; |
| 471 return NO_RETRY_EXPECTED(setsockopt(fd, | 458 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 472 IPPROTO_TCP, | 459 reinterpret_cast<char*>(&on), |
| 473 TCP_NODELAY, | |
| 474 reinterpret_cast<char *>(&on), | |
| 475 sizeof(on))) == 0; | 460 sizeof(on))) == 0; |
| 476 } | 461 } |
| 477 | 462 |
| 478 | 463 |
| 479 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 464 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { |
| 480 uint8_t on; | 465 uint8_t on; |
| 481 socklen_t len = sizeof(on); | 466 socklen_t len = sizeof(on); |
| 482 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 467 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 483 int optname = protocol == SocketAddress::TYPE_IPV4 | 468 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 484 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 469 : IPV6_MULTICAST_LOOP; |
| 485 if (NO_RETRY_EXPECTED(getsockopt(fd, | 470 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 486 level, | 471 reinterpret_cast<char*>(&on), &len)) == 0) { |
| 487 optname, | |
| 488 reinterpret_cast<char *>(&on), | |
| 489 &len)) == 0) { | |
| 490 *enabled = (on == 1); | 472 *enabled = (on == 1); |
| 491 return true; | 473 return true; |
| 492 } | 474 } |
| 493 return false; | 475 return false; |
| 494 } | 476 } |
| 495 | 477 |
| 496 | 478 |
| 497 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 479 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { |
| 498 int on = enabled ? 1 : 0; | 480 int on = enabled ? 1 : 0; |
| 499 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 481 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 500 int optname = protocol == SocketAddress::TYPE_IPV4 | 482 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 501 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 483 : IPV6_MULTICAST_LOOP; |
| 502 return NO_RETRY_EXPECTED(setsockopt(fd, | 484 return NO_RETRY_EXPECTED(setsockopt( |
| 503 level, | 485 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
| 504 optname, | 486 0; |
| 505 reinterpret_cast<char *>(&on), | |
| 506 sizeof(on))) == 0; | |
| 507 } | 487 } |
| 508 | 488 |
| 509 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 489 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| 510 uint8_t v; | 490 uint8_t v; |
| 511 socklen_t len = sizeof(v); | 491 socklen_t len = sizeof(v); |
| 512 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 492 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 513 int optname = protocol == SocketAddress::TYPE_IPV4 | 493 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 514 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 494 : IPV6_MULTICAST_HOPS; |
| 515 if (NO_RETRY_EXPECTED(getsockopt(fd, | 495 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 516 level, | 496 reinterpret_cast<char*>(&v), &len)) == 0) { |
| 517 optname, | |
| 518 reinterpret_cast<char *>(&v), | |
| 519 &len)) == 0) { | |
| 520 *value = v; | 497 *value = v; |
| 521 return true; | 498 return true; |
| 522 } | 499 } |
| 523 return false; | 500 return false; |
| 524 } | 501 } |
| 525 | 502 |
| 526 | 503 |
| 527 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 504 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| 528 int v = value; | 505 int v = value; |
| 529 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 506 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 530 int optname = protocol == SocketAddress::TYPE_IPV4 | 507 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 531 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 508 : IPV6_MULTICAST_HOPS; |
| 532 return NO_RETRY_EXPECTED(setsockopt(fd, | 509 return NO_RETRY_EXPECTED(setsockopt( |
| 533 level, | 510 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
| 534 optname, | |
| 535 reinterpret_cast<char *>(&v), | |
| 536 sizeof(v))) == 0; | |
| 537 } | 511 } |
| 538 | 512 |
| 539 | 513 |
| 540 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 514 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
| 541 int on; | 515 int on; |
| 542 socklen_t len = sizeof(on); | 516 socklen_t len = sizeof(on); |
| 543 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 517 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 544 SOL_SOCKET, | 518 reinterpret_cast<char*>(&on), &len)); |
| 545 SO_BROADCAST, | |
| 546 reinterpret_cast<char *>(&on), | |
| 547 &len)); | |
| 548 if (err == 0) { | 519 if (err == 0) { |
| 549 *enabled = (on == 1); | 520 *enabled = (on == 1); |
| 550 } | 521 } |
| 551 return (err == 0); | 522 return (err == 0); |
| 552 } | 523 } |
| 553 | 524 |
| 554 | 525 |
| 555 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 526 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
| 556 int on = enabled ? 1 : 0; | 527 int on = enabled ? 1 : 0; |
| 557 return NO_RETRY_EXPECTED(setsockopt(fd, | 528 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 558 SOL_SOCKET, | 529 reinterpret_cast<char*>(&on), |
| 559 SO_BROADCAST, | |
| 560 reinterpret_cast<char *>(&on), | |
| 561 sizeof(on))) == 0; | 530 sizeof(on))) == 0; |
| 562 } | 531 } |
| 563 | 532 |
| 564 | 533 |
| 565 bool Socket::JoinMulticast( | 534 bool Socket::JoinMulticast(intptr_t fd, |
| 566 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { | 535 const RawAddr& addr, |
| 536 const RawAddr&, |
| 537 int interfaceIndex) { |
| 567 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 538 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 568 struct group_req mreq; | 539 struct group_req mreq; |
| 569 mreq.gr_interface = interfaceIndex; | 540 mreq.gr_interface = interfaceIndex; |
| 570 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 541 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 571 return NO_RETRY_EXPECTED(setsockopt( | 542 return NO_RETRY_EXPECTED( |
| 572 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 543 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| 573 } | 544 } |
| 574 | 545 |
| 575 | 546 |
| 576 bool Socket::LeaveMulticast( | 547 bool Socket::LeaveMulticast(intptr_t fd, |
| 577 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { | 548 const RawAddr& addr, |
| 549 const RawAddr&, |
| 550 int interfaceIndex) { |
| 578 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 551 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 579 struct group_req mreq; | 552 struct group_req mreq; |
| 580 mreq.gr_interface = interfaceIndex; | 553 mreq.gr_interface = interfaceIndex; |
| 581 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 554 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 582 return NO_RETRY_EXPECTED(setsockopt( | 555 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, |
| 583 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 556 sizeof(mreq))) == 0; |
| 584 } | 557 } |
| 585 | 558 |
| 586 } // namespace bin | 559 } // namespace bin |
| 587 } // namespace dart | 560 } // namespace dart |
| 588 | 561 |
| 589 #endif // defined(TARGET_OS_ANDROID) | 562 #endif // defined(TARGET_OS_ANDROID) |
| 590 | 563 |
| 591 #endif // !defined(DART_IO_DISABLED) | 564 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |