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

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

Issue 2780063002: Pulled a significant portion of Socket implementation into BaseSocket in order to prepare for the s… (Closed)
Patch Set: Changed TARGET_OS_* to HOST_OS_*, removed extra include 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
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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_LINUX) 8 #if defined(HOST_OS_LINUX)
9 9
10 #include "bin/socket.h" 10 #include "bin/socket.h"
zra 2017/04/03 20:30:34 ditto
11 #include "bin/socket_linux.h" 11 #include "bin/socket_base_linux.h"
12 12
13 #include <errno.h> // NOLINT 13 #include <errno.h> // NOLINT
14 #include <ifaddrs.h> // NOLINT 14 #include <ifaddrs.h> // NOLINT
15 #include <net/if.h> // NOLINT 15 #include <net/if.h> // NOLINT
16 #include <netinet/tcp.h> // NOLINT 16 #include <netinet/tcp.h> // NOLINT
17 #include <stdio.h> // NOLINT 17 #include <stdio.h> // NOLINT
18 #include <stdlib.h> // NOLINT 18 #include <stdlib.h> // NOLINT
19 #include <string.h> // NOLINT 19 #include <string.h> // NOLINT
20 #include <sys/stat.h> // NOLINT 20 #include <sys/stat.h> // NOLINT
21 #include <unistd.h> // NOLINT 21 #include <unistd.h> // NOLINT
22 22
23 #include "bin/fdutils.h" 23 #include "bin/fdutils.h"
24 #include "bin/file.h" 24 #include "bin/file.h"
25 #include "bin/thread.h" 25 #include "bin/thread.h"
26 #include "platform/signal_blocker.h" 26 #include "platform/signal_blocker.h"
27 27
28 namespace dart { 28 namespace dart {
29 namespace bin { 29 namespace bin {
30 30
31 SocketAddress::SocketAddress(struct sockaddr* sa) { 31 SocketAddress::SocketAddress(struct sockaddr* sa) {
32 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 32 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
33 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, 33 if (!SocketBase::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa),
34 INET6_ADDRSTRLEN)) { 34 as_string_, INET6_ADDRSTRLEN)) {
35 as_string_[0] = 0; 35 as_string_[0] = 0;
36 } 36 }
37 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); 37 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa));
38 memmove(reinterpret_cast<void*>(&addr_), sa, salen); 38 memmove(reinterpret_cast<void*>(&addr_), sa, salen);
39 } 39 }
40 40
41 41
42 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { 42 bool SocketBase::FormatNumericAddress(const RawAddr& addr,
43 char* address,
44 int len) {
43 socklen_t salen = SocketAddress::GetAddrLength(addr); 45 socklen_t salen = SocketAddress::GetAddrLength(addr);
44 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL, 46 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
45 0, NI_NUMERICHOST) == 0)); 47 0, NI_NUMERICHOST) == 0));
46 } 48 }
47 49
48 50
49 Socket::Socket(intptr_t fd) 51 bool SocketBase::IsBindError(intptr_t error_number) {
50 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {}
51
52
53 void Socket::SetClosedFd() {
54 fd_ = kClosedFd;
55 }
56
57
58 bool Socket::Initialize() {
59 // Nothing to do on Linux.
60 return true;
61 }
62
63
64 static intptr_t Create(const RawAddr& addr) {
65 intptr_t fd;
66 fd = NO_RETRY_EXPECTED(
67 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
68 if (fd < 0) {
69 return -1;
70 }
71 return fd;
72 }
73
74
75 static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
76 intptr_t result = TEMP_FAILURE_RETRY(
77 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr)));
78 if ((result == 0) || (errno == EINPROGRESS)) {
79 return fd;
80 }
81 FDUtils::FDUtils::SaveErrorAndClose(fd);
82 return -1;
83 }
84
85
86 intptr_t Socket::CreateConnect(const RawAddr& addr) {
87 intptr_t fd = Create(addr);
88 if (fd < 0) {
89 return fd;
90 }
91 return Connect(fd, addr);
92 }
93
94
95 intptr_t Socket::CreateBindConnect(const RawAddr& addr,
96 const RawAddr& source_addr) {
97 intptr_t fd = Create(addr);
98 if (fd < 0) {
99 return fd;
100 }
101
102 intptr_t result = TEMP_FAILURE_RETRY(
103 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr)));
104 if ((result != 0) && (errno != EINPROGRESS)) {
105 FDUtils::SaveErrorAndClose(fd);
106 return -1;
107 }
108
109 return Connect(fd, addr);
110 }
111
112
113 bool Socket::IsBindError(intptr_t error_number) {
114 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || 52 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
115 error_number == EINVAL; 53 error_number == EINVAL;
116 } 54 }
117 55
118 56
119 intptr_t Socket::Available(intptr_t fd) { 57 intptr_t SocketBase::Available(intptr_t fd) {
120 return FDUtils::AvailableBytes(fd); 58 return FDUtils::AvailableBytes(fd);
121 } 59 }
122 60
123 61
124 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { 62 intptr_t SocketBase::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
125 ASSERT(fd >= 0); 63 ASSERT(fd >= 0);
126 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); 64 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes));
127 ASSERT(EAGAIN == EWOULDBLOCK); 65 ASSERT(EAGAIN == EWOULDBLOCK);
128 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 66 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
129 // If the read would block we need to retry and therefore return 0 67 // If the read would block we need to retry and therefore return 0
130 // as the number of bytes written. 68 // as the number of bytes written.
131 read_bytes = 0; 69 read_bytes = 0;
132 } 70 }
133 return read_bytes; 71 return read_bytes;
134 } 72 }
135 73
136 74
137 intptr_t Socket::RecvFrom(intptr_t fd, 75 intptr_t SocketBase::RecvFrom(intptr_t fd,
138 void* buffer, 76 void* buffer,
139 intptr_t num_bytes, 77 intptr_t num_bytes,
140 RawAddr* addr) { 78 RawAddr* addr) {
141 ASSERT(fd >= 0); 79 ASSERT(fd >= 0);
142 socklen_t addr_len = sizeof(addr->ss); 80 socklen_t addr_len = sizeof(addr->ss);
143 ssize_t read_bytes = TEMP_FAILURE_RETRY( 81 ssize_t read_bytes = TEMP_FAILURE_RETRY(
144 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); 82 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len));
145 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 83 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
146 // If the read would block we need to retry and therefore return 0 84 // If the read would block we need to retry and therefore return 0
147 // as the number of bytes written. 85 // as the number of bytes written.
148 read_bytes = 0; 86 read_bytes = 0;
149 } 87 }
150 return read_bytes; 88 return read_bytes;
151 } 89 }
152 90
153 91
154 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { 92 intptr_t SocketBase::Write(intptr_t fd,
93 const void* buffer,
94 intptr_t num_bytes) {
155 ASSERT(fd >= 0); 95 ASSERT(fd >= 0);
156 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); 96 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
157 ASSERT(EAGAIN == EWOULDBLOCK); 97 ASSERT(EAGAIN == EWOULDBLOCK);
158 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 98 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
159 // If the would block we need to retry and therefore return 0 as 99 // If the would block we need to retry and therefore return 0 as
160 // the number of bytes written. 100 // the number of bytes written.
161 written_bytes = 0; 101 written_bytes = 0;
162 } 102 }
163 return written_bytes; 103 return written_bytes;
164 } 104 }
165 105
166 106
167 intptr_t Socket::SendTo(intptr_t fd, 107 intptr_t SocketBase::SendTo(intptr_t fd,
168 const void* buffer, 108 const void* buffer,
169 intptr_t num_bytes, 109 intptr_t num_bytes,
170 const RawAddr& addr) { 110 const RawAddr& addr) {
171 ASSERT(fd >= 0); 111 ASSERT(fd >= 0);
172 ssize_t written_bytes = 112 ssize_t written_bytes =
173 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr, 113 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr,
174 SocketAddress::GetAddrLength(addr))); 114 SocketAddress::GetAddrLength(addr)));
175 ASSERT(EAGAIN == EWOULDBLOCK); 115 ASSERT(EAGAIN == EWOULDBLOCK);
176 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 116 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
177 // If the would block we need to retry and therefore return 0 as 117 // If the would block we need to retry and therefore return 0 as
178 // the number of bytes written. 118 // the number of bytes written.
179 written_bytes = 0; 119 written_bytes = 0;
180 } 120 }
181 return written_bytes; 121 return written_bytes;
182 } 122 }
183 123
184 124
185 intptr_t Socket::GetPort(intptr_t fd) { 125 intptr_t SocketBase::GetPort(intptr_t fd) {
186 ASSERT(fd >= 0); 126 ASSERT(fd >= 0);
187 RawAddr raw; 127 RawAddr raw;
188 socklen_t size = sizeof(raw); 128 socklen_t size = sizeof(raw);
189 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { 129 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) {
190 return 0; 130 return 0;
191 } 131 }
192 return SocketAddress::GetAddrPort(raw); 132 return SocketAddress::GetAddrPort(raw);
193 } 133 }
194 134
195 135
196 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { 136 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) {
197 ASSERT(fd >= 0); 137 ASSERT(fd >= 0);
198 RawAddr raw; 138 RawAddr raw;
199 socklen_t size = sizeof(raw); 139 socklen_t size = sizeof(raw);
200 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { 140 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) {
201 return NULL; 141 return NULL;
202 } 142 }
203 *port = SocketAddress::GetAddrPort(raw); 143 *port = SocketAddress::GetAddrPort(raw);
204 return new SocketAddress(&raw.addr); 144 return new SocketAddress(&raw.addr);
205 } 145 }
206 146
207 147
208 void Socket::GetError(intptr_t fd, OSError* os_error) { 148 void SocketBase::GetError(intptr_t fd, OSError* os_error) {
209 int len = sizeof(errno); 149 int len = sizeof(errno);
210 int err = 0; 150 int err = 0;
211 VOID_NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, 151 VOID_NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err,
212 reinterpret_cast<socklen_t*>(&len))); 152 reinterpret_cast<socklen_t*>(&len)));
213 errno = err; 153 errno = err;
214 os_error->SetCodeAndMessage(OSError::kSystem, errno); 154 os_error->SetCodeAndMessage(OSError::kSystem, errno);
215 } 155 }
216 156
217 157
218 int Socket::GetType(intptr_t fd) { 158 int SocketBase::GetType(intptr_t fd) {
219 struct stat64 buf; 159 struct stat64 buf;
220 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf)); 160 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
221 if (result == -1) { 161 if (result == -1) {
222 return -1; 162 return -1;
223 } 163 }
224 if (S_ISCHR(buf.st_mode)) { 164 if (S_ISCHR(buf.st_mode)) {
225 return File::kTerminal; 165 return File::kTerminal;
226 } 166 }
227 if (S_ISFIFO(buf.st_mode)) { 167 if (S_ISFIFO(buf.st_mode)) {
228 return File::kPipe; 168 return File::kPipe;
229 } 169 }
230 if (S_ISREG(buf.st_mode)) { 170 if (S_ISREG(buf.st_mode)) {
231 return File::kFile; 171 return File::kFile;
232 } 172 }
233 return File::kOther; 173 return File::kOther;
234 } 174 }
235 175
236 176
237 intptr_t Socket::GetStdioHandle(intptr_t num) { 177 intptr_t SocketBase::GetStdioHandle(intptr_t num) {
238 return num; 178 return num;
239 } 179 }
240 180
241 181
242 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, 182 AddressList<SocketAddress>* SocketBase::LookupAddress(const char* host,
243 int type, 183 int type,
244 OSError** os_error) { 184 OSError** os_error) {
245 // Perform a name lookup for a host name. 185 // Perform a name lookup for a host name.
246 struct addrinfo hints; 186 struct addrinfo hints;
247 memset(&hints, 0, sizeof(hints)); 187 memset(&hints, 0, sizeof(hints));
248 hints.ai_family = SocketAddress::FromType(type); 188 hints.ai_family = SocketAddress::FromType(type);
249 hints.ai_socktype = SOCK_STREAM; 189 hints.ai_socktype = SOCK_STREAM;
250 hints.ai_flags = AI_ADDRCONFIG; 190 hints.ai_flags = AI_ADDRCONFIG;
251 hints.ai_protocol = IPPROTO_TCP; 191 hints.ai_protocol = IPPROTO_TCP;
252 struct addrinfo* info = NULL; 192 struct addrinfo* info = NULL;
253 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info)); 193 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
254 if (status != 0) { 194 if (status != 0) {
(...skipping 20 matching lines...) Expand all
275 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { 215 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
276 addresses->SetAt(i, new SocketAddress(c->ai_addr)); 216 addresses->SetAt(i, new SocketAddress(c->ai_addr));
277 i++; 217 i++;
278 } 218 }
279 } 219 }
280 freeaddrinfo(info); 220 freeaddrinfo(info);
281 return addresses; 221 return addresses;
282 } 222 }
283 223
284 224
285 bool Socket::ReverseLookup(const RawAddr& addr, 225 bool SocketBase::ReverseLookup(const RawAddr& addr,
286 char* host, 226 char* host,
287 intptr_t host_len, 227 intptr_t host_len,
288 OSError** os_error) { 228 OSError** os_error) {
289 ASSERT(host_len >= NI_MAXHOST); 229 ASSERT(host_len >= NI_MAXHOST);
290 int status = NO_RETRY_EXPECTED( 230 int status = NO_RETRY_EXPECTED(
291 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, 231 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host,
292 host_len, NULL, 0, NI_NAMEREQD)); 232 host_len, NULL, 0, NI_NAMEREQD));
293 if (status != 0) { 233 if (status != 0) {
294 ASSERT(*os_error == NULL); 234 ASSERT(*os_error == NULL);
295 *os_error = 235 *os_error =
296 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); 236 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
297 return false; 237 return false;
298 } 238 }
299 return true; 239 return true;
300 } 240 }
301 241
302 242
303 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { 243 bool SocketBase::ParseAddress(int type, const char* address, RawAddr* addr) {
304 int result; 244 int result;
305 if (type == SocketAddress::TYPE_IPV4) { 245 if (type == SocketAddress::TYPE_IPV4) {
306 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); 246 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr));
307 } else { 247 } else {
308 ASSERT(type == SocketAddress::TYPE_IPV6); 248 ASSERT(type == SocketAddress::TYPE_IPV6);
309 result = 249 result =
310 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); 250 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr));
311 } 251 }
312 return (result == 1); 252 return (result == 1);
313 } 253 }
314 254
315 255
316 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
317 intptr_t fd;
318
319 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family,
320 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
321 IPPROTO_UDP));
322 if (fd < 0) {
323 return -1;
324 }
325
326 if (reuseAddress) {
327 int optval = 1;
328 VOID_NO_RETRY_EXPECTED(
329 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
330 }
331
332 if (NO_RETRY_EXPECTED(
333 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
334 FDUtils::SaveErrorAndClose(fd);
335 return -1;
336 }
337 return fd;
338 }
339
340
341 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { 256 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
342 if (ifa->ifa_addr == NULL) { 257 if (ifa->ifa_addr == NULL) {
343 // OpenVPN's virtual device tun0. 258 // OpenVPN's virtual device tun0.
344 return false; 259 return false;
345 } 260 }
346 int family = ifa->ifa_addr->sa_family; 261 int family = ifa->ifa_addr->sa_family;
347 return ((lookup_family == family) || 262 return ((lookup_family == family) ||
348 (((lookup_family == AF_UNSPEC) && 263 (((lookup_family == AF_UNSPEC) &&
349 ((family == AF_INET) || (family == AF_INET6))))); 264 ((family == AF_INET) || (family == AF_INET6)))));
350 } 265 }
351 266
352 267
353 bool Socket::ListInterfacesSupported() { 268 bool SocketBase::ListInterfacesSupported() {
354 return true; 269 return true;
355 } 270 }
356 271
357 272
358 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( 273 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces(
359 int type, 274 int type,
360 OSError** os_error) { 275 OSError** os_error) {
361 struct ifaddrs* ifaddr; 276 struct ifaddrs* ifaddr;
362 277
363 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr)); 278 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr));
364 if (status != 0) { 279 if (status != 0) {
365 ASSERT(*os_error == NULL); 280 ASSERT(*os_error == NULL);
366 *os_error = 281 *os_error =
367 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo); 282 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
368 return NULL; 283 return NULL;
(...skipping 18 matching lines...) Expand all
387 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name, 302 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name,
388 if_nametoindex(ifa->ifa_name))); 303 if_nametoindex(ifa->ifa_name)));
389 i++; 304 i++;
390 } 305 }
391 } 306 }
392 freeifaddrs(ifaddr); 307 freeifaddrs(ifaddr);
393 return addresses; 308 return addresses;
394 } 309 }
395 310
396 311
397 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, 312 void SocketBase::Close(intptr_t fd) {
398 intptr_t backlog,
399 bool v6_only) {
400 intptr_t fd;
401
402 fd = NO_RETRY_EXPECTED(
403 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
404 if (fd < 0) {
405 return -1;
406 }
407
408 int optval = 1;
409 VOID_NO_RETRY_EXPECTED(
410 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
411
412 if (addr.ss.ss_family == AF_INET6) {
413 optval = v6_only ? 1 : 0;
414 VOID_NO_RETRY_EXPECTED(
415 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
416 }
417
418 if (NO_RETRY_EXPECTED(
419 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
420 FDUtils::SaveErrorAndClose(fd);
421 return -1;
422 }
423
424 // Test for invalid socket port 65535 (some browsers disallow it).
425 if ((SocketAddress::GetAddrPort(addr) == 0) &&
426 (Socket::GetPort(fd) == 65535)) {
427 // Don't close the socket until we have created a new socket, ensuring
428 // that we do not get the bad port number again.
429 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
430 FDUtils::SaveErrorAndClose(fd);
431 return new_fd;
432 }
433
434 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
435 FDUtils::SaveErrorAndClose(fd);
436 return -1;
437 }
438
439 return fd;
440 }
441
442
443 bool ServerSocket::StartAccept(intptr_t fd) {
444 USE(fd);
445 return true;
446 }
447
448
449 static bool IsTemporaryAcceptError(int error) {
450 // On Linux a number of protocol errors should be treated as EAGAIN.
451 // These are the ones for TCP/IP.
452 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
453 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
454 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
455 (error == ENETUNREACH);
456 }
457
458
459 intptr_t ServerSocket::Accept(intptr_t fd) {
460 intptr_t socket;
461 struct sockaddr clientaddr;
462 socklen_t addrlen = sizeof(clientaddr);
463 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
464 if (socket == -1) {
465 if (IsTemporaryAcceptError(errno)) {
466 // We need to signal to the caller that this is actually not an
467 // error. We got woken up from the poll on the listening socket,
468 // but there is no connection ready to be accepted.
469 ASSERT(kTemporaryFailure != -1);
470 socket = kTemporaryFailure;
471 }
472 } else {
473 if (!FDUtils::SetCloseOnExec(socket)) {
474 FDUtils::SaveErrorAndClose(socket);
475 return -1;
476 }
477 if (!FDUtils::SetNonBlocking(socket)) {
478 FDUtils::SaveErrorAndClose(socket);
479 return -1;
480 }
481 }
482 return socket;
483 }
484
485
486 void Socket::Close(intptr_t fd) {
487 ASSERT(fd >= 0); 313 ASSERT(fd >= 0);
488 VOID_TEMP_FAILURE_RETRY(close(fd)); 314 VOID_TEMP_FAILURE_RETRY(close(fd));
489 } 315 }
490 316
491 317
492 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { 318 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) {
493 int on; 319 int on;
494 socklen_t len = sizeof(on); 320 socklen_t len = sizeof(on);
495 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, 321 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
496 reinterpret_cast<void*>(&on), &len)); 322 reinterpret_cast<void*>(&on), &len));
497 if (err == 0) { 323 if (err == 0) {
498 *enabled = (on == 1); 324 *enabled = (on == 1);
499 } 325 }
500 return (err == 0); 326 return (err == 0);
501 } 327 }
502 328
503 329
504 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 330 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) {
505 int on = enabled ? 1 : 0; 331 int on = enabled ? 1 : 0;
506 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, 332 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
507 reinterpret_cast<char*>(&on), 333 reinterpret_cast<char*>(&on),
508 sizeof(on))) == 0; 334 sizeof(on))) == 0;
509 } 335 }
510 336
511 337
512 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { 338 bool SocketBase::GetMulticastLoop(intptr_t fd,
339 intptr_t protocol,
340 bool* enabled) {
513 uint8_t on; 341 uint8_t on;
514 socklen_t len = sizeof(on); 342 socklen_t len = sizeof(on);
515 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 343 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
516 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP 344 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
517 : IPV6_MULTICAST_LOOP; 345 : IPV6_MULTICAST_LOOP;
518 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, 346 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
519 reinterpret_cast<char*>(&on), &len)) == 0) { 347 reinterpret_cast<char*>(&on), &len)) == 0) {
520 *enabled = (on == 1); 348 *enabled = (on == 1);
521 return true; 349 return true;
522 } 350 }
523 return false; 351 return false;
524 } 352 }
525 353
526 354
527 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { 355 bool SocketBase::SetMulticastLoop(intptr_t fd,
356 intptr_t protocol,
357 bool enabled) {
528 int on = enabled ? 1 : 0; 358 int on = enabled ? 1 : 0;
529 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 359 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
530 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP 360 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
531 : IPV6_MULTICAST_LOOP; 361 : IPV6_MULTICAST_LOOP;
532 return NO_RETRY_EXPECTED(setsockopt( 362 return NO_RETRY_EXPECTED(setsockopt(
533 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) == 363 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) ==
534 0; 364 0;
535 } 365 }
536 366
537 367
538 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { 368 bool SocketBase::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
539 uint8_t v; 369 uint8_t v;
540 socklen_t len = sizeof(v); 370 socklen_t len = sizeof(v);
541 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 371 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
542 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL 372 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
543 : IPV6_MULTICAST_HOPS; 373 : IPV6_MULTICAST_HOPS;
544 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname, 374 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
545 reinterpret_cast<char*>(&v), &len)) == 0) { 375 reinterpret_cast<char*>(&v), &len)) == 0) {
546 *value = v; 376 *value = v;
547 return true; 377 return true;
548 } 378 }
549 return false; 379 return false;
550 } 380 }
551 381
552 382
553 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { 383 bool SocketBase::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
554 int v = value; 384 int v = value;
555 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 385 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
556 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL 386 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
557 : IPV6_MULTICAST_HOPS; 387 : IPV6_MULTICAST_HOPS;
558 return NO_RETRY_EXPECTED(setsockopt( 388 return NO_RETRY_EXPECTED(setsockopt(
559 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0; 389 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0;
560 } 390 }
561 391
562 392
563 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { 393 bool SocketBase::GetBroadcast(intptr_t fd, bool* enabled) {
564 int on; 394 int on;
565 socklen_t len = sizeof(on); 395 socklen_t len = sizeof(on);
566 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST, 396 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST,
567 reinterpret_cast<char*>(&on), &len)); 397 reinterpret_cast<char*>(&on), &len));
568 if (err == 0) { 398 if (err == 0) {
569 *enabled = (on == 1); 399 *enabled = (on == 1);
570 } 400 }
571 return (err == 0); 401 return (err == 0);
572 } 402 }
573 403
574 404
575 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { 405 bool SocketBase::SetBroadcast(intptr_t fd, bool enabled) {
576 int on = enabled ? 1 : 0; 406 int on = enabled ? 1 : 0;
577 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, 407 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
578 reinterpret_cast<char*>(&on), 408 reinterpret_cast<char*>(&on),
579 sizeof(on))) == 0; 409 sizeof(on))) == 0;
580 } 410 }
581 411
582 412
583 bool Socket::JoinMulticast(intptr_t fd, 413 bool SocketBase::JoinMulticast(intptr_t fd,
584 const RawAddr& addr, 414 const RawAddr& addr,
585 const RawAddr&, 415 const RawAddr&,
586 int interfaceIndex) { 416 int interfaceIndex) {
587 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 417 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
588 struct group_req mreq; 418 struct group_req mreq;
589 mreq.gr_interface = interfaceIndex; 419 mreq.gr_interface = interfaceIndex;
590 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 420 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
591 return NO_RETRY_EXPECTED( 421 return NO_RETRY_EXPECTED(
592 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; 422 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0;
593 } 423 }
594 424
595 425
596 bool Socket::LeaveMulticast(intptr_t fd, 426 bool SocketBase::LeaveMulticast(intptr_t fd,
597 const RawAddr& addr, 427 const RawAddr& addr,
598 const RawAddr&, 428 const RawAddr&,
599 int interfaceIndex) { 429 int interfaceIndex) {
600 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 430 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
601 struct group_req mreq; 431 struct group_req mreq;
602 mreq.gr_interface = interfaceIndex; 432 mreq.gr_interface = interfaceIndex;
603 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 433 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
604 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, 434 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq,
605 sizeof(mreq))) == 0; 435 sizeof(mreq))) == 0;
606 } 436 }
607 437
608 } // namespace bin 438 } // namespace bin
609 } // namespace dart 439 } // namespace dart
610 440
611 #endif // defined(HOST_OS_LINUX) 441 #endif // defined(HOST_OS_LINUX)
612 442
613 #endif // !defined(DART_IO_DISABLED) 443 #endif // !defined(DART_IO_DISABLED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698