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

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: 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(TARGET_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 bool Socket::Initialize() {
76 // Nothing to do on Fuchsia.
77 return true;
78 }
79
80
81 static intptr_t Create(const RawAddr& addr) {
82 LOG_INFO("Create: calling socket(SOCK_STREAM)\n");
83 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
84 if (fd < 0) {
85 LOG_ERR("Create: socket(SOCK_STREAM) failed\n");
86 return -1;
87 }
88 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd);
89 if (!FDUtils::SetCloseOnExec(fd)) {
90 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd);
91 FDUtils::SaveErrorAndClose(fd);
92 return -1;
93 }
94 return fd;
95 }
96
97
98 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
99 LOG_INFO("Connect: calling connect(%ld)\n", fd);
100 intptr_t result = NO_RETRY_EXPECTED(
101 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr)));
102 if ((result == 0) || (errno == EINPROGRESS)) {
103 return fd;
104 }
105 LOG_ERR("Connect: connect(%ld) failed\n", fd);
106 FDUtils::SaveErrorAndClose(fd);
107 return -1;
108 }
109
110
111 intptr_t Socket::CreateConnect(const RawAddr& addr) {
112 intptr_t fd = Create(addr);
113 if (fd < 0) {
114 return fd;
115 }
116 if (!FDUtils::SetNonBlocking(fd)) {
117 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", fd);
118 FDUtils::SaveErrorAndClose(fd);
119 return -1;
120 }
121 return Connect(fd, addr);
122 }
123
124
125 intptr_t Socket::CreateBindConnect(const RawAddr& addr,
126 const RawAddr& source_addr) {
127 LOG_ERR("Socket::CreateBindConnect is unimplemented\n");
128 UNIMPLEMENTED();
129 return -1;
130 }
131
132
133 bool Socket::IsBindError(intptr_t error_number) {
134 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
135 error_number == EINVAL;
136 }
137
138
139 intptr_t Socket::Available(intptr_t fd) {
140 intptr_t available = FDUtils::AvailableBytes(fd);
141 LOG_INFO("Socket::Available(%ld) = %ld\n", fd, available);
142 return available;
143 }
144
145
146 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
147 ASSERT(fd >= 0);
148 LOG_INFO("Socket::Read: calling read(%ld, %p, %ld)\n", fd, buffer, num_bytes);
149 ssize_t read_bytes = NO_RETRY_EXPECTED(read(fd, buffer, num_bytes));
150 ASSERT(EAGAIN == EWOULDBLOCK);
151 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
152 // If the read would block we need to retry and therefore return 0
153 // as the number of bytes written.
154 read_bytes = 0;
155 } else if (read_bytes == -1) {
156 LOG_ERR("Socket::Read: read(%ld, %p, %ld) failed\n", fd, buffer, num_bytes);
157 } else {
158 LOG_INFO("Socket::Read: read(%ld, %p, %ld) succeeded\n", fd, buffer,
159 num_bytes);
160 }
161 return read_bytes;
162 }
163
164
165 intptr_t Socket::RecvFrom(intptr_t fd,
166 void* buffer,
167 intptr_t num_bytes,
168 RawAddr* addr) {
169 LOG_ERR("Socket::RecvFrom is unimplemented\n");
170 UNIMPLEMENTED();
171 return -1;
172 }
173
174
175 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
176 ASSERT(fd >= 0);
177 LOG_INFO("Socket::Write: calling write(%ld, %p, %ld)\n", fd, buffer,
178 num_bytes);
179 ssize_t written_bytes = NO_RETRY_EXPECTED(write(fd, buffer, num_bytes));
180 ASSERT(EAGAIN == EWOULDBLOCK);
181 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
182 // If the would block we need to retry and therefore return 0 as
183 // the number of bytes written.
184 written_bytes = 0;
185 } else if (written_bytes == -1) {
186 LOG_ERR("Socket::Write: write(%ld, %p, %ld) failed\n", fd, buffer,
187 num_bytes);
188 } else {
189 LOG_INFO("Socket::Write: write(%ld, %p, %ld) succeeded\n", fd, buffer,
190 num_bytes);
191 }
192 return written_bytes;
193 }
194
195
196 intptr_t Socket::SendTo(intptr_t fd,
197 const void* buffer,
198 intptr_t num_bytes,
199 const RawAddr& addr) {
200 LOG_ERR("Socket::SendTo is unimplemented\n");
201 UNIMPLEMENTED();
202 return -1;
203 }
204
205
206 intptr_t Socket::GetPort(intptr_t fd) {
207 ASSERT(fd >= 0);
208 RawAddr raw;
209 socklen_t size = sizeof(raw);
210 LOG_INFO("Socket::GetPort: calling getsockname(%ld)\n", fd);
211 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) {
212 return 0;
213 }
214 return SocketAddress::GetAddrPort(raw);
215 }
216
217
218 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
219 ASSERT(fd >= 0);
220 RawAddr raw;
221 socklen_t size = sizeof(raw);
222 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) {
223 return NULL;
224 }
225 *port = SocketAddress::GetAddrPort(raw);
226 return new SocketAddress(&raw.addr);
227 }
228
229
230 void Socket::GetError(intptr_t fd, OSError* os_error) {
231 LOG_ERR("Socket::GetError is unimplemented\n");
232 UNIMPLEMENTED();
233 }
234
235
236 int Socket::GetType(intptr_t fd) {
237 LOG_ERR("Socket::GetType is unimplemented\n");
238 UNIMPLEMENTED();
239 return File::kOther;
240 }
241
242
243 intptr_t Socket::GetStdioHandle(intptr_t num) {
244 LOG_ERR("Socket::GetStdioHandle is unimplemented\n");
245 UNIMPLEMENTED();
246 return num;
247 }
248
249
250 AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
251 int type,
252 OSError** os_error) {
253 // Perform a name lookup for a host name.
254 struct addrinfo hints;
255 memset(&hints, 0, sizeof(hints));
256 hints.ai_family = SocketAddress::FromType(type);
257 hints.ai_socktype = SOCK_STREAM;
258 hints.ai_flags = AI_ADDRCONFIG;
259 hints.ai_protocol = IPPROTO_TCP;
260 struct addrinfo* info = NULL;
261 LOG_INFO("Socket::LookupAddress: calling getaddrinfo\n");
262 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
263 if (status != 0) {
264 // We failed, try without AI_ADDRCONFIG. This can happen when looking up
265 // e.g. '::1', when there are no global IPv6 addresses.
266 hints.ai_flags = 0;
267 LOG_INFO("Socket::LookupAddress: calling getaddrinfo again\n");
268 status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
269 if (status != 0) {
270 ASSERT(*os_error == NULL);
271 *os_error =
272 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
273 return NULL;
274 }
275 }
276 intptr_t count = 0;
277 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
278 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
279 count++;
280 }
281 }
282 intptr_t i = 0;
283 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count);
284 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
285 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
286 addresses->SetAt(i, new SocketAddress(c->ai_addr));
287 i++;
288 }
289 }
290 freeaddrinfo(info);
291 return addresses;
292 }
293
294
295 bool Socket::ReverseLookup(const RawAddr& addr,
296 char* host,
297 intptr_t host_len,
298 OSError** os_error) {
299 LOG_ERR("Socket::ReverseLookup is unimplemented\n");
300 UNIMPLEMENTED();
301 return false;
302 }
303
304
305 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
306 int result;
307 if (type == SocketAddress::TYPE_IPV4) {
308 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr));
309 } else {
310 ASSERT(type == SocketAddress::TYPE_IPV6);
311 result =
312 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr));
313 }
314 return (result == 1);
315 }
316
317
318 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
319 LOG_ERR("Socket::CreateBindDatagram is unimplemented\n");
320 UNIMPLEMENTED();
321 return -1;
322 }
323
324
325 bool Socket::ListInterfacesSupported() {
326 return false;
327 }
328
329
330 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
331 int type,
332 OSError** os_error) {
333 UNIMPLEMENTED();
334 return NULL;
335 }
336
337
338 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
339 intptr_t backlog,
340 bool v6_only) {
341 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n");
342 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
343 if (fd < 0) {
344 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n");
345 return -1;
346 }
347 LOG_INFO("ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd);
348
349 if (!FDUtils::SetCloseOnExec(fd)) {
350 LOG_ERR("ServerSocket::CreateBindListen: SetCloseOnExec(%ld) failed\n", fd);
351 FDUtils::SaveErrorAndClose(fd);
352 return -1;
353 }
354
355 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
356 int optval = 1;
357 VOID_NO_RETRY_EXPECTED(
358 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
359
360 if (addr.ss.ss_family == AF_INET6) {
361 optval = v6_only ? 1 : 0;
362 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
363 VOID_NO_RETRY_EXPECTED(
364 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
365 }
366
367 LOG_INFO("ServerSocket::CreateBindListen: calling bind(%ld)\n", fd);
368 if (NO_RETRY_EXPECTED(
369 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
370 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd);
371 FDUtils::SaveErrorAndClose(fd);
372 return -1;
373 }
374 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd);
375
376 // Test for invalid socket port 65535 (some browsers disallow it).
377 if ((SocketAddress::GetAddrPort(addr) == 0) &&
378 (Socket::GetPort(fd) == 65535)) {
379 // Don't close the socket until we have created a new socket, ensuring
380 // that we do not get the bad port number again.
381 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
382 FDUtils::SaveErrorAndClose(fd);
383 return new_fd;
384 }
385
386 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd);
387 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
388 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd);
389 FDUtils::SaveErrorAndClose(fd);
390 return -1;
391 }
392 LOG_INFO("ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd);
393
394 if (!FDUtils::SetNonBlocking(fd)) {
395 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd);
396 FDUtils::SaveErrorAndClose(fd);
397 return -1;
398 }
399 return fd;
400 }
401
402
403 bool ServerSocket::StartAccept(intptr_t fd) {
404 USE(fd);
405 return true;
406 }
407
408
409 static bool IsTemporaryAcceptError(int error) {
410 // On Linux a number of protocol errors should be treated as EAGAIN.
411 // These are the ones for TCP/IP.
412 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
413 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
414 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
415 (error == ENETUNREACH);
416 }
417
418
419 intptr_t ServerSocket::Accept(intptr_t fd) {
420 intptr_t socket;
421 struct sockaddr clientaddr;
422 socklen_t addrlen = sizeof(clientaddr);
423 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", fd);
424 socket = NO_RETRY_EXPECTED(accept(fd, &clientaddr, &addrlen));
425 if (socket == -1) {
426 if (IsTemporaryAcceptError(errno)) {
427 // We need to signal to the caller that this is actually not an
428 // error. We got woken up from the poll on the listening socket,
429 // but there is no connection ready to be accepted.
430 ASSERT(kTemporaryFailure != -1);
431 socket = kTemporaryFailure;
432 } else {
433 LOG_ERR("ServerSocket::Accept: accept(%ld) failed\n", fd);
434 }
435 } else {
436 LOG_INFO("ServerSocket::Accept: accept(%ld) -> socket %ld\n", fd, socket);
437 if (!FDUtils::SetCloseOnExec(socket)) {
438 LOG_ERR("FDUtils::SetCloseOnExec(%ld) failed\n", socket);
439 FDUtils::SaveErrorAndClose(socket);
440 return -1;
441 }
442 if (!FDUtils::SetNonBlocking(socket)) {
443 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket);
444 FDUtils::SaveErrorAndClose(socket);
445 return -1;
446 }
447 }
448 return socket;
449 }
450
451
452 void Socket::Close(intptr_t fd) {
453 ASSERT(fd >= 0);
454 NO_RETRY_EXPECTED(close(fd));
455 }
456
457
458 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
459 LOG_ERR("Socket::GetNoDelay is unimplemented\n");
460 UNIMPLEMENTED();
461 return false;
462 }
463
464
465 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
466 int on = enabled ? 1 : 0;
467 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
468 reinterpret_cast<char*>(&on),
469 sizeof(on))) == 0;
470 }
471
472
473 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
474 LOG_ERR("Socket::GetMulticastLoop is unimplemented\n");
475 UNIMPLEMENTED();
476 return false;
477 }
478
479
480 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
481 LOG_ERR("Socket::SetMulticastLoop is unimplemented\n");
482 UNIMPLEMENTED();
483 return false;
484 }
485
486
487 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
488 LOG_ERR("Socket::GetMulticastHops is unimplemented\n");
489 UNIMPLEMENTED();
490 return false;
491 }
492
493
494 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
495 LOG_ERR("Socket::SetMulticastHops is unimplemented\n");
496 UNIMPLEMENTED();
497 return false;
498 }
499
500
501 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
502 LOG_ERR("Socket::GetBroadcast is unimplemented\n");
503 UNIMPLEMENTED();
504 return false;
505 }
506
507
508 bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
509 LOG_ERR("Socket::SetBroadcast is unimplemented\n");
510 UNIMPLEMENTED();
511 return false;
512 }
513
514
515 bool Socket::JoinMulticast(intptr_t fd,
516 const RawAddr& addr,
517 const RawAddr&,
518 int interfaceIndex) {
519 LOG_ERR("Socket::JoinMulticast is unimplemented\n");
520 UNIMPLEMENTED();
521 return false;
522 }
523
524
525 bool Socket::LeaveMulticast(intptr_t fd,
526 const RawAddr& addr,
527 const RawAddr&,
528 int interfaceIndex) {
529 LOG_ERR("Socket::LeaveMulticast is unimplemented\n");
530 UNIMPLEMENTED();
531 return false;
532 }
533
534 } // namespace bin
535 } // namespace dart
536
537 #endif // defined(TARGET_OS_FUCHSIA)
538
539 #endif // !defined(DART_IO_DISABLED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698