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