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" | 18 #include "bin/log.h" |
19 #include "bin/signal_blocker.h" | |
20 #include "bin/socket.h" | 19 #include "bin/socket.h" |
21 | 20 |
| 21 #include "platform/signal_blocker.h" |
| 22 |
22 | 23 |
23 namespace dart { | 24 namespace dart { |
24 namespace bin { | 25 namespace bin { |
25 | 26 |
26 SocketAddress::SocketAddress(struct sockaddr* sa) { | 27 SocketAddress::SocketAddress(struct sockaddr* sa) { |
27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
28 if (!Socket::FormatNumericAddress( | 29 if (!Socket::FormatNumericAddress( |
29 reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 30 reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { |
30 as_string_[0] = 0; | 31 as_string_[0] = 0; |
31 } | 32 } |
32 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa)); | 33 socklen_t salen = GetAddrLength(reinterpret_cast<RawAddr*>(sa)); |
33 memmove(reinterpret_cast<void *>(&addr_), sa, salen); | 34 memmove(reinterpret_cast<void *>(&addr_), sa, salen); |
34 } | 35 } |
35 | 36 |
36 | 37 |
37 bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) { | 38 bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) { |
38 socklen_t salen = SocketAddress::GetAddrLength(addr); | 39 socklen_t salen = SocketAddress::GetAddrLength(addr); |
39 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getnameinfo(&addr->addr, | 40 if (NO_RETRY_EXPECTED(getnameinfo(&addr->addr, |
40 salen, | 41 salen, |
41 address, | 42 address, |
42 len, | 43 len, |
43 NULL, | 44 NULL, |
44 0, | 45 0, |
45 NI_NUMERICHOST)) != 0) { | 46 NI_NUMERICHOST)) != 0) { |
46 return false; | 47 return false; |
47 } | 48 } |
48 return true; | 49 return true; |
49 } | 50 } |
50 | 51 |
51 | 52 |
52 bool Socket::Initialize() { | 53 bool Socket::Initialize() { |
53 // Nothing to do on Android. | 54 // Nothing to do on Android. |
54 return true; | 55 return true; |
55 } | 56 } |
56 | 57 |
57 | 58 |
58 intptr_t Socket::Create(RawAddr addr) { | 59 intptr_t Socket::Create(RawAddr addr) { |
59 intptr_t fd; | 60 intptr_t fd; |
60 fd = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(socket(addr.ss.ss_family, SOCK_STREAM, | 61 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
61 0)); | |
62 if (fd < 0) { | 62 if (fd < 0) { |
63 const int kBufferSize = 1024; | 63 const int kBufferSize = 1024; |
64 char error_message[kBufferSize]; | 64 char error_message[kBufferSize]; |
65 strerror_r(errno, error_message, kBufferSize); | 65 strerror_r(errno, error_message, kBufferSize); |
66 Log::PrintErr("Error Create: %s\n", error_message); | 66 Log::PrintErr("Error Create: %s\n", error_message); |
67 return -1; | 67 return -1; |
68 } | 68 } |
69 | 69 |
70 FDUtils::SetCloseOnExec(fd); | 70 FDUtils::SetCloseOnExec(fd); |
71 return fd; | 71 return fd; |
72 } | 72 } |
73 | 73 |
74 | 74 |
75 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 75 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
76 SocketAddress::SetAddrPort(&addr, port); | 76 SocketAddress::SetAddrPort(&addr, port); |
77 intptr_t result = TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 77 intptr_t result = TEMP_FAILURE_RETRY( |
78 connect(fd, | 78 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); |
79 &addr.addr, | |
80 SocketAddress::GetAddrLength(&addr))); | |
81 if (result == 0 || errno == EINPROGRESS) { | 79 if (result == 0 || errno == EINPROGRESS) { |
82 return fd; | 80 return fd; |
83 } | 81 } |
84 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | 82 VOID_TEMP_FAILURE_RETRY(close(fd)); |
85 return -1; | 83 return -1; |
86 } | 84 } |
87 | 85 |
88 | 86 |
89 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { | 87 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { |
90 intptr_t fd = Socket::Create(addr); | 88 intptr_t fd = Socket::Create(addr); |
91 if (fd < 0) { | 89 if (fd < 0) { |
92 return fd; | 90 return fd; |
93 } | 91 } |
94 | 92 |
95 Socket::SetNonBlocking(fd); | 93 Socket::SetNonBlocking(fd); |
96 | 94 |
97 return Socket::Connect(fd, addr, port); | 95 return Socket::Connect(fd, addr, port); |
98 } | 96 } |
99 | 97 |
100 | 98 |
101 intptr_t Socket::Available(intptr_t fd) { | 99 intptr_t Socket::Available(intptr_t fd) { |
102 return FDUtils::AvailableBytes(fd); | 100 return FDUtils::AvailableBytes(fd); |
103 } | 101 } |
104 | 102 |
105 | 103 |
106 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 104 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
107 ASSERT(fd >= 0); | 105 ASSERT(fd >= 0); |
108 ssize_t read_bytes = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(read(fd, buffer, | 106 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
109 num_bytes)); | |
110 ASSERT(EAGAIN == EWOULDBLOCK); | 107 ASSERT(EAGAIN == EWOULDBLOCK); |
111 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 108 if (read_bytes == -1 && errno == EWOULDBLOCK) { |
112 // If the read would block we need to retry and therefore return 0 | 109 // If the read would block we need to retry and therefore return 0 |
113 // as the number of bytes written. | 110 // as the number of bytes written. |
114 read_bytes = 0; | 111 read_bytes = 0; |
115 } | 112 } |
116 return read_bytes; | 113 return read_bytes; |
117 } | 114 } |
118 | 115 |
119 | 116 |
120 int Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes, | 117 int Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes, |
121 RawAddr* addr) { | 118 RawAddr* addr) { |
122 ASSERT(fd >= 0); | 119 ASSERT(fd >= 0); |
123 socklen_t addr_len = sizeof(addr->ss); | 120 socklen_t addr_len = sizeof(addr->ss); |
124 ssize_t read_bytes = | 121 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
125 TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 122 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
126 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | |
127 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 123 if (read_bytes == -1 && errno == EWOULDBLOCK) { |
128 // If the read would block we need to retry and therefore return 0 | 124 // If the read would block we need to retry and therefore return 0 |
129 // as the number of bytes written. | 125 // as the number of bytes written. |
130 read_bytes = 0; | 126 read_bytes = 0; |
131 } | 127 } |
132 return read_bytes; | 128 return read_bytes; |
133 } | 129 } |
134 | 130 |
135 | 131 |
136 int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 132 int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
137 ASSERT(fd >= 0); | 133 ASSERT(fd >= 0); |
138 ssize_t written_bytes = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(write(fd, buffer, | 134 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
139 num_bytes)); | |
140 ASSERT(EAGAIN == EWOULDBLOCK); | 135 ASSERT(EAGAIN == EWOULDBLOCK); |
141 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 136 if (written_bytes == -1 && errno == EWOULDBLOCK) { |
142 // If the would block we need to retry and therefore return 0 as | 137 // If the would block we need to retry and therefore return 0 as |
143 // the number of bytes written. | 138 // the number of bytes written. |
144 written_bytes = 0; | 139 written_bytes = 0; |
145 } | 140 } |
146 return written_bytes; | 141 return written_bytes; |
147 } | 142 } |
148 | 143 |
149 | 144 |
150 int Socket::SendTo(intptr_t fd, const void* buffer, intptr_t num_bytes, | 145 int Socket::SendTo(intptr_t fd, const void* buffer, intptr_t num_bytes, |
151 RawAddr addr) { | 146 RawAddr addr) { |
152 ASSERT(fd >= 0); | 147 ASSERT(fd >= 0); |
153 ssize_t written_bytes = | 148 ssize_t written_bytes = TEMP_FAILURE_RETRY( |
154 TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 149 sendto(fd, buffer, num_bytes, 0, |
155 sendto(fd, buffer, num_bytes, 0, | 150 &addr.addr, SocketAddress::GetAddrLength(&addr))); |
156 &addr.addr, SocketAddress::GetAddrLength(&addr))); | |
157 ASSERT(EAGAIN == EWOULDBLOCK); | 151 ASSERT(EAGAIN == EWOULDBLOCK); |
158 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 152 if (written_bytes == -1 && errno == EWOULDBLOCK) { |
159 // If the would block we need to retry and therefore return 0 as | 153 // If the would block we need to retry and therefore return 0 as |
160 // the number of bytes written. | 154 // the number of bytes written. |
161 written_bytes = 0; | 155 written_bytes = 0; |
162 } | 156 } |
163 return written_bytes; | 157 return written_bytes; |
164 } | 158 } |
165 | 159 |
166 | 160 |
167 intptr_t Socket::GetPort(intptr_t fd) { | 161 intptr_t Socket::GetPort(intptr_t fd) { |
168 ASSERT(fd >= 0); | 162 ASSERT(fd >= 0); |
169 RawAddr raw; | 163 RawAddr raw; |
170 socklen_t size = sizeof(raw); | 164 socklen_t size = sizeof(raw); |
171 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 165 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
172 getsockname(fd, | |
173 &raw.addr, | |
174 &size))) { | |
175 const int kBufferSize = 1024; | 166 const int kBufferSize = 1024; |
176 char error_message[kBufferSize]; | 167 char error_message[kBufferSize]; |
177 strerror_r(errno, error_message, kBufferSize); | 168 strerror_r(errno, error_message, kBufferSize); |
178 Log::PrintErr("Error getsockname: %s\n", error_message); | 169 Log::PrintErr("Error getsockname: %s\n", error_message); |
179 return 0; | 170 return 0; |
180 } | 171 } |
181 return SocketAddress::GetAddrPort(&raw); | 172 return SocketAddress::GetAddrPort(&raw); |
182 } | 173 } |
183 | 174 |
184 | 175 |
185 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 176 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
186 ASSERT(fd >= 0); | 177 ASSERT(fd >= 0); |
187 RawAddr raw; | 178 RawAddr raw; |
188 socklen_t size = sizeof(raw); | 179 socklen_t size = sizeof(raw); |
189 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 180 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { |
190 getpeername(fd, | |
191 &raw.addr, | |
192 &size))) { | |
193 return NULL; | 181 return NULL; |
194 } | 182 } |
195 *port = SocketAddress::GetAddrPort(&raw); | 183 *port = SocketAddress::GetAddrPort(&raw); |
196 return new SocketAddress(&raw.addr); | 184 return new SocketAddress(&raw.addr); |
197 } | 185 } |
198 | 186 |
199 | 187 |
200 void Socket::GetError(intptr_t fd, OSError* os_error) { | 188 void Socket::GetError(intptr_t fd, OSError* os_error) { |
201 int errorNumber; | 189 int errorNumber; |
202 socklen_t len = sizeof(errorNumber); | 190 socklen_t len = sizeof(errorNumber); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 freeaddrinfo(info); | 253 freeaddrinfo(info); |
266 return addresses; | 254 return addresses; |
267 } | 255 } |
268 | 256 |
269 | 257 |
270 bool Socket::ReverseLookup(RawAddr addr, | 258 bool Socket::ReverseLookup(RawAddr addr, |
271 char* host, | 259 char* host, |
272 intptr_t host_len, | 260 intptr_t host_len, |
273 OSError** os_error) { | 261 OSError** os_error) { |
274 ASSERT(host_len >= NI_MAXHOST); | 262 ASSERT(host_len >= NI_MAXHOST); |
275 int status = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getnameinfo( | 263 int status = NO_RETRY_EXPECTED(getnameinfo( |
276 &addr.addr, | 264 &addr.addr, |
277 SocketAddress::GetAddrLength(&addr), | 265 SocketAddress::GetAddrLength(&addr), |
278 host, | 266 host, |
279 host_len, | 267 host_len, |
280 NULL, | 268 NULL, |
281 0, | 269 0, |
282 NI_NAMEREQD)); | 270 NI_NAMEREQD)); |
283 if (status != 0) { | 271 if (status != 0) { |
284 ASSERT(*os_error == NULL); | 272 ASSERT(*os_error == NULL); |
285 *os_error = new OSError(status, | 273 *os_error = new OSError(status, |
(...skipping 14 matching lines...) Expand all Loading... |
300 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); | 288 result = inet_pton(AF_INET6, address, &addr->in6.sin6_addr); |
301 } | 289 } |
302 return result == 1; | 290 return result == 1; |
303 } | 291 } |
304 | 292 |
305 | 293 |
306 intptr_t Socket::CreateBindDatagram( | 294 intptr_t Socket::CreateBindDatagram( |
307 RawAddr* addr, intptr_t port, bool reuseAddress) { | 295 RawAddr* addr, intptr_t port, bool reuseAddress) { |
308 intptr_t fd; | 296 intptr_t fd; |
309 | 297 |
310 fd = TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 298 fd = NO_RETRY_EXPECTED(socket(addr->addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
311 socket(addr->addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | |
312 if (fd < 0) return -1; | 299 if (fd < 0) return -1; |
313 | 300 |
314 FDUtils::SetCloseOnExec(fd); | 301 FDUtils::SetCloseOnExec(fd); |
315 | 302 |
316 if (reuseAddress) { | 303 if (reuseAddress) { |
317 int optval = 1; | 304 int optval = 1; |
318 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 305 VOID_NO_RETRY_EXPECTED( |
319 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 306 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
320 } | 307 } |
321 | 308 |
322 SocketAddress::SetAddrPort(addr, port); | 309 SocketAddress::SetAddrPort(addr, port); |
323 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 310 if (NO_RETRY_EXPECTED( |
324 bind(fd, | 311 bind(fd, |
325 &addr->addr, | 312 &addr->addr, |
326 SocketAddress::GetAddrLength(addr))) < 0) { | 313 SocketAddress::GetAddrLength(addr))) < 0) { |
327 TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | 314 VOID_TEMP_FAILURE_RETRY(close(fd)); |
328 return -1; | 315 return -1; |
329 } | 316 } |
330 | 317 |
331 Socket::SetNonBlocking(fd); | 318 Socket::SetNonBlocking(fd); |
332 return fd; | 319 return fd; |
333 } | 320 } |
334 | 321 |
335 | 322 |
336 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 323 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
337 int type, | 324 int type, |
338 OSError** os_error) { | 325 OSError** os_error) { |
339 // The ifaddrs.h header is not provided on Android. An Android | 326 // The ifaddrs.h header is not provided on Android. An Android |
340 // implementation would have to use IOCTL or netlink. | 327 // implementation would have to use IOCTL or netlink. |
341 return NULL; | 328 return NULL; |
342 } | 329 } |
343 | 330 |
344 | 331 |
345 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 332 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
346 intptr_t port, | 333 intptr_t port, |
347 intptr_t backlog, | 334 intptr_t backlog, |
348 bool v6_only) { | 335 bool v6_only) { |
349 intptr_t fd; | 336 intptr_t fd; |
350 | 337 |
351 fd = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(socket(addr.ss.ss_family, SOCK_STREAM, | 338 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
352 0)); | |
353 if (fd < 0) return -1; | 339 if (fd < 0) return -1; |
354 | 340 |
355 FDUtils::SetCloseOnExec(fd); | 341 FDUtils::SetCloseOnExec(fd); |
356 | 342 |
357 int optval = 1; | 343 int optval = 1; |
358 TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 344 VOID_NO_RETRY_EXPECTED( |
359 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 345 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
360 | 346 |
361 if (addr.ss.ss_family == AF_INET6) { | 347 if (addr.ss.ss_family == AF_INET6) { |
362 optval = v6_only ? 1 : 0; | 348 optval = v6_only ? 1 : 0; |
363 TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 349 VOID_NO_RETRY_EXPECTED( |
364 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 350 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
365 } | 351 } |
366 | 352 |
367 SocketAddress::SetAddrPort(&addr, port); | 353 SocketAddress::SetAddrPort(&addr, port); |
368 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 354 if (NO_RETRY_EXPECTED( |
369 bind(fd, | 355 bind(fd, |
370 &addr.addr, | 356 &addr.addr, |
371 SocketAddress::GetAddrLength(&addr))) < 0) { | 357 SocketAddress::GetAddrLength(&addr))) < 0) { |
372 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | 358 VOID_TEMP_FAILURE_RETRY(close(fd)); |
373 return -1; | 359 return -1; |
374 } | 360 } |
375 | 361 |
376 // Test for invalid socket port 65535 (some browsers disallow it). | 362 // Test for invalid socket port 65535 (some browsers disallow it). |
377 if (port == 0 && Socket::GetPort(fd) == 65535) { | 363 if (port == 0 && Socket::GetPort(fd) == 65535) { |
378 // Don't close the socket until we have created a new socket, ensuring | 364 // Don't close the socket until we have created a new socket, ensuring |
379 // that we do not get the bad port number again. | 365 // that we do not get the bad port number again. |
380 intptr_t new_fd = CreateBindListen(addr, 0, backlog, v6_only); | 366 intptr_t new_fd = CreateBindListen(addr, 0, backlog, v6_only); |
381 int err = errno; | 367 int err = errno; |
382 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | 368 VOID_TEMP_FAILURE_RETRY(close(fd)); |
383 errno = err; | 369 errno = err; |
384 return new_fd; | 370 return new_fd; |
385 } | 371 } |
386 | 372 |
387 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | 373 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
388 listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 374 VOID_TEMP_FAILURE_RETRY(close(fd)); |
389 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | |
390 return -1; | 375 return -1; |
391 } | 376 } |
392 | 377 |
393 Socket::SetNonBlocking(fd); | 378 Socket::SetNonBlocking(fd); |
394 return fd; | 379 return fd; |
395 } | 380 } |
396 | 381 |
397 | 382 |
398 static bool IsTemporaryAcceptError(int error) { | 383 static bool IsTemporaryAcceptError(int error) { |
399 // On Android a number of protocol errors should be treated as EAGAIN. | 384 // On Android a number of protocol errors should be treated as EAGAIN. |
400 // These are the ones for TCP/IP. | 385 // These are the ones for TCP/IP. |
401 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 386 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
402 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 387 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
403 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 388 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
404 (error == ENETUNREACH); | 389 (error == ENETUNREACH); |
405 } | 390 } |
406 | 391 |
407 | 392 |
408 intptr_t ServerSocket::Accept(intptr_t fd) { | 393 intptr_t ServerSocket::Accept(intptr_t fd) { |
409 intptr_t socket; | 394 intptr_t socket; |
410 struct sockaddr clientaddr; | 395 struct sockaddr clientaddr; |
411 socklen_t addrlen = sizeof(clientaddr); | 396 socklen_t addrlen = sizeof(clientaddr); |
412 socket = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(accept(fd, &clientaddr, &addrlen)); | 397 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
413 if (socket == -1) { | 398 if (socket == -1) { |
414 if (IsTemporaryAcceptError(errno)) { | 399 if (IsTemporaryAcceptError(errno)) { |
415 // We need to signal to the caller that this is actually not an | 400 // We need to signal to the caller that this is actually not an |
416 // error. We got woken up from the poll on the listening socket, | 401 // error. We got woken up from the poll on the listening socket, |
417 // but there is no connection ready to be accepted. | 402 // but there is no connection ready to be accepted. |
418 ASSERT(kTemporaryFailure != -1); | 403 ASSERT(kTemporaryFailure != -1); |
419 socket = kTemporaryFailure; | 404 socket = kTemporaryFailure; |
420 } | 405 } |
421 } else { | 406 } else { |
422 Socket::SetNonBlocking(socket); | 407 Socket::SetNonBlocking(socket); |
423 } | 408 } |
424 return socket; | 409 return socket; |
425 } | 410 } |
426 | 411 |
427 | 412 |
428 void Socket::Close(intptr_t fd) { | 413 void Socket::Close(intptr_t fd) { |
429 ASSERT(fd >= 0); | 414 ASSERT(fd >= 0); |
430 int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fd)); | 415 int err = TEMP_FAILURE_RETRY(close(fd)); |
431 if (err != 0) { | 416 if (err != 0) { |
432 const int kBufferSize = 1024; | 417 const int kBufferSize = 1024; |
433 char error_message[kBufferSize]; | 418 char error_message[kBufferSize]; |
434 strerror_r(errno, error_message, kBufferSize); | 419 strerror_r(errno, error_message, kBufferSize); |
435 Log::PrintErr("%s\n", error_message); | 420 Log::PrintErr("%s\n", error_message); |
436 } | 421 } |
437 } | 422 } |
438 | 423 |
439 | 424 |
440 bool Socket::SetNonBlocking(intptr_t fd) { | 425 bool Socket::SetNonBlocking(intptr_t fd) { |
441 return FDUtils::SetNonBlocking(fd); | 426 return FDUtils::SetNonBlocking(fd); |
442 } | 427 } |
443 | 428 |
444 | 429 |
445 bool Socket::SetBlocking(intptr_t fd) { | 430 bool Socket::SetBlocking(intptr_t fd) { |
446 return FDUtils::SetBlocking(fd); | 431 return FDUtils::SetBlocking(fd); |
447 } | 432 } |
448 | 433 |
449 | 434 |
450 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 435 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
451 int on; | 436 int on; |
452 socklen_t len = sizeof(on); | 437 socklen_t len = sizeof(on); |
453 int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getsockopt(fd, | 438 int err = NO_RETRY_EXPECTED(getsockopt(fd, |
454 IPPROTO_TCP, | 439 IPPROTO_TCP, |
455 TCP_NODELAY, | 440 TCP_NODELAY, |
456 reinterpret_cast<void *>(&on), | 441 reinterpret_cast<void *>(&on), |
457 &len)); | 442 &len)); |
458 if (err == 0) { | 443 if (err == 0) { |
459 *enabled = on == 1; | 444 *enabled = on == 1; |
460 } | 445 } |
461 return err == 0; | 446 return err == 0; |
462 } | 447 } |
463 | 448 |
464 | 449 |
465 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 450 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
466 int on = enabled ? 1 : 0; | 451 int on = enabled ? 1 : 0; |
467 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt(fd, | 452 return NO_RETRY_EXPECTED(setsockopt(fd, |
468 IPPROTO_TCP, | 453 IPPROTO_TCP, |
469 TCP_NODELAY, | 454 TCP_NODELAY, |
470 reinterpret_cast<char *>(&on), | 455 reinterpret_cast<char *>(&on), |
471 sizeof(on))) == 0; | 456 sizeof(on))) == 0; |
472 } | 457 } |
473 | 458 |
474 | 459 |
475 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 460 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { |
476 uint8_t on; | 461 uint8_t on; |
477 socklen_t len = sizeof(on); | 462 socklen_t len = sizeof(on); |
478 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 463 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
479 int optname = protocol == SocketAddress::TYPE_IPV4 | 464 int optname = protocol == SocketAddress::TYPE_IPV4 |
480 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 465 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; |
481 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getsockopt(fd, | 466 if (NO_RETRY_EXPECTED(getsockopt(fd, |
482 level, | 467 level, |
483 optname, | 468 optname, |
484 reinterpret_cast<char *>(&on), | 469 reinterpret_cast<char *>(&on), |
485 &len)) == 0) { | 470 &len)) == 0) { |
486 *enabled = (on == 1); | 471 *enabled = (on == 1); |
487 return true; | 472 return true; |
488 } | 473 } |
489 return false; | 474 return false; |
490 } | 475 } |
491 | 476 |
492 | 477 |
493 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 478 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { |
494 int on = enabled ? 1 : 0; | 479 int on = enabled ? 1 : 0; |
495 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 480 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
496 int optname = protocol == SocketAddress::TYPE_IPV4 | 481 int optname = protocol == SocketAddress::TYPE_IPV4 |
497 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 482 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; |
498 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt(fd, | 483 return NO_RETRY_EXPECTED(setsockopt(fd, |
499 level, | 484 level, |
500 optname, | 485 optname, |
501 reinterpret_cast<char *>(&on), | 486 reinterpret_cast<char *>(&on), |
502 sizeof(on))) == 0; | 487 sizeof(on))) == 0; |
503 } | 488 } |
504 | 489 |
505 | |
506 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 490 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
507 uint8_t v; | 491 uint8_t v; |
508 socklen_t len = sizeof(v); | 492 socklen_t len = sizeof(v); |
509 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 493 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
510 int optname = protocol == SocketAddress::TYPE_IPV4 | 494 int optname = protocol == SocketAddress::TYPE_IPV4 |
511 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 495 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; |
512 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getsockopt(fd, | 496 if (NO_RETRY_EXPECTED(getsockopt(fd, |
513 level, | 497 level, |
514 optname, | 498 optname, |
515 reinterpret_cast<char *>(&v), | 499 reinterpret_cast<char *>(&v), |
516 &len)) == 0) { | 500 &len)) == 0) { |
517 *value = v; | 501 *value = v; |
518 return true; | 502 return true; |
519 } | 503 } |
520 return false; | 504 return false; |
521 } | 505 } |
522 | 506 |
523 | 507 |
524 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 508 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
525 int v = value; | 509 int v = value; |
526 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 510 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
527 int optname = protocol == SocketAddress::TYPE_IPV4 | 511 int optname = protocol == SocketAddress::TYPE_IPV4 |
528 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 512 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; |
529 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt(fd, | 513 return NO_RETRY_EXPECTED(setsockopt(fd, |
530 level, | 514 level, |
531 optname, | 515 optname, |
532 reinterpret_cast<char *>(&v), | 516 reinterpret_cast<char *>(&v), |
533 sizeof(v))) == 0; | 517 sizeof(v))) == 0; |
534 } | 518 } |
535 | 519 |
536 | 520 |
537 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 521 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
538 int on; | 522 int on; |
539 socklen_t len = sizeof(on); | 523 socklen_t len = sizeof(on); |
540 int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(getsockopt(fd, | 524 int err = NO_RETRY_EXPECTED(getsockopt(fd, |
541 SOL_SOCKET, | 525 SOL_SOCKET, |
542 SO_BROADCAST, | 526 SO_BROADCAST, |
543 reinterpret_cast<char *>(&on), | 527 reinterpret_cast<char *>(&on), |
544 &len)); | 528 &len)); |
545 if (err == 0) { | 529 if (err == 0) { |
546 *enabled = on == 1; | 530 *enabled = on == 1; |
547 } | 531 } |
548 return err == 0; | 532 return err == 0; |
549 } | 533 } |
550 | 534 |
551 | 535 |
552 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 536 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
553 int on = enabled ? 1 : 0; | 537 int on = enabled ? 1 : 0; |
554 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt(fd, | 538 return NO_RETRY_EXPECTED(setsockopt(fd, |
555 SOL_SOCKET, | 539 SOL_SOCKET, |
556 SO_BROADCAST, | 540 SO_BROADCAST, |
557 reinterpret_cast<char *>(&on), | 541 reinterpret_cast<char *>(&on), |
558 sizeof(on))) == 0; | 542 sizeof(on))) == 0; |
559 } | 543 } |
560 | 544 |
561 | 545 |
562 bool Socket::JoinMulticast( | 546 bool Socket::JoinMulticast( |
563 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { | 547 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { |
564 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 548 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
565 struct group_req mreq; | 549 struct group_req mreq; |
566 mreq.gr_interface = interfaceIndex; | 550 mreq.gr_interface = interfaceIndex; |
567 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 551 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
568 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt( | 552 return NO_RETRY_EXPECTED(setsockopt( |
569 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; | 553 fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
570 } | 554 } |
571 | 555 |
572 | 556 |
573 bool Socket::LeaveMulticast( | 557 bool Socket::LeaveMulticast( |
574 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { | 558 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { |
575 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 559 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
576 struct group_req mreq; | 560 struct group_req mreq; |
577 mreq.gr_interface = interfaceIndex; | 561 mreq.gr_interface = interfaceIndex; |
578 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 562 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
579 return TEMP_FAILURE_RETRY_BLOCK_SIGNALS(setsockopt( | 563 return NO_RETRY_EXPECTED(setsockopt( |
580 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 564 fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
581 } | 565 } |
582 | 566 |
583 } // namespace bin | 567 } // namespace bin |
584 } // namespace dart | 568 } // namespace dart |
585 | 569 |
586 #endif // defined(TARGET_OS_ANDROID) | 570 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |