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

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

Issue 2791163004: Reverting until bots clear up. (Closed)
Patch Set: 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
« no previous file with comments | « runtime/bin/socket_fuchsia.h ('k') | runtime/bin/socket_linux.h » ('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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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_FUCHSIA) 8 #if defined(HOST_OS_FUCHSIA)
9 9
10 #include "bin/socket.h"
11 #include "bin/socket_fuchsia.h"
12
10 #include <errno.h> // NOLINT 13 #include <errno.h> // NOLINT
11 #include <fcntl.h> // NOLINT 14 #include <fcntl.h> // NOLINT
12 #include <ifaddrs.h> // NOLINT 15 #include <ifaddrs.h> // NOLINT
13 #include <net/if.h> // NOLINT 16 #include <net/if.h> // NOLINT
14 #include <netinet/tcp.h> // NOLINT 17 #include <netinet/tcp.h> // NOLINT
15 #include <stdio.h> // NOLINT 18 #include <stdio.h> // NOLINT
16 #include <stdlib.h> // NOLINT 19 #include <stdlib.h> // NOLINT
17 #include <string.h> // NOLINT 20 #include <string.h> // NOLINT
18 #include <sys/ioctl.h> // NOLINT 21 #include <sys/ioctl.h> // NOLINT
19 #include <sys/stat.h> // NOLINT 22 #include <sys/stat.h> // NOLINT
20 #include <unistd.h> // NOLINT 23 #include <unistd.h> // NOLINT
21 24
22 #include "bin/fdutils.h" 25 #include "bin/fdutils.h"
23 #include "bin/file.h" 26 #include "bin/file.h"
24 #include "bin/socket.h"
25 #include "bin/socket_base_fuchsia.h"
26 #include "platform/signal_blocker.h" 27 #include "platform/signal_blocker.h"
27 28
28 // #define SOCKET_LOG_INFO 1 29 // #define SOCKET_LOG_INFO 1
29 // #define SOCKET_LOG_ERROR 1 30 // #define SOCKET_LOG_ERROR 1
30 31
31 // define SOCKET_LOG_ERROR to get log messages only for errors. 32 // define SOCKET_LOG_ERROR to get log messages only for errors.
32 // define SOCKET_LOG_INFO to get log messages for both information and errors. 33 // define SOCKET_LOG_INFO to get log messages for both information and errors.
33 #if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) 34 #if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
34 #define LOG_ERR(msg, ...) \ 35 #define LOG_ERR(msg, ...) \
35 { \ 36 { \
36 int err = errno; \ 37 int err = errno; \
37 Log::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \ 38 Log::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \
38 ##__VA_ARGS__); \ 39 ##__VA_ARGS__); \
39 errno = err; \ 40 errno = err; \
40 } 41 }
41 #if defined(SOCKET_LOG_INFO) 42 #if defined(SOCKET_LOG_INFO)
42 #define LOG_INFO(msg, ...) \ 43 #define LOG_INFO(msg, ...) \
43 Log::Print("Dart Socket INFO: %s:%d: " msg, __FILE__, __LINE__, ##__VA_ARGS__) 44 Log::Print("Dart Socket INFO: %s:%d: " msg, __FILE__, __LINE__, ##__VA_ARGS__)
44 #else 45 #else
45 #define LOG_INFO(msg, ...) 46 #define LOG_INFO(msg, ...)
46 #endif // defined(SOCKET_LOG_INFO) 47 #endif // defined(SOCKET_LOG_INFO)
47 #else 48 #else
48 #define LOG_ERR(msg, ...) 49 #define LOG_ERR(msg, ...)
49 #define LOG_INFO(msg, ...) 50 #define LOG_INFO(msg, ...)
50 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) 51 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
51 52
52 namespace dart { 53 namespace dart {
53 namespace bin { 54 namespace bin {
54 55
56 SocketAddress::SocketAddress(struct sockaddr* sa) {
57 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
58 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_,
59 INET6_ADDRSTRLEN)) {
60 as_string_[0] = 0;
61 }
62 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa));
63 memmove(reinterpret_cast<void*>(&addr_), sa, salen);
64 }
65
66
67 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) {
68 socklen_t salen = SocketAddress::GetAddrLength(addr);
69 LOG_INFO("Socket::FormatNumericAddress: calling getnameinfo\n");
70 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
71 0, NI_NUMERICHOST) == 0));
72 }
73
74
55 Socket::Socket(intptr_t fd) 75 Socket::Socket(intptr_t fd)
56 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} 76 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {}
57 77
58 78
59 void Socket::SetClosedFd() { 79 void Socket::SetClosedFd() {
60 fd_ = kClosedFd; 80 fd_ = kClosedFd;
61 } 81 }
62 82
63 83
64 bool Socket::Initialize() { 84 bool Socket::Initialize() {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", fd); 126 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", fd);
107 FDUtils::SaveErrorAndClose(fd); 127 FDUtils::SaveErrorAndClose(fd);
108 return -1; 128 return -1;
109 } 129 }
110 return Connect(fd, addr); 130 return Connect(fd, addr);
111 } 131 }
112 132
113 133
114 intptr_t Socket::CreateBindConnect(const RawAddr& addr, 134 intptr_t Socket::CreateBindConnect(const RawAddr& addr,
115 const RawAddr& source_addr) { 135 const RawAddr& source_addr) {
116 LOG_ERR("SocketBase::CreateBindConnect is unimplemented\n"); 136 LOG_ERR("Socket::CreateBindConnect is unimplemented\n");
117 UNIMPLEMENTED(); 137 UNIMPLEMENTED();
118 return -1; 138 return -1;
139 }
140
141
142 bool Socket::IsBindError(intptr_t error_number) {
143 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
144 error_number == EINVAL;
145 }
146
147
148 intptr_t Socket::Available(intptr_t fd) {
149 intptr_t available = FDUtils::AvailableBytes(fd);
150 LOG_INFO("Socket::Available(%ld) = %ld\n", fd, available);
151 return available;
152 }
153
154
155 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
156 ASSERT(fd >= 0);
157 LOG_INFO("Socket::Read: calling read(%ld, %p, %ld)\n", fd, buffer, num_bytes);
158 ssize_t read_bytes = NO_RETRY_EXPECTED(read(fd, buffer, num_bytes));
159 ASSERT(EAGAIN == EWOULDBLOCK);
160 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
161 // If the read would block we need to retry and therefore return 0
162 // as the number of bytes written.
163 read_bytes = 0;
164 } else if (read_bytes == -1) {
165 LOG_ERR("Socket::Read: read(%ld, %p, %ld) failed\n", fd, buffer, num_bytes);
166 } else {
167 LOG_INFO("Socket::Read: read(%ld, %p, %ld) succeeded\n", fd, buffer,
168 num_bytes);
169 }
170 return read_bytes;
171 }
172
173
174 intptr_t Socket::RecvFrom(intptr_t fd,
175 void* buffer,
176 intptr_t num_bytes,
177 RawAddr* addr) {
178 LOG_ERR("Socket::RecvFrom is unimplemented\n");
179 UNIMPLEMENTED();
180 return -1;
181 }
182
183
184 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
185 ASSERT(fd >= 0);
186 LOG_INFO("Socket::Write: calling write(%ld, %p, %ld)\n", fd, buffer,
187 num_bytes);
188 ssize_t written_bytes = NO_RETRY_EXPECTED(write(fd, buffer, num_bytes));
189 ASSERT(EAGAIN == EWOULDBLOCK);
190 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
191 // If the would block we need to retry and therefore return 0 as
192 // the number of bytes written.
193 written_bytes = 0;
194 } else if (written_bytes == -1) {
195 LOG_ERR("Socket::Write: write(%ld, %p, %ld) failed\n", fd, buffer,
196 num_bytes);
197 } else {
198 LOG_INFO("Socket::Write: write(%ld, %p, %ld) succeeded\n", fd, buffer,
199 num_bytes);
200 }
201 return written_bytes;
202 }
203
204
205 intptr_t Socket::SendTo(intptr_t fd,
206 const void* buffer,
207 intptr_t num_bytes,
208 const RawAddr& addr) {
209 LOG_ERR("Socket::SendTo is unimplemented\n");
210 UNIMPLEMENTED();
211 return -1;
212 }
213
214
215 intptr_t Socket::GetPort(intptr_t fd) {
216 ASSERT(fd >= 0);
217 RawAddr raw;
218 socklen_t size = sizeof(raw);
219 LOG_INFO("Socket::GetPort: calling getsockname(%ld)\n", fd);
220 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) {
221 return 0;
222 }
223 return SocketAddress::GetAddrPort(raw);
224 }
225
226
227 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
228 ASSERT(fd >= 0);
229 RawAddr raw;
230 socklen_t size = sizeof(raw);
231 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) {
232 return NULL;
233 }
234 *port = SocketAddress::GetAddrPort(raw);
235 return new SocketAddress(&raw.addr);
236 }
237
238
239 void Socket::GetError(intptr_t fd, OSError* os_error) {
240 LOG_ERR("Socket::GetError is unimplemented\n");
241 UNIMPLEMENTED();
242 }
243
244
245 int Socket::GetType(intptr_t fd) {
246 LOG_ERR("Socket::GetType is unimplemented\n");
247 UNIMPLEMENTED();
248 return File::kOther;
249 }
250
251
252 intptr_t Socket::GetStdioHandle(intptr_t num) {
253 LOG_ERR("Socket::GetStdioHandle is unimplemented\n");
254 UNIMPLEMENTED();
255 return num;
256 }
257
258
259 AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
260 int type,
261 OSError** os_error) {
262 // Perform a name lookup for a host name.
263 struct addrinfo hints;
264 memset(&hints, 0, sizeof(hints));
265 hints.ai_family = SocketAddress::FromType(type);
266 hints.ai_socktype = SOCK_STREAM;
267 hints.ai_flags = AI_ADDRCONFIG;
268 hints.ai_protocol = IPPROTO_TCP;
269 struct addrinfo* info = NULL;
270 LOG_INFO("Socket::LookupAddress: calling getaddrinfo\n");
271 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
272 if (status != 0) {
273 // We failed, try without AI_ADDRCONFIG. This can happen when looking up
274 // e.g. '::1', when there are no global IPv6 addresses.
275 hints.ai_flags = 0;
276 LOG_INFO("Socket::LookupAddress: calling getaddrinfo again\n");
277 status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
278 if (status != 0) {
279 ASSERT(*os_error == NULL);
280 *os_error =
281 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
282 return NULL;
283 }
284 }
285 intptr_t count = 0;
286 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
287 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
288 count++;
289 }
290 }
291 intptr_t i = 0;
292 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count);
293 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
294 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
295 addresses->SetAt(i, new SocketAddress(c->ai_addr));
296 i++;
297 }
298 }
299 freeaddrinfo(info);
300 return addresses;
301 }
302
303
304 bool Socket::ReverseLookup(const RawAddr& addr,
305 char* host,
306 intptr_t host_len,
307 OSError** os_error) {
308 LOG_ERR("Socket::ReverseLookup is unimplemented\n");
309 UNIMPLEMENTED();
310 return false;
311 }
312
313
314 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
315 int result;
316 if (type == SocketAddress::TYPE_IPV4) {
317 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr));
318 } else {
319 ASSERT(type == SocketAddress::TYPE_IPV6);
320 result =
321 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr));
322 }
323 return (result == 1);
119 } 324 }
120 325
121 326
122 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { 327 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
123 LOG_ERR("SocketBase::CreateBindDatagram is unimplemented\n"); 328 LOG_ERR("Socket::CreateBindDatagram is unimplemented\n");
124 UNIMPLEMENTED(); 329 UNIMPLEMENTED();
125 return -1; 330 return -1;
331 }
332
333
334 bool Socket::ListInterfacesSupported() {
335 return false;
336 }
337
338
339 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
340 int type,
341 OSError** os_error) {
342 UNIMPLEMENTED();
343 return NULL;
126 } 344 }
127 345
128 346
129 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, 347 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
130 intptr_t backlog, 348 intptr_t backlog,
131 bool v6_only) { 349 bool v6_only) {
132 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n"); 350 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n");
133 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 351 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
134 if (fd < 0) { 352 if (fd < 0) {
135 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n"); 353 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n");
(...skipping 23 matching lines...) Expand all
159 if (NO_RETRY_EXPECTED( 377 if (NO_RETRY_EXPECTED(
160 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { 378 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
161 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd); 379 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd);
162 FDUtils::SaveErrorAndClose(fd); 380 FDUtils::SaveErrorAndClose(fd);
163 return -1; 381 return -1;
164 } 382 }
165 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd); 383 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd);
166 384
167 // Test for invalid socket port 65535 (some browsers disallow it). 385 // Test for invalid socket port 65535 (some browsers disallow it).
168 if ((SocketAddress::GetAddrPort(addr) == 0) && 386 if ((SocketAddress::GetAddrPort(addr) == 0) &&
169 (SocketBase::GetPort(fd) == 65535)) { 387 (Socket::GetPort(fd) == 65535)) {
170 // Don't close the socket until we have created a new socket, ensuring 388 // Don't close the socket until we have created a new socket, ensuring
171 // that we do not get the bad port number again. 389 // that we do not get the bad port number again.
172 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); 390 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
173 FDUtils::SaveErrorAndClose(fd); 391 FDUtils::SaveErrorAndClose(fd);
174 return new_fd; 392 return new_fd;
175 } 393 }
176 394
177 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd); 395 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd);
178 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 396 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
179 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd); 397 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 } 450 }
233 if (!FDUtils::SetNonBlocking(socket)) { 451 if (!FDUtils::SetNonBlocking(socket)) {
234 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket); 452 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket);
235 FDUtils::SaveErrorAndClose(socket); 453 FDUtils::SaveErrorAndClose(socket);
236 return -1; 454 return -1;
237 } 455 }
238 } 456 }
239 return socket; 457 return socket;
240 } 458 }
241 459
460
461 void Socket::Close(intptr_t fd) {
462 ASSERT(fd >= 0);
463 NO_RETRY_EXPECTED(close(fd));
464 }
465
466
467 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
468 LOG_ERR("Socket::GetNoDelay is unimplemented\n");
469 UNIMPLEMENTED();
470 return false;
471 }
472
473
474 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
475 int on = enabled ? 1 : 0;
476 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
477 reinterpret_cast<char*>(&on),
478 sizeof(on))) == 0;
479 }
480
481
482 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
483 LOG_ERR("Socket::GetMulticastLoop is unimplemented\n");
484 UNIMPLEMENTED();
485 return false;
486 }
487
488
489 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
490 LOG_ERR("Socket::SetMulticastLoop is unimplemented\n");
491 UNIMPLEMENTED();
492 return false;
493 }
494
495
496 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
497 LOG_ERR("Socket::GetMulticastHops is unimplemented\n");
498 UNIMPLEMENTED();
499 return false;
500 }
501
502
503 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
504 LOG_ERR("Socket::SetMulticastHops is unimplemented\n");
505 UNIMPLEMENTED();
506 return false;
507 }
508
509
510 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
511 LOG_ERR("Socket::GetBroadcast is unimplemented\n");
512 UNIMPLEMENTED();
513 return false;
514 }
515
516
517 bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
518 LOG_ERR("Socket::SetBroadcast is unimplemented\n");
519 UNIMPLEMENTED();
520 return false;
521 }
522
523
524 bool Socket::JoinMulticast(intptr_t fd,
525 const RawAddr& addr,
526 const RawAddr&,
527 int interfaceIndex) {
528 LOG_ERR("Socket::JoinMulticast is unimplemented\n");
529 UNIMPLEMENTED();
530 return false;
531 }
532
533
534 bool Socket::LeaveMulticast(intptr_t fd,
535 const RawAddr& addr,
536 const RawAddr&,
537 int interfaceIndex) {
538 LOG_ERR("Socket::LeaveMulticast is unimplemented\n");
539 UNIMPLEMENTED();
540 return false;
541 }
542
242 } // namespace bin 543 } // namespace bin
243 } // namespace dart 544 } // namespace dart
244 545
245 #endif // defined(HOST_OS_FUCHSIA) 546 #endif // defined(HOST_OS_FUCHSIA)
246 547
247 #endif // !defined(DART_IO_DISABLED) 548 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/socket_fuchsia.h ('k') | runtime/bin/socket_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698