| 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 #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(HOST_OS_ANDROID) | 8 #if defined(HOST_OS_ANDROID) |
| 9 | 9 |
| 10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
| 11 | 11 |
| 12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 13 | 13 |
| 14 #include "bin/fdutils.h" | 14 #include "bin/fdutils.h" |
| 15 #include "platform/signal_blocker.h" | 15 #include "platform/signal_blocker.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 namespace bin { | 18 namespace bin { |
| 19 | 19 |
| 20 Socket::Socket(intptr_t fd) | 20 Socket::Socket(intptr_t fd) |
| 21 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 21 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
| 22 | 22 |
| 23 | |
| 24 void Socket::SetClosedFd() { | 23 void Socket::SetClosedFd() { |
| 25 fd_ = kClosedFd; | 24 fd_ = kClosedFd; |
| 26 } | 25 } |
| 27 | 26 |
| 28 | |
| 29 static intptr_t Create(const RawAddr& addr) { | 27 static intptr_t Create(const RawAddr& addr) { |
| 30 intptr_t fd; | 28 intptr_t fd; |
| 31 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 29 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 32 if (fd < 0) { | 30 if (fd < 0) { |
| 33 return -1; | 31 return -1; |
| 34 } | 32 } |
| 35 if (!FDUtils::SetCloseOnExec(fd)) { | 33 if (!FDUtils::SetCloseOnExec(fd)) { |
| 36 FDUtils::SaveErrorAndClose(fd); | 34 FDUtils::SaveErrorAndClose(fd); |
| 37 return -1; | 35 return -1; |
| 38 } | 36 } |
| 39 return fd; | 37 return fd; |
| 40 } | 38 } |
| 41 | 39 |
| 42 | |
| 43 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 40 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 44 intptr_t result = TEMP_FAILURE_RETRY( | 41 intptr_t result = TEMP_FAILURE_RETRY( |
| 45 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 42 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 46 if ((result == 0) || (errno == EINPROGRESS)) { | 43 if ((result == 0) || (errno == EINPROGRESS)) { |
| 47 return fd; | 44 return fd; |
| 48 } | 45 } |
| 49 FDUtils::SaveErrorAndClose(fd); | 46 FDUtils::SaveErrorAndClose(fd); |
| 50 return -1; | 47 return -1; |
| 51 } | 48 } |
| 52 | 49 |
| 53 | |
| 54 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 50 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
| 55 intptr_t fd = Create(addr); | 51 intptr_t fd = Create(addr); |
| 56 if (fd < 0) { | 52 if (fd < 0) { |
| 57 return fd; | 53 return fd; |
| 58 } | 54 } |
| 59 | 55 |
| 60 if (!FDUtils::SetNonBlocking(fd)) { | 56 if (!FDUtils::SetNonBlocking(fd)) { |
| 61 FDUtils::SaveErrorAndClose(fd); | 57 FDUtils::SaveErrorAndClose(fd); |
| 62 return -1; | 58 return -1; |
| 63 } | 59 } |
| 64 return Connect(fd, addr); | 60 return Connect(fd, addr); |
| 65 } | 61 } |
| 66 | 62 |
| 67 | |
| 68 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 63 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 69 const RawAddr& source_addr) { | 64 const RawAddr& source_addr) { |
| 70 intptr_t fd = Create(addr); | 65 intptr_t fd = Create(addr); |
| 71 if (fd < 0) { | 66 if (fd < 0) { |
| 72 return fd; | 67 return fd; |
| 73 } | 68 } |
| 74 | 69 |
| 75 intptr_t result = TEMP_FAILURE_RETRY( | 70 intptr_t result = TEMP_FAILURE_RETRY( |
| 76 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 71 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
| 77 if ((result != 0) && (errno != EINPROGRESS)) { | 72 if ((result != 0) && (errno != EINPROGRESS)) { |
| 78 FDUtils::SaveErrorAndClose(fd); | 73 FDUtils::SaveErrorAndClose(fd); |
| 79 return -1; | 74 return -1; |
| 80 } | 75 } |
| 81 | 76 |
| 82 return Connect(fd, addr); | 77 return Connect(fd, addr); |
| 83 } | 78 } |
| 84 | 79 |
| 85 | |
| 86 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 80 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
| 87 intptr_t fd; | 81 intptr_t fd; |
| 88 | 82 |
| 89 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 83 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
| 90 if (fd < 0) { | 84 if (fd < 0) { |
| 91 return -1; | 85 return -1; |
| 92 } | 86 } |
| 93 | 87 |
| 94 if (!FDUtils::SetCloseOnExec(fd)) { | 88 if (!FDUtils::SetCloseOnExec(fd)) { |
| 95 FDUtils::SaveErrorAndClose(fd); | 89 FDUtils::SaveErrorAndClose(fd); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 return -1; | 102 return -1; |
| 109 } | 103 } |
| 110 | 104 |
| 111 if (!FDUtils::SetNonBlocking(fd)) { | 105 if (!FDUtils::SetNonBlocking(fd)) { |
| 112 FDUtils::SaveErrorAndClose(fd); | 106 FDUtils::SaveErrorAndClose(fd); |
| 113 return -1; | 107 return -1; |
| 114 } | 108 } |
| 115 return fd; | 109 return fd; |
| 116 } | 110 } |
| 117 | 111 |
| 118 | |
| 119 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 112 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
| 120 intptr_t backlog, | 113 intptr_t backlog, |
| 121 bool v6_only) { | 114 bool v6_only) { |
| 122 intptr_t fd; | 115 intptr_t fd; |
| 123 | 116 |
| 124 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 117 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 125 if (fd < 0) { | 118 if (fd < 0) { |
| 126 return -1; | 119 return -1; |
| 127 } | 120 } |
| 128 | 121 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 return -1; | 155 return -1; |
| 163 } | 156 } |
| 164 | 157 |
| 165 if (!FDUtils::SetNonBlocking(fd)) { | 158 if (!FDUtils::SetNonBlocking(fd)) { |
| 166 FDUtils::SaveErrorAndClose(fd); | 159 FDUtils::SaveErrorAndClose(fd); |
| 167 return -1; | 160 return -1; |
| 168 } | 161 } |
| 169 return fd; | 162 return fd; |
| 170 } | 163 } |
| 171 | 164 |
| 172 | |
| 173 bool ServerSocket::StartAccept(intptr_t fd) { | 165 bool ServerSocket::StartAccept(intptr_t fd) { |
| 174 USE(fd); | 166 USE(fd); |
| 175 return true; | 167 return true; |
| 176 } | 168 } |
| 177 | 169 |
| 178 | |
| 179 static bool IsTemporaryAcceptError(int error) { | 170 static bool IsTemporaryAcceptError(int error) { |
| 180 // On Android a number of protocol errors should be treated as EAGAIN. | 171 // On Android a number of protocol errors should be treated as EAGAIN. |
| 181 // These are the ones for TCP/IP. | 172 // These are the ones for TCP/IP. |
| 182 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 173 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
| 183 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 174 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
| 184 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 175 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
| 185 (error == ENETUNREACH); | 176 (error == ENETUNREACH); |
| 186 } | 177 } |
| 187 | 178 |
| 188 | |
| 189 intptr_t ServerSocket::Accept(intptr_t fd) { | 179 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 190 intptr_t socket; | 180 intptr_t socket; |
| 191 struct sockaddr clientaddr; | 181 struct sockaddr clientaddr; |
| 192 socklen_t addrlen = sizeof(clientaddr); | 182 socklen_t addrlen = sizeof(clientaddr); |
| 193 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 183 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 194 if (socket == -1) { | 184 if (socket == -1) { |
| 195 if (IsTemporaryAcceptError(errno)) { | 185 if (IsTemporaryAcceptError(errno)) { |
| 196 // We need to signal to the caller that this is actually not an | 186 // We need to signal to the caller that this is actually not an |
| 197 // error. We got woken up from the poll on the listening socket, | 187 // error. We got woken up from the poll on the listening socket, |
| 198 // but there is no connection ready to be accepted. | 188 // but there is no connection ready to be accepted. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 211 } | 201 } |
| 212 return socket; | 202 return socket; |
| 213 } | 203 } |
| 214 | 204 |
| 215 } // namespace bin | 205 } // namespace bin |
| 216 } // namespace dart | 206 } // namespace dart |
| 217 | 207 |
| 218 #endif // defined(HOST_OS_ANDROID) | 208 #endif // defined(HOST_OS_ANDROID) |
| 219 | 209 |
| 220 #endif // !defined(DART_IO_DISABLED) | 210 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |