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