Chromium Code Reviews| 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_common_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 (!BaseSocket::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 BaseSocket::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 Socket::Socket(intptr_t fd) |
|
zra
2017/03/30 19:59:44
Socket:: implementation shouldn't be in here, I do
| |
| 47 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 49 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
| 48 | 50 |
| 49 | 51 |
| 50 void Socket::SetClosedFd() { | 52 void Socket::SetClosedFd() { |
| 51 fd_ = kClosedFd; | 53 fd_ = kClosedFd; |
| 52 } | 54 } |
| 53 | 55 |
| 54 | 56 |
| 55 bool Socket::Initialize() { | 57 bool Socket::Initialize() { |
| 56 // Nothing to do on Android. | 58 // Nothing to do on Android. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 110 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 109 if ((result != 0) && (errno != EINPROGRESS)) { | 111 if ((result != 0) && (errno != EINPROGRESS)) { |
| 110 FDUtils::SaveErrorAndClose(fd); | 112 FDUtils::SaveErrorAndClose(fd); |
| 111 return -1; | 113 return -1; |
| 112 } | 114 } |
| 113 | 115 |
| 114 return Connect(fd, addr); | 116 return Connect(fd, addr); |
| 115 } | 117 } |
| 116 | 118 |
| 117 | 119 |
| 118 bool Socket::IsBindError(intptr_t error_number) { | 120 bool BaseSocket::IsBindError(intptr_t error_number) { |
| 119 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 121 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
| 120 error_number == EINVAL; | 122 error_number == EINVAL; |
| 121 } | 123 } |
| 122 | 124 |
| 123 | 125 |
| 124 intptr_t Socket::Available(intptr_t fd) { | 126 intptr_t BaseSocket::Available(intptr_t fd) { |
| 125 return FDUtils::AvailableBytes(fd); | 127 return FDUtils::AvailableBytes(fd); |
| 126 } | 128 } |
| 127 | 129 |
| 128 | 130 |
| 129 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 131 intptr_t BaseSocket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| 130 ASSERT(fd >= 0); | 132 ASSERT(fd >= 0); |
| 131 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 133 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
| 132 ASSERT(EAGAIN == EWOULDBLOCK); | 134 ASSERT(EAGAIN == EWOULDBLOCK); |
| 133 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 135 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 134 // If the read would block we need to retry and therefore return 0 | 136 // If the read would block we need to retry and therefore return 0 |
| 135 // as the number of bytes written. | 137 // as the number of bytes written. |
| 136 read_bytes = 0; | 138 read_bytes = 0; |
| 137 } | 139 } |
| 138 return read_bytes; | 140 return read_bytes; |
| 139 } | 141 } |
| 140 | 142 |
| 141 | 143 |
| 142 intptr_t Socket::RecvFrom(intptr_t fd, | 144 intptr_t BaseSocket::RecvFrom(intptr_t fd, |
| 143 void* buffer, | 145 void* buffer, |
| 144 intptr_t num_bytes, | 146 intptr_t num_bytes, |
| 145 RawAddr* addr) { | 147 RawAddr* addr) { |
| 146 ASSERT(fd >= 0); | 148 ASSERT(fd >= 0); |
| 147 socklen_t addr_len = sizeof(addr->ss); | 149 socklen_t addr_len = sizeof(addr->ss); |
| 148 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 150 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
| 149 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 151 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
| 150 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 152 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 151 // If the read would block we need to retry and therefore return 0 | 153 // If the read would block we need to retry and therefore return 0 |
| 152 // as the number of bytes written. | 154 // as the number of bytes written. |
| 153 read_bytes = 0; | 155 read_bytes = 0; |
| 154 } | 156 } |
| 155 return read_bytes; | 157 return read_bytes; |
| 156 } | 158 } |
| 157 | 159 |
| 158 | 160 |
| 159 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 161 intptr_t BaseSocket::Write(intptr_t fd, |
| 162 const void* buffer, | |
| 163 intptr_t num_bytes) { | |
| 160 ASSERT(fd >= 0); | 164 ASSERT(fd >= 0); |
| 161 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 165 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| 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 |
| 172 intptr_t Socket::SendTo(intptr_t fd, | 176 intptr_t BaseSocket::SendTo(intptr_t fd, |
| 173 const void* buffer, | 177 const void* buffer, |
| 174 intptr_t num_bytes, | 178 intptr_t num_bytes, |
| 175 const RawAddr& addr) { | 179 const RawAddr& addr) { |
| 176 ASSERT(fd >= 0); | 180 ASSERT(fd >= 0); |
| 177 ssize_t written_bytes = | 181 ssize_t written_bytes = |
| 178 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, | 182 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
| 179 SocketAddress::GetAddrLength(addr))); | 183 SocketAddress::GetAddrLength(addr))); |
| 180 ASSERT(EAGAIN == EWOULDBLOCK); | 184 ASSERT(EAGAIN == EWOULDBLOCK); |
| 181 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 185 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
| 182 // If the would block we need to retry and therefore return 0 as | 186 // If the would block we need to retry and therefore return 0 as |
| 183 // the number of bytes written. | 187 // the number of bytes written. |
| 184 written_bytes = 0; | 188 written_bytes = 0; |
| 185 } | 189 } |
| 186 return written_bytes; | 190 return written_bytes; |
| 187 } | 191 } |
| 188 | 192 |
| 189 | 193 |
| 190 intptr_t Socket::GetPort(intptr_t fd) { | 194 intptr_t BaseSocket::GetPort(intptr_t fd) { |
| 191 ASSERT(fd >= 0); | 195 ASSERT(fd >= 0); |
| 192 RawAddr raw; | 196 RawAddr raw; |
| 193 socklen_t size = sizeof(raw); | 197 socklen_t size = sizeof(raw); |
| 194 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 198 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 195 return 0; | 199 return 0; |
| 196 } | 200 } |
| 197 return SocketAddress::GetAddrPort(raw); | 201 return SocketAddress::GetAddrPort(raw); |
| 198 } | 202 } |
| 199 | 203 |
| 200 | 204 |
| 201 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 205 SocketAddress* BaseSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 202 ASSERT(fd >= 0); | 206 ASSERT(fd >= 0); |
| 203 RawAddr raw; | 207 RawAddr raw; |
| 204 socklen_t size = sizeof(raw); | 208 socklen_t size = sizeof(raw); |
| 205 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 209 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
| 206 return NULL; | 210 return NULL; |
| 207 } | 211 } |
| 208 *port = SocketAddress::GetAddrPort(raw); | 212 *port = SocketAddress::GetAddrPort(raw); |
| 209 return new SocketAddress(&raw.addr); | 213 return new SocketAddress(&raw.addr); |
| 210 } | 214 } |
| 211 | 215 |
| 212 | 216 |
| 213 void Socket::GetError(intptr_t fd, OSError* os_error) { | 217 void BaseSocket::GetError(intptr_t fd, OSError* os_error) { |
| 214 int errorNumber; | 218 int errorNumber; |
| 215 socklen_t len = sizeof(errorNumber); | 219 socklen_t len = sizeof(errorNumber); |
| 216 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), | 220 getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<void*>(&errorNumber), |
| 217 &len); | 221 &len); |
| 218 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); | 222 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); |
| 219 } | 223 } |
| 220 | 224 |
| 221 | 225 |
| 222 int Socket::GetType(intptr_t fd) { | 226 int BaseSocket::GetType(intptr_t fd) { |
| 223 struct stat buf; | 227 struct stat buf; |
| 224 int result = fstat(fd, &buf); | 228 int result = fstat(fd, &buf); |
| 225 if (result == -1) { | 229 if (result == -1) { |
| 226 return -1; | 230 return -1; |
| 227 } | 231 } |
| 228 if (S_ISCHR(buf.st_mode)) { | 232 if (S_ISCHR(buf.st_mode)) { |
| 229 return File::kTerminal; | 233 return File::kTerminal; |
| 230 } | 234 } |
| 231 if (S_ISFIFO(buf.st_mode)) { | 235 if (S_ISFIFO(buf.st_mode)) { |
| 232 return File::kPipe; | 236 return File::kPipe; |
| 233 } | 237 } |
| 234 if (S_ISREG(buf.st_mode)) { | 238 if (S_ISREG(buf.st_mode)) { |
| 235 return File::kFile; | 239 return File::kFile; |
| 236 } | 240 } |
| 237 return File::kOther; | 241 return File::kOther; |
| 238 } | 242 } |
| 239 | 243 |
| 240 | 244 |
| 241 intptr_t Socket::GetStdioHandle(intptr_t num) { | 245 intptr_t BaseSocket::GetStdioHandle(intptr_t num) { |
| 242 return num; | 246 return num; |
| 243 } | 247 } |
| 244 | 248 |
| 245 | 249 |
| 246 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 250 AddressList<SocketAddress>* BaseSocket::LookupAddress(const char* host, |
| 247 int type, | 251 int type, |
| 248 OSError** os_error) { | 252 OSError** os_error) { |
| 249 // Perform a name lookup for a host name. | 253 // Perform a name lookup for a host name. |
| 250 struct addrinfo hints; | 254 struct addrinfo hints; |
| 251 memset(&hints, 0, sizeof(hints)); | 255 memset(&hints, 0, sizeof(hints)); |
| 252 hints.ai_family = SocketAddress::FromType(type); | 256 hints.ai_family = SocketAddress::FromType(type); |
| 253 hints.ai_socktype = SOCK_STREAM; | 257 hints.ai_socktype = SOCK_STREAM; |
| 254 hints.ai_flags = AI_ADDRCONFIG; | 258 hints.ai_flags = AI_ADDRCONFIG; |
| 255 hints.ai_protocol = IPPROTO_TCP; | 259 hints.ai_protocol = IPPROTO_TCP; |
| 256 struct addrinfo* info = NULL; | 260 struct addrinfo* info = NULL; |
| 257 int status = getaddrinfo(host, 0, &hints, &info); | 261 int status = getaddrinfo(host, 0, &hints, &info); |
| 258 if (status != 0) { | 262 if (status != 0) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 279 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 283 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 280 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 284 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 281 i++; | 285 i++; |
| 282 } | 286 } |
| 283 } | 287 } |
| 284 freeaddrinfo(info); | 288 freeaddrinfo(info); |
| 285 return addresses; | 289 return addresses; |
| 286 } | 290 } |
| 287 | 291 |
| 288 | 292 |
| 289 bool Socket::ReverseLookup(const RawAddr& addr, | 293 bool BaseSocket::ReverseLookup(const RawAddr& addr, |
| 290 char* host, | 294 char* host, |
| 291 intptr_t host_len, | 295 intptr_t host_len, |
| 292 OSError** os_error) { | 296 OSError** os_error) { |
| 293 ASSERT(host_len >= NI_MAXHOST); | 297 ASSERT(host_len >= NI_MAXHOST); |
| 294 int status = NO_RETRY_EXPECTED( | 298 int status = NO_RETRY_EXPECTED( |
| 295 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, | 299 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
| 296 host_len, NULL, 0, NI_NAMEREQD)); | 300 host_len, NULL, 0, NI_NAMEREQD)); |
| 297 if (status != 0) { | 301 if (status != 0) { |
| 298 ASSERT(*os_error == NULL); | 302 ASSERT(*os_error == NULL); |
| 299 *os_error = | 303 *os_error = |
| 300 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); | 304 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
| 301 return false; | 305 return false; |
| 302 } | 306 } |
| 303 return true; | 307 return true; |
| 304 } | 308 } |
| 305 | 309 |
| 306 | 310 |
| 307 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 311 bool BaseSocket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| 308 int result; | 312 int result; |
| 309 if (type == SocketAddress::TYPE_IPV4) { | 313 if (type == SocketAddress::TYPE_IPV4) { |
| 310 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 314 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
| 311 } else { | 315 } else { |
| 312 ASSERT(type == SocketAddress::TYPE_IPV6); | 316 ASSERT(type == SocketAddress::TYPE_IPV6); |
| 313 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 317 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
| 314 } | 318 } |
| 315 return (result == 1); | 319 return (result == 1); |
| 316 } | 320 } |
| 317 | 321 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 342 } | 346 } |
| 343 | 347 |
| 344 if (!FDUtils::SetNonBlocking(fd)) { | 348 if (!FDUtils::SetNonBlocking(fd)) { |
| 345 FDUtils::SaveErrorAndClose(fd); | 349 FDUtils::SaveErrorAndClose(fd); |
| 346 return -1; | 350 return -1; |
| 347 } | 351 } |
| 348 return fd; | 352 return fd; |
| 349 } | 353 } |
| 350 | 354 |
| 351 | 355 |
| 352 bool Socket::ListInterfacesSupported() { | 356 bool BaseSocket::ListInterfacesSupported() { |
| 353 return false; | 357 return false; |
| 354 } | 358 } |
| 355 | 359 |
| 356 | 360 |
| 357 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 361 AddressList<InterfaceSocketAddress>* BaseSocket::ListInterfaces( |
| 358 int type, | 362 int type, |
| 359 OSError** os_error) { | 363 OSError** os_error) { |
| 360 // The ifaddrs.h header is not provided on Android. An Android | 364 // The ifaddrs.h header is not provided on Android. An Android |
| 361 // implementation would have to use IOCTL or netlink. | 365 // implementation would have to use IOCTL or netlink. |
| 362 ASSERT(*os_error == NULL); | 366 ASSERT(*os_error == NULL); |
| 363 *os_error = new OSError(-1, | 367 *os_error = new OSError(-1, |
| 364 "Listing interfaces is not supported " | 368 "Listing interfaces is not supported " |
| 365 "on this platform", | 369 "on this platform", |
| 366 OSError::kSystem); | 370 OSError::kSystem); |
| 367 return NULL; | 371 return NULL; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 394 } | 398 } |
| 395 | 399 |
| 396 if (NO_RETRY_EXPECTED( | 400 if (NO_RETRY_EXPECTED( |
| 397 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 401 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
| 398 FDUtils::SaveErrorAndClose(fd); | 402 FDUtils::SaveErrorAndClose(fd); |
| 399 return -1; | 403 return -1; |
| 400 } | 404 } |
| 401 | 405 |
| 402 // Test for invalid socket port 65535 (some browsers disallow it). | 406 // Test for invalid socket port 65535 (some browsers disallow it). |
| 403 if ((SocketAddress::GetAddrPort(addr)) == 0 && | 407 if ((SocketAddress::GetAddrPort(addr)) == 0 && |
| 404 (Socket::GetPort(fd) == 65535)) { | 408 (BaseSocket::GetPort(fd) == 65535)) { |
| 405 // Don't close the socket until we have created a new socket, ensuring | 409 // Don't close the socket until we have created a new socket, ensuring |
| 406 // that we do not get the bad port number again. | 410 // that we do not get the bad port number again. |
| 407 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 411 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
| 408 FDUtils::SaveErrorAndClose(fd); | 412 FDUtils::SaveErrorAndClose(fd); |
| 409 return new_fd; | 413 return new_fd; |
| 410 } | 414 } |
| 411 | 415 |
| 412 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 416 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 413 FDUtils::SaveErrorAndClose(fd); | 417 FDUtils::SaveErrorAndClose(fd); |
| 414 return -1; | 418 return -1; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 } | 462 } |
| 459 if (!FDUtils::SetNonBlocking(socket)) { | 463 if (!FDUtils::SetNonBlocking(socket)) { |
| 460 FDUtils::SaveErrorAndClose(socket); | 464 FDUtils::SaveErrorAndClose(socket); |
| 461 return -1; | 465 return -1; |
| 462 } | 466 } |
| 463 } | 467 } |
| 464 return socket; | 468 return socket; |
| 465 } | 469 } |
| 466 | 470 |
| 467 | 471 |
| 468 void Socket::Close(intptr_t fd) { | 472 void BaseSocket::Close(intptr_t fd) { |
| 469 ASSERT(fd >= 0); | 473 ASSERT(fd >= 0); |
| 470 VOID_TEMP_FAILURE_RETRY(close(fd)); | 474 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 471 } | 475 } |
| 472 | 476 |
| 473 | 477 |
| 474 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 478 bool BaseSocket::GetNoDelay(intptr_t fd, bool* enabled) { |
| 475 int on; | 479 int on; |
| 476 socklen_t len = sizeof(on); | 480 socklen_t len = sizeof(on); |
| 477 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 481 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 478 reinterpret_cast<void*>(&on), &len)); | 482 reinterpret_cast<void*>(&on), &len)); |
| 479 if (err == 0) { | 483 if (err == 0) { |
| 480 *enabled = (on == 1); | 484 *enabled = (on == 1); |
| 481 } | 485 } |
| 482 return (err == 0); | 486 return (err == 0); |
| 483 } | 487 } |
| 484 | 488 |
| 485 | 489 |
| 486 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 490 bool BaseSocket::SetNoDelay(intptr_t fd, bool enabled) { |
| 487 int on = enabled ? 1 : 0; | 491 int on = enabled ? 1 : 0; |
| 488 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 492 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 489 reinterpret_cast<char*>(&on), | 493 reinterpret_cast<char*>(&on), |
| 490 sizeof(on))) == 0; | 494 sizeof(on))) == 0; |
| 491 } | 495 } |
| 492 | 496 |
| 493 | 497 |
| 494 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 498 bool BaseSocket::GetMulticastLoop(intptr_t fd, |
| 499 intptr_t protocol, | |
| 500 bool* enabled) { | |
| 495 uint8_t on; | 501 uint8_t on; |
| 496 socklen_t len = sizeof(on); | 502 socklen_t len = sizeof(on); |
| 497 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 503 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 498 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 504 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 499 : IPV6_MULTICAST_LOOP; | 505 : IPV6_MULTICAST_LOOP; |
| 500 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 506 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 501 reinterpret_cast<char*>(&on), &len)) == 0) { | 507 reinterpret_cast<char*>(&on), &len)) == 0) { |
| 502 *enabled = (on == 1); | 508 *enabled = (on == 1); |
| 503 return true; | 509 return true; |
| 504 } | 510 } |
| 505 return false; | 511 return false; |
| 506 } | 512 } |
| 507 | 513 |
| 508 | 514 |
| 509 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 515 bool BaseSocket::SetMulticastLoop(intptr_t fd, |
| 516 intptr_t protocol, | |
| 517 bool enabled) { | |
| 510 int on = enabled ? 1 : 0; | 518 int on = enabled ? 1 : 0; |
| 511 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 519 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 512 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP | 520 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
| 513 : IPV6_MULTICAST_LOOP; | 521 : IPV6_MULTICAST_LOOP; |
| 514 return NO_RETRY_EXPECTED(setsockopt( | 522 return NO_RETRY_EXPECTED(setsockopt( |
| 515 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == | 523 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
| 516 0; | 524 0; |
| 517 } | 525 } |
| 518 | 526 |
| 519 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 527 bool BaseSocket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| 520 uint8_t v; | 528 uint8_t v; |
| 521 socklen_t len = sizeof(v); | 529 socklen_t len = sizeof(v); |
| 522 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 530 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 523 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 531 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 524 : IPV6_MULTICAST_HOPS; | 532 : IPV6_MULTICAST_HOPS; |
| 525 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, | 533 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
| 526 reinterpret_cast<char*>(&v), &len)) == 0) { | 534 reinterpret_cast<char*>(&v), &len)) == 0) { |
| 527 *value = v; | 535 *value = v; |
| 528 return true; | 536 return true; |
| 529 } | 537 } |
| 530 return false; | 538 return false; |
| 531 } | 539 } |
| 532 | 540 |
| 533 | 541 |
| 534 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 542 bool BaseSocket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| 535 int v = value; | 543 int v = value; |
| 536 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 544 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
| 537 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL | 545 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
| 538 : IPV6_MULTICAST_HOPS; | 546 : IPV6_MULTICAST_HOPS; |
| 539 return NO_RETRY_EXPECTED(setsockopt( | 547 return NO_RETRY_EXPECTED(setsockopt( |
| 540 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; | 548 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
| 541 } | 549 } |
| 542 | 550 |
| 543 | 551 |
| 544 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 552 bool BaseSocket::GetBroadcast(intptr_t fd, bool* enabled) { |
| 545 int on; | 553 int on; |
| 546 socklen_t len = sizeof(on); | 554 socklen_t len = sizeof(on); |
| 547 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 555 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 548 reinterpret_cast<char*>(&on), &len)); | 556 reinterpret_cast<char*>(&on), &len)); |
| 549 if (err == 0) { | 557 if (err == 0) { |
| 550 *enabled = (on == 1); | 558 *enabled = (on == 1); |
| 551 } | 559 } |
| 552 return (err == 0); | 560 return (err == 0); |
| 553 } | 561 } |
| 554 | 562 |
| 555 | 563 |
| 556 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 564 bool BaseSocket::SetBroadcast(intptr_t fd, bool enabled) { |
| 557 int on = enabled ? 1 : 0; | 565 int on = enabled ? 1 : 0; |
| 558 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, | 566 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 559 reinterpret_cast<char*>(&on), | 567 reinterpret_cast<char*>(&on), |
| 560 sizeof(on))) == 0; | 568 sizeof(on))) == 0; |
| 561 } | 569 } |
| 562 | 570 |
| 563 | 571 |
| 564 bool Socket::JoinMulticast(intptr_t fd, | 572 bool BaseSocket::JoinMulticast(intptr_t fd, |
| 565 const RawAddr& addr, | 573 const RawAddr& addr, |
| 566 const RawAddr&, | 574 const RawAddr&, |
| 567 int interfaceIndex) { | 575 int interfaceIndex) { |
| 568 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 576 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 569 struct group_req mreq; | 577 struct group_req mreq; |
| 570 mreq.gr_interface = interfaceIndex; | 578 mreq.gr_interface = interfaceIndex; |
| 571 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 579 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 572 return NO_RETRY_EXPECTED( | 580 return NO_RETRY_EXPECTED( |
| 573 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 581 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| 574 } | 582 } |
| 575 | 583 |
| 576 | 584 |
| 577 bool Socket::LeaveMulticast(intptr_t fd, | 585 bool BaseSocket::LeaveMulticast(intptr_t fd, |
| 578 const RawAddr& addr, | 586 const RawAddr& addr, |
| 579 const RawAddr&, | 587 const RawAddr&, |
| 580 int interfaceIndex) { | 588 int interfaceIndex) { |
| 581 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | 589 int proto = (addr.addr.sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; |
| 582 struct group_req mreq; | 590 struct group_req mreq; |
| 583 mreq.gr_interface = interfaceIndex; | 591 mreq.gr_interface = interfaceIndex; |
| 584 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 592 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
| 585 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, | 593 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, |
| 586 sizeof(mreq))) == 0; | 594 sizeof(mreq))) == 0; |
| 587 } | 595 } |
| 588 | 596 |
| 589 } // namespace bin | 597 } // namespace bin |
| 590 } // namespace dart | 598 } // namespace dart |
| 591 | 599 |
| 592 #endif // defined(HOST_OS_ANDROID) | 600 #endif // defined(HOST_OS_ANDROID) |
| 593 | 601 |
| 594 #endif // !defined(DART_IO_DISABLED) | 602 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |