Chromium Code Reviews| 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) && defined(SO_REUSEPORT) | |
|
Sergey Ulanov
2012/09/14 19:38:45
You don't need defined(SO_REUSEPORT) here - we exp
ygorshenin1
2012/09/18 11:34:05
Done.
| |
| 469 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value, | 472 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value, |
|
Sergey Ulanov
2012/09/14 19:38:45
Please add comments here to explain why we need th
ygorshenin1
2012/09/18 11:34:05
Done.
| |
| 470 sizeof(true_value)); | 473 sizeof(true_value)); |
| 471 if (rv < 0) | 474 if (rv < 0) |
| 472 return MapSystemError(errno); | 475 return MapSystemError(errno); |
| 473 #endif | 476 #endif // defined(OS_MACOSX) && defined(SO_REUSEPORT) |
| 474 } | 477 rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value, |
| 475 if (socket_options_ & SOCKET_OPTION_BROADCAST) { | 478 sizeof(true_value)); |
| 476 int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value, | |
| 477 sizeof(true_value)); | |
| 478 if (rv < 0) | 479 if (rv < 0) |
| 479 return MapSystemError(errno); | 480 return MapSystemError(errno); |
| 480 } | 481 } |
| 481 return OK; | 482 return OK; |
| 482 } | 483 } |
| 483 | 484 |
| 484 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { | 485 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { |
| 485 SockaddrStorage storage; | 486 SockaddrStorage storage; |
| 486 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 487 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 487 return ERR_UNEXPECTED; | 488 return ERR_UNEXPECTED; |
| 488 int rv = bind(socket_, storage.addr, storage.addr_len); | 489 int rv = bind(socket_, storage.addr, storage.addr_len); |
| 489 return rv < 0 ? MapSystemError(errno) : rv; | 490 return rv < 0 ? MapSystemError(errno) : rv; |
| 490 } | 491 } |
| 491 | 492 |
| 492 int UDPSocketLibevent::RandomBind(const IPEndPoint& address) { | 493 int UDPSocketLibevent::RandomBind(const IPEndPoint& address) { |
| 493 DCHECK(bind_type_ == DatagramSocket::RANDOM_BIND && !rand_int_cb_.is_null()); | 494 DCHECK(bind_type_ == DatagramSocket::RANDOM_BIND && !rand_int_cb_.is_null()); |
| 494 | 495 |
| 495 // Construct IPAddressNumber of appropriate size (IPv4 or IPv6) of 0s. | 496 // Construct IPAddressNumber of appropriate size (IPv4 or IPv6) of 0s. |
| 496 IPAddressNumber ip(address.address().size()); | 497 IPAddressNumber ip(address.address().size()); |
| 497 | 498 |
| 498 for (int i = 0; i < kBindRetries; ++i) { | 499 for (int i = 0; i < kBindRetries; ++i) { |
| 499 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); | 500 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); |
| 500 if (rv == OK || rv != ERR_ADDRESS_IN_USE) | 501 if (rv == OK || rv != ERR_ADDRESS_IN_USE) |
| 501 return rv; | 502 return rv; |
| 502 } | 503 } |
| 503 return DoBind(IPEndPoint(ip, 0)); | 504 return DoBind(IPEndPoint(ip, 0)); |
| 504 } | 505 } |
| 505 | 506 |
| 506 } // namespace net | 507 } // namespace net |
| OLD | NEW |