OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
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 <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
15 | 15 |
16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
17 #include "bin/file.h" | 17 #include "bin/file.h" |
18 #include "bin/log.h" | |
19 #include "bin/socket.h" | 18 #include "bin/socket.h" |
20 | 19 |
21 #include "platform/signal_blocker.h" | 20 #include "platform/signal_blocker.h" |
22 | 21 |
23 | 22 |
24 namespace dart { | 23 namespace dart { |
25 namespace bin { | 24 namespace bin { |
26 | 25 |
27 SocketAddress::SocketAddress(struct sockaddr* sa) { | 26 SocketAddress::SocketAddress(struct sockaddr* sa) { |
28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
(...skipping 24 matching lines...) Expand all Loading... |
53 bool Socket::Initialize() { | 52 bool Socket::Initialize() { |
54 // Nothing to do on Android. | 53 // Nothing to do on Android. |
55 return true; | 54 return true; |
56 } | 55 } |
57 | 56 |
58 | 57 |
59 intptr_t Socket::Create(RawAddr addr) { | 58 intptr_t Socket::Create(RawAddr addr) { |
60 intptr_t fd; | 59 intptr_t fd; |
61 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 60 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
62 if (fd < 0) { | 61 if (fd < 0) { |
63 const int kBufferSize = 1024; | |
64 char error_message[kBufferSize]; | |
65 strerror_r(errno, error_message, kBufferSize); | |
66 Log::PrintErr("Error Create: %s\n", error_message); | |
67 return -1; | 62 return -1; |
68 } | 63 } |
69 | |
70 FDUtils::SetCloseOnExec(fd); | 64 FDUtils::SetCloseOnExec(fd); |
71 return fd; | 65 return fd; |
72 } | 66 } |
73 | 67 |
74 | 68 |
75 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 69 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
76 SocketAddress::SetAddrPort(&addr, port); | 70 SocketAddress::SetAddrPort(&addr, port); |
77 intptr_t result = TEMP_FAILURE_RETRY( | 71 intptr_t result = TEMP_FAILURE_RETRY( |
78 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); | 72 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); |
79 if (result == 0 || errno == EINPROGRESS) { | 73 if (result == 0 || errno == EINPROGRESS) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 150 } |
157 return written_bytes; | 151 return written_bytes; |
158 } | 152 } |
159 | 153 |
160 | 154 |
161 intptr_t Socket::GetPort(intptr_t fd) { | 155 intptr_t Socket::GetPort(intptr_t fd) { |
162 ASSERT(fd >= 0); | 156 ASSERT(fd >= 0); |
163 RawAddr raw; | 157 RawAddr raw; |
164 socklen_t size = sizeof(raw); | 158 socklen_t size = sizeof(raw); |
165 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 159 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
166 const int kBufferSize = 1024; | |
167 char error_message[kBufferSize]; | |
168 strerror_r(errno, error_message, kBufferSize); | |
169 Log::PrintErr("Error getsockname: %s\n", error_message); | |
170 return 0; | 160 return 0; |
171 } | 161 } |
172 return SocketAddress::GetAddrPort(&raw); | 162 return SocketAddress::GetAddrPort(&raw); |
173 } | 163 } |
174 | 164 |
175 | 165 |
176 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 166 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
177 ASSERT(fd >= 0); | 167 ASSERT(fd >= 0); |
178 RawAddr raw; | 168 RawAddr raw; |
179 socklen_t size = sizeof(raw); | 169 socklen_t size = sizeof(raw); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 } | 401 } |
412 } else { | 402 } else { |
413 Socket::SetNonBlocking(socket); | 403 Socket::SetNonBlocking(socket); |
414 } | 404 } |
415 return socket; | 405 return socket; |
416 } | 406 } |
417 | 407 |
418 | 408 |
419 void Socket::Close(intptr_t fd) { | 409 void Socket::Close(intptr_t fd) { |
420 ASSERT(fd >= 0); | 410 ASSERT(fd >= 0); |
421 int err = TEMP_FAILURE_RETRY(close(fd)); | 411 VOID_TEMP_FAILURE_RETRY(close(fd)); |
422 if (err != 0) { | |
423 const int kBufferSize = 1024; | |
424 char error_message[kBufferSize]; | |
425 strerror_r(errno, error_message, kBufferSize); | |
426 Log::PrintErr("%s\n", error_message); | |
427 } | |
428 } | 412 } |
429 | 413 |
430 | 414 |
431 bool Socket::SetNonBlocking(intptr_t fd) { | 415 bool Socket::SetNonBlocking(intptr_t fd) { |
432 return FDUtils::SetNonBlocking(fd); | 416 return FDUtils::SetNonBlocking(fd); |
433 } | 417 } |
434 | 418 |
435 | 419 |
436 bool Socket::SetBlocking(intptr_t fd) { | 420 bool Socket::SetBlocking(intptr_t fd) { |
437 return FDUtils::SetBlocking(fd); | 421 return FDUtils::SetBlocking(fd); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 mreq.gr_interface = interfaceIndex; | 551 mreq.gr_interface = interfaceIndex; |
568 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 552 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
569 return NO_RETRY_EXPECTED(setsockopt( | 553 return NO_RETRY_EXPECTED(setsockopt( |
570 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 554 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
571 } | 555 } |
572 | 556 |
573 } // namespace bin | 557 } // namespace bin |
574 } // namespace dart | 558 } // namespace dart |
575 | 559 |
576 #endif // defined(TARGET_OS_ANDROID) | 560 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |