Commit cf5906ec authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

fewer bind-to-device conditions in AsyncUDPSocket

Differential Revision: D28163554

fbshipit-source-id: 2167a39e88f7aa6053dea5119a30947d2f3304f3
parent b0a01afc
...@@ -127,14 +127,9 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback, ...@@ -127,14 +127,9 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback,
validateSocketOptions( validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::PRE_BIND), options, addy.getFamily(), SocketOptionKey::ApplyPos::PRE_BIND),
SocketOptionKey::ApplyPos::PRE_BIND); SocketOptionKey::ApplyPos::PRE_BIND);
#ifdef FOLLY_HAVE_BIND_OPTIONS_IFNAME
AsyncUDPSocket::BindOptions bindOptions; AsyncUDPSocket::BindOptions bindOptions;
bindOptions.ifName = ifName; bindOptions.ifName = ifName;
socket_->bind(addy, bindOptions); socket_->bind(addy, bindOptions);
#else
(void)ifName;
socket_->bind(addy);
#endif
socket_->applyOptions( socket_->applyOptions(
validateSocketOptions( validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::POST_BIND), options, addy.getFamily(), SocketOptionKey::ApplyPos::POST_BIND),
......
...@@ -207,22 +207,23 @@ void AsyncUDPSocket::bind( ...@@ -207,22 +207,23 @@ void AsyncUDPSocket::bind(
const folly::SocketAddress& address, BindOptions bindOptions) { const folly::SocketAddress& address, BindOptions bindOptions) {
init(address.getFamily(), bindOptions); init(address.getFamily(), bindOptions);
// bind the socket to the interface {
#ifdef FOLLY_HAVE_BIND_OPTIONS_IFNAME // bind the socket to the interface
if (!bindOptions.ifName.empty() && int optname = 0;
netops::setsockopt( #if defined(SO_BINDTODEVICE)
fd_, optname = SO_BINDTODEVICE;
SOL_SOCKET,
SO_BINDTODEVICE,
bindOptions.ifName.c_str(),
bindOptions.ifName.length())) {
auto errnoCopy = errno;
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
"failed to bind to device: " + bindOptions.ifName,
errnoCopy);
}
#endif #endif
auto& ifName = bindOptions.ifName;
if (optname && !ifName.empty() &&
netops::setsockopt(
fd_, SOL_SOCKET, optname, ifName.data(), ifName.length())) {
auto errnoCopy = errno;
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
"failed to bind to device: " + ifName,
errnoCopy);
}
}
// bind to the address // bind to the address
sockaddr_storage addrStorage; sockaddr_storage addrStorage;
......
...@@ -30,10 +30,6 @@ ...@@ -30,10 +30,6 @@
#include <folly/net/NetOps.h> #include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h> #include <folly/net/NetworkSocket.h>
#if defined(__linux__) && !FOLLY_MOBILE
#define FOLLY_HAVE_BIND_OPTIONS_IFNAME
#endif
namespace folly { namespace folly {
/** /**
...@@ -177,9 +173,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -177,9 +173,7 @@ class AsyncUDPSocket : public EventHandler {
BindOptions() noexcept {} BindOptions() noexcept {}
// Whether IPV6_ONLY should be set on the socket. // Whether IPV6_ONLY should be set on the socket.
bool bindV6Only{true}; bool bindV6Only{true};
#ifdef FOLLY_HAVE_BIND_OPTIONS_IFNAME
std::string ifName; std::string ifName;
#endif
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment