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(TARGET_OS_MACOS) | 8 #if defined(TARGET_OS_MACOS) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
11 #include "bin/socket_macos.h" | 11 #include "bin/socket_macos.h" |
12 | 12 |
13 #include <errno.h> // NOLINT | 13 #include <errno.h> // NOLINT |
14 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
15 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
16 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
17 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
18 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
19 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
20 #include <sys/stat.h> // NOLINT | 20 #include <sys/stat.h> // NOLINT |
21 #include <unistd.h> // NOLINT | 21 #include <unistd.h> // NOLINT |
22 | 22 |
23 #include "bin/fdutils.h" | 23 #include "bin/fdutils.h" |
24 #include "bin/file.h" | 24 #include "bin/file.h" |
25 #include "platform/signal_blocker.h" | 25 #include "platform/signal_blocker.h" |
26 | 26 |
27 namespace dart { | 27 namespace dart { |
28 namespace bin { | 28 namespace bin { |
29 | 29 |
30 SocketAddress::SocketAddress(struct sockaddr* sa) { | 30 SocketAddress::SocketAddress(struct sockaddr* sa) { |
31 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 31 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
32 if (!Socket::FormatNumericAddress( | 32 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
33 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 33 INET6_ADDRSTRLEN)) { |
34 as_string_[0] = 0; | 34 as_string_[0] = 0; |
35 } | 35 } |
36 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 36 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
37 memmove(reinterpret_cast<void *>(&addr_), sa, salen); | 37 memmove(reinterpret_cast<void*>(&addr_), sa, salen); |
38 } | 38 } |
39 | 39 |
40 | 40 |
41 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 41 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { |
42 socklen_t salen = SocketAddress::GetAddrLength(addr); | 42 socklen_t salen = SocketAddress::GetAddrLength(addr); |
43 return (NO_RETRY_EXPECTED(getnameinfo( | 43 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, |
44 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST)) == 0); | 44 0, NI_NUMERICHOST)) == 0); |
45 } | 45 } |
46 | 46 |
47 | 47 |
48 bool Socket::Initialize() { | 48 bool Socket::Initialize() { |
49 // Nothing to do on Mac OS. | 49 // Nothing to do on Mac OS. |
50 return true; | 50 return true; |
51 } | 51 } |
52 | 52 |
53 | 53 |
54 static intptr_t Create(const RawAddr& addr) { | 54 static intptr_t Create(const RawAddr& addr) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 VOID_TEMP_FAILURE_RETRY(close(fd)); | 97 VOID_TEMP_FAILURE_RETRY(close(fd)); |
98 return -1; | 98 return -1; |
99 } | 99 } |
100 | 100 |
101 return Connect(fd, addr); | 101 return Connect(fd, addr); |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 bool Socket::IsBindError(intptr_t error_number) { | 105 bool Socket::IsBindError(intptr_t error_number) { |
106 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 106 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
107 error_number == EINVAL; | 107 error_number == EINVAL; |
108 } | 108 } |
109 | 109 |
110 | 110 |
111 intptr_t Socket::Available(intptr_t fd) { | 111 intptr_t Socket::Available(intptr_t fd) { |
112 return FDUtils::AvailableBytes(fd); | 112 return FDUtils::AvailableBytes(fd); |
113 } | 113 } |
114 | 114 |
115 | 115 |
116 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 116 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
117 ASSERT(fd >= 0); | 117 ASSERT(fd >= 0); |
118 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 118 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
119 ASSERT(EAGAIN == EWOULDBLOCK); | 119 ASSERT(EAGAIN == EWOULDBLOCK); |
120 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 120 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
121 // If the read would block we need to retry and therefore return 0 | 121 // If the read would block we need to retry and therefore return 0 |
122 // as the number of bytes written. | 122 // as the number of bytes written. |
123 read_bytes = 0; | 123 read_bytes = 0; |
124 } | 124 } |
125 return read_bytes; | 125 return read_bytes; |
126 } | 126 } |
127 | 127 |
128 | 128 |
129 intptr_t Socket::RecvFrom( | 129 intptr_t Socket::RecvFrom(intptr_t fd, |
130 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { | 130 void* buffer, |
| 131 intptr_t num_bytes, |
| 132 RawAddr* addr) { |
131 ASSERT(fd >= 0); | 133 ASSERT(fd >= 0); |
132 socklen_t addr_len = sizeof(addr->ss); | 134 socklen_t addr_len = sizeof(addr->ss); |
133 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 135 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
134 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 136 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
135 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { | 137 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
136 // If the read would block we need to retry and therefore return 0 | 138 // If the read would block we need to retry and therefore return 0 |
137 // as the number of bytes written. | 139 // as the number of bytes written. |
138 read_bytes = 0; | 140 read_bytes = 0; |
139 } | 141 } |
140 return read_bytes; | 142 return read_bytes; |
141 } | 143 } |
142 | 144 |
143 | 145 |
144 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 146 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
145 ASSERT(fd >= 0); | 147 ASSERT(fd >= 0); |
146 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 148 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
147 ASSERT(EAGAIN == EWOULDBLOCK); | 149 ASSERT(EAGAIN == EWOULDBLOCK); |
148 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 150 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
149 // If the would block we need to retry and therefore return 0 as | 151 // If the would block we need to retry and therefore return 0 as |
150 // the number of bytes written. | 152 // the number of bytes written. |
151 written_bytes = 0; | 153 written_bytes = 0; |
152 } | 154 } |
153 return written_bytes; | 155 return written_bytes; |
154 } | 156 } |
155 | 157 |
156 | 158 |
157 intptr_t Socket::SendTo( | 159 intptr_t Socket::SendTo(intptr_t fd, |
158 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { | 160 const void* buffer, |
| 161 intptr_t num_bytes, |
| 162 const RawAddr& addr) { |
159 ASSERT(fd >= 0); | 163 ASSERT(fd >= 0); |
160 ssize_t written_bytes = TEMP_FAILURE_RETRY( | 164 ssize_t written_bytes = |
161 sendto(fd, buffer, num_bytes, 0, | 165 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, |
162 &addr.addr, SocketAddress::GetAddrLength(addr))); | 166 SocketAddress::GetAddrLength(addr))); |
163 ASSERT(EAGAIN == EWOULDBLOCK); | 167 ASSERT(EAGAIN == EWOULDBLOCK); |
164 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { | 168 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
165 // If the would block we need to retry and therefore return 0 as | 169 // If the would block we need to retry and therefore return 0 as |
166 // the number of bytes written. | 170 // the number of bytes written. |
167 written_bytes = 0; | 171 written_bytes = 0; |
168 } | 172 } |
169 return written_bytes; | 173 return written_bytes; |
170 } | 174 } |
171 | 175 |
172 | 176 |
(...skipping 15 matching lines...) Expand all Loading... |
188 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 192 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
189 return NULL; | 193 return NULL; |
190 } | 194 } |
191 *port = SocketAddress::GetAddrPort(raw); | 195 *port = SocketAddress::GetAddrPort(raw); |
192 return new SocketAddress(&raw.addr); | 196 return new SocketAddress(&raw.addr); |
193 } | 197 } |
194 | 198 |
195 | 199 |
196 void Socket::GetError(intptr_t fd, OSError* os_error) { | 200 void Socket::GetError(intptr_t fd, OSError* os_error) { |
197 int len = sizeof(errno); | 201 int len = sizeof(errno); |
198 getsockopt(fd, | 202 getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, |
199 SOL_SOCKET, | |
200 SO_ERROR, | |
201 &errno, | |
202 reinterpret_cast<socklen_t*>(&len)); | 203 reinterpret_cast<socklen_t*>(&len)); |
203 os_error->SetCodeAndMessage(OSError::kSystem, errno); | 204 os_error->SetCodeAndMessage(OSError::kSystem, errno); |
204 } | 205 } |
205 | 206 |
206 | 207 |
207 int Socket::GetType(intptr_t fd) { | 208 int Socket::GetType(intptr_t fd) { |
208 struct stat buf; | 209 struct stat buf; |
209 int result = fstat(fd, &buf); | 210 int result = fstat(fd, &buf); |
210 if (result == -1) { | 211 if (result == -1) { |
211 return -1; | 212 return -1; |
(...skipping 23 matching lines...) Expand all Loading... |
235 struct addrinfo hints; | 236 struct addrinfo hints; |
236 memset(&hints, 0, sizeof(hints)); | 237 memset(&hints, 0, sizeof(hints)); |
237 hints.ai_family = SocketAddress::FromType(type); | 238 hints.ai_family = SocketAddress::FromType(type); |
238 hints.ai_socktype = SOCK_STREAM; | 239 hints.ai_socktype = SOCK_STREAM; |
239 hints.ai_flags = 0; | 240 hints.ai_flags = 0; |
240 hints.ai_protocol = IPPROTO_TCP; | 241 hints.ai_protocol = IPPROTO_TCP; |
241 struct addrinfo* info = NULL; | 242 struct addrinfo* info = NULL; |
242 int status = getaddrinfo(host, 0, &hints, &info); | 243 int status = getaddrinfo(host, 0, &hints, &info); |
243 if (status != 0) { | 244 if (status != 0) { |
244 ASSERT(*os_error == NULL); | 245 ASSERT(*os_error == NULL); |
245 *os_error = new OSError(status, | 246 *os_error = |
246 gai_strerror(status), | 247 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
247 OSError::kGetAddressInfo); | |
248 return NULL; | 248 return NULL; |
249 } | 249 } |
250 intptr_t count = 0; | 250 intptr_t count = 0; |
251 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 251 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
252 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 252 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
253 count++; | 253 count++; |
254 } | 254 } |
255 } | 255 } |
256 intptr_t i = 0; | 256 intptr_t i = 0; |
257 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); | 257 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); |
258 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 258 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
259 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { | 259 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
260 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 260 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
261 i++; | 261 i++; |
262 } | 262 } |
263 } | 263 } |
264 freeaddrinfo(info); | 264 freeaddrinfo(info); |
265 return addresses; | 265 return addresses; |
266 } | 266 } |
267 | 267 |
268 | 268 |
269 bool Socket::ReverseLookup(const RawAddr& addr, | 269 bool Socket::ReverseLookup(const RawAddr& addr, |
270 char* host, | 270 char* host, |
271 intptr_t host_len, | 271 intptr_t host_len, |
272 OSError** os_error) { | 272 OSError** os_error) { |
273 ASSERT(host_len >= NI_MAXHOST); | 273 ASSERT(host_len >= NI_MAXHOST); |
274 int status = NO_RETRY_EXPECTED(getnameinfo( | 274 int status = NO_RETRY_EXPECTED( |
275 &addr.addr, | 275 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
276 SocketAddress::GetAddrLength(addr), | 276 host_len, NULL, 0, NI_NAMEREQD)); |
277 host, | |
278 host_len, | |
279 NULL, | |
280 0, | |
281 NI_NAMEREQD)); | |
282 if (status != 0) { | 277 if (status != 0) { |
283 ASSERT(*os_error == NULL); | 278 ASSERT(*os_error == NULL); |
284 *os_error = new OSError(status, | 279 *os_error = |
285 gai_strerror(status), | 280 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
286 OSError::kGetAddressInfo); | |
287 return false; | 281 return false; |
288 } | 282 } |
289 return true; | 283 return true; |
290 } | 284 } |
291 | 285 |
292 | 286 |
293 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 287 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
294 int result; | 288 int result; |
295 if (type == SocketAddress::TYPE_IPV4) { | 289 if (type == SocketAddress::TYPE_IPV4) { |
296 result = inet_pton(AF_INET, address, &addr->in.sin_addr); | 290 result = inet_pton(AF_INET, address, &addr->in.sin_addr); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 } | 323 } |
330 | 324 |
331 | 325 |
332 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 326 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
333 if (ifa->ifa_addr == NULL) { | 327 if (ifa->ifa_addr == NULL) { |
334 // OpenVPN's virtual device tun0. | 328 // OpenVPN's virtual device tun0. |
335 return false; | 329 return false; |
336 } | 330 } |
337 int family = ifa->ifa_addr->sa_family; | 331 int family = ifa->ifa_addr->sa_family; |
338 return ((lookup_family == family) || | 332 return ((lookup_family == family) || |
339 ((lookup_family == AF_UNSPEC) && | 333 ((lookup_family == AF_UNSPEC) && |
340 ((family == AF_INET) || (family == AF_INET6)))); | 334 ((family == AF_INET) || (family == AF_INET6)))); |
341 } | 335 } |
342 | 336 |
343 | 337 |
344 bool Socket::ListInterfacesSupported() { | 338 bool Socket::ListInterfacesSupported() { |
345 return true; | 339 return true; |
346 } | 340 } |
347 | 341 |
348 | 342 |
349 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 343 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
350 int type, | 344 int type, |
351 OSError** os_error) { | 345 OSError** os_error) { |
352 struct ifaddrs* ifaddr; | 346 struct ifaddrs* ifaddr; |
353 | 347 |
354 int status = getifaddrs(&ifaddr); | 348 int status = getifaddrs(&ifaddr); |
355 if (status != 0) { | 349 if (status != 0) { |
356 ASSERT(*os_error == NULL); | 350 ASSERT(*os_error == NULL); |
357 *os_error = new OSError(status, | 351 *os_error = |
358 gai_strerror(status), | 352 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); |
359 OSError::kGetAddressInfo); | |
360 return NULL; | 353 return NULL; |
361 } | 354 } |
362 | 355 |
363 int lookup_family = SocketAddress::FromType(type); | 356 int lookup_family = SocketAddress::FromType(type); |
364 | 357 |
365 intptr_t count = 0; | 358 intptr_t count = 0; |
366 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 359 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
367 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++; | 360 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++; |
368 } | 361 } |
369 | 362 |
370 AddressList<InterfaceSocketAddress>* addresses = | 363 AddressList<InterfaceSocketAddress>* addresses = |
371 new AddressList<InterfaceSocketAddress>(count); | 364 new AddressList<InterfaceSocketAddress>(count); |
372 int i = 0; | 365 int i = 0; |
373 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 366 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
374 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { | 367 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
375 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); | 368 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); |
376 addresses->SetAt(i, new InterfaceSocketAddress( | 369 addresses->SetAt( |
377 ifa->ifa_addr, ifa_name, if_nametoindex(ifa->ifa_name))); | 370 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name, |
| 371 if_nametoindex(ifa->ifa_name))); |
378 i++; | 372 i++; |
379 } | 373 } |
380 } | 374 } |
381 freeifaddrs(ifaddr); | 375 freeifaddrs(ifaddr); |
382 return addresses; | 376 return addresses; |
383 } | 377 } |
384 | 378 |
385 | 379 |
386 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 380 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
387 intptr_t backlog, | 381 intptr_t backlog, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 | 456 |
463 void Socket::Close(intptr_t fd) { | 457 void Socket::Close(intptr_t fd) { |
464 ASSERT(fd >= 0); | 458 ASSERT(fd >= 0); |
465 VOID_TEMP_FAILURE_RETRY(close(fd)); | 459 VOID_TEMP_FAILURE_RETRY(close(fd)); |
466 } | 460 } |
467 | 461 |
468 | 462 |
469 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 463 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
470 int on; | 464 int on; |
471 socklen_t len = sizeof(on); | 465 socklen_t len = sizeof(on); |
472 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 466 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
473 IPPROTO_TCP, | 467 reinterpret_cast<void*>(&on), &len)); |
474 TCP_NODELAY, | |
475 reinterpret_cast<void *>(&on), | |
476 &len)); | |
477 if (err == 0) { | 468 if (err == 0) { |
478 *enabled = (on == 1); | 469 *enabled = (on == 1); |
479 } | 470 } |
480 return (err == 0); | 471 return (err == 0); |
481 } | 472 } |
482 | 473 |
483 | 474 |
484 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 475 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
485 int on = enabled ? 1 : 0; | 476 int on = enabled ? 1 : 0; |
486 return NO_RETRY_EXPECTED(setsockopt(fd, | 477 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
487 IPPROTO_TCP, | 478 reinterpret_cast<char*>(&on), |
488 TCP_NODELAY, | |
489 reinterpret_cast<char *>(&on), | |
490 sizeof(on))) == 0; | 479 sizeof(on))) == 0; |
491 } | 480 } |
492 | 481 |
493 | 482 |
494 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 483 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { |
495 uint8_t on; | 484 uint8_t on; |
496 socklen_t len = sizeof(on); | 485 socklen_t len = sizeof(on); |
497 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 486 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
498 int optname = protocol == SocketAddress::TYPE_IPV4 | 487 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
499 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 488 : IPV6_MULTICAST_LOOP; |
500 if (NO_RETRY_EXPECTED(getsockopt(fd, | 489 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
501 level, | 490 reinterpret_cast<char*>(&on), &len)) == 0) { |
502 optname, | |
503 reinterpret_cast<char *>(&on), | |
504 &len)) == 0) { | |
505 *enabled = (on == 1); | 491 *enabled = (on == 1); |
506 return true; | 492 return true; |
507 } | 493 } |
508 return false; | 494 return false; |
509 } | 495 } |
510 | 496 |
511 | 497 |
512 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 498 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { |
513 u_int on = enabled ? 1 : 0; | 499 u_int on = enabled ? 1 : 0; |
514 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 500 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
515 int optname = protocol == SocketAddress::TYPE_IPV4 | 501 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
516 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 502 : IPV6_MULTICAST_LOOP; |
517 return NO_RETRY_EXPECTED(setsockopt(fd, | 503 return NO_RETRY_EXPECTED(setsockopt( |
518 level, | 504 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == |
519 optname, | 505 0; |
520 reinterpret_cast<char *>(&on), | |
521 sizeof(on))) == 0; | |
522 } | 506 } |
523 | 507 |
524 | 508 |
525 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 509 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
526 uint8_t v; | 510 uint8_t v; |
527 socklen_t len = sizeof(v); | 511 socklen_t len = sizeof(v); |
528 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 512 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
529 int optname = protocol == SocketAddress::TYPE_IPV4 | 513 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
530 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 514 : IPV6_MULTICAST_HOPS; |
531 if (NO_RETRY_EXPECTED(getsockopt(fd, | 515 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, |
532 level, | 516 reinterpret_cast<char*>(&v), &len)) == 0) { |
533 optname, | |
534 reinterpret_cast<char *>(&v), | |
535 &len)) == 0) { | |
536 *value = v; | 517 *value = v; |
537 return true; | 518 return true; |
538 } | 519 } |
539 return false; | 520 return false; |
540 } | 521 } |
541 | 522 |
542 | 523 |
543 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 524 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
544 int v = value; | 525 int v = value; |
545 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 526 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
546 int optname = protocol == SocketAddress::TYPE_IPV4 | 527 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
547 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 528 : IPV6_MULTICAST_HOPS; |
548 return NO_RETRY_EXPECTED(setsockopt(fd, | 529 return NO_RETRY_EXPECTED(setsockopt( |
549 level, | 530 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; |
550 optname, | |
551 reinterpret_cast<char *>(&v), | |
552 sizeof(v))) == 0; | |
553 } | 531 } |
554 | 532 |
555 | 533 |
556 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 534 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
557 int on; | 535 int on; |
558 socklen_t len = sizeof(on); | 536 socklen_t len = sizeof(on); |
559 int err = NO_RETRY_EXPECTED(getsockopt(fd, | 537 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
560 SOL_SOCKET, | 538 reinterpret_cast<char*>(&on), &len)); |
561 SO_BROADCAST, | |
562 reinterpret_cast<char *>(&on), | |
563 &len)); | |
564 if (err == 0) { | 539 if (err == 0) { |
565 *enabled = (on == 1); | 540 *enabled = (on == 1); |
566 } | 541 } |
567 return (err == 0); | 542 return (err == 0); |
568 } | 543 } |
569 | 544 |
570 | 545 |
571 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 546 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
572 int on = enabled ? 1 : 0; | 547 int on = enabled ? 1 : 0; |
573 return NO_RETRY_EXPECTED(setsockopt(fd, | 548 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
574 SOL_SOCKET, | 549 reinterpret_cast<char*>(&on), |
575 SO_BROADCAST, | |
576 reinterpret_cast<char *>(&on), | |
577 sizeof(on))) == 0; | 550 sizeof(on))) == 0; |
578 } | 551 } |
579 | 552 |
580 | 553 |
581 static bool JoinOrLeaveMulticast(intptr_t fd, | 554 static bool JoinOrLeaveMulticast(intptr_t fd, |
582 const RawAddr& addr, | 555 const RawAddr& addr, |
583 const RawAddr& interface, | 556 const RawAddr& interface, |
584 int interfaceIndex, | 557 int interfaceIndex, |
585 bool join) { | 558 bool join) { |
586 if (addr.addr.sa_family == AF_INET) { | 559 if (addr.addr.sa_family == AF_INET) { |
587 ASSERT(interface.addr.sa_family == AF_INET); | 560 ASSERT(interface.addr.sa_family == AF_INET); |
588 struct ip_mreq mreq; | 561 struct ip_mreq mreq; |
589 memmove(&mreq.imr_multiaddr, | 562 memmove(&mreq.imr_multiaddr, &addr.in.sin_addr, |
590 &addr.in.sin_addr, | |
591 SocketAddress::GetInAddrLength(addr)); | 563 SocketAddress::GetInAddrLength(addr)); |
592 memmove(&mreq.imr_interface, | 564 memmove(&mreq.imr_interface, &interface.in.sin_addr, |
593 &interface.in.sin_addr, | |
594 SocketAddress::GetInAddrLength(interface)); | 565 SocketAddress::GetInAddrLength(interface)); |
595 if (join) { | 566 if (join) { |
596 return NO_RETRY_EXPECTED(setsockopt( | 567 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
597 fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) == 0; | 568 &mreq, sizeof(mreq))) == 0; |
598 } else { | 569 } else { |
599 return NO_RETRY_EXPECTED(setsockopt( | 570 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
600 fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq))) == 0; | 571 &mreq, sizeof(mreq))) == 0; |
601 } | 572 } |
602 } else { | 573 } else { |
603 ASSERT(addr.addr.sa_family == AF_INET6); | 574 ASSERT(addr.addr.sa_family == AF_INET6); |
604 struct ipv6_mreq mreq; | 575 struct ipv6_mreq mreq; |
605 memmove(&mreq.ipv6mr_multiaddr, | 576 memmove(&mreq.ipv6mr_multiaddr, &addr.in6.sin6_addr, |
606 &addr.in6.sin6_addr, | |
607 SocketAddress::GetInAddrLength(addr)); | 577 SocketAddress::GetInAddrLength(addr)); |
608 mreq.ipv6mr_interface = interfaceIndex; | 578 mreq.ipv6mr_interface = interfaceIndex; |
609 if (join) { | 579 if (join) { |
610 return NO_RETRY_EXPECTED(setsockopt( | 580 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
611 fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 581 &mreq, sizeof(mreq))) == 0; |
612 } else { | 582 } else { |
613 return NO_RETRY_EXPECTED(setsockopt( | 583 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
614 fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 584 &mreq, sizeof(mreq))) == 0; |
615 } | 585 } |
616 } | 586 } |
617 } | 587 } |
618 | 588 |
619 bool Socket::JoinMulticast(intptr_t fd, | 589 bool Socket::JoinMulticast(intptr_t fd, |
620 const RawAddr& addr, | 590 const RawAddr& addr, |
621 const RawAddr& interface, | 591 const RawAddr& interface, |
622 int interfaceIndex) { | 592 int interfaceIndex) { |
623 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, true); | 593 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, true); |
624 } | 594 } |
625 | 595 |
626 | 596 |
627 bool Socket::LeaveMulticast(intptr_t fd, | 597 bool Socket::LeaveMulticast(intptr_t fd, |
628 const RawAddr& addr, | 598 const RawAddr& addr, |
629 const RawAddr& interface, | 599 const RawAddr& interface, |
630 int interfaceIndex) { | 600 int interfaceIndex) { |
631 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); | 601 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); |
632 } | 602 } |
633 | 603 |
634 } // namespace bin | 604 } // namespace bin |
635 } // namespace dart | 605 } // namespace dart |
636 | 606 |
637 #endif // defined(TARGET_OS_MACOS) | 607 #endif // defined(TARGET_OS_MACOS) |
638 | 608 |
639 #endif // !defined(DART_IO_DISABLED) | 609 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |