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

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

Issue 85993002: Add UDP support to dart:io (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix Windows build Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/socket_android.cc ('k') | runtime/bin/socket_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_LINUX) 6 #if defined(TARGET_OS_LINUX)
7 7
8 #include <errno.h> // NOLINT 8 #include <errno.h> // NOLINT
9 #include <stdio.h> // NOLINT 9 #include <stdio.h> // NOLINT
10 #include <stdlib.h> // NOLINT 10 #include <stdlib.h> // NOLINT
11 #include <string.h> // NOLINT 11 #include <string.h> // NOLINT
12 #include <sys/stat.h> // NOLINT 12 #include <sys/stat.h> // NOLINT
13 #include <unistd.h> // NOLINT 13 #include <unistd.h> // NOLINT
14 #include <net/if.h> // NOLINT 14 #include <net/if.h> // NOLINT
15 #include <netinet/tcp.h> // NOLINT 15 #include <netinet/tcp.h> // NOLINT
16 #include <ifaddrs.h> // NOLINT 16 #include <ifaddrs.h> // NOLINT
17 17
18 #include "bin/fdutils.h" 18 #include "bin/fdutils.h"
19 #include "bin/file.h" 19 #include "bin/file.h"
20 #include "bin/log.h" 20 #include "bin/log.h"
21 #include "bin/socket.h" 21 #include "bin/socket.h"
22 22
23 23
24 namespace dart { 24 namespace dart {
25 namespace bin { 25 namespace bin {
26 26
27 SocketAddress::SocketAddress(struct sockaddr* sa) { 27 SocketAddress::SocketAddress(struct sockaddr* sa) {
28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
29 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa)); 29 if (!Socket::FormatNumericAddress(
30 if (TEMP_FAILURE_RETRY(getnameinfo(sa, 30 reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) {
31 salen,
32 as_string_,
33 INET6_ADDRSTRLEN,
34 NULL,
35 0,
36 NI_NUMERICHOST)) != 0) {
37 as_string_[0] = 0; 31 as_string_[0] = 0;
38 } 32 }
33 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa));
39 memmove(reinterpret_cast<void *>(&addr_), sa, salen); 34 memmove(reinterpret_cast<void *>(&addr_), sa, salen);
40 } 35 }
41 36
42 37
38 bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) {
39 socklen_t salen = SocketAddress::GetAddrLength(addr);
40 if (TEMP_FAILURE_RETRY(getnameinfo(&addr->addr,
41 salen,
42 address,
43 len,
44 NULL,
45 0,
46 NI_NUMERICHOST)) != 0) {
47 return false;
48 }
49 return true;
50 }
51
52
43 bool Socket::Initialize() { 53 bool Socket::Initialize() {
44 // Nothing to do on Linux. 54 // Nothing to do on Linux.
45 return true; 55 return true;
46 } 56 }
47 57
48 58
49 intptr_t Socket::Create(RawAddr addr) { 59 intptr_t Socket::Create(RawAddr addr) {
50 intptr_t fd; 60 intptr_t fd;
51 61
52 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 62 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 ASSERT(EAGAIN == EWOULDBLOCK); 110 ASSERT(EAGAIN == EWOULDBLOCK);
101 if (read_bytes == -1 && errno == EWOULDBLOCK) { 111 if (read_bytes == -1 && errno == EWOULDBLOCK) {
102 // If the read would block we need to retry and therefore return 0 112 // If the read would block we need to retry and therefore return 0
103 // as the number of bytes written. 113 // as the number of bytes written.
104 read_bytes = 0; 114 read_bytes = 0;
105 } 115 }
106 return read_bytes; 116 return read_bytes;
107 } 117 }
108 118
109 119
120 int Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes,
121 RawAddr* addr) {
122 ASSERT(fd >= 0);
123 socklen_t addr_len = sizeof(addr->ss);
124 ssize_t read_bytes =
125 TEMP_FAILURE_RETRY(
126 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len));
127 if (read_bytes == -1 && errno == EWOULDBLOCK) {
128 // If the read would block we need to retry and therefore return 0
129 // as the number of bytes written.
130 read_bytes = 0;
131 }
132 return read_bytes;
133 }
134
135
110 int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { 136 int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
111 ASSERT(fd >= 0); 137 ASSERT(fd >= 0);
112 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); 138 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
113 ASSERT(EAGAIN == EWOULDBLOCK); 139 ASSERT(EAGAIN == EWOULDBLOCK);
114 if (written_bytes == -1 && errno == EWOULDBLOCK) { 140 if (written_bytes == -1 && errno == EWOULDBLOCK) {
115 // If the would block we need to retry and therefore return 0 as 141 // If the would block we need to retry and therefore return 0 as
116 // the number of bytes written. 142 // the number of bytes written.
117 written_bytes = 0; 143 written_bytes = 0;
118 } 144 }
119 return written_bytes; 145 return written_bytes;
120 } 146 }
121 147
122 148
149 int Socket::SendTo(intptr_t fd, const void* buffer, intptr_t num_bytes,
150 RawAddr addr) {
151 ASSERT(fd >= 0);
152 ssize_t written_bytes =
153 TEMP_FAILURE_RETRY(
154 sendto(fd, buffer, num_bytes, 0,
155 &addr.addr, SocketAddress::GetAddrLength(&addr)));
156 ASSERT(EAGAIN == EWOULDBLOCK);
157 if (written_bytes == -1 && errno == EWOULDBLOCK) {
158 // If the would block we need to retry and therefore return 0 as
159 // the number of bytes written.
160 written_bytes = 0;
161 }
162 return written_bytes;
163 }
164
165
123 intptr_t Socket::GetPort(intptr_t fd) { 166 intptr_t Socket::GetPort(intptr_t fd) {
124 ASSERT(fd >= 0); 167 ASSERT(fd >= 0);
125 RawAddr raw; 168 RawAddr raw;
126 socklen_t size = sizeof(raw); 169 socklen_t size = sizeof(raw);
127 if (TEMP_FAILURE_RETRY( 170 if (TEMP_FAILURE_RETRY(
128 getsockname(fd, 171 getsockname(fd,
129 &raw.addr, 172 &raw.addr,
130 &size))) { 173 &size))) {
131 const int kBufferSize = 1024; 174 const int kBufferSize = 1024;
132 char error_buf[kBufferSize]; 175 char error_buf[kBufferSize];
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 if (type == SocketAddress::TYPE_IPV4) { 293 if (type == SocketAddress::TYPE_IPV4) {
251 result = inet_pton(AF_INET, address, &addr->in.sin_addr); 294 result = inet_pton(AF_INET, address, &addr->in.sin_addr);
252 } else { 295 } else {
253 ASSERT(type == SocketAddress::TYPE_IPV6); 296 ASSERT(type == SocketAddress::TYPE_IPV6);
254 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); 297 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr);
255 } 298 }
256 return result == 1; 299 return result == 1;
257 } 300 }
258 301
259 302
303 intptr_t Socket::CreateBindDatagram(
304 RawAddr* addr, intptr_t port, bool reuseAddress) {
305 intptr_t fd;
306
307 fd = TEMP_FAILURE_RETRY(
308 socket(addr->addr.sa_family, SOCK_DGRAM, IPPROTO_UDP));
309 if (fd < 0) return -1;
310
311 FDUtils::SetCloseOnExec(fd);
312
313 if (reuseAddress) {
314 int optval = 1;
315 TEMP_FAILURE_RETRY(
316 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
317 }
318
319 SocketAddress::SetAddrPort(addr, port);
320 if (TEMP_FAILURE_RETRY(
321 bind(fd,
322 &addr->addr,
323 SocketAddress::GetAddrLength(addr))) < 0) {
324 TEMP_FAILURE_RETRY(close(fd));
325 return -1;
326 }
327
328 Socket::SetNonBlocking(fd);
329 return fd;
330 }
331
332
260 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { 333 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
261 if (ifa->ifa_addr == NULL) { 334 if (ifa->ifa_addr == NULL) {
262 // OpenVPN's virtual device tun0. 335 // OpenVPN's virtual device tun0.
263 return false; 336 return false;
264 } 337 }
265 int family = ifa->ifa_addr->sa_family; 338 int family = ifa->ifa_addr->sa_family;
266 if (lookup_family == family) return true; 339 if (lookup_family == family) return true;
267 if (lookup_family == AF_UNSPEC && 340 if (lookup_family == AF_UNSPEC &&
268 (family == AF_INET || family == AF_INET6)) { 341 (family == AF_INET || family == AF_INET6)) {
269 return true; 342 return true;
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 bool Socket::SetNonBlocking(intptr_t fd) { 476 bool Socket::SetNonBlocking(intptr_t fd) {
404 return FDUtils::SetNonBlocking(fd); 477 return FDUtils::SetNonBlocking(fd);
405 } 478 }
406 479
407 480
408 bool Socket::SetBlocking(intptr_t fd) { 481 bool Socket::SetBlocking(intptr_t fd) {
409 return FDUtils::SetBlocking(fd); 482 return FDUtils::SetBlocking(fd);
410 } 483 }
411 484
412 485
486 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
487 int on;
488 socklen_t len = sizeof(on);
489 int err = TEMP_FAILURE_RETRY(getsockopt(fd,
490 IPPROTO_TCP,
491 TCP_NODELAY,
492 reinterpret_cast<void *>(&on),
493 &len));
494 if (err == 0) {
495 *enabled = on == 1;
496 }
497 return err == 0;
498 }
499
500
413 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 501 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
414 int on = enabled ? 1 : 0; 502 int on = enabled ? 1 : 0;
415 return TEMP_FAILURE_RETRY(setsockopt(fd, 503 return TEMP_FAILURE_RETRY(setsockopt(fd,
416 IPPROTO_TCP, 504 IPPROTO_TCP,
417 TCP_NODELAY, 505 TCP_NODELAY,
418 reinterpret_cast<char *>(&on), 506 reinterpret_cast<char *>(&on),
419 sizeof(on))) == 0; 507 sizeof(on))) == 0;
420 } 508 }
421 509
510
511 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
512 uint8_t on;
513 socklen_t len = sizeof(on);
514 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
515 int optname = protocol == SocketAddress::TYPE_IPV4
516 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP;
517 if (TEMP_FAILURE_RETRY(getsockopt(fd,
518 level,
519 optname,
520 reinterpret_cast<char *>(&on),
521 &len)) == 0) {
522 *enabled = (on == 1);
523 return true;
524 }
525 return false;
526 }
527
528
529 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
530 int on = enabled ? 1 : 0;
531 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
532 int optname = protocol == SocketAddress::TYPE_IPV4
533 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP;
534 return TEMP_FAILURE_RETRY(setsockopt(fd,
535 level,
536 optname,
537 reinterpret_cast<char *>(&on),
538 sizeof(on))) == 0;
539 }
540
541
542 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
543 uint8_t v;
544 socklen_t len = sizeof(v);
545 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
546 int optname = protocol == SocketAddress::TYPE_IPV4
547 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS;
548 if (TEMP_FAILURE_RETRY(getsockopt(fd,
549 level,
550 optname,
551 reinterpret_cast<char *>(&v),
552 &len)) == 0) {
553 *value = v;
554 return true;
555 }
556 return false;
557 }
558
559
560 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
561 int v = value;
562 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
563 int optname = protocol == SocketAddress::TYPE_IPV4
564 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS;
565 return TEMP_FAILURE_RETRY(setsockopt(fd,
566 level,
567 optname,
568 reinterpret_cast<char *>(&v),
569 sizeof(v))) == 0;
570 }
571
572
573 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
574 int on;
575 socklen_t len = sizeof(on);
576 int err = TEMP_FAILURE_RETRY(getsockopt(fd,
577 SOL_SOCKET,
578 SO_BROADCAST,
579 reinterpret_cast<char *>(&on),
580 &len));
581 if (err == 0) {
582 *enabled = on == 1;
583 }
584 return err == 0;
585 }
586
587
588 bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
589 int on = enabled ? 1 : 0;
590 return TEMP_FAILURE_RETRY(setsockopt(fd,
591 SOL_SOCKET,
592 SO_BROADCAST,
593 reinterpret_cast<char *>(&on),
594 sizeof(on))) == 0;
595 }
596
597
598 bool Socket::JoinMulticast(
599 intptr_t fd, RawAddr* addr, RawAddr*, 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 TEMP_FAILURE_RETRY(setsockopt(
605 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0;
606 }
607
608
609 bool Socket::LeaveMulticast(
610 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) {
611 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
612 struct group_req mreq;
613 mreq.gr_interface = interfaceIndex;
614 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr));
615 return TEMP_FAILURE_RETRY(setsockopt(
616 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0;
617 }
618
422 } // namespace bin 619 } // namespace bin
423 } // namespace dart 620 } // namespace dart
424 621
425 #endif // defined(TARGET_OS_LINUX) 622 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « runtime/bin/socket_android.cc ('k') | runtime/bin/socket_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698