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