| 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_base.h" |
| 11 #include "bin/socket_android.h" | |
| 12 | 11 |
| 13 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT | 13 #include <netinet/tcp.h> // NOLINT |
| 15 #include <stdio.h> // NOLINT | 14 #include <stdio.h> // NOLINT |
| 16 #include <stdlib.h> // NOLINT | 15 #include <stdlib.h> // NOLINT |
| 17 #include <string.h> // NOLINT | 16 #include <string.h> // NOLINT |
| 18 #include <sys/stat.h> // NOLINT | 17 #include <sys/stat.h> // NOLINT |
| 19 #include <unistd.h> // NOLINT | 18 #include <unistd.h> // NOLINT |
| 20 | 19 |
| 21 #include "bin/fdutils.h" | 20 #include "bin/fdutils.h" |
| 22 #include "bin/file.h" | 21 #include "bin/file.h" |
| 22 #include "bin/socket_base_android.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::Initialize() { |
| 40 // Nothing to do on Android. |
| 41 return true; |
| 42 } |
| 43 |
| 44 |
| 45 bool SocketBase::FormatNumericAddress(const RawAddr& addr, |
| 46 char* address, |
| 47 int len) { |
| 40 socklen_t salen = SocketAddress::GetAddrLength(addr); | 48 socklen_t salen = SocketAddress::GetAddrLength(addr); |
| 41 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, | 49 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, |
| 42 0, NI_NUMERICHOST)) == 0); | 50 0, NI_NUMERICHOST)) == 0); |
| 43 } | 51 } |
| 44 | 52 |
| 45 | 53 |
| 46 Socket::Socket(intptr_t fd) | 54 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 || | 55 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
| 120 error_number == EINVAL; | 56 error_number == EINVAL; |
| 121 } | 57 } |
| 122 | 58 |
| 123 | 59 |
| 124 intptr_t Socket::Available(intptr_t fd) { | 60 intptr_t SocketBase::Available(intptr_t fd) { |
| 125 return FDUtils::AvailableBytes(fd); | 61 return FDUtils::AvailableBytes(fd); |
| 126 } | 62 } |
| 127 | 63 |
| 128 | 64 |
| 129 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 65 intptr_t SocketBase::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 130 ASSERT(fd >= 0); | 66 ASSERT(fd >= 0); |
| 131 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 67 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 132 ASSERT(EAGAIN == EWOULDBLOCK); | 68 ASSERT(EAGAIN == EWOULDBLOCK); |
| 133 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 69 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 134 // If the read would block we need to retry and therefore return 0 | 70 // If the read would block we need to retry and therefore return 0 |
| 135 // as the number of bytes written. | 71 // as the number of bytes written. |
| 136 read_bytes = 0; | 72 read_bytes = 0; |
| 137 } | 73 } |
| 138 return read_bytes; | 74 return read_bytes; |
| 139 } | 75 } |
| 140 | 76 |
| 141 | 77 |
| 142 intptr_t Socket::RecvFrom(intptr_t fd, | 78 intptr_t SocketBase::RecvFrom(intptr_t fd, |
| 143 void* buffer, | 79 void* buffer, |
| 144 intptr_t num_bytes, | 80 intptr_t num_bytes, |
| 145 RawAddr* addr) { | 81 RawAddr* addr) { |
| 146 ASSERT(fd >= 0); | 82 ASSERT(fd >= 0); |
| 147 socklen_t addr_len = sizeof(addr->ss); | 83 socklen_t addr_len = sizeof(addr->ss); |
| 148 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 84 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 149 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 85 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 150 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 86 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 151 // If the read would block we need to retry and therefore return 0 | 87 // If the read would block we need to retry and therefore return 0 |
| 152 // as the number of bytes written. | 88 // as the number of bytes written. |
| 153 read_bytes = 0; | 89 read_bytes = 0; |
| 154 } | 90 } |
| 155 return read_bytes; | 91 return read_bytes; |
| 156 } | 92 } |
| 157 | 93 |
| 158 | 94 |
| 159 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 95 intptr_t SocketBase::Write(intptr_t fd, |
| 96 const void* buffer, |
| 97 intptr_t num_bytes) { |
| 160 ASSERT(fd >= 0); | 98 ASSERT(fd >= 0); |
| 161 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 99 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 162 ASSERT(EAGAIN == EWOULDBLOCK); | 100 ASSERT(EAGAIN == EWOULDBLOCK); |
| 163 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 101 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 164 // If the would block we need to retry and therefore return 0 as | 102 // If the would block we need to retry and therefore return 0 as |
| 165 // the number of bytes written. | 103 // the number of bytes written. |
| 166 written_bytes = 0; | 104 written_bytes = 0; |
| 167 } | 105 } |
| 168 return written_bytes; | 106 return written_bytes; |
| 169 } | 107 } |
| 170 | 108 |
| 171 | 109 |
| 172 intptr_t Socket::SendTo(intptr_t fd, | 110 intptr_t SocketBase::SendTo(intptr_t fd, |
| 173 const void* buffer, | 111 const void* buffer, |
| 174 intptr_t num_bytes, | 112 intptr_t num_bytes, |
| 175 const RawAddr& addr) { | 113 const RawAddr& addr) { |
| 176 ASSERT(fd >= 0); | 114 ASSERT(fd >= 0); |
| 177 ssize_t written_bytes = | 115 ssize_t written_bytes = |
| 178 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, | 116 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
| 179 SocketAddress::GetAddrLength(addr))); | 117 SocketAddress::GetAddrLength(addr))); |
| 180 ASSERT(EAGAIN == EWOULDBLOCK); | 118 ASSERT(EAGAIN == EWOULDBLOCK); |
| 181 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 119 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 182 // If the would block we need to retry and therefore return 0 as | 120 // If the would block we need to retry and therefore return 0 as |
| 183 // the number of bytes written. | 121 // the number of bytes written. |
| 184 written_bytes = 0; | 122 written_bytes = 0; |
| 185 } | 123 } |
| 186 return written_bytes; | 124 return written_bytes; |
| 187 } | 125 } |
| 188 | 126 |
| 189 | 127 |
| 190 intptr_t Socket::GetPort(intptr_t fd) { | 128 intptr_t SocketBase::GetPort(intptr_t fd) { |
| 191 ASSERT(fd >= 0); | 129 ASSERT(fd >= 0); |
| 192 RawAddr raw; | 130 RawAddr raw; |
| 193 socklen_t size = sizeof(raw); | 131 socklen_t size = sizeof(raw); |
| 194 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 132 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 195 return 0; | 133 return 0; |
| 196 } | 134 } |
| 197 return SocketAddress::GetAddrPort(raw); | 135 return SocketAddress::GetAddrPort(raw); |
| 198 } | 136 } |
| 199 | 137 |
| 200 | 138 |
| 201 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 139 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 202 ASSERT(fd >= 0); | 140 ASSERT(fd >= 0); |
| 203 RawAddr raw; | 141 RawAddr raw; |
| 204 socklen_t size = sizeof(raw); | 142 socklen_t size = sizeof(raw); |
| 205 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 143 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
| 206 return NULL; | 144 return NULL; |
| 207 } | 145 } |
| 208 *port = SocketAddress::GetAddrPort(raw); | 146 *port = SocketAddress::GetAddrPort(raw); |
| 209 return new SocketAddress(&raw.addr); | 147 return new SocketAddress(&raw.addr); |
| 210 } | 148 } |
| 211 | 149 |
| 212 | 150 |
| 213 void Socket::GetError(intptr_t fd, OSError* os_error) { | 151 void SocketBase::GetError(intptr_t fd, OSError* os_error) { |
| 214 int errorNumber; | 152 int errorNumber; |
| 215 socklen_t len = sizeof(errorNumber); | 153 socklen_t len = sizeof(errorNumber); |
| 216 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), | 154 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), |
| 217 &len); | 155 &len); |
| 218 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); | 156 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); |
| 219 } | 157 } |
| 220 | 158 |
| 221 | 159 |
| 222 int Socket::GetType(intptr_t fd) { | 160 int SocketBase::GetType(intptr_t fd) { |
| 223 struct stat buf; | 161 struct stat buf; |
| 224 int result = fstat(fd, &buf); | 162 int result = fstat(fd, &buf); |
| 225 if (result == -1) { | 163 if (result == -1) { |
| 226 return -1; | 164 return -1; |
| 227 } | 165 } |
| 228 if (S_ISCHR(buf.st_mode)) { | 166 if (S_ISCHR(buf.st_mode)) { |
| 229 return File::kTerminal; | 167 return File::kTerminal; |
| 230 } | 168 } |
| 231 if (S_ISFIFO(buf.st_mode)) { | 169 if (S_ISFIFO(buf.st_mode)) { |
| 232 return File::kPipe; | 170 return File::kPipe; |
| 233 } | 171 } |
| 234 if (S_ISREG(buf.st_mode)) { | 172 if (S_ISREG(buf.st_mode)) { |
| 235 return File::kFile; | 173 return File::kFile; |
| 236 } | 174 } |
| 237 return File::kOther; | 175 return File::kOther; |
| 238 } | 176 } |
| 239 | 177 |
| 240 | 178 |
| 241 intptr_t Socket::GetStdioHandle(intptr_t num) { | 179 intptr_t SocketBase::GetStdioHandle(intptr_t num) { |
| 242 return num; | 180 return num; |
| 243 } | 181 } |
| 244 | 182 |
| 245 | 183 |
| 246 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 184 AddressList<SocketAddress>* SocketBase::LookupAddress(const char* host, |
| 247 int type, | 185 int type, |
| 248 OSError** os_error) { | 186 OSError** os_error) { |
| 249 // Perform a name lookup for a host name. | 187 // Perform a name lookup for a host name. |
| 250 struct addrinfo hints; | 188 struct addrinfo hints; |
| 251 memset(&hints, 0, sizeof(hints)); | 189 memset(&hints, 0, sizeof(hints)); |
| 252 hints.ai_family = SocketAddress::FromType(type); | 190 hints.ai_family = SocketAddress::FromType(type); |
| 253 hints.ai_socktype = SOCK_STREAM; | 191 hints.ai_socktype = SOCK_STREAM; |
| 254 hints.ai_flags = AI_ADDRCONFIG; | 192 hints.ai_flags = AI_ADDRCONFIG; |
| 255 hints.ai_protocol = IPPROTO_TCP; | 193 hints.ai_protocol = IPPROTO_TCP; |
| 256 struct addrinfo* info = NULL; | 194 struct addrinfo* info = NULL; |
| 257 int status = getaddrinfo(host, 0, &hints, &info); | 195 int status = getaddrinfo(host, 0, &hints, &info); |
| 258 if (status != 0) { | 196 if (status != 0) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 279 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 217 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 280 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 218 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 281 i++; | 219 i++; |
| 282 } | 220 } |
| 283 } | 221 } |
| 284 freeaddrinfo(info); | 222 freeaddrinfo(info); |
| 285 return addresses; | 223 return addresses; |
| 286 } | 224 } |
| 287 | 225 |
| 288 | 226 |
| 289 bool Socket::ReverseLookup(const RawAddr& addr, | 227 bool SocketBase::ReverseLookup(const RawAddr& addr, |
| 290 char* host, | 228 char* host, |
| 291 intptr_t host_len, | 229 intptr_t host_len, |
| 292 OSError** os_error) { | 230 OSError** os_error) { |
| 293 ASSERT(host_len >= NI_MAXHOST); | 231 ASSERT(host_len >= NI_MAXHOST); |
| 294 int status = NO_RETRY_EXPECTED( | 232 int status = NO_RETRY_EXPECTED( |
| 295 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, | 233 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
| 296 host_len, NULL, 0, NI_NAMEREQD)); | 234 host_len, NULL, 0, NI_NAMEREQD)); |
| 297 if (status != 0) { | 235 if (status != 0) { |
| 298 ASSERT(*os_error == NULL); | 236 ASSERT(*os_error == NULL); |
| 299 *os_error = | 237 *os_error = |
| 300 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 238 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 301 return false; | 239 return false; |
| 302 } | 240 } |
| 303 return true; | 241 return true; |
| 304 } | 242 } |
| 305 | 243 |
| 306 | 244 |
| 307 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 245 bool SocketBase::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 308 int result; | 246 int result; |
| 309 if (type == SocketAddress::TYPE_IPV4) { | 247 if (type == SocketAddress::TYPE_IPV4) { |
| 310 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 248 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 311 } else { | 249 } else { |
| 312 ASSERT(type == SocketAddress::TYPE_IPV6); | 250 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 313 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 251 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 314 } | 252 } |
| 315 return (result == 1); | 253 return (result == 1); |
| 316 } | 254 } |
| 317 | 255 |
| 318 | 256 |
| 319 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 257 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; | 258 return false; |
| 354 } | 259 } |
| 355 | 260 |
| 356 | 261 |
| 357 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 262 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( |
| 358 int type, | 263 int type, |
| 359 OSError** os_error) { | 264 OSError** os_error) { |
| 360 // The ifaddrs.h header is not provided on Android. An Android | 265 // The ifaddrs.h header is not provided on Android. An Android |
| 361 // implementation would have to use IOCTL or netlink. | 266 // implementation would have to use IOCTL or netlink. |
| 362 ASSERT(*os_error == NULL); | 267 ASSERT(*os_error == NULL); |
| 363 *os_error = new OSError(-1, | 268 *os_error = new OSError(-1, |
| 364 "Listing interfaces is not supported " | 269 "Listing interfaces is not supported " |
| 365 "on this platform", | 270 "on this platform", |
| 366 OSError::kSystem); | 271 OSError::kSystem); |
| 367 return NULL; | 272 return NULL; |
| 368 } | 273 } |
| 369 | 274 |
| 370 | 275 |
| 371 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 276 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); | 277 ASSERT(fd >= 0); |
| 470 VOID_TEMP_FAILURE_RETRY(close(fd)); | 278 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 471 } | 279 } |
| 472 | 280 |
| 473 | 281 |
| 474 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 282 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { |
| 475 int on; | 283 int on; |
| 476 socklen_t len = sizeof(on); | 284 socklen_t len = sizeof(on); |
| 477 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 285 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 478 reinterpret_cast<void*>(&on), &len)); | 286 reinterpret_cast<void*>(&on), &len)); |
| 479 if (err == 0) { | 287 if (err == 0) { |
| 480 *enabled = (on == 1); | 288 *enabled = (on == 1); |
| 481 } | 289 } |
| 482 return (err == 0); | 290 return (err == 0); |
| 483 } | 291 } |
| 484 | 292 |
| 485 | 293 |
| 486 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 294 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { |
| 487 int on = enabled ? 1 : 0; | 295 int on = enabled ? 1 : 0; |
| 488 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 296 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 489 reinterpret_cast<char*>(&on), | 297 reinterpret_cast<char*>(&on), |
| 490 sizeof(on))) == 0; | 298 sizeof(on))) == 0; |
| 491 } | 299 } |
| 492 | 300 |
| 493 | 301 |
| 494 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 302 bool SocketBase::GetMulticastLoop(intptr_t fd, |
| 303 intptr_t protocol, |
| 304 bool* enabled) { |
| 495 uint8_t on; | 305 uint8_t on; |
| 496 socklen_t len = sizeof(on); | 306 socklen_t len = sizeof(on); |
| 497 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 307 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 498 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 308 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 499 : IPV6_MULTICAST_LOOP; | 309 : IPV6_MULTICAST_LOOP; |
| 500 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 310 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 501 reinterpret_cast<char*>(&on), &len)) == 0) { | 311 reinterpret_cast<char*>(&on), &len)) == 0) { |
| 502 *enabled = (on == 1); | 312 *enabled = (on == 1); |
| 503 return true; | 313 return true; |
| 504 } | 314 } |
| 505 return false; | 315 return false; |
| 506 } | 316 } |
| 507 | 317 |
| 508 | 318 |
| 509 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 319 bool SocketBase::SetMulticastLoop(intptr_t fd, |
| 320 intptr_t protocol, |
| 321 bool enabled) { |
| 510 int on = enabled ? 1 : 0; | 322 int on = enabled ? 1 : 0; |
| 511 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 323 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 512 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 324 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 513 : IPV6_MULTICAST_LOOP; | 325 : IPV6_MULTICAST_LOOP; |
| 514 return NO_RETRY_EXPECTED(setsockopt( | 326 return NO_RETRY_EXPECTED(setsockopt( |
| 515 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == | 327 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
| 516 0; | 328 0; |
| 517 } | 329 } |
| 518 | 330 |
| 519 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 331 bool SocketBase::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| 520 uint8_t v; | 332 uint8_t v; |
| 521 socklen_t len = sizeof(v); | 333 socklen_t len = sizeof(v); |
| 522 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 334 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 523 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 335 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 524 : IPV6_MULTICAST_HOPS; | 336 : IPV6_MULTICAST_HOPS; |
| 525 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 337 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 526 reinterpret_cast<char*>(&v), &len)) == 0) { | 338 reinterpret_cast<char*>(&v), &len)) == 0) { |
| 527 *value = v; | 339 *value = v; |
| 528 return true; | 340 return true; |
| 529 } | 341 } |
| 530 return false; | 342 return false; |
| 531 } | 343 } |
| 532 | 344 |
| 533 | 345 |
| 534 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 346 bool SocketBase::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| 535 int v = value; | 347 int v = value; |
| 536 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 348 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 537 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 349 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 538 : IPV6_MULTICAST_HOPS; | 350 : IPV6_MULTICAST_HOPS; |
| 539 return NO_RETRY_EXPECTED(setsockopt( | 351 return NO_RETRY_EXPECTED(setsockopt( |
| 540 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; | 352 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
| 541 } | 353 } |
| 542 | 354 |
| 543 | 355 |
| 544 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 356 bool SocketBase::GetBroadcast(intptr_t fd, bool* enabled) { |
| 545 int on; | 357 int on; |
| 546 socklen_t len = sizeof(on); | 358 socklen_t len = sizeof(on); |
| 547 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 359 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 548 reinterpret_cast<char*>(&on), &len)); | 360 reinterpret_cast<char*>(&on), &len)); |
| 549 if (err == 0) { | 361 if (err == 0) { |
| 550 *enabled = (on == 1); | 362 *enabled = (on == 1); |
| 551 } | 363 } |
| 552 return (err == 0); | 364 return (err == 0); |
| 553 } | 365 } |
| 554 | 366 |
| 555 | 367 |
| 556 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 368 bool SocketBase::SetBroadcast(intptr_t fd, bool enabled) { |
| 557 int on = enabled ? 1 : 0; | 369 int on = enabled ? 1 : 0; |
| 558 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 370 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 559 reinterpret_cast<char*>(&on), | 371 reinterpret_cast<char*>(&on), |
| 560 sizeof(on))) == 0; | 372 sizeof(on))) == 0; |
| 561 } | 373 } |
| 562 | 374 |
| 563 | 375 |
| 564 bool Socket::JoinMulticast(intptr_t fd, | 376 bool SocketBase::JoinMulticast(intptr_t fd, |
| 565 const RawAddr& addr, | 377 const RawAddr& addr, |
| 566 const RawAddr&, | 378 const RawAddr&, |
| 567 int interfaceIndex) { | 379 int interfaceIndex) { |
| 568 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 380 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 569 struct group_req mreq; | 381 struct group_req mreq; |
| 570 mreq.gr_interface = interfaceIndex; | 382 mreq.gr_interface = interfaceIndex; |
| 571 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 383 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 572 return NO_RETRY_EXPECTED( | 384 return NO_RETRY_EXPECTED( |
| 573 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 385 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| 574 } | 386 } |
| 575 | 387 |
| 576 | 388 |
| 577 bool Socket::LeaveMulticast(intptr_t fd, | 389 bool SocketBase::LeaveMulticast(intptr_t fd, |
| 578 const RawAddr& addr, | 390 const RawAddr& addr, |
| 579 const RawAddr&, | 391 const RawAddr&, |
| 580 int interfaceIndex) { | 392 int interfaceIndex) { |
| 581 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 393 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 582 struct group_req mreq; | 394 struct group_req mreq; |
| 583 mreq.gr_interface = interfaceIndex; | 395 mreq.gr_interface = interfaceIndex; |
| 584 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 396 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 585 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, | 397 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, |
| 586 sizeof(mreq))) == 0; | 398 sizeof(mreq))) == 0; |
| 587 } | 399 } |
| 588 | 400 |
| 589 } // namespace bin | 401 } // namespace bin |
| 590 } // namespace dart | 402 } // namespace dart |
| 591 | 403 |
| 592 #endif // defined(HOST_OS_ANDROID) | 404 #endif // defined(HOST_OS_ANDROID) |
| 593 | 405 |
| 594 #endif // !defined(DART_IO_DISABLED) | 406 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |