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

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

Issue 2480793002: clang-format runtime/bin (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « runtime/bin/socket_fuchsia.cc ('k') | runtime/bin/socket_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(TARGET_OS_LINUX) 8 #if defined(TARGET_OS_LINUX)
9 9
10 #include "bin/socket.h" 10 #include "bin/socket.h"
11 #include "bin/socket_linux.h" 11 #include "bin/socket_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( 33 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_,
34 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { 34 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 Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) {
43 socklen_t salen = SocketAddress::GetAddrLength(addr); 43 socklen_t salen = SocketAddress::GetAddrLength(addr);
44 return (NO_RETRY_EXPECTED(getnameinfo( 44 return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
45 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST) == 0)); 45 0, NI_NUMERICHOST) == 0));
46 } 46 }
47 47
48 48
49 bool Socket::Initialize() { 49 bool Socket::Initialize() {
50 // Nothing to do on Linux. 50 // Nothing to do on Linux.
51 return true; 51 return true;
52 } 52 }
53 53
54 54
55 static intptr_t Create(const RawAddr& addr) { 55 static intptr_t Create(const RawAddr& addr) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 VOID_TEMP_FAILURE_RETRY(close(fd)); 96 VOID_TEMP_FAILURE_RETRY(close(fd));
97 return -1; 97 return -1;
98 } 98 }
99 99
100 return Connect(fd, addr); 100 return Connect(fd, addr);
101 } 101 }
102 102
103 103
104 bool Socket::IsBindError(intptr_t error_number) { 104 bool Socket::IsBindError(intptr_t error_number) {
105 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || 105 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
106 error_number == EINVAL; 106 error_number == EINVAL;
107 } 107 }
108 108
109 109
110 intptr_t Socket::Available(intptr_t fd) { 110 intptr_t Socket::Available(intptr_t fd) {
111 return FDUtils::AvailableBytes(fd); 111 return FDUtils::AvailableBytes(fd);
112 } 112 }
113 113
114 114
115 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { 115 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
116 ASSERT(fd >= 0); 116 ASSERT(fd >= 0);
117 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); 117 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes));
118 ASSERT(EAGAIN == EWOULDBLOCK); 118 ASSERT(EAGAIN == EWOULDBLOCK);
119 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 119 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
120 // If the read would block we need to retry and therefore return 0 120 // If the read would block we need to retry and therefore return 0
121 // as the number of bytes written. 121 // as the number of bytes written.
122 read_bytes = 0; 122 read_bytes = 0;
123 } 123 }
124 return read_bytes; 124 return read_bytes;
125 } 125 }
126 126
127 127
128 intptr_t Socket::RecvFrom( 128 intptr_t Socket::RecvFrom(intptr_t fd,
129 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { 129 void* buffer,
130 intptr_t num_bytes,
131 RawAddr* addr) {
130 ASSERT(fd >= 0); 132 ASSERT(fd >= 0);
131 socklen_t addr_len = sizeof(addr->ss); 133 socklen_t addr_len = sizeof(addr->ss);
132 ssize_t read_bytes = TEMP_FAILURE_RETRY( 134 ssize_t read_bytes = TEMP_FAILURE_RETRY(
133 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); 135 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len));
134 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { 136 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) {
135 // If the read would block we need to retry and therefore return 0 137 // If the read would block we need to retry and therefore return 0
136 // as the number of bytes written. 138 // as the number of bytes written.
137 read_bytes = 0; 139 read_bytes = 0;
138 } 140 }
139 return read_bytes; 141 return read_bytes;
140 } 142 }
141 143
142 144
143 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { 145 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
144 ASSERT(fd >= 0); 146 ASSERT(fd >= 0);
145 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); 147 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
146 ASSERT(EAGAIN == EWOULDBLOCK); 148 ASSERT(EAGAIN == EWOULDBLOCK);
147 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 149 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
148 // If the would block we need to retry and therefore return 0 as 150 // If the would block we need to retry and therefore return 0 as
149 // the number of bytes written. 151 // the number of bytes written.
150 written_bytes = 0; 152 written_bytes = 0;
151 } 153 }
152 return written_bytes; 154 return written_bytes;
153 } 155 }
154 156
155 157
156 intptr_t Socket::SendTo( 158 intptr_t Socket::SendTo(intptr_t fd,
157 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { 159 const void* buffer,
160 intptr_t num_bytes,
161 const RawAddr& addr) {
158 ASSERT(fd >= 0); 162 ASSERT(fd >= 0);
159 ssize_t written_bytes = TEMP_FAILURE_RETRY( 163 ssize_t written_bytes =
160 sendto(fd, buffer, num_bytes, 0, 164 TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr,
161 &addr.addr, SocketAddress::GetAddrLength(addr))); 165 SocketAddress::GetAddrLength(addr)));
162 ASSERT(EAGAIN == EWOULDBLOCK); 166 ASSERT(EAGAIN == EWOULDBLOCK);
163 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { 167 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) {
164 // If the would block we need to retry and therefore return 0 as 168 // If the would block we need to retry and therefore return 0 as
165 // the number of bytes written. 169 // the number of bytes written.
166 written_bytes = 0; 170 written_bytes = 0;
167 } 171 }
168 return written_bytes; 172 return written_bytes;
169 } 173 }
170 174
171 175
(...skipping 16 matching lines...) Expand all
188 return NULL; 192 return NULL;
189 } 193 }
190 *port = SocketAddress::GetAddrPort(raw); 194 *port = SocketAddress::GetAddrPort(raw);
191 return new SocketAddress(&raw.addr); 195 return new SocketAddress(&raw.addr);
192 } 196 }
193 197
194 198
195 void Socket::GetError(intptr_t fd, OSError* os_error) { 199 void Socket::GetError(intptr_t fd, OSError* os_error) {
196 int len = sizeof(errno); 200 int len = sizeof(errno);
197 int err = 0; 201 int err = 0;
198 VOID_NO_RETRY_EXPECTED(getsockopt( 202 VOID_NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err,
199 fd, SOL_SOCKET, SO_ERROR, &err, reinterpret_cast<socklen_t*>(&len))); 203 reinterpret_cast<socklen_t*>(&len)));
200 errno = err; 204 errno = err;
201 os_error->SetCodeAndMessage(OSError::kSystem, errno); 205 os_error->SetCodeAndMessage(OSError::kSystem, errno);
202 } 206 }
203 207
204 208
205 int Socket::GetType(intptr_t fd) { 209 int Socket::GetType(intptr_t fd) {
206 struct stat64 buf; 210 struct stat64 buf;
207 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf)); 211 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
208 if (result == -1) { 212 if (result == -1) {
209 return -1; 213 return -1;
(...skipping 28 matching lines...) Expand all
238 hints.ai_protocol = IPPROTO_TCP; 242 hints.ai_protocol = IPPROTO_TCP;
239 struct addrinfo* info = NULL; 243 struct addrinfo* info = NULL;
240 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info)); 244 int status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
241 if (status != 0) { 245 if (status != 0) {
242 // We failed, try without AI_ADDRCONFIG. This can happen when looking up 246 // We failed, try without AI_ADDRCONFIG. This can happen when looking up
243 // e.g. '::1', when there are no global IPv6 addresses. 247 // e.g. '::1', when there are no global IPv6 addresses.
244 hints.ai_flags = 0; 248 hints.ai_flags = 0;
245 status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info)); 249 status = NO_RETRY_EXPECTED(getaddrinfo(host, 0, &hints, &info));
246 if (status != 0) { 250 if (status != 0) {
247 ASSERT(*os_error == NULL); 251 ASSERT(*os_error == NULL);
248 *os_error = new OSError(status, 252 *os_error =
249 gai_strerror(status), 253 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
250 OSError::kGetAddressInfo);
251 return NULL; 254 return NULL;
252 } 255 }
253 } 256 }
254 intptr_t count = 0; 257 intptr_t count = 0;
255 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 258 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
256 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { 259 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
257 count++; 260 count++;
258 } 261 }
259 } 262 }
260 intptr_t i = 0; 263 intptr_t i = 0;
261 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); 264 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count);
262 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 265 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
263 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { 266 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) {
264 addresses->SetAt(i, new SocketAddress(c->ai_addr)); 267 addresses->SetAt(i, new SocketAddress(c->ai_addr));
265 i++; 268 i++;
266 } 269 }
267 } 270 }
268 freeaddrinfo(info); 271 freeaddrinfo(info);
269 return addresses; 272 return addresses;
270 } 273 }
271 274
272 275
273 bool Socket::ReverseLookup(const RawAddr& addr, 276 bool Socket::ReverseLookup(const RawAddr& addr,
274 char* host, 277 char* host,
275 intptr_t host_len, 278 intptr_t host_len,
276 OSError** os_error) { 279 OSError** os_error) {
277 ASSERT(host_len >= NI_MAXHOST); 280 ASSERT(host_len >= NI_MAXHOST);
278 int status = NO_RETRY_EXPECTED(getnameinfo( 281 int status = NO_RETRY_EXPECTED(
279 &addr.addr, 282 getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host,
280 SocketAddress::GetAddrLength(addr), 283 host_len, NULL, 0, NI_NAMEREQD));
281 host,
282 host_len,
283 NULL,
284 0,
285 NI_NAMEREQD));
286 if (status != 0) { 284 if (status != 0) {
287 ASSERT(*os_error == NULL); 285 ASSERT(*os_error == NULL);
288 *os_error = new OSError(status, 286 *os_error =
289 gai_strerror(status), 287 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
290 OSError::kGetAddressInfo);
291 return false; 288 return false;
292 } 289 }
293 return true; 290 return true;
294 } 291 }
295 292
296 293
297 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { 294 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
298 int result; 295 int result;
299 if (type == SocketAddress::TYPE_IPV4) { 296 if (type == SocketAddress::TYPE_IPV4) {
300 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); 297 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr));
301 } else { 298 } else {
302 ASSERT(type == SocketAddress::TYPE_IPV6); 299 ASSERT(type == SocketAddress::TYPE_IPV6);
303 result = NO_RETRY_EXPECTED( 300 result =
304 inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); 301 NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr));
305 } 302 }
306 return (result == 1); 303 return (result == 1);
307 } 304 }
308 305
309 306
310 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { 307 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
311 intptr_t fd; 308 intptr_t fd;
312 309
313 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, 310 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family,
314 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 311 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
315 IPPROTO_UDP)); 312 IPPROTO_UDP));
316 if (fd < 0) { 313 if (fd < 0) {
317 return -1; 314 return -1;
318 } 315 }
319 316
320 if (reuseAddress) { 317 if (reuseAddress) {
321 int optval = 1; 318 int optval = 1;
322 VOID_NO_RETRY_EXPECTED( 319 VOID_NO_RETRY_EXPECTED(
323 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 320 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
324 } 321 }
325 322
326 if (NO_RETRY_EXPECTED( 323 if (NO_RETRY_EXPECTED(
327 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { 324 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
328 VOID_TEMP_FAILURE_RETRY(close(fd)); 325 VOID_TEMP_FAILURE_RETRY(close(fd));
329 return -1; 326 return -1;
330 } 327 }
331 return fd; 328 return fd;
332 } 329 }
333 330
334 331
335 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { 332 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
336 if (ifa->ifa_addr == NULL) { 333 if (ifa->ifa_addr == NULL) {
337 // OpenVPN's virtual device tun0. 334 // OpenVPN's virtual device tun0.
338 return false; 335 return false;
339 } 336 }
340 int family = ifa->ifa_addr->sa_family; 337 int family = ifa->ifa_addr->sa_family;
341 return ((lookup_family == family) || 338 return ((lookup_family == family) ||
342 (((lookup_family == AF_UNSPEC) && 339 (((lookup_family == AF_UNSPEC) &&
343 ((family == AF_INET) || (family == AF_INET6))))); 340 ((family == AF_INET) || (family == AF_INET6)))));
344 } 341 }
345 342
346 343
347 bool Socket::ListInterfacesSupported() { 344 bool Socket::ListInterfacesSupported() {
348 return true; 345 return true;
349 } 346 }
350 347
351 348
352 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( 349 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
353 int type, 350 int type,
354 OSError** os_error) { 351 OSError** os_error) {
355 struct ifaddrs* ifaddr; 352 struct ifaddrs* ifaddr;
356 353
357 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr)); 354 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr));
358 if (status != 0) { 355 if (status != 0) {
359 ASSERT(*os_error == NULL); 356 ASSERT(*os_error == NULL);
360 *os_error = new OSError(status, 357 *os_error =
361 gai_strerror(status), 358 new OSError(status, gai_strerror(status), OSError::kGetAddressInfo);
362 OSError::kGetAddressInfo);
363 return NULL; 359 return NULL;
364 } 360 }
365 361
366 int lookup_family = SocketAddress::FromType(type); 362 int lookup_family = SocketAddress::FromType(type);
367 363
368 intptr_t count = 0; 364 intptr_t count = 0;
369 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { 365 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
370 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { 366 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) {
371 count++; 367 count++;
372 } 368 }
373 } 369 }
374 370
375 AddressList<InterfaceSocketAddress>* addresses = 371 AddressList<InterfaceSocketAddress>* addresses =
376 new AddressList<InterfaceSocketAddress>(count); 372 new AddressList<InterfaceSocketAddress>(count);
377 int i = 0; 373 int i = 0;
378 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { 374 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
379 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { 375 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) {
380 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); 376 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name);
381 addresses->SetAt(i, new InterfaceSocketAddress( 377 addresses->SetAt(
382 ifa->ifa_addr, ifa_name, if_nametoindex(ifa->ifa_name))); 378 i, new InterfaceSocketAddress(ifa->ifa_addr, ifa_name,
379 if_nametoindex(ifa->ifa_name)));
383 i++; 380 i++;
384 } 381 }
385 } 382 }
386 freeifaddrs(ifaddr); 383 freeifaddrs(ifaddr);
387 return addresses; 384 return addresses;
388 } 385 }
389 386
390 387
391 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, 388 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
392 intptr_t backlog, 389 intptr_t backlog,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 bool ServerSocket::StartAccept(intptr_t fd) { 436 bool ServerSocket::StartAccept(intptr_t fd) {
440 USE(fd); 437 USE(fd);
441 return true; 438 return true;
442 } 439 }
443 440
444 441
445 static bool IsTemporaryAcceptError(int error) { 442 static bool IsTemporaryAcceptError(int error) {
446 // On Linux a number of protocol errors should be treated as EAGAIN. 443 // On Linux a number of protocol errors should be treated as EAGAIN.
447 // These are the ones for TCP/IP. 444 // These are the ones for TCP/IP.
448 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || 445 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
449 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || 446 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
450 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || 447 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
451 (error == ENETUNREACH); 448 (error == ENETUNREACH);
452 } 449 }
453 450
454 451
455 intptr_t ServerSocket::Accept(intptr_t fd) { 452 intptr_t ServerSocket::Accept(intptr_t fd) {
456 intptr_t socket; 453 intptr_t socket;
457 struct sockaddr clientaddr; 454 struct sockaddr clientaddr;
458 socklen_t addrlen = sizeof(clientaddr); 455 socklen_t addrlen = sizeof(clientaddr);
459 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); 456 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
460 if (socket == -1) { 457 if (socket == -1) {
461 if (IsTemporaryAcceptError(errno)) { 458 if (IsTemporaryAcceptError(errno)) {
(...skipping 13 matching lines...) Expand all
475 472
476 void Socket::Close(intptr_t fd) { 473 void Socket::Close(intptr_t fd) {
477 ASSERT(fd >= 0); 474 ASSERT(fd >= 0);
478 VOID_TEMP_FAILURE_RETRY(close(fd)); 475 VOID_TEMP_FAILURE_RETRY(close(fd));
479 } 476 }
480 477
481 478
482 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { 479 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
483 int on; 480 int on;
484 socklen_t len = sizeof(on); 481 socklen_t len = sizeof(on);
485 int err = NO_RETRY_EXPECTED(getsockopt( 482 int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
486 fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<void *>(&on), &len)); 483 reinterpret_cast<void*>(&on), &len));
487 if (err == 0) { 484 if (err == 0) {
488 *enabled = (on == 1); 485 *enabled = (on == 1);
489 } 486 }
490 return (err == 0); 487 return (err == 0);
491 } 488 }
492 489
493 490
494 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { 491 bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
495 int on = enabled ? 1 : 0; 492 int on = enabled ? 1 : 0;
496 return NO_RETRY_EXPECTED(setsockopt(fd, 493 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
497 IPPROTO_TCP, 494 reinterpret_cast<char*>(&on),
498 TCP_NODELAY, 495 sizeof(on))) == 0;
499 reinterpret_cast<char *>(&on),
500 sizeof(on))) == 0;
501 } 496 }
502 497
503 498
504 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { 499 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
505 uint8_t on; 500 uint8_t on;
506 socklen_t len = sizeof(on); 501 socklen_t len = sizeof(on);
507 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 502 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
508 int optname = protocol == SocketAddress::TYPE_IPV4 503 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
509 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; 504 : IPV6_MULTICAST_LOOP;
510 if (NO_RETRY_EXPECTED(getsockopt( 505 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
511 fd, level, optname, reinterpret_cast<char *>(&on), &len)) == 0) { 506 reinterpret_cast<char*>(&on), &len)) == 0) {
512 *enabled = (on == 1); 507 *enabled = (on == 1);
513 return true; 508 return true;
514 } 509 }
515 return false; 510 return false;
516 } 511 }
517 512
518 513
519 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { 514 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
520 int on = enabled ? 1 : 0; 515 int on = enabled ? 1 : 0;
521 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 516 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
522 int optname = protocol == SocketAddress::TYPE_IPV4 517 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
523 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; 518 : IPV6_MULTICAST_LOOP;
524 return NO_RETRY_EXPECTED(setsockopt( 519 return NO_RETRY_EXPECTED(setsockopt(
525 fd, level, optname, reinterpret_cast<char *>(&on), sizeof(on))) == 0; 520 fd, level, optname, reinterpret_cast<char*>(&on), sizeof(on))) ==
521 0;
526 } 522 }
527 523
528 524
529 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { 525 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
530 uint8_t v; 526 uint8_t v;
531 socklen_t len = sizeof(v); 527 socklen_t len = sizeof(v);
532 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 528 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
533 int optname = protocol == SocketAddress::TYPE_IPV4 529 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
534 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; 530 : IPV6_MULTICAST_HOPS;
535 if (NO_RETRY_EXPECTED(getsockopt( 531 if (NO_RETRY_EXPECTED(getsockopt(fd, level, optname,
536 fd, level, optname, reinterpret_cast<char *>(&v), &len)) == 0) { 532 reinterpret_cast<char*>(&v), &len)) == 0) {
537 *value = v; 533 *value = v;
538 return true; 534 return true;
539 } 535 }
540 return false; 536 return false;
541 } 537 }
542 538
543 539
544 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { 540 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
545 int v = value; 541 int v = value;
546 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; 542 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
547 int optname = protocol == SocketAddress::TYPE_IPV4 543 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
548 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; 544 : IPV6_MULTICAST_HOPS;
549 return NO_RETRY_EXPECTED(setsockopt( 545 return NO_RETRY_EXPECTED(setsockopt(
550 fd, level, optname, reinterpret_cast<char *>(&v), sizeof(v))) == 0; 546 fd, level, optname, reinterpret_cast<char*>(&v), sizeof(v))) == 0;
551 } 547 }
552 548
553 549
554 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { 550 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
555 int on; 551 int on;
556 socklen_t len = sizeof(on); 552 socklen_t len = sizeof(on);
557 int err = NO_RETRY_EXPECTED(getsockopt( 553 int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST,
558 fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char *>(&on), &len)); 554 reinterpret_cast<char*>(&on), &len));
559 if (err == 0) { 555 if (err == 0) {
560 *enabled = (on == 1); 556 *enabled = (on == 1);
561 } 557 }
562 return (err == 0); 558 return (err == 0);
563 } 559 }
564 560
565 561
566 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { 562 bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
567 int on = enabled ? 1 : 0; 563 int on = enabled ? 1 : 0;
568 return NO_RETRY_EXPECTED(setsockopt(fd, 564 return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
569 SOL_SOCKET, 565 reinterpret_cast<char*>(&on),
570 SO_BROADCAST, 566 sizeof(on))) == 0;
571 reinterpret_cast<char *>(&on),
572 sizeof(on))) == 0;
573 } 567 }
574 568
575 569
576 bool Socket::JoinMulticast( 570 bool Socket::JoinMulticast(intptr_t fd,
577 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { 571 const RawAddr& addr,
572 const RawAddr&,
573 int interfaceIndex) {
578 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 574 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
579 struct group_req mreq; 575 struct group_req mreq;
580 mreq.gr_interface = interfaceIndex; 576 mreq.gr_interface = interfaceIndex;
581 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 577 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
582 return NO_RETRY_EXPECTED( 578 return NO_RETRY_EXPECTED(
583 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; 579 setsockopt(fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0;
584 } 580 }
585 581
586 582
587 bool Socket::LeaveMulticast( 583 bool Socket::LeaveMulticast(intptr_t fd,
588 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { 584 const RawAddr& addr,
585 const RawAddr&,
586 int interfaceIndex) {
589 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 587 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
590 struct group_req mreq; 588 struct group_req mreq;
591 mreq.gr_interface = interfaceIndex; 589 mreq.gr_interface = interfaceIndex;
592 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); 590 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
593 return NO_RETRY_EXPECTED( 591 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq,
594 setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; 592 sizeof(mreq))) == 0;
595 } 593 }
596 594
597 } // namespace bin 595 } // namespace bin
598 } // namespace dart 596 } // namespace dart
599 597
600 #endif // defined(TARGET_OS_LINUX) 598 #endif // defined(TARGET_OS_LINUX)
601 599
602 #endif // !defined(DART_IO_DISABLED) 600 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/socket_fuchsia.cc ('k') | runtime/bin/socket_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698