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