Chromium Code Reviews| Index: net/udp/udp_socket_libevent.cc |
| diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc |
| index bb1cf91ede1fac18c855dca63eb4b12d701a9244..3782db3c24b5bcacd8ed01af2a39270357055d3d 100644 |
| --- a/net/udp/udp_socket_libevent.cc |
| +++ b/net/udp/udp_socket_libevent.cc |
| @@ -41,6 +41,7 @@ UDPSocketLibevent::UDPSocketLibevent( |
| net::NetLog* net_log, |
| const net::NetLog::Source& source) |
| : socket_(kInvalidSocket), |
| + socket_options_(0), |
| bind_type_(bind_type), |
| rand_int_cb_(rand_int_cb), |
| read_watcher_(this), |
| @@ -251,6 +252,9 @@ int UDPSocketLibevent::Bind(const IPEndPoint& address) { |
| int rv = CreateSocket(address); |
| if (rv < 0) |
| return rv; |
| + rv = SetSocketOptions(); |
| + if (rv < 0) |
| + return rv; |
| rv = DoBind(address); |
| if (rv < 0) |
| return rv; |
| @@ -274,6 +278,20 @@ bool UDPSocketLibevent::SetSendBufferSize(int32 size) { |
| return rv == 0; |
| } |
| +void UDPSocketLibevent::AllowAddressReuse() { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!is_connected()); |
| + |
| + socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS; |
| +} |
| + |
| +void UDPSocketLibevent::AllowBroadcast() { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!is_connected()); |
| + |
| + socket_options_ |= SOCKET_OPTION_BROADCAST; |
| +} |
| + |
| void UDPSocketLibevent::DoReadCallback(int rv) { |
| DCHECK_NE(rv, ERR_IO_PENDING); |
| DCHECK(!read_callback_.is_null()); |
| @@ -430,6 +448,29 @@ int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, |
| return result; |
| } |
| +int UDPSocketLibevent::SetSocketOptions() { |
| + int true_value = 1; |
| + if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) { |
| + int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, |
| + sizeof(true_value)); |
| + if (rv < 0) |
| + return MapSystemError(errno); |
| +#if defined(SO_REUSEPORT) |
| + rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value, |
|
Sergey Ulanov
2012/09/12 19:13:10
Why do we really need this option here? It seems l
ygorshenin1
2012/09/13 18:29:16
Thanks, Sergey, you're right. SO_REUSEPORT is used
|
| + sizeof(true_value)); |
| + if (rv < 0) |
| + return MapSystemError(errno); |
| +#endif |
| + } |
| + if (socket_options_ & SOCKET_OPTION_BROADCAST) { |
| + int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value, |
| + sizeof(true_value)); |
| + if (rv < 0) |
| + return MapSystemError(errno); |
| + } |
| + return OK; |
| +} |
| + |
| int UDPSocketLibevent::DoBind(const IPEndPoint& address) { |
| SockaddrStorage storage; |
| if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |