OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/udp/udp_socket_libevent.h" | 5 #include "net/udp/udp_socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 return result; | 458 return result; |
459 } | 459 } |
460 | 460 |
461 int UDPSocketLibevent::SetSocketOptions() { | 461 int UDPSocketLibevent::SetSocketOptions() { |
462 int true_value = 1; | 462 int true_value = 1; |
463 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) { | 463 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) { |
464 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, | 464 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, |
465 sizeof(true_value)); | 465 sizeof(true_value)); |
466 if (rv < 0) | 466 if (rv < 0) |
467 return MapSystemError(errno); | 467 return MapSystemError(errno); |
468 #if defined(SO_REUSEPORT) | 468 } |
469 if (socket_options_ & SOCKET_OPTION_BROADCAST) { | |
470 int rv; | |
471 #if defined(OS_MACOSX) | |
472 // SO_REUSEPORT on OSX permits multiple instances of a program to | |
wtc
2012/09/18 14:40:32
Did you mean "multiple instances of the same progr
ygorshenin1
2012/09/18 14:54:22
I thought "multiple processes" will be better. Tha
| |
473 // each receive UDP multicast or broadcast datagrams destined for | |
474 // the bound port. | |
469 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value, | 475 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value, |
470 sizeof(true_value)); | 476 sizeof(true_value)); |
471 if (rv < 0) | 477 if (rv < 0) |
472 return MapSystemError(errno); | 478 return MapSystemError(errno); |
473 #endif | 479 #endif // defined(OS_MACOSX) |
474 } | 480 rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value, |
475 if (socket_options_ & SOCKET_OPTION_BROADCAST) { | 481 sizeof(true_value)); |
476 int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value, | |
477 sizeof(true_value)); | |
478 if (rv < 0) | 482 if (rv < 0) |
479 return MapSystemError(errno); | 483 return MapSystemError(errno); |
480 } | 484 } |
481 return OK; | 485 return OK; |
482 } | 486 } |
483 | 487 |
484 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { | 488 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { |
485 SockaddrStorage storage; | 489 SockaddrStorage storage; |
486 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 490 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
487 return ERR_UNEXPECTED; | 491 return ERR_UNEXPECTED; |
488 int rv = bind(socket_, storage.addr, storage.addr_len); | 492 int rv = bind(socket_, storage.addr, storage.addr_len); |
489 return rv < 0 ? MapSystemError(errno) : rv; | 493 return rv < 0 ? MapSystemError(errno) : rv; |
490 } | 494 } |
491 | 495 |
492 int UDPSocketLibevent::RandomBind(const IPEndPoint& address) { | 496 int UDPSocketLibevent::RandomBind(const IPEndPoint& address) { |
493 DCHECK(bind_type_ == DatagramSocket::RANDOM_BIND && !rand_int_cb_.is_null()); | 497 DCHECK(bind_type_ == DatagramSocket::RANDOM_BIND && !rand_int_cb_.is_null()); |
494 | 498 |
495 // Construct IPAddressNumber of appropriate size (IPv4 or IPv6) of 0s. | 499 // Construct IPAddressNumber of appropriate size (IPv4 or IPv6) of 0s. |
496 IPAddressNumber ip(address.address().size()); | 500 IPAddressNumber ip(address.address().size()); |
497 | 501 |
498 for (int i = 0; i < kBindRetries; ++i) { | 502 for (int i = 0; i < kBindRetries; ++i) { |
499 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); | 503 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); |
500 if (rv == OK || rv != ERR_ADDRESS_IN_USE) | 504 if (rv == OK || rv != ERR_ADDRESS_IN_USE) |
501 return rv; | 505 return rv; |
502 } | 506 } |
503 return DoBind(IPEndPoint(ip, 0)); | 507 return DoBind(IPEndPoint(ip, 0)); |
504 } | 508 } |
505 | 509 |
506 } // namespace net | 510 } // namespace net |
OLD | NEW |