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

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

Issue 2780063002: Pulled a significant portion of Socket implementation into BaseSocket in order to prepare for the s… (Closed)
Patch Set: Minor change to socket_common_unsupported.cc 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) 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
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(HOST_OS_FUCHSIA)
9
10 #include "bin/socket.h"
11 #include "bin/socket_fuchsia.h"
12
13 #include <errno.h> // NOLINT
14 #include <fcntl.h> // NOLINT
15 #include <ifaddrs.h> // NOLINT
16 #include <net/if.h> // NOLINT
17 #include <netinet/tcp.h> // NOLINT
18 #include <stdio.h> // NOLINT
19 #include <stdlib.h> // NOLINT
20 #include <string.h> // NOLINT
21 #include <sys/ioctl.h> // NOLINT
22 #include <sys/stat.h> // NOLINT
23 #include <unistd.h> // NOLINT
24
25 #include "bin/fdutils.h"
26 #include "bin/file.h"
27 #include "platform/signal_blocker.h"
28
29 // #define SOCKET_LOG_INFO 1
30 // #define SOCKET_LOG_ERROR 1
31
32 // define SOCKET_LOG_ERROR to get log messages only for errors.
33 // define SOCKET_LOG_INFO to get log messages for both information and errors.
34 #if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
35 #define LOG_ERR(msg, ...) \
36 { \
37 int err = errno; \
38 Log::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \
39 ##__VA_ARGS__); \
40 errno = err; \
41 }
42 #if defined(SOCKET_LOG_INFO)
43 #define LOG_INFO(msg, ...) \
44 Log::Print("Dart Socket INFO: %s:%d: " msg, __FILE__, __LINE__, ##__VA_ARGS__)
45 #else
46 #define LOG_INFO(msg, ...)
47 #endif // defined(SOCKET_LOG_INFO)
48 #else
49 #define LOG_ERR(msg, ...)
50 #define LOG_INFO(msg, ...)
51 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
52
53 namespace dart {
54 namespace bin {
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
75 Socket::Socket(intptr_t fd)
76 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {}
77
78
79 void Socket::SetClosedFd() {
80 fd_ = kClosedFd;
81 }
82
83
84 bool Socket::Initialize() {
85 // Nothing to do on Fuchsia.
86 return true;
87 }
88
89
90 static intptr_t Create(const RawAddr& addr) {
91 LOG_INFO("Create: calling socket(SOCK_STREAM)\n");
92 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
93 if (fd < 0) {
94 LOG_ERR("Create: socket(SOCK_STREAM) failed\n");
95 return -1;
96 }
97 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd);
98 if (!FDUtils::SetCloseOnExec(fd)) {
99 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd);
100 FDUtils::SaveErrorAndClose(fd);
101 return -1;
102 }
103 return fd;
104 }
105
106
107 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
108 LOG_INFO("Connect: calling connect(%ld)\n", fd);
109 intptr_t result = NO_RETRY_EXPECTED(
110 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr)));
111 if ((result == 0) || (errno == EINPROGRESS)) {
112 return fd;
113 }
114 LOG_ERR("Connect: connect(%ld) failed\n", fd);
115 FDUtils::SaveErrorAndClose(fd);
116 return -1;
117 }
118
119
120 intptr_t Socket::CreateConnect(const RawAddr& addr) {
121 intptr_t fd = Create(addr);
122 if (fd < 0) {
123 return fd;
124 }
125 if (!FDUtils::SetNonBlocking(fd)) {
126 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", fd);
127 FDUtils::SaveErrorAndClose(fd);
128 return -1;
129 }
130 return Connect(fd, addr);
131 }
132
133
134 intptr_t Socket::CreateBindConnect(const RawAddr& addr,
135 const RawAddr& source_addr) {
136 LOG_ERR("Socket::CreateBindConnect is unimplemented\n");
137 UNIMPLEMENTED();
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);
324 }
325
326
327 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
328 LOG_ERR("Socket::CreateBindDatagram is unimplemented\n");
329 UNIMPLEMENTED();
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;
344 }
345
346
347 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
348 intptr_t backlog,
349 bool v6_only) {
350 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n");
351 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
352 if (fd < 0) {
353 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n");
354 return -1;
355 }
356 LOG_INFO("ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd);
357
358 if (!FDUtils::SetCloseOnExec(fd)) {
359 LOG_ERR("ServerSocket::CreateBindListen: SetCloseOnExec(%ld) failed\n", fd);
360 FDUtils::SaveErrorAndClose(fd);
361 return -1;
362 }
363
364 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
365 int optval = 1;
366 VOID_NO_RETRY_EXPECTED(
367 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
368
369 if (addr.ss.ss_family == AF_INET6) {
370 optval = v6_only ? 1 : 0;
371 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
372 VOID_NO_RETRY_EXPECTED(
373 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
374 }
375
376 LOG_INFO("ServerSocket::CreateBindListen: calling bind(%ld)\n", fd);
377 if (NO_RETRY_EXPECTED(
378 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
379 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd);
380 FDUtils::SaveErrorAndClose(fd);
381 return -1;
382 }
383 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd);
384
385 // Test for invalid socket port 65535 (some browsers disallow it).
386 if ((SocketAddress::GetAddrPort(addr) == 0) &&
387 (Socket::GetPort(fd) == 65535)) {
388 // Don't close the socket until we have created a new socket, ensuring
389 // that we do not get the bad port number again.
390 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
391 FDUtils::SaveErrorAndClose(fd);
392 return new_fd;
393 }
394
395 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd);
396 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
397 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd);
398 FDUtils::SaveErrorAndClose(fd);
399 return -1;
400 }
401 LOG_INFO("ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd);
402
403 if (!FDUtils::SetNonBlocking(fd)) {
404 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd);
405 FDUtils::SaveErrorAndClose(fd);
406 return -1;
407 }
408 return fd;
409 }
410
411
412 bool ServerSocket::StartAccept(intptr_t fd) {
413 USE(fd);
414 return true;
415 }
416
417
418 static bool IsTemporaryAcceptError(int error) {
419 // On Linux a number of protocol errors should be treated as EAGAIN.
420 // These are the ones for TCP/IP.
421 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
422 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
423 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
424 (error == ENETUNREACH);
425 }
426
427
428 intptr_t ServerSocket::Accept(intptr_t fd) {
429 intptr_t socket;
430 struct sockaddr clientaddr;
431 socklen_t addrlen = sizeof(clientaddr);
432 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", fd);
433 socket = NO_RETRY_EXPECTED(accept(fd, &clientaddr, &addrlen));
434 if (socket == -1) {
435 if (IsTemporaryAcceptError(errno)) {
436 // We need to signal to the caller that this is actually not an
437 // error. We got woken up from the poll on the listening socket,
438 // but there is no connection ready to be accepted.
439 ASSERT(kTemporaryFailure != -1);
440 socket = kTemporaryFailure;
441 } else {
442 LOG_ERR("ServerSocket::Accept: accept(%ld) failed\n", fd);
443 }
444 } else {
445 LOG_INFO("ServerSocket::Accept: accept(%ld) -> socket %ld\n", fd, socket);
446 if (!FDUtils::SetCloseOnExec(socket)) {
447 LOG_ERR("FDUtils::SetCloseOnExec(%ld) failed\n", socket);
448 FDUtils::SaveErrorAndClose(socket);
449 return -1;
450 }
451 if (!FDUtils::SetNonBlocking(socket)) {
452 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket);
453 FDUtils::SaveErrorAndClose(socket);
454 return -1;
455 }
456 }
457 return socket;
458 }
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
543 } // namespace bin
544 } // namespace dart
545
546 #endif // defined(HOST_OS_FUCHSIA)
547
548 #endif // !defined(DART_IO_DISABLED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698