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 |
10 #include <errno.h> // NOLINT | 13 #include <errno.h> // NOLINT |
11 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
12 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
13 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
14 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
15 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
16 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
17 #include <sys/stat.h> // NOLINT | 20 #include <sys/stat.h> // NOLINT |
18 #include <unistd.h> // NOLINT | 21 #include <unistd.h> // NOLINT |
19 | 22 |
20 #include "bin/fdutils.h" | 23 #include "bin/fdutils.h" |
21 #include "bin/file.h" | 24 #include "bin/file.h" |
22 #include "bin/socket.h" | |
23 #include "bin/socket_base_linux.h" | |
24 #include "bin/thread.h" | 25 #include "bin/thread.h" |
25 #include "platform/signal_blocker.h" | 26 #include "platform/signal_blocker.h" |
26 | 27 |
27 namespace dart { | 28 namespace dart { |
28 namespace bin { | 29 namespace bin { |
29 | 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 |
30 Socket::Socket(intptr_t fd) | 49 Socket::Socket(intptr_t fd) |
31 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 50 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
32 | 51 |
33 | 52 |
34 void Socket::SetClosedFd() { | 53 void Socket::SetClosedFd() { |
35 fd_ = kClosedFd; | 54 fd_ = kClosedFd; |
36 } | 55 } |
37 | 56 |
38 | 57 |
39 bool Socket::Initialize() { | 58 bool Socket::Initialize() { |
40 // Nothing to do on Linux. | 59 // Nothing to do on Linux. |
41 return true; | 60 return true; |
42 } | 61 } |
43 | 62 |
44 | 63 |
45 static intptr_t Create(const RawAddr& addr) { | 64 static intptr_t Create(const RawAddr& addr) { |
46 intptr_t fd; | 65 intptr_t fd; |
47 intptr_t type = SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC; | 66 fd = NO_RETRY_EXPECTED( |
48 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, type, 0)); | 67 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); |
49 if (fd < 0) { | 68 if (fd < 0) { |
50 return -1; | 69 return -1; |
51 } | 70 } |
52 return fd; | 71 return fd; |
53 } | 72 } |
54 | 73 |
55 | 74 |
56 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 75 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
57 intptr_t result = TEMP_FAILURE_RETRY( | 76 intptr_t result = TEMP_FAILURE_RETRY( |
58 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 77 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
(...skipping 25 matching lines...) Expand all Loading... |
84 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 103 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
85 if ((result != 0) && (errno != EINPROGRESS)) { | 104 if ((result != 0) && (errno != EINPROGRESS)) { |
86 FDUtils::SaveErrorAndClose(fd); | 105 FDUtils::SaveErrorAndClose(fd); |
87 return -1; | 106 return -1; |
88 } | 107 } |
89 | 108 |
90 return Connect(fd, addr); | 109 return Connect(fd, addr); |
91 } | 110 } |
92 | 111 |
93 | 112 |
| 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 |
94 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 316 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
95 intptr_t fd; | 317 intptr_t fd; |
96 | 318 |
97 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, | 319 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, |
98 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, | 320 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, |
99 IPPROTO_UDP)); | 321 IPPROTO_UDP)); |
100 if (fd < 0) { | 322 if (fd < 0) { |
101 return -1; | 323 return -1; |
102 } | 324 } |
103 | 325 |
104 if (reuseAddress) { | 326 if (reuseAddress) { |
105 int optval = 1; | 327 int optval = 1; |
106 VOID_NO_RETRY_EXPECTED( | 328 VOID_NO_RETRY_EXPECTED( |
107 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 329 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
108 } | 330 } |
109 | 331 |
110 if (NO_RETRY_EXPECTED( | 332 if (NO_RETRY_EXPECTED( |
111 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 333 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
112 FDUtils::SaveErrorAndClose(fd); | 334 FDUtils::SaveErrorAndClose(fd); |
113 return -1; | 335 return -1; |
114 } | 336 } |
115 return fd; | 337 return fd; |
116 } | 338 } |
117 | 339 |
118 | 340 |
| 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 |
119 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 397 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
120 intptr_t backlog, | 398 intptr_t backlog, |
121 bool v6_only) { | 399 bool v6_only) { |
122 intptr_t fd; | 400 intptr_t fd; |
123 | 401 |
124 fd = NO_RETRY_EXPECTED( | 402 fd = NO_RETRY_EXPECTED( |
125 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); | 403 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
126 if (fd < 0) { | 404 if (fd < 0) { |
127 return -1; | 405 return -1; |
128 } | 406 } |
129 | 407 |
130 int optval = 1; | 408 int optval = 1; |
131 VOID_NO_RETRY_EXPECTED( | 409 VOID_NO_RETRY_EXPECTED( |
132 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 410 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
133 | 411 |
134 if (addr.ss.ss_family == AF_INET6) { | 412 if (addr.ss.ss_family == AF_INET6) { |
135 optval = v6_only ? 1 : 0; | 413 optval = v6_only ? 1 : 0; |
136 VOID_NO_RETRY_EXPECTED( | 414 VOID_NO_RETRY_EXPECTED( |
137 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 415 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
138 } | 416 } |
139 | 417 |
140 if (NO_RETRY_EXPECTED( | 418 if (NO_RETRY_EXPECTED( |
141 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 419 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
142 FDUtils::SaveErrorAndClose(fd); | 420 FDUtils::SaveErrorAndClose(fd); |
143 return -1; | 421 return -1; |
144 } | 422 } |
145 | 423 |
146 // Test for invalid socket port 65535 (some browsers disallow it). | 424 // Test for invalid socket port 65535 (some browsers disallow it). |
147 if ((SocketAddress::GetAddrPort(addr) == 0) && | 425 if ((SocketAddress::GetAddrPort(addr) == 0) && |
148 (SocketBase::GetPort(fd) == 65535)) { | 426 (Socket::GetPort(fd) == 65535)) { |
149 // Don't close the socket until we have created a new socket, ensuring | 427 // Don't close the socket until we have created a new socket, ensuring |
150 // that we do not get the bad port number again. | 428 // that we do not get the bad port number again. |
151 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 429 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
152 FDUtils::SaveErrorAndClose(fd); | 430 FDUtils::SaveErrorAndClose(fd); |
153 return new_fd; | 431 return new_fd; |
154 } | 432 } |
155 | 433 |
156 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 434 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
157 FDUtils::SaveErrorAndClose(fd); | 435 FDUtils::SaveErrorAndClose(fd); |
158 return -1; | 436 return -1; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 return -1; | 475 return -1; |
198 } | 476 } |
199 if (!FDUtils::SetNonBlocking(socket)) { | 477 if (!FDUtils::SetNonBlocking(socket)) { |
200 FDUtils::SaveErrorAndClose(socket); | 478 FDUtils::SaveErrorAndClose(socket); |
201 return -1; | 479 return -1; |
202 } | 480 } |
203 } | 481 } |
204 return socket; | 482 return socket; |
205 } | 483 } |
206 | 484 |
| 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 |
207 } // namespace bin | 608 } // namespace bin |
208 } // namespace dart | 609 } // namespace dart |
209 | 610 |
210 #endif // defined(HOST_OS_LINUX) | 611 #endif // defined(HOST_OS_LINUX) |
211 | 612 |
212 #endif // !defined(DART_IO_DISABLED) | 613 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |