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 <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { | 300 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
301 addresses->SetAt(i, new InterfaceSocketAddress( | 301 addresses->SetAt(i, new InterfaceSocketAddress( |
302 ifa->ifa_addr, strdup(ifa->ifa_name))); | 302 ifa->ifa_addr, strdup(ifa->ifa_name))); |
303 i++; | 303 i++; |
304 } | 304 } |
305 } | 305 } |
306 freeifaddrs(ifaddr); | 306 freeifaddrs(ifaddr); |
307 return addresses; | 307 return addresses; |
308 } | 308 } |
309 | 309 |
| 310 #ifndef SO_REUSEPORT |
| 311 #define SO_REUSEPORT 15 |
| 312 #endif // SO_REUSEPORT |
310 | 313 |
311 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 314 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
312 intptr_t port, | 315 intptr_t port, |
313 intptr_t backlog, | 316 intptr_t backlog, |
314 bool v6_only) { | 317 bool v6_only, |
| 318 bool reuse_port) { |
315 intptr_t fd; | 319 intptr_t fd; |
316 | 320 |
317 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 321 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
318 if (fd < 0) return -1; | 322 if (fd < 0) return -1; |
319 | 323 |
320 FDUtils::SetCloseOnExec(fd); | 324 FDUtils::SetCloseOnExec(fd); |
321 | 325 |
322 int optval = 1; | 326 int optval = 1; |
| 327 |
| 328 if (reuse_port) { |
| 329 if (TEMP_FAILURE_RETRY(setsockopt(fd, |
| 330 SOL_SOCKET, |
| 331 SO_REUSEPORT, |
| 332 &optval, |
| 333 sizeof(optval))) != 0) { |
| 334 int e = errno; |
| 335 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 336 errno = e; |
| 337 return -1; |
| 338 } |
| 339 } else { |
| 340 optval = 0; |
| 341 VOID_TEMP_FAILURE_RETRY(setsockopt(fd, |
| 342 SOL_SOCKET, |
| 343 SO_REUSEPORT, |
| 344 &optval, |
| 345 sizeof(optval))); |
| 346 } |
| 347 |
| 348 optval = 1; |
323 TEMP_FAILURE_RETRY( | 349 TEMP_FAILURE_RETRY( |
324 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 350 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
325 | 351 |
326 if (addr.ss.ss_family == AF_INET6) { | 352 if (addr.ss.ss_family == AF_INET6) { |
327 optval = v6_only ? 1 : 0; | 353 optval = v6_only ? 1 : 0; |
328 TEMP_FAILURE_RETRY( | 354 TEMP_FAILURE_RETRY( |
329 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 355 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
330 } | 356 } |
331 | 357 |
332 SocketAddress::SetAddrPort(&addr, port); | 358 SocketAddress::SetAddrPort(&addr, port); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 IPPROTO_TCP, | 442 IPPROTO_TCP, |
417 TCP_NODELAY, | 443 TCP_NODELAY, |
418 reinterpret_cast<char *>(&on), | 444 reinterpret_cast<char *>(&on), |
419 sizeof(on))) == 0; | 445 sizeof(on))) == 0; |
420 } | 446 } |
421 | 447 |
422 } // namespace bin | 448 } // namespace bin |
423 } // namespace dart | 449 } // namespace dart |
424 | 450 |
425 #endif // defined(TARGET_OS_LINUX) | 451 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |