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

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

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

Powered by Google App Engine
This is Rietveld 408576698