| 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
| 10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
| 11 #include <string.h> // NOLINT | 11 #include <string.h> // NOLINT |
| 12 #include <sys/stat.h> // NOLINT | 12 #include <sys/stat.h> // NOLINT |
| 13 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
| 15 | 15 |
| 16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
| 17 #include "bin/file.h" | 17 #include "bin/file.h" |
| 18 #include "bin/socket.h" | 18 #include "bin/socket.h" |
| 19 | 19 |
| 20 #include "platform/signal_blocker.h" | 20 #include "platform/signal_blocker.h" |
| 21 | 21 |
| 22 | 22 |
| 23 namespace dart { | 23 namespace dart { |
| 24 namespace bin { | 24 namespace bin { |
| 25 | 25 |
| 26 SocketAddress::SocketAddress(struct sockaddr* sa) { | 26 SocketAddress::SocketAddress(struct sockaddr* sa) { |
| 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 28 if (!Socket::FormatNumericAddress( | 28 if (!Socket::FormatNumericAddress( |
| 29 reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 29 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { |
| 30 as_string_[0] = 0; | 30 as_string_[0] = 0; |
| 31 } | 31 } |
| 32 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa)); | 32 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
| 33 memmove(reinterpret_cast<void *>(&addr_), sa, salen); | 33 memmove(reinterpret_cast<void *>(&addr_), sa, salen); |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) { | 37 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { |
| 38 socklen_t salen = SocketAddress::GetAddrLength(addr); | 38 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 39 if (NO_RETRY_EXPECTED(getnameinfo(&addr->addr, | 39 if (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, |
| 40 salen, | 40 salen, |
| 41 address, | 41 address, |
| 42 len, | 42 len, |
| 43 NULL, | 43 NULL, |
| 44 0, | 44 0, |
| 45 NI_NUMERICHOST)) != 0) { | 45 NI_NUMERICHOST)) != 0) { |
| 46 return false; | 46 return false; |
| 47 } | 47 } |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 bool Socket::Initialize() { | 52 bool Socket::Initialize() { |
| 53 // Nothing to do on Android. | 53 // Nothing to do on Android. |
| 54 return true; | 54 return true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 | 57 |
| 58 static intptr_t Create(RawAddr addr) { | 58 static intptr_t Create(const RawAddr& addr) { |
| 59 intptr_t fd; | 59 intptr_t fd; |
| 60 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 60 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 61 if (fd < 0) { | 61 if (fd < 0) { |
| 62 return -1; | 62 return -1; |
| 63 } | 63 } |
| 64 FDUtils::SetCloseOnExec(fd); | 64 FDUtils::SetCloseOnExec(fd); |
| 65 return fd; | 65 return fd; |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 static intptr_t Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 69 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 70 SocketAddress::SetAddrPort(&addr, port); | |
| 71 intptr_t result = TEMP_FAILURE_RETRY( | 70 intptr_t result = TEMP_FAILURE_RETRY( |
| 72 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); | 71 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 73 if (result == 0 || errno == EINPROGRESS) { | 72 if (result == 0 || errno == EINPROGRESS) { |
| 74 return fd; | 73 return fd; |
| 75 } | 74 } |
| 76 VOID_TEMP_FAILURE_RETRY(close(fd)); | 75 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 77 return -1; | 76 return -1; |
| 78 } | 77 } |
| 79 | 78 |
| 80 | 79 |
| 81 intptr_t Socket::CreateConnect(const RawAddr& addr, const intptr_t port) { | 80 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
| 82 intptr_t fd = Create(addr); | 81 intptr_t fd = Create(addr); |
| 83 if (fd < 0) { | 82 if (fd < 0) { |
| 84 return fd; | 83 return fd; |
| 85 } | 84 } |
| 86 | 85 |
| 87 FDUtils::SetNonBlocking(fd); | 86 FDUtils::SetNonBlocking(fd); |
| 88 | 87 |
| 89 return Connect(fd, addr, port); | 88 return Connect(fd, addr); |
| 90 } | 89 } |
| 91 | 90 |
| 92 | 91 |
| 93 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 92 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 94 const intptr_t port, | |
| 95 const RawAddr& source_addr) { | 93 const RawAddr& source_addr) { |
| 96 intptr_t fd = Create(addr); | 94 intptr_t fd = Create(addr); |
| 97 if (fd < 0) { | 95 if (fd < 0) { |
| 98 return fd; | 96 return fd; |
| 99 } | 97 } |
| 100 | 98 |
| 101 intptr_t result = TEMP_FAILURE_RETRY( | 99 intptr_t result = TEMP_FAILURE_RETRY( |
| 102 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(&source_addr))); | 100 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 103 if (result != 0 && errno != EINPROGRESS) { | 101 if (result != 0 && errno != EINPROGRESS) { |
| 104 VOID_TEMP_FAILURE_RETRY(close(fd)); | 102 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 105 return -1; | 103 return -1; |
| 106 } | 104 } |
| 107 | 105 |
| 108 return Connect(fd, addr, port); | 106 return Connect(fd, addr); |
| 109 } | 107 } |
| 110 | 108 |
| 111 | 109 |
| 112 intptr_t Socket::Available(intptr_t fd) { | 110 intptr_t Socket::Available(intptr_t fd) { |
| 113 return FDUtils::AvailableBytes(fd); | 111 return FDUtils::AvailableBytes(fd); |
| 114 } | 112 } |
| 115 | 113 |
| 116 | 114 |
| 117 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) { |
| 118 ASSERT(fd >= 0); | 116 ASSERT(fd >= 0); |
| 119 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)); |
| 120 ASSERT(EAGAIN == EWOULDBLOCK); | 118 ASSERT(EAGAIN == EWOULDBLOCK); |
| 121 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 119 if (read_bytes == -1 && errno == EWOULDBLOCK) { |
| 122 // 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 |
| 123 // as the number of bytes written. | 121 // as the number of bytes written. |
| 124 read_bytes = 0; | 122 read_bytes = 0; |
| 125 } | 123 } |
| 126 return read_bytes; | 124 return read_bytes; |
| 127 } | 125 } |
| 128 | 126 |
| 129 | 127 |
| 130 intptr_t Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes, | 128 intptr_t Socket::RecvFrom( |
| 131 RawAddr* addr) { | 129 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { |
| 132 ASSERT(fd >= 0); | 130 ASSERT(fd >= 0); |
| 133 socklen_t addr_len = sizeof(addr->ss); | 131 socklen_t addr_len = sizeof(addr->ss); |
| 134 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 132 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 135 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 133 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 136 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 134 if (read_bytes == -1 && errno == EWOULDBLOCK) { |
| 137 // If the read would block we need to retry and therefore return 0 | 135 // If the read would block we need to retry and therefore return 0 |
| 138 // as the number of bytes written. | 136 // as the number of bytes written. |
| 139 read_bytes = 0; | 137 read_bytes = 0; |
| 140 } | 138 } |
| 141 return read_bytes; | 139 return read_bytes; |
| 142 } | 140 } |
| 143 | 141 |
| 144 | 142 |
| 145 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 143 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
| 146 ASSERT(fd >= 0); | 144 ASSERT(fd >= 0); |
| 147 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 145 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 148 ASSERT(EAGAIN == EWOULDBLOCK); | 146 ASSERT(EAGAIN == EWOULDBLOCK); |
| 149 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 147 if (written_bytes == -1 && errno == EWOULDBLOCK) { |
| 150 // If the would block we need to retry and therefore return 0 as | 148 // If the would block we need to retry and therefore return 0 as |
| 151 // the number of bytes written. | 149 // the number of bytes written. |
| 152 written_bytes = 0; | 150 written_bytes = 0; |
| 153 } | 151 } |
| 154 return written_bytes; | 152 return written_bytes; |
| 155 } | 153 } |
| 156 | 154 |
| 157 | 155 |
| 158 intptr_t Socket::SendTo(intptr_t fd, const void* buffer, intptr_t num_bytes, | 156 intptr_t Socket::SendTo( |
| 159 RawAddr addr) { | 157 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { |
| 160 ASSERT(fd >= 0); | 158 ASSERT(fd >= 0); |
| 161 ssize_t written_bytes = TEMP_FAILURE_RETRY( | 159 ssize_t written_bytes = TEMP_FAILURE_RETRY( |
| 162 sendto(fd, buffer, num_bytes, 0, | 160 sendto(fd, buffer, num_bytes, 0, |
| 163 &addr.addr, SocketAddress::GetAddrLength(&addr))); | 161 &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 164 ASSERT(EAGAIN == EWOULDBLOCK); | 162 ASSERT(EAGAIN == EWOULDBLOCK); |
| 165 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 163 if (written_bytes == -1 && errno == EWOULDBLOCK) { |
| 166 // If the would block we need to retry and therefore return 0 as | 164 // If the would block we need to retry and therefore return 0 as |
| 167 // the number of bytes written. | 165 // the number of bytes written. |
| 168 written_bytes = 0; | 166 written_bytes = 0; |
| 169 } | 167 } |
| 170 return written_bytes; | 168 return written_bytes; |
| 171 } | 169 } |
| 172 | 170 |
| 173 | 171 |
| 174 intptr_t Socket::GetPort(intptr_t fd) { | 172 intptr_t Socket::GetPort(intptr_t fd) { |
| 175 ASSERT(fd >= 0); | 173 ASSERT(fd >= 0); |
| 176 RawAddr raw; | 174 RawAddr raw; |
| 177 socklen_t size = sizeof(raw); | 175 socklen_t size = sizeof(raw); |
| 178 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 176 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 179 return 0; | 177 return 0; |
| 180 } | 178 } |
| 181 return SocketAddress::GetAddrPort(&raw); | 179 return SocketAddress::GetAddrPort(raw); |
| 182 } | 180 } |
| 183 | 181 |
| 184 | 182 |
| 185 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 183 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 186 ASSERT(fd >= 0); | 184 ASSERT(fd >= 0); |
| 187 RawAddr raw; | 185 RawAddr raw; |
| 188 socklen_t size = sizeof(raw); | 186 socklen_t size = sizeof(raw); |
| 189 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 187 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
| 190 return NULL; | 188 return NULL; |
| 191 } | 189 } |
| 192 *port = SocketAddress::GetAddrPort(&raw); | 190 *port = SocketAddress::GetAddrPort(raw); |
| 193 return new SocketAddress(&raw.addr); | 191 return new SocketAddress(&raw.addr); |
| 194 } | 192 } |
| 195 | 193 |
| 196 | 194 |
| 197 void Socket::GetError(intptr_t fd, OSError* os_error) { | 195 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 198 int errorNumber; | 196 int errorNumber; |
| 199 socklen_t len = sizeof(errorNumber); | 197 socklen_t len = sizeof(errorNumber); |
| 200 getsockopt(fd, | 198 getsockopt(fd, |
| 201 SOL_SOCKET, | 199 SOL_SOCKET, |
| 202 SO_ERROR, | 200 SO_ERROR, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 255 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 258 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 256 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 259 i++; | 257 i++; |
| 260 } | 258 } |
| 261 } | 259 } |
| 262 freeaddrinfo(info); | 260 freeaddrinfo(info); |
| 263 return addresses; | 261 return addresses; |
| 264 } | 262 } |
| 265 | 263 |
| 266 | 264 |
| 267 bool Socket::ReverseLookup(RawAddr addr, | 265 bool Socket::ReverseLookup(const RawAddr &addr, |
| 268 char* host, | 266 char* host, |
| 269 intptr_t host_len, | 267 intptr_t host_len, |
| 270 OSError** os_error) { | 268 OSError** os_error) { |
| 271 ASSERT(host_len >= NI_MAXHOST); | 269 ASSERT(host_len >= NI_MAXHOST); |
| 272 int status = NO_RETRY_EXPECTED(getnameinfo( | 270 int status = NO_RETRY_EXPECTED(getnameinfo( |
| 273 &addr.addr, | 271 &addr.addr, |
| 274 SocketAddress::GetAddrLength(&addr), | 272 SocketAddress::GetAddrLength(addr), |
| 275 host, | 273 host, |
| 276 host_len, | 274 host_len, |
| 277 NULL, | 275 NULL, |
| 278 0, | 276 0, |
| 279 NI_NAMEREQD)); | 277 NI_NAMEREQD)); |
| 280 if (status != 0) { | 278 if (status != 0) { |
| 281 ASSERT(*os_error == NULL); | 279 ASSERT(*os_error == NULL); |
| 282 *os_error = new OSError(status, | 280 *os_error = new OSError(status, |
| 283 gai_strerror(status), | 281 gai_strerror(status), |
| 284 OSError::kGetAddressInfo); | 282 OSError::kGetAddressInfo); |
| 285 return false; | 283 return false; |
| 286 } | 284 } |
| 287 return true; | 285 return true; |
| 288 } | 286 } |
| 289 | 287 |
| 290 | 288 |
| 291 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 289 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 292 int result; | 290 int result; |
| 293 if (type == SocketAddress::TYPE_IPV4) { | 291 if (type == SocketAddress::TYPE_IPV4) { |
| 294 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 292 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 295 } else { | 293 } else { |
| 296 ASSERT(type == SocketAddress::TYPE_IPV6); | 294 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 297 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 295 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 298 } | 296 } |
| 299 return result == 1; | 297 return result == 1; |
| 300 } | 298 } |
| 301 | 299 |
| 302 | 300 |
| 303 intptr_t Socket::CreateBindDatagram( | 301 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
| 304 RawAddr* addr, intptr_t port, bool reuseAddress) { | |
| 305 intptr_t fd; | 302 intptr_t fd; |
| 306 | 303 |
| 307 fd = NO_RETRY_EXPECTED(socket(addr->addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 304 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
| 308 if (fd < 0) return -1; | 305 if (fd < 0) return -1; |
| 309 | 306 |
| 310 FDUtils::SetCloseOnExec(fd); | 307 FDUtils::SetCloseOnExec(fd); |
| 311 | 308 |
| 312 if (reuseAddress) { | 309 if (reuseAddress) { |
| 313 int optval = 1; | 310 int optval = 1; |
| 314 VOID_NO_RETRY_EXPECTED( | 311 VOID_NO_RETRY_EXPECTED( |
| 315 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 312 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 316 } | 313 } |
| 317 | 314 |
| 318 SocketAddress::SetAddrPort(addr, port); | |
| 319 if (NO_RETRY_EXPECTED( | 315 if (NO_RETRY_EXPECTED( |
| 320 bind(fd, | 316 bind(fd, |
| 321 &addr->addr, | 317 &addr.addr, |
| 322 SocketAddress::GetAddrLength(addr))) < 0) { | 318 SocketAddress::GetAddrLength(addr))) < 0) { |
| 323 VOID_TEMP_FAILURE_RETRY(close(fd)); | 319 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 324 return -1; | 320 return -1; |
| 325 } | 321 } |
| 326 | 322 |
| 327 FDUtils::SetNonBlocking(fd); | 323 FDUtils::SetNonBlocking(fd); |
| 328 return fd; | 324 return fd; |
| 329 } | 325 } |
| 330 | 326 |
| 331 | 327 |
| 332 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 328 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
| 333 int type, | 329 int type, |
| 334 OSError** os_error) { | 330 OSError** os_error) { |
| 335 // The ifaddrs.h header is not provided on Android. An Android | 331 // The ifaddrs.h header is not provided on Android. An Android |
| 336 // implementation would have to use IOCTL or netlink. | 332 // implementation would have to use IOCTL or netlink. |
| 337 return NULL; | 333 return NULL; |
| 338 } | 334 } |
| 339 | 335 |
| 340 | 336 |
| 341 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 337 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
| 342 intptr_t port, | |
| 343 intptr_t backlog, | 338 intptr_t backlog, |
| 344 bool v6_only) { | 339 bool v6_only) { |
| 345 intptr_t fd; | 340 intptr_t fd; |
| 346 | 341 |
| 347 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 342 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 348 if (fd < 0) return -1; | 343 if (fd < 0) return -1; |
| 349 | 344 |
| 350 FDUtils::SetCloseOnExec(fd); | 345 FDUtils::SetCloseOnExec(fd); |
| 351 | 346 |
| 352 int optval = 1; | 347 int optval = 1; |
| 353 VOID_NO_RETRY_EXPECTED( | 348 VOID_NO_RETRY_EXPECTED( |
| 354 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 349 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 355 | 350 |
| 356 if (addr.ss.ss_family == AF_INET6) { | 351 if (addr.ss.ss_family == AF_INET6) { |
| 357 optval = v6_only ? 1 : 0; | 352 optval = v6_only ? 1 : 0; |
| 358 VOID_NO_RETRY_EXPECTED( | 353 VOID_NO_RETRY_EXPECTED( |
| 359 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 354 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 360 } | 355 } |
| 361 | 356 |
| 362 SocketAddress::SetAddrPort(&addr, port); | |
| 363 if (NO_RETRY_EXPECTED( | 357 if (NO_RETRY_EXPECTED( |
| 364 bind(fd, | 358 bind(fd, |
| 365 &addr.addr, | 359 &addr.addr, |
| 366 SocketAddress::GetAddrLength(&addr))) < 0) { | 360 SocketAddress::GetAddrLength(addr))) < 0) { |
| 367 VOID_TEMP_FAILURE_RETRY(close(fd)); | 361 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 368 return -1; | 362 return -1; |
| 369 } | 363 } |
| 370 | 364 |
| 371 // Test for invalid socket port 65535 (some browsers disallow it). | 365 // Test for invalid socket port 65535 (some browsers disallow it). |
| 372 if (port == 0 && Socket::GetPort(fd) == 65535) { | 366 if (SocketAddress::GetAddrPort(addr) == 0 && Socket::GetPort(fd) == 65535) { |
| 373 // Don't close the socket until we have created a new socket, ensuring | 367 // Don't close the socket until we have created a new socket, ensuring |
| 374 // that we do not get the bad port number again. | 368 // that we do not get the bad port number again. |
| 375 intptr_t new_fd = CreateBindListen(addr, 0, backlog, v6_only); | 369 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
| 376 int err = errno; | 370 int err = errno; |
| 377 VOID_TEMP_FAILURE_RETRY(close(fd)); | 371 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 378 errno = err; | 372 errno = err; |
| 379 return new_fd; | 373 return new_fd; |
| 380 } | 374 } |
| 381 | 375 |
| 382 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 376 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 383 VOID_TEMP_FAILURE_RETRY(close(fd)); | 377 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 384 return -1; | 378 return -1; |
| 385 } | 379 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 int on = enabled ? 1 : 0; | 531 int on = enabled ? 1 : 0; |
| 538 return NO_RETRY_EXPECTED(setsockopt(fd, | 532 return NO_RETRY_EXPECTED(setsockopt(fd, |
| 539 SOL_SOCKET, | 533 SOL_SOCKET, |
| 540 SO_BROADCAST, | 534 SO_BROADCAST, |
| 541 reinterpret_cast<char *>(&on), | 535 reinterpret_cast<char *>(&on), |
| 542 sizeof(on))) == 0; | 536 sizeof(on))) == 0; |
| 543 } | 537 } |
| 544 | 538 |
| 545 | 539 |
| 546 bool Socket::JoinMulticast( | 540 bool Socket::JoinMulticast( |
| 547 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { | 541 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { |
| 548 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 542 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
| 549 struct group_req mreq; | 543 struct group_req mreq; |
| 550 mreq.gr_interface = interfaceIndex; | 544 mreq.gr_interface = interfaceIndex; |
| 551 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 545 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
| 552 return NO_RETRY_EXPECTED(setsockopt( | 546 return NO_RETRY_EXPECTED(setsockopt( |
| 553 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 547 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| 554 } | 548 } |
| 555 | 549 |
| 556 | 550 |
| 557 bool Socket::LeaveMulticast( | 551 bool Socket::LeaveMulticast( |
| 558 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { | 552 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { |
| 559 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 553 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
| 560 struct group_req mreq; | 554 struct group_req mreq; |
| 561 mreq.gr_interface = interfaceIndex; | 555 mreq.gr_interface = interfaceIndex; |
| 562 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 556 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 563 return NO_RETRY_EXPECTED(setsockopt( | 557 return NO_RETRY_EXPECTED(setsockopt( |
| 564 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 558 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
| 565 } | 559 } |
| 566 | 560 |
| 567 } // namespace bin | 561 } // namespace bin |
| 568 } // namespace dart | 562 } // namespace dart |
| 569 | 563 |
| 570 #endif // defined(TARGET_OS_ANDROID) | 564 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |