Flutter Engine
The Flutter Engine
socket_fuchsia.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_FUCHSIA)
7
8#include "bin/socket.h"
9
10#include <errno.h> // NOLINT
11
12#include "bin/eventhandler.h"
13#include "bin/fdutils.h"
15#include "platform/syslog.h"
16
17// #define SOCKET_LOG_INFO 1
18// #define SOCKET_LOG_ERROR 1
19
20// define SOCKET_LOG_ERROR to get log messages only for errors.
21// define SOCKET_LOG_INFO to get log messages for both information and errors.
22#if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
23
24#define LOG_ERR(msg, ...) \
25 { \
26 int err = errno; \
27 Syslog::PrintErr("Dart Socket ERROR: %s:%d: " msg, __FILE__, __LINE__, \
28 ##__VA_ARGS__); \
29 errno = err; \
30 }
31#if defined(SOCKET_LOG_INFO)
32#define LOG_INFO(msg, ...) \
33 Syslog::Print("Dart Socket INFO: %s:%d: " msg, __FILE__, __LINE__, \
34 ##__VA_ARGS__)
35#else
36#define LOG_INFO(msg, ...)
37#endif // defined(SOCKET_LOG_INFO)
38#else
39#define LOG_ERR(msg, ...)
40#define LOG_INFO(msg, ...)
41#endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR)
42
43namespace dart {
44namespace bin {
45
46Socket::Socket(intptr_t fd)
47 : ReferenceCounted(),
48 fd_(fd),
49 isolate_port_(Dart_GetMainPortId()),
50 port_(ILLEGAL_PORT),
51 udp_receive_buffer_(nullptr) {}
52
53void Socket::SetClosedFd() {
54 fd_ = kClosedFd;
55}
56
57void Socket::CloseFd() {
58 ASSERT(fd_ != kClosedFd);
59 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_);
60 ASSERT(handle != nullptr);
61 handle->Release();
62 SetClosedFd();
63}
64
65static intptr_t Create(const RawAddr& addr) {
66 LOG_INFO("Create: calling socket(SOCK_STREAM)\n");
67 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
68 if (fd < 0) {
69 LOG_ERR("Create: socket(SOCK_STREAM) failed\n");
70 return -1;
71 }
72 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd);
73 if (!FDUtils::SetCloseOnExec(fd)) {
74 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd);
75 FDUtils::SaveErrorAndClose(fd);
76 return -1;
77 }
78 IOHandle* io_handle = new IOHandle(fd);
79 return reinterpret_cast<intptr_t>(io_handle);
80}
81
82static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
83 IOHandle* handle = reinterpret_cast<IOHandle*>(fd);
84 LOG_INFO("Connect: calling connect(%ld)\n", handle->fd());
85 intptr_t result = NO_RETRY_EXPECTED(
86 connect(handle->fd(), &addr.addr, SocketAddress::GetAddrLength(addr)));
87 if ((result == 0) || (errno == EINPROGRESS)) {
88 return reinterpret_cast<intptr_t>(handle);
89 }
90 LOG_ERR("Connect: connect(%ld) failed\n", handle->fd());
91 FDUtils::SaveErrorAndClose(handle->fd());
92 handle->Release();
93 return -1;
94}
95
96intptr_t Socket::CreateConnect(const RawAddr& addr) {
97 intptr_t fd = Create(addr);
98 if (fd < 0) {
99 return fd;
100 }
101 IOHandle* handle = reinterpret_cast<IOHandle*>(fd);
102 if (!FDUtils::SetNonBlocking(handle->fd())) {
103 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n",
104 handle->fd());
105 FDUtils::SaveErrorAndClose(handle->fd());
106 handle->Release();
107 return -1;
108 }
109 return Connect(fd, addr);
110}
111
112intptr_t Socket::CreateUnixDomainConnect(const RawAddr& addr) {
113 // Fuchsia does not support unix domain socket
114 errno = ENOSYS;
115 return -1;
116}
117
118intptr_t Socket::CreateBindConnect(const RawAddr& addr,
119 const RawAddr& source_addr) {
120 errno = ENOSYS;
121 return -1;
122}
123
124intptr_t Socket::CreateUnixDomainBindConnect(const RawAddr& addr,
125 const RawAddr& source_addr) {
126 // Fuchsia does not support unix domain socket
127 errno = ENOSYS;
128 return -1;
129}
130
131intptr_t Socket::CreateBindDatagram(const RawAddr& addr,
132 bool reuseAddress,
133 bool reusePort,
134 int ttl) {
135 errno = ENOSYS;
136 return -1;
137}
138
139intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
140 intptr_t backlog,
141 bool v6_only) {
142 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n");
143 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
144 if (fd < 0) {
145 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n");
146 return -1;
147 }
148 LOG_INFO("ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd);
149
150 if (!FDUtils::SetCloseOnExec(fd)) {
151 LOG_ERR("ServerSocket::CreateBindListen: SetCloseOnExec(%ld) failed\n", fd);
152 FDUtils::SaveErrorAndClose(fd);
153 return -1;
154 }
155
156 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
157 int optval = 1;
159 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
160
161 if (addr.ss.ss_family == AF_INET6) {
162 optval = v6_only ? 1 : 0;
163 LOG_INFO("ServerSocket::CreateBindListen: calling setsockopt(%ld)\n", fd);
165 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
166 }
167
168 LOG_INFO("ServerSocket::CreateBindListen: calling bind(%ld)\n", fd);
170 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
171 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd);
172 FDUtils::SaveErrorAndClose(fd);
173 return -1;
174 }
175 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd);
176
177 IOHandle* io_handle = new IOHandle(fd);
178
179 // Test for invalid socket port 65535 (some browsers disallow it).
180 if ((SocketAddress::GetAddrPort(addr) == 0) &&
181 (SocketBase::GetPort(reinterpret_cast<intptr_t>(io_handle)) == 65535)) {
182 // Don't close the socket until we have created a new socket, ensuring
183 // that we do not get the bad port number again.
184 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
185 FDUtils::SaveErrorAndClose(fd);
186 io_handle->Release();
187 return new_fd;
188 }
189
190 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd);
191 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
192 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd);
193 FDUtils::SaveErrorAndClose(fd);
194 io_handle->Release();
195 return -1;
196 }
197 LOG_INFO("ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd);
198
199 if (!FDUtils::SetNonBlocking(fd)) {
200 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd);
201 FDUtils::SaveErrorAndClose(fd);
202 io_handle->Release();
203 return -1;
204 }
205 return reinterpret_cast<intptr_t>(io_handle);
206}
207
208intptr_t ServerSocket::CreateUnixDomainBindListen(const RawAddr& addr,
209 intptr_t backlog) {
210 // Fuchsia does not support unix domain socket.
211 errno = ENOSYS;
212 return -1;
213}
214
215bool ServerSocket::StartAccept(intptr_t fd) {
216 USE(fd);
217 return true;
218}
219
220static bool IsTemporaryAcceptError(int error) {
221 // On Linux a number of protocol errors should be treated as EAGAIN.
222 // These are the ones for TCP/IP.
223 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
224 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
225 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
226 (error == ENETUNREACH);
227}
228
229intptr_t ServerSocket::Accept(intptr_t fd) {
230 IOHandle* listen_handle = reinterpret_cast<IOHandle*>(fd);
231 intptr_t socket;
232 struct sockaddr clientaddr;
233 socklen_t addrlen = sizeof(clientaddr);
234 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", fd);
235 socket = listen_handle->Accept(&clientaddr, &addrlen);
236 if (socket == -1) {
237 if (IsTemporaryAcceptError(errno)) {
238 // We need to signal to the caller that this is actually not an
239 // error. We got woken up from the poll on the listening socket,
240 // but there is no connection ready to be accepted.
241 ASSERT(kTemporaryFailure != -1);
242 socket = kTemporaryFailure;
243 } else {
244 LOG_ERR("ServerSocket::Accept: accept(%ld) failed\n", fd);
245 }
246 } else {
247 IOHandle* io_handle = new IOHandle(socket);
248 LOG_INFO("ServerSocket::Accept: accept(%ld) -> socket %ld\n", fd, socket);
249 if (!FDUtils::SetCloseOnExec(socket)) {
250 LOG_ERR("FDUtils::SetCloseOnExec(%ld) failed\n", socket);
251 FDUtils::SaveErrorAndClose(socket);
252 io_handle->Release();
253 return -1;
254 }
255 if (!FDUtils::SetNonBlocking(socket)) {
256 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket);
257 FDUtils::SaveErrorAndClose(socket);
258 io_handle->Release();
259 return -1;
260 }
261 socket = reinterpret_cast<intptr_t>(io_handle);
262 }
263 return socket;
264}
265
266} // namespace bin
267} // namespace dart
268
269#endif // defined(DART_HOST_OS_FUCHSIA)
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
#define LOG_INFO(...)
Socket(intptr_t fd)
#define ILLEGAL_PORT
Definition: dart_api.h:1535
#define ASSERT(E)
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
Definition: dart_vm.cc:33
DART_EXPORT Dart_Port Dart_GetMainPortId()
static void USE(T &&)
Definition: globals.h:618
#define NO_RETRY_EXPECTED(expression)
#define VOID_NO_RETRY_EXPECTED(expression)