| 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(HOST_OS_ANDROID) | 8 #if defined(HOST_OS_ANDROID) |
| 9 | 9 |
| 10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
| 11 #include "bin/socket_android.h" | 11 #include "bin/socket_base_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(*reinterpret_cast<RawAddr*>(sa), as_string_, | 30 if (!SocketBase::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), |
| 31 INET6_ADDRSTRLEN)) { | 31 as_string_, INET6_ADDRSTRLEN)) { |
| 32 as_string_[0] = 0; | 32 as_string_[0] = 0; |
| 33 } | 33 } |
| 34 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 34 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
| 35 memmove(reinterpret_cast<void*>(&addr_), sa, salen); | 35 memmove(reinterpret_cast<void*>(&addr_), sa, salen); |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 39 bool SocketBase::FormatNumericAddress(const RawAddr& addr, |
| 40 char* address, |
| 41 int len) { |
| 40 socklen_t salen = SocketAddress::GetAddrLength(addr); | 42 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 41 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, | 43 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, |
| 42 0, NI_NUMERICHOST)) == 0); | 44 0, NI_NUMERICHOST)) == 0); |
| 43 } | 45 } |
| 44 | 46 |
| 45 | 47 |
| 46 Socket::Socket(intptr_t fd) | 48 bool SocketBase::IsBindError(intptr_t error_number) { |
| 47 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | |
| 48 | |
| 49 | |
| 50 void Socket::SetClosedFd() { | |
| 51 fd_ = kClosedFd; | |
| 52 } | |
| 53 | |
| 54 | |
| 55 bool Socket::Initialize() { | |
| 56 // Nothing to do on Android. | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 | |
| 61 static intptr_t Create(const RawAddr& addr) { | |
| 62 intptr_t fd; | |
| 63 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | |
| 64 if (fd < 0) { | |
| 65 return -1; | |
| 66 } | |
| 67 if (!FDUtils::SetCloseOnExec(fd)) { | |
| 68 FDUtils::SaveErrorAndClose(fd); | |
| 69 return -1; | |
| 70 } | |
| 71 return fd; | |
| 72 } | |
| 73 | |
| 74 | |
| 75 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | |
| 76 intptr_t result = TEMP_FAILURE_RETRY( | |
| 77 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | |
| 78 if ((result == 0) || (errno == EINPROGRESS)) { | |
| 79 return fd; | |
| 80 } | |
| 81 FDUtils::SaveErrorAndClose(fd); | |
| 82 return -1; | |
| 83 } | |
| 84 | |
| 85 | |
| 86 intptr_t Socket::CreateConnect(const RawAddr& addr) { | |
| 87 intptr_t fd = Create(addr); | |
| 88 if (fd < 0) { | |
| 89 return fd; | |
| 90 } | |
| 91 | |
| 92 if (!FDUtils::SetNonBlocking(fd)) { | |
| 93 FDUtils::SaveErrorAndClose(fd); | |
| 94 return -1; | |
| 95 } | |
| 96 return Connect(fd, addr); | |
| 97 } | |
| 98 | |
| 99 | |
| 100 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | |
| 101 const RawAddr& source_addr) { | |
| 102 intptr_t fd = Create(addr); | |
| 103 if (fd < 0) { | |
| 104 return fd; | |
| 105 } | |
| 106 | |
| 107 intptr_t result = TEMP_FAILURE_RETRY( | |
| 108 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | |
| 109 if ((result != 0) && (errno != EINPROGRESS)) { | |
| 110 FDUtils::SaveErrorAndClose(fd); | |
| 111 return -1; | |
| 112 } | |
| 113 | |
| 114 return Connect(fd, addr); | |
| 115 } | |
| 116 | |
| 117 | |
| 118 bool Socket::IsBindError(intptr_t error_number) { | |
| 119 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 49 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
| 120 error_number == EINVAL; | 50 error_number == EINVAL; |
| 121 } | 51 } |
| 122 | 52 |
| 123 | 53 |
| 124 intptr_t Socket::Available(intptr_t fd) { | 54 intptr_t SocketBase::Available(intptr_t fd) { |
| 125 return FDUtils::AvailableBytes(fd); | 55 return FDUtils::AvailableBytes(fd); |
| 126 } | 56 } |
| 127 | 57 |
| 128 | 58 |
| 129 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 59 intptr_t SocketBase::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 130 ASSERT(fd >= 0); | 60 ASSERT(fd >= 0); |
| 131 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 61 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 132 ASSERT(EAGAIN == EWOULDBLOCK); | 62 ASSERT(EAGAIN == EWOULDBLOCK); |
| 133 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 63 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 134 // If the read would block we need to retry and therefore return 0 | 64 // If the read would block we need to retry and therefore return 0 |
| 135 // as the number of bytes written. | 65 // as the number of bytes written. |
| 136 read_bytes = 0; | 66 read_bytes = 0; |
| 137 } | 67 } |
| 138 return read_bytes; | 68 return read_bytes; |
| 139 } | 69 } |
| 140 | 70 |
| 141 | 71 |
| 142 intptr_t Socket::RecvFrom(intptr_t fd, | 72 intptr_t SocketBase::RecvFrom(intptr_t fd, |
| 143 void* buffer, | 73 void* buffer, |
| 144 intptr_t num_bytes, | 74 intptr_t num_bytes, |
| 145 RawAddr* addr) { | 75 RawAddr* addr) { |
| 146 ASSERT(fd >= 0); | 76 ASSERT(fd >= 0); |
| 147 socklen_t addr_len = sizeof(addr->ss); | 77 socklen_t addr_len = sizeof(addr->ss); |
| 148 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 78 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 149 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 79 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 150 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 80 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 151 // If the read would block we need to retry and therefore return 0 | 81 // If the read would block we need to retry and therefore return 0 |
| 152 // as the number of bytes written. | 82 // as the number of bytes written. |
| 153 read_bytes = 0; | 83 read_bytes = 0; |
| 154 } | 84 } |
| 155 return read_bytes; | 85 return read_bytes; |
| 156 } | 86 } |
| 157 | 87 |
| 158 | 88 |
| 159 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 89 intptr_t SocketBase::Write(intptr_t fd, |
| 90 const void* buffer, |
| 91 intptr_t num_bytes) { |
| 160 ASSERT(fd >= 0); | 92 ASSERT(fd >= 0); |
| 161 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 93 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 162 ASSERT(EAGAIN == EWOULDBLOCK); | 94 ASSERT(EAGAIN == EWOULDBLOCK); |
| 163 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 95 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 164 // If the would block we need to retry and therefore return 0 as | 96 // If the would block we need to retry and therefore return 0 as |
| 165 // the number of bytes written. | 97 // the number of bytes written. |
| 166 written_bytes = 0; | 98 written_bytes = 0; |
| 167 } | 99 } |
| 168 return written_bytes; | 100 return written_bytes; |
| 169 } | 101 } |
| 170 | 102 |
| 171 | 103 |
| 172 intptr_t Socket::SendTo(intptr_t fd, | 104 intptr_t SocketBase::SendTo(intptr_t fd, |
| 173 const void* buffer, | 105 const void* buffer, |
| 174 intptr_t num_bytes, | 106 intptr_t num_bytes, |
| 175 const RawAddr& addr) { | 107 const RawAddr& addr) { |
| 176 ASSERT(fd >= 0); | 108 ASSERT(fd >= 0); |
| 177 ssize_t written_bytes = | 109 ssize_t written_bytes = |
| 178 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, | 110 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
| 179 SocketAddress::GetAddrLength(addr))); | 111 SocketAddress::GetAddrLength(addr))); |
| 180 ASSERT(EAGAIN == EWOULDBLOCK); | 112 ASSERT(EAGAIN == EWOULDBLOCK); |
| 181 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 113 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 182 // If the would block we need to retry and therefore return 0 as | 114 // If the would block we need to retry and therefore return 0 as |
| 183 // the number of bytes written. | 115 // the number of bytes written. |
| 184 written_bytes = 0; | 116 written_bytes = 0; |
| 185 } | 117 } |
| 186 return written_bytes; | 118 return written_bytes; |
| 187 } | 119 } |
| 188 | 120 |
| 189 | 121 |
| 190 intptr_t Socket::GetPort(intptr_t fd) { | 122 intptr_t SocketBase::GetPort(intptr_t fd) { |
| 191 ASSERT(fd >= 0); | 123 ASSERT(fd >= 0); |
| 192 RawAddr raw; | 124 RawAddr raw; |
| 193 socklen_t size = sizeof(raw); | 125 socklen_t size = sizeof(raw); |
| 194 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 126 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 195 return 0; | 127 return 0; |
| 196 } | 128 } |
| 197 return SocketAddress::GetAddrPort(raw); | 129 return SocketAddress::GetAddrPort(raw); |
| 198 } | 130 } |
| 199 | 131 |
| 200 | 132 |
| 201 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 133 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 202 ASSERT(fd >= 0); | 134 ASSERT(fd >= 0); |
| 203 RawAddr raw; | 135 RawAddr raw; |
| 204 socklen_t size = sizeof(raw); | 136 socklen_t size = sizeof(raw); |
| 205 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 137 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
| 206 return NULL; | 138 return NULL; |
| 207 } | 139 } |
| 208 *port = SocketAddress::GetAddrPort(raw); | 140 *port = SocketAddress::GetAddrPort(raw); |
| 209 return new SocketAddress(&raw.addr); | 141 return new SocketAddress(&raw.addr); |
| 210 } | 142 } |
| 211 | 143 |
| 212 | 144 |
| 213 void Socket::GetError(intptr_t fd, OSError* os_error) { | 145 void SocketBase::GetError(intptr_t fd, OSError* os_error) { |
| 214 int errorNumber; | 146 int errorNumber; |
| 215 socklen_t len = sizeof(errorNumber); | 147 socklen_t len = sizeof(errorNumber); |
| 216 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), | 148 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), |
| 217 &len); | 149 &len); |
| 218 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); | 150 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); |
| 219 } | 151 } |
| 220 | 152 |
| 221 | 153 |
| 222 int Socket::GetType(intptr_t fd) { | 154 int SocketBase::GetType(intptr_t fd) { |
| 223 struct stat buf; | 155 struct stat buf; |
| 224 int result = fstat(fd, &buf); | 156 int result = fstat(fd, &buf); |
| 225 if (result == -1) { | 157 if (result == -1) { |
| 226 return -1; | 158 return -1; |
| 227 } | 159 } |
| 228 if (S_ISCHR(buf.st_mode)) { | 160 if (S_ISCHR(buf.st_mode)) { |
| 229 return File::kTerminal; | 161 return File::kTerminal; |
| 230 } | 162 } |
| 231 if (S_ISFIFO(buf.st_mode)) { | 163 if (S_ISFIFO(buf.st_mode)) { |
| 232 return File::kPipe; | 164 return File::kPipe; |
| 233 } | 165 } |
| 234 if (S_ISREG(buf.st_mode)) { | 166 if (S_ISREG(buf.st_mode)) { |
| 235 return File::kFile; | 167 return File::kFile; |
| 236 } | 168 } |
| 237 return File::kOther; | 169 return File::kOther; |
| 238 } | 170 } |
| 239 | 171 |
| 240 | 172 |
| 241 intptr_t Socket::GetStdioHandle(intptr_t num) { | 173 intptr_t SocketBase::GetStdioHandle(intptr_t num) { |
| 242 return num; | 174 return num; |
| 243 } | 175 } |
| 244 | 176 |
| 245 | 177 |
| 246 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 178 AddressList<SocketAddress>* SocketBase::LookupAddress(const char* host, |
| 247 int type, | 179 int type, |
| 248 OSError** os_error) { | 180 OSError** os_error) { |
| 249 // Perform a name lookup for a host name. | 181 // Perform a name lookup for a host name. |
| 250 struct addrinfo hints; | 182 struct addrinfo hints; |
| 251 memset(&hints, 0, sizeof(hints)); | 183 memset(&hints, 0, sizeof(hints)); |
| 252 hints.ai_family = SocketAddress::FromType(type); | 184 hints.ai_family = SocketAddress::FromType(type); |
| 253 hints.ai_socktype = SOCK_STREAM; | 185 hints.ai_socktype = SOCK_STREAM; |
| 254 hints.ai_flags = AI_ADDRCONFIG; | 186 hints.ai_flags = AI_ADDRCONFIG; |
| 255 hints.ai_protocol = IPPROTO_TCP; | 187 hints.ai_protocol = IPPROTO_TCP; |
| 256 struct addrinfo* info = NULL; | 188 struct addrinfo* info = NULL; |
| 257 int status = getaddrinfo(host, 0, &hints, &info); | 189 int status = getaddrinfo(host, 0, &hints, &info); |
| 258 if (status != 0) { | 190 if (status != 0) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 279 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 211 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 280 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 212 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 281 i++; | 213 i++; |
| 282 } | 214 } |
| 283 } | 215 } |
| 284 freeaddrinfo(info); | 216 freeaddrinfo(info); |
| 285 return addresses; | 217 return addresses; |
| 286 } | 218 } |
| 287 | 219 |
| 288 | 220 |
| 289 bool Socket::ReverseLookup(const RawAddr& addr, | 221 bool SocketBase::ReverseLookup(const RawAddr& addr, |
| 290 char* host, | 222 char* host, |
| 291 intptr_t host_len, | 223 intptr_t host_len, |
| 292 OSError** os_error) { | 224 OSError** os_error) { |
| 293 ASSERT(host_len >= NI_MAXHOST); | 225 ASSERT(host_len >= NI_MAXHOST); |
| 294 int status = NO_RETRY_EXPECTED( | 226 int status = NO_RETRY_EXPECTED( |
| 295 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, | 227 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
| 296 host_len, NULL, 0, NI_NAMEREQD)); | 228 host_len, NULL, 0, NI_NAMEREQD)); |
| 297 if (status != 0) { | 229 if (status != 0) { |
| 298 ASSERT(*os_error == NULL); | 230 ASSERT(*os_error == NULL); |
| 299 *os_error = | 231 *os_error = |
| 300 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 232 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 301 return false; | 233 return false; |
| 302 } | 234 } |
| 303 return true; | 235 return true; |
| 304 } | 236 } |
| 305 | 237 |
| 306 | 238 |
| 307 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 239 bool SocketBase::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 308 int result; | 240 int result; |
| 309 if (type == SocketAddress::TYPE_IPV4) { | 241 if (type == SocketAddress::TYPE_IPV4) { |
| 310 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 242 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 311 } else { | 243 } else { |
| 312 ASSERT(type == SocketAddress::TYPE_IPV6); | 244 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 313 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 245 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 314 } | 246 } |
| 315 return (result == 1); | 247 return (result == 1); |
| 316 } | 248 } |
| 317 | 249 |
| 318 | 250 |
| 319 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 251 bool SocketBase::ListInterfacesSupported() { |
| 320 intptr_t fd; | |
| 321 | |
| 322 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | |
| 323 if (fd < 0) { | |
| 324 return -1; | |
| 325 } | |
| 326 | |
| 327 if (!FDUtils::SetCloseOnExec(fd)) { | |
| 328 FDUtils::SaveErrorAndClose(fd); | |
| 329 return -1; | |
| 330 } | |
| 331 | |
| 332 if (reuseAddress) { | |
| 333 int optval = 1; | |
| 334 VOID_NO_RETRY_EXPECTED( | |
| 335 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | |
| 336 } | |
| 337 | |
| 338 if (NO_RETRY_EXPECTED( | |
| 339 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | |
| 340 FDUtils::SaveErrorAndClose(fd); | |
| 341 return -1; | |
| 342 } | |
| 343 | |
| 344 if (!FDUtils::SetNonBlocking(fd)) { | |
| 345 FDUtils::SaveErrorAndClose(fd); | |
| 346 return -1; | |
| 347 } | |
| 348 return fd; | |
| 349 } | |
| 350 | |
| 351 | |
| 352 bool Socket::ListInterfacesSupported() { | |
| 353 return false; | 252 return false; |
| 354 } | 253 } |
| 355 | 254 |
| 356 | 255 |
| 357 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 256 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( |
| 358 int type, | 257 int type, |
| 359 OSError** os_error) { | 258 OSError** os_error) { |
| 360 // The ifaddrs.h header is not provided on Android. An Android | 259 // The ifaddrs.h header is not provided on Android. An Android |
| 361 // implementation would have to use IOCTL or netlink. | 260 // implementation would have to use IOCTL or netlink. |
| 362 ASSERT(*os_error == NULL); | 261 ASSERT(*os_error == NULL); |
| 363 *os_error = new OSError(-1, | 262 *os_error = new OSError(-1, |
| 364 "Listing interfaces is not supported " | 263 "Listing interfaces is not supported " |
| 365 "on this platform", | 264 "on this platform", |
| 366 OSError::kSystem); | 265 OSError::kSystem); |
| 367 return NULL; | 266 return NULL; |
| 368 } | 267 } |
| 369 | 268 |
| 370 | 269 |
| 371 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 270 void SocketBase::Close(intptr_t fd) { |
| 372 intptr_t backlog, | |
| 373 bool v6_only) { | |
| 374 intptr_t fd; | |
| 375 | |
| 376 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | |
| 377 if (fd < 0) { | |
| 378 return -1; | |
| 379 } | |
| 380 | |
| 381 if (!FDUtils::SetCloseOnExec(fd)) { | |
| 382 FDUtils::SaveErrorAndClose(fd); | |
| 383 return -1; | |
| 384 } | |
| 385 | |
| 386 int optval = 1; | |
| 387 VOID_NO_RETRY_EXPECTED( | |
| 388 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | |
| 389 | |
| 390 if (addr.ss.ss_family == AF_INET6) { | |
| 391 optval = v6_only ? 1 : 0; | |
| 392 VOID_NO_RETRY_EXPECTED( | |
| 393 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | |
| 394 } | |
| 395 | |
| 396 if (NO_RETRY_EXPECTED( | |
| 397 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | |
| 398 FDUtils::SaveErrorAndClose(fd); | |
| 399 return -1; | |
| 400 } | |
| 401 | |
| 402 // Test for invalid socket port 65535 (some browsers disallow it). | |
| 403 if ((SocketAddress::GetAddrPort(addr)) == 0 && | |
| 404 (Socket::GetPort(fd) == 65535)) { | |
| 405 // Don't close the socket until we have created a new socket, ensuring | |
| 406 // that we do not get the bad port number again. | |
| 407 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | |
| 408 FDUtils::SaveErrorAndClose(fd); | |
| 409 return new_fd; | |
| 410 } | |
| 411 | |
| 412 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | |
| 413 FDUtils::SaveErrorAndClose(fd); | |
| 414 return -1; | |
| 415 } | |
| 416 | |
| 417 if (!FDUtils::SetNonBlocking(fd)) { | |
| 418 FDUtils::SaveErrorAndClose(fd); | |
| 419 return -1; | |
| 420 } | |
| 421 return fd; | |
| 422 } | |
| 423 | |
| 424 | |
| 425 bool ServerSocket::StartAccept(intptr_t fd) { | |
| 426 USE(fd); | |
| 427 return true; | |
| 428 } | |
| 429 | |
| 430 | |
| 431 static bool IsTemporaryAcceptError(int error) { | |
| 432 // On Android a number of protocol errors should be treated as EAGAIN. | |
| 433 // These are the ones for TCP/IP. | |
| 434 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | |
| 435 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | |
| 436 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | |
| 437 (error == ENETUNREACH); | |
| 438 } | |
| 439 | |
| 440 | |
| 441 intptr_t ServerSocket::Accept(intptr_t fd) { | |
| 442 intptr_t socket; | |
| 443 struct sockaddr clientaddr; | |
| 444 socklen_t addrlen = sizeof(clientaddr); | |
| 445 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | |
| 446 if (socket == -1) { | |
| 447 if (IsTemporaryAcceptError(errno)) { | |
| 448 // We need to signal to the caller that this is actually not an | |
| 449 // error. We got woken up from the poll on the listening socket, | |
| 450 // but there is no connection ready to be accepted. | |
| 451 ASSERT(kTemporaryFailure != -1); | |
| 452 socket = kTemporaryFailure; | |
| 453 } | |
| 454 } else { | |
| 455 if (!FDUtils::SetCloseOnExec(socket)) { | |
| 456 FDUtils::SaveErrorAndClose(socket); | |
| 457 return -1; | |
| 458 } | |
| 459 if (!FDUtils::SetNonBlocking(socket)) { | |
| 460 FDUtils::SaveErrorAndClose(socket); | |
| 461 return -1; | |
| 462 } | |
| 463 } | |
| 464 return socket; | |
| 465 } | |
| 466 | |
| 467 | |
| 468 void Socket::Close(intptr_t fd) { | |
| 469 ASSERT(fd >= 0); | 271 ASSERT(fd >= 0); |
| 470 VOID_TEMP_FAILURE_RETRY(close(fd)); | 272 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 471 } | 273 } |
| 472 | 274 |
| 473 | 275 |
| 474 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 276 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { |
| 475 int on; | 277 int on; |
| 476 socklen_t len = sizeof(on); | 278 socklen_t len = sizeof(on); |
| 477 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 279 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 478 reinterpret_cast<void*>(&on), &len)); | 280 reinterpret_cast<void*>(&on), &len)); |
| 479 if (err == 0) { | 281 if (err == 0) { |
| 480 *enabled = (on == 1); | 282 *enabled = (on == 1); |
| 481 } | 283 } |
| 482 return (err == 0); | 284 return (err == 0); |
| 483 } | 285 } |
| 484 | 286 |
| 485 | 287 |
| 486 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 288 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { |
| 487 int on = enabled ? 1 : 0; | 289 int on = enabled ? 1 : 0; |
| 488 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 290 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 489 reinterpret_cast<char*>(&on), | 291 reinterpret_cast<char*>(&on), |
| 490 sizeof(on))) == 0; | 292 sizeof(on))) == 0; |
| 491 } | 293 } |
| 492 | 294 |
| 493 | 295 |
| 494 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 296 bool SocketBase::GetMulticastLoop(intptr_t fd, |
| 297 intptr_t protocol, |
| 298 bool* enabled) { |
| 495 uint8_t on; | 299 uint8_t on; |
| 496 socklen_t len = sizeof(on); | 300 socklen_t len = sizeof(on); |
| 497 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 301 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 498 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 302 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 499 : IPV6_MULTICAST_LOOP; | 303 : IPV6_MULTICAST_LOOP; |
| 500 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 304 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 501 reinterpret_cast<char*>(&on), &len)) == 0) { | 305 reinterpret_cast<char*>(&on), &len)) == 0) { |
| 502 *enabled = (on == 1); | 306 *enabled = (on == 1); |
| 503 return true; | 307 return true; |
| 504 } | 308 } |
| 505 return false; | 309 return false; |
| 506 } | 310 } |
| 507 | 311 |
| 508 | 312 |
| 509 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 313 bool SocketBase::SetMulticastLoop(intptr_t fd, |
| 314 intptr_t protocol, |
| 315 bool enabled) { |
| 510 int on = enabled ? 1 : 0; | 316 int on = enabled ? 1 : 0; |
| 511 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 317 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 512 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 318 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 513 : IPV6_MULTICAST_LOOP; | 319 : IPV6_MULTICAST_LOOP; |
| 514 return NO_RETRY_EXPECTED(setsockopt( | 320 return NO_RETRY_EXPECTED(setsockopt( |
| 515 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == | 321 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
| 516 0; | 322 0; |
| 517 } | 323 } |
| 518 | 324 |
| 519 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 325 bool SocketBase::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| 520 uint8_t v; | 326 uint8_t v; |
| 521 socklen_t len = sizeof(v); | 327 socklen_t len = sizeof(v); |
| 522 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 328 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 523 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 329 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 524 : IPV6_MULTICAST_HOPS; | 330 : IPV6_MULTICAST_HOPS; |
| 525 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 331 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 526 reinterpret_cast<char*>(&v), &len)) == 0) { | 332 reinterpret_cast<char*>(&v), &len)) == 0) { |
| 527 *value = v; | 333 *value = v; |
| 528 return true; | 334 return true; |
| 529 } | 335 } |
| 530 return false; | 336 return false; |
| 531 } | 337 } |
| 532 | 338 |
| 533 | 339 |
| 534 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 340 bool SocketBase::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| 535 int v = value; | 341 int v = value; |
| 536 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 342 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 537 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 343 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 538 : IPV6_MULTICAST_HOPS; | 344 : IPV6_MULTICAST_HOPS; |
| 539 return NO_RETRY_EXPECTED(setsockopt( | 345 return NO_RETRY_EXPECTED(setsockopt( |
| 540 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; | 346 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
| 541 } | 347 } |
| 542 | 348 |
| 543 | 349 |
| 544 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 350 bool SocketBase::GetBroadcast(intptr_t fd, bool* enabled) { |
| 545 int on; | 351 int on; |
| 546 socklen_t len = sizeof(on); | 352 socklen_t len = sizeof(on); |
| 547 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 353 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 548 reinterpret_cast<char*>(&on), &len)); | 354 reinterpret_cast<char*>(&on), &len)); |
| 549 if (err == 0) { | 355 if (err == 0) { |
| 550 *enabled = (on == 1); | 356 *enabled = (on == 1); |
| 551 } | 357 } |
| 552 return (err == 0); | 358 return (err == 0); |
| 553 } | 359 } |
| 554 | 360 |
| 555 | 361 |
| 556 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 362 bool SocketBase::SetBroadcast(intptr_t fd, bool enabled) { |
| 557 int on = enabled ? 1 : 0; | 363 int on = enabled ? 1 : 0; |
| 558 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 364 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 559 reinterpret_cast<char*>(&on), | 365 reinterpret_cast<char*>(&on), |
| 560 sizeof(on))) == 0; | 366 sizeof(on))) == 0; |
| 561 } | 367 } |
| 562 | 368 |
| 563 | 369 |
| 564 bool Socket::JoinMulticast(intptr_t fd, | 370 bool SocketBase::JoinMulticast(intptr_t fd, |
| 565 const RawAddr& addr, | 371 const RawAddr& addr, |
| 566 const RawAddr&, | 372 const RawAddr&, |
| 567 int interfaceIndex) { | 373 int interfaceIndex) { |
| 568 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 374 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 569 struct group_req mreq; | 375 struct group_req mreq; |
| 570 mreq.gr_interface = interfaceIndex; | 376 mreq.gr_interface = interfaceIndex; |
| 571 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 377 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 572 return NO_RETRY_EXPECTED( | 378 return NO_RETRY_EXPECTED( |
| 573 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 379 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| 574 } | 380 } |
| 575 | 381 |
| 576 | 382 |
| 577 bool Socket::LeaveMulticast(intptr_t fd, | 383 bool SocketBase::LeaveMulticast(intptr_t fd, |
| 578 const RawAddr& addr, | 384 const RawAddr& addr, |
| 579 const RawAddr&, | 385 const RawAddr&, |
| 580 int interfaceIndex) { | 386 int interfaceIndex) { |
| 581 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 387 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 582 struct group_req mreq; | 388 struct group_req mreq; |
| 583 mreq.gr_interface = interfaceIndex; | 389 mreq.gr_interface = interfaceIndex; |
| 584 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 390 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 585 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, | 391 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, |
| 586 sizeof(mreq))) == 0; | 392 sizeof(mreq))) == 0; |
| 587 } | 393 } |
| 588 | 394 |
| 589 } // namespace bin | 395 } // namespace bin |
| 590 } // namespace dart | 396 } // namespace dart |
| 591 | 397 |
| 592 #endif // defined(HOST_OS_ANDROID) | 398 #endif // defined(HOST_OS_ANDROID) |
| 593 | 399 |
| 594 #endif // !defined(DART_IO_DISABLED) | 400 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |