Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Side by Side Diff: runtime/bin/socket_macos.cc

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

Powered by Google App Engine
This is Rietveld 408576698