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