OLD | NEW |
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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
7 | 7 |
8 #include "bin/socket.h" | 8 #include "bin/socket.h" |
9 #include "bin/socket_linux.h" | 9 #include "bin/socket_linux.h" |
10 | 10 |
11 #include <errno.h> // NOLINT | 11 #include <errno.h> // NOLINT |
| 12 #include <ifaddrs.h> // NOLINT |
| 13 #include <net/if.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT |
12 #include <stdio.h> // NOLINT | 15 #include <stdio.h> // NOLINT |
13 #include <stdlib.h> // NOLINT | 16 #include <stdlib.h> // NOLINT |
14 #include <string.h> // NOLINT | 17 #include <string.h> // NOLINT |
15 #include <sys/stat.h> // NOLINT | 18 #include <sys/stat.h> // NOLINT |
16 #include <unistd.h> // NOLINT | 19 #include <unistd.h> // NOLINT |
17 #include <net/if.h> // NOLINT | |
18 #include <netinet/tcp.h> // NOLINT | |
19 #include <ifaddrs.h> // NOLINT | |
20 | 20 |
21 #include "bin/fdutils.h" | 21 #include "bin/fdutils.h" |
22 #include "bin/file.h" | 22 #include "bin/file.h" |
23 #include "bin/thread.h" | 23 #include "bin/thread.h" |
24 #include "platform/signal_blocker.h" | 24 #include "platform/signal_blocker.h" |
25 | 25 |
26 namespace dart { | 26 namespace dart { |
27 namespace bin { | 27 namespace bin { |
28 | 28 |
29 SocketAddress::SocketAddress(struct sockaddr* sa) { | 29 SocketAddress::SocketAddress(struct sockaddr* sa) { |
30 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 30 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
31 if (!Socket::FormatNumericAddress( | 31 if (!Socket::FormatNumericAddress( |
32 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { | 32 *reinterpret_cast<RawAddr*>(sa), as_string_, INET6_ADDRSTRLEN)) { |
33 as_string_[0] = 0; | 33 as_string_[0] = 0; |
34 } | 34 } |
35 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 35 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
36 memmove(reinterpret_cast<void *>(&addr_), sa, salen); | 36 memmove(reinterpret_cast<void *>(&addr_), sa, salen); |
37 } | 37 } |
38 | 38 |
39 | 39 |
40 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 40 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { |
41 socklen_t salen = SocketAddress::GetAddrLength(addr); | 41 socklen_t salen = SocketAddress::GetAddrLength(addr); |
42 if (NO_RETRY_EXPECTED(getnameinfo( | 42 return (NO_RETRY_EXPECTED(getnameinfo( |
43 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST) != 0)) { | 43 &addr.addr, salen, address, len, NULL, 0, NI_NUMERICHOST) == 0)); |
44 return false; | |
45 } | |
46 return true; | |
47 } | 44 } |
48 | 45 |
49 | 46 |
50 bool Socket::Initialize() { | 47 bool Socket::Initialize() { |
51 // Nothing to do on Linux. | 48 // Nothing to do on Linux. |
52 return true; | 49 return true; |
53 } | 50 } |
54 | 51 |
55 | 52 |
56 static intptr_t Create(const RawAddr& addr) { | 53 static intptr_t Create(const RawAddr& addr) { |
57 intptr_t fd; | 54 intptr_t fd; |
58 fd = NO_RETRY_EXPECTED( | 55 fd = NO_RETRY_EXPECTED( |
59 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); | 56 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); |
60 if (fd < 0) { | 57 if (fd < 0) { |
61 return -1; | 58 return -1; |
62 } | 59 } |
63 return fd; | 60 return fd; |
64 } | 61 } |
65 | 62 |
66 | 63 |
67 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 64 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
68 intptr_t result = TEMP_FAILURE_RETRY( | 65 intptr_t result = TEMP_FAILURE_RETRY( |
69 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 66 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
70 if (result == 0 || errno == EINPROGRESS) { | 67 if ((result == 0) || (errno == EINPROGRESS)) { |
71 return fd; | 68 return fd; |
72 } | 69 } |
73 VOID_TEMP_FAILURE_RETRY(close(fd)); | 70 VOID_TEMP_FAILURE_RETRY(close(fd)); |
74 return -1; | 71 return -1; |
75 } | 72 } |
76 | 73 |
77 | 74 |
78 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 75 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
79 intptr_t fd = Create(addr); | 76 intptr_t fd = Create(addr); |
80 if (fd < 0) { | 77 if (fd < 0) { |
81 return fd; | 78 return fd; |
82 } | 79 } |
83 return Connect(fd, addr); | 80 return Connect(fd, addr); |
84 } | 81 } |
85 | 82 |
86 | 83 |
87 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 84 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
88 const RawAddr& source_addr) { | 85 const RawAddr& source_addr) { |
89 intptr_t fd = Create(addr); | 86 intptr_t fd = Create(addr); |
90 if (fd < 0) { | 87 if (fd < 0) { |
91 return fd; | 88 return fd; |
92 } | 89 } |
93 | 90 |
94 intptr_t result = TEMP_FAILURE_RETRY( | 91 intptr_t result = TEMP_FAILURE_RETRY( |
95 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 92 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
96 if (result != 0 && errno != EINPROGRESS) { | 93 if ((result != 0) && (errno != EINPROGRESS)) { |
97 VOID_TEMP_FAILURE_RETRY(close(fd)); | 94 VOID_TEMP_FAILURE_RETRY(close(fd)); |
98 return -1; | 95 return -1; |
99 } | 96 } |
100 | 97 |
101 return Connect(fd, addr); | 98 return Connect(fd, addr); |
102 } | 99 } |
103 | 100 |
104 | 101 |
105 intptr_t Socket::Available(intptr_t fd) { | 102 intptr_t Socket::Available(intptr_t fd) { |
106 return FDUtils::AvailableBytes(fd); | 103 return FDUtils::AvailableBytes(fd); |
107 } | 104 } |
108 | 105 |
109 | 106 |
110 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 107 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
111 ASSERT(fd >= 0); | 108 ASSERT(fd >= 0); |
112 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); | 109 ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes)); |
113 ASSERT(EAGAIN == EWOULDBLOCK); | 110 ASSERT(EAGAIN == EWOULDBLOCK); |
114 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 111 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
115 // If the read would block we need to retry and therefore return 0 | 112 // If the read would block we need to retry and therefore return 0 |
116 // as the number of bytes written. | 113 // as the number of bytes written. |
117 read_bytes = 0; | 114 read_bytes = 0; |
118 } | 115 } |
119 return read_bytes; | 116 return read_bytes; |
120 } | 117 } |
121 | 118 |
122 | 119 |
123 intptr_t Socket::RecvFrom( | 120 intptr_t Socket::RecvFrom( |
124 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { | 121 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { |
125 ASSERT(fd >= 0); | 122 ASSERT(fd >= 0); |
126 socklen_t addr_len = sizeof(addr->ss); | 123 socklen_t addr_len = sizeof(addr->ss); |
127 ssize_t read_bytes = TEMP_FAILURE_RETRY( | 124 ssize_t read_bytes = TEMP_FAILURE_RETRY( |
128 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); | 125 recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len)); |
129 if (read_bytes == -1 && errno == EWOULDBLOCK) { | 126 if ((read_bytes == -1) && (errno == EWOULDBLOCK)) { |
130 // If the read would block we need to retry and therefore return 0 | 127 // If the read would block we need to retry and therefore return 0 |
131 // as the number of bytes written. | 128 // as the number of bytes written. |
132 read_bytes = 0; | 129 read_bytes = 0; |
133 } | 130 } |
134 return read_bytes; | 131 return read_bytes; |
135 } | 132 } |
136 | 133 |
137 | 134 |
138 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 135 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
139 ASSERT(fd >= 0); | 136 ASSERT(fd >= 0); |
140 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); | 137 ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
141 ASSERT(EAGAIN == EWOULDBLOCK); | 138 ASSERT(EAGAIN == EWOULDBLOCK); |
142 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 139 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
143 // If the would block we need to retry and therefore return 0 as | 140 // If the would block we need to retry and therefore return 0 as |
144 // the number of bytes written. | 141 // the number of bytes written. |
145 written_bytes = 0; | 142 written_bytes = 0; |
146 } | 143 } |
147 return written_bytes; | 144 return written_bytes; |
148 } | 145 } |
149 | 146 |
150 | 147 |
151 intptr_t Socket::SendTo( | 148 intptr_t Socket::SendTo( |
152 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { | 149 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { |
153 ASSERT(fd >= 0); | 150 ASSERT(fd >= 0); |
154 ssize_t written_bytes = TEMP_FAILURE_RETRY( | 151 ssize_t written_bytes = TEMP_FAILURE_RETRY( |
155 sendto(fd, buffer, num_bytes, 0, | 152 sendto(fd, buffer, num_bytes, 0, |
156 &addr.addr, SocketAddress::GetAddrLength(addr))); | 153 &addr.addr, SocketAddress::GetAddrLength(addr))); |
157 ASSERT(EAGAIN == EWOULDBLOCK); | 154 ASSERT(EAGAIN == EWOULDBLOCK); |
158 if (written_bytes == -1 && errno == EWOULDBLOCK) { | 155 if ((written_bytes == -1) && (errno == EWOULDBLOCK)) { |
159 // If the would block we need to retry and therefore return 0 as | 156 // If the would block we need to retry and therefore return 0 as |
160 // the number of bytes written. | 157 // the number of bytes written. |
161 written_bytes = 0; | 158 written_bytes = 0; |
162 } | 159 } |
163 return written_bytes; | 160 return written_bytes; |
164 } | 161 } |
165 | 162 |
166 | 163 |
167 intptr_t Socket::GetPort(intptr_t fd) { | 164 intptr_t Socket::GetPort(intptr_t fd) { |
168 ASSERT(fd >= 0); | 165 ASSERT(fd >= 0); |
(...skipping 24 matching lines...) Expand all Loading... |
193 VOID_NO_RETRY_EXPECTED(getsockopt( | 190 VOID_NO_RETRY_EXPECTED(getsockopt( |
194 fd, SOL_SOCKET, SO_ERROR, &err, reinterpret_cast<socklen_t*>(&len))); | 191 fd, SOL_SOCKET, SO_ERROR, &err, reinterpret_cast<socklen_t*>(&len))); |
195 errno = err; | 192 errno = err; |
196 os_error->SetCodeAndMessage(OSError::kSystem, errno); | 193 os_error->SetCodeAndMessage(OSError::kSystem, errno); |
197 } | 194 } |
198 | 195 |
199 | 196 |
200 int Socket::GetType(intptr_t fd) { | 197 int Socket::GetType(intptr_t fd) { |
201 struct stat64 buf; | 198 struct stat64 buf; |
202 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf)); | 199 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf)); |
203 if (result == -1) return -1; | 200 if (result == -1) { |
204 if (S_ISCHR(buf.st_mode)) return File::kTerminal; | 201 return -1; |
205 if (S_ISFIFO(buf.st_mode)) return File::kPipe; | 202 } |
206 if (S_ISREG(buf.st_mode)) return File::kFile; | 203 if (S_ISCHR(buf.st_mode)) { |
| 204 return File::kTerminal; |
| 205 } |
| 206 if (S_ISFIFO(buf.st_mode)) { |
| 207 return File::kPipe; |
| 208 } |
| 209 if (S_ISREG(buf.st_mode)) { |
| 210 return File::kFile; |
| 211 } |
207 return File::kOther; | 212 return File::kOther; |
208 } | 213 } |
209 | 214 |
210 | 215 |
211 intptr_t Socket::GetStdioHandle(intptr_t num) { | 216 intptr_t Socket::GetStdioHandle(intptr_t num) { |
212 return num; | 217 return num; |
213 } | 218 } |
214 | 219 |
215 | 220 |
216 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, | 221 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, |
(...skipping 16 matching lines...) Expand all Loading... |
233 if (status != 0) { | 238 if (status != 0) { |
234 ASSERT(*os_error == NULL); | 239 ASSERT(*os_error == NULL); |
235 *os_error = new OSError(status, | 240 *os_error = new OSError(status, |
236 gai_strerror(status), | 241 gai_strerror(status), |
237 OSError::kGetAddressInfo); | 242 OSError::kGetAddressInfo); |
238 return NULL; | 243 return NULL; |
239 } | 244 } |
240 } | 245 } |
241 intptr_t count = 0; | 246 intptr_t count = 0; |
242 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 247 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
243 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; | 248 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
| 249 count++; |
| 250 } |
244 } | 251 } |
245 intptr_t i = 0; | 252 intptr_t i = 0; |
246 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); | 253 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); |
247 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 254 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
248 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 255 if ((c->ai_family == AF_INET) || (c->ai_family == AF_INET6)) { |
249 addresses->SetAt(i, new SocketAddress(c->ai_addr)); | 256 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
250 i++; | 257 i++; |
251 } | 258 } |
252 } | 259 } |
253 freeaddrinfo(info); | 260 freeaddrinfo(info); |
254 return addresses; | 261 return addresses; |
255 } | 262 } |
256 | 263 |
257 | 264 |
258 bool Socket::ReverseLookup(const RawAddr& addr, | 265 bool Socket::ReverseLookup(const RawAddr& addr, |
(...skipping 22 matching lines...) Expand all Loading... |
281 | 288 |
282 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { | 289 bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
283 int result; | 290 int result; |
284 if (type == SocketAddress::TYPE_IPV4) { | 291 if (type == SocketAddress::TYPE_IPV4) { |
285 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); | 292 result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); |
286 } else { | 293 } else { |
287 ASSERT(type == SocketAddress::TYPE_IPV6); | 294 ASSERT(type == SocketAddress::TYPE_IPV6); |
288 result = NO_RETRY_EXPECTED( | 295 result = NO_RETRY_EXPECTED( |
289 inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); | 296 inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); |
290 } | 297 } |
291 return result == 1; | 298 return (result == 1); |
292 } | 299 } |
293 | 300 |
294 | 301 |
295 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 302 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
296 intptr_t fd; | 303 intptr_t fd; |
297 | 304 |
298 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, | 305 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, |
299 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, | 306 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, |
300 IPPROTO_UDP)); | 307 IPPROTO_UDP)); |
301 if (fd < 0) return -1; | 308 if (fd < 0) { |
| 309 return -1; |
| 310 } |
302 | 311 |
303 if (reuseAddress) { | 312 if (reuseAddress) { |
304 int optval = 1; | 313 int optval = 1; |
305 VOID_NO_RETRY_EXPECTED( | 314 VOID_NO_RETRY_EXPECTED( |
306 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 315 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
307 } | 316 } |
308 | 317 |
309 if (NO_RETRY_EXPECTED( | 318 if (NO_RETRY_EXPECTED( |
310 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 319 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
311 VOID_TEMP_FAILURE_RETRY(close(fd)); | 320 VOID_TEMP_FAILURE_RETRY(close(fd)); |
312 return -1; | 321 return -1; |
313 } | 322 } |
314 return fd; | 323 return fd; |
315 } | 324 } |
316 | 325 |
317 | 326 |
318 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 327 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
319 if (ifa->ifa_addr == NULL) { | 328 if (ifa->ifa_addr == NULL) { |
320 // OpenVPN's virtual device tun0. | 329 // OpenVPN's virtual device tun0. |
321 return false; | 330 return false; |
322 } | 331 } |
323 int family = ifa->ifa_addr->sa_family; | 332 int family = ifa->ifa_addr->sa_family; |
324 if (lookup_family == family) return true; | 333 return ((lookup_family == family) || |
325 if (lookup_family == AF_UNSPEC && | 334 (((lookup_family == AF_UNSPEC) && |
326 (family == AF_INET || family == AF_INET6)) { | 335 ((family == AF_INET) || (family == AF_INET6))))); |
327 return true; | |
328 } | |
329 return false; | |
330 } | 336 } |
331 | 337 |
332 | 338 |
333 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 339 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
334 int type, | 340 int type, |
335 OSError** os_error) { | 341 OSError** os_error) { |
336 struct ifaddrs* ifaddr; | 342 struct ifaddrs* ifaddr; |
337 | 343 |
338 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr)); | 344 int status = NO_RETRY_EXPECTED(getifaddrs(&ifaddr)); |
339 if (status != 0) { | 345 if (status != 0) { |
340 ASSERT(*os_error == NULL); | 346 ASSERT(*os_error == NULL); |
341 *os_error = new OSError(status, | 347 *os_error = new OSError(status, |
342 gai_strerror(status), | 348 gai_strerror(status), |
343 OSError::kGetAddressInfo); | 349 OSError::kGetAddressInfo); |
344 return NULL; | 350 return NULL; |
345 } | 351 } |
346 | 352 |
347 int lookup_family = SocketAddress::FromType(type); | 353 int lookup_family = SocketAddress::FromType(type); |
348 | 354 |
349 intptr_t count = 0; | 355 intptr_t count = 0; |
350 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 356 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
351 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++; | 357 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
| 358 count++; |
| 359 } |
352 } | 360 } |
353 | 361 |
354 AddressList<InterfaceSocketAddress>* addresses = | 362 AddressList<InterfaceSocketAddress>* addresses = |
355 new AddressList<InterfaceSocketAddress>(count); | 363 new AddressList<InterfaceSocketAddress>(count); |
356 int i = 0; | 364 int i = 0; |
357 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 365 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
358 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { | 366 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
359 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); | 367 char* ifa_name = DartUtils::ScopedCopyCString(ifa->ifa_name); |
360 addresses->SetAt(i, new InterfaceSocketAddress( | 368 addresses->SetAt(i, new InterfaceSocketAddress( |
361 ifa->ifa_addr, ifa_name, if_nametoindex(ifa->ifa_name))); | 369 ifa->ifa_addr, ifa_name, if_nametoindex(ifa->ifa_name))); |
362 i++; | 370 i++; |
363 } | 371 } |
364 } | 372 } |
365 freeifaddrs(ifaddr); | 373 freeifaddrs(ifaddr); |
366 return addresses; | 374 return addresses; |
367 } | 375 } |
368 | 376 |
369 | 377 |
370 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 378 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
371 intptr_t backlog, | 379 intptr_t backlog, |
372 bool v6_only) { | 380 bool v6_only) { |
373 intptr_t fd; | 381 intptr_t fd; |
374 | 382 |
375 fd = NO_RETRY_EXPECTED( | 383 fd = NO_RETRY_EXPECTED( |
376 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); | 384 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
377 if (fd < 0) return -1; | 385 if (fd < 0) { |
| 386 return -1; |
| 387 } |
378 | 388 |
379 int optval = 1; | 389 int optval = 1; |
380 VOID_NO_RETRY_EXPECTED( | 390 VOID_NO_RETRY_EXPECTED( |
381 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 391 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
382 | 392 |
383 if (addr.ss.ss_family == AF_INET6) { | 393 if (addr.ss.ss_family == AF_INET6) { |
384 optval = v6_only ? 1 : 0; | 394 optval = v6_only ? 1 : 0; |
385 VOID_NO_RETRY_EXPECTED( | 395 VOID_NO_RETRY_EXPECTED( |
386 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 396 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
387 } | 397 } |
388 | 398 |
389 if (NO_RETRY_EXPECTED( | 399 if (NO_RETRY_EXPECTED( |
390 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 400 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
391 VOID_TEMP_FAILURE_RETRY(close(fd)); | 401 VOID_TEMP_FAILURE_RETRY(close(fd)); |
392 return -1; | 402 return -1; |
393 } | 403 } |
394 | 404 |
395 // Test for invalid socket port 65535 (some browsers disallow it). | 405 // Test for invalid socket port 65535 (some browsers disallow it). |
396 if (SocketAddress::GetAddrPort(addr) == 0 && Socket::GetPort(fd) == 65535) { | 406 if ((SocketAddress::GetAddrPort(addr) == 0) && |
| 407 (Socket::GetPort(fd) == 65535)) { |
397 // Don't close the socket until we have created a new socket, ensuring | 408 // Don't close the socket until we have created a new socket, ensuring |
398 // that we do not get the bad port number again. | 409 // that we do not get the bad port number again. |
399 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 410 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
400 int err = errno; | 411 int err = errno; |
401 VOID_TEMP_FAILURE_RETRY(close(fd)); | 412 VOID_TEMP_FAILURE_RETRY(close(fd)); |
402 errno = err; | 413 errno = err; |
403 return new_fd; | 414 return new_fd; |
404 } | 415 } |
405 | 416 |
406 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 417 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 VOID_TEMP_FAILURE_RETRY(close(fd)); | 465 VOID_TEMP_FAILURE_RETRY(close(fd)); |
455 } | 466 } |
456 | 467 |
457 | 468 |
458 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 469 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
459 int on; | 470 int on; |
460 socklen_t len = sizeof(on); | 471 socklen_t len = sizeof(on); |
461 int err = NO_RETRY_EXPECTED(getsockopt( | 472 int err = NO_RETRY_EXPECTED(getsockopt( |
462 fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<void *>(&on), &len)); | 473 fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<void *>(&on), &len)); |
463 if (err == 0) { | 474 if (err == 0) { |
464 *enabled = on == 1; | 475 *enabled = (on == 1); |
465 } | 476 } |
466 return err == 0; | 477 return (err == 0); |
467 } | 478 } |
468 | 479 |
469 | 480 |
470 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 481 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
471 int on = enabled ? 1 : 0; | 482 int on = enabled ? 1 : 0; |
472 return NO_RETRY_EXPECTED(setsockopt(fd, | 483 return NO_RETRY_EXPECTED(setsockopt(fd, |
473 IPPROTO_TCP, | 484 IPPROTO_TCP, |
474 TCP_NODELAY, | 485 TCP_NODELAY, |
475 reinterpret_cast<char *>(&on), | 486 reinterpret_cast<char *>(&on), |
476 sizeof(on))) == 0; | 487 sizeof(on))) == 0; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 fd, level, optname, reinterpret_cast<char *>(&v), sizeof(v))) == 0; | 537 fd, level, optname, reinterpret_cast<char *>(&v), sizeof(v))) == 0; |
527 } | 538 } |
528 | 539 |
529 | 540 |
530 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 541 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
531 int on; | 542 int on; |
532 socklen_t len = sizeof(on); | 543 socklen_t len = sizeof(on); |
533 int err = NO_RETRY_EXPECTED(getsockopt( | 544 int err = NO_RETRY_EXPECTED(getsockopt( |
534 fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char *>(&on), &len)); | 545 fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char *>(&on), &len)); |
535 if (err == 0) { | 546 if (err == 0) { |
536 *enabled = on == 1; | 547 *enabled = (on == 1); |
537 } | 548 } |
538 return err == 0; | 549 return (err == 0); |
539 } | 550 } |
540 | 551 |
541 | 552 |
542 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 553 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
543 int on = enabled ? 1 : 0; | 554 int on = enabled ? 1 : 0; |
544 return NO_RETRY_EXPECTED(setsockopt(fd, | 555 return NO_RETRY_EXPECTED(setsockopt(fd, |
545 SOL_SOCKET, | 556 SOL_SOCKET, |
546 SO_BROADCAST, | 557 SO_BROADCAST, |
547 reinterpret_cast<char *>(&on), | 558 reinterpret_cast<char *>(&on), |
548 sizeof(on))) == 0; | 559 sizeof(on))) == 0; |
(...skipping 18 matching lines...) Expand all Loading... |
567 mreq.gr_interface = interfaceIndex; | 578 mreq.gr_interface = interfaceIndex; |
568 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 579 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
569 return NO_RETRY_EXPECTED( | 580 return NO_RETRY_EXPECTED( |
570 setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 581 setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
571 } | 582 } |
572 | 583 |
573 } // namespace bin | 584 } // namespace bin |
574 } // namespace dart | 585 } // namespace dart |
575 | 586 |
576 #endif // defined(TARGET_OS_LINUX) | 587 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |