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,
validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::PRE_BIND),
SocketOptionKey::ApplyPos::PRE_BIND);
#ifdef FOLLY_HAVE_BIND_OPTIONS_IFNAME
AsyncUDPSocket::BindOptions bindOptions;
bindOptions.ifName = ifName;
socket_->bind(addy, bindOptions);
#else
(void)ifName;
socket_->bind(addy);
#endif
socket_->applyOptions(
validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::POST_BIND),
......
......@@ -207,22 +207,23 @@ void AsyncUDPSocket::bind(
const folly::SocketAddress& address, BindOptions bindOptions) {
init(address.getFamily(), bindOptions);
// bind the socket to the interface
#ifdef FOLLY_HAVE_BIND_OPTIONS_IFNAME
if (!bindOptions.ifName.empty() &&
netops::setsockopt(
fd_,
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);
}
{
// bind the socket to the interface
int optname = 0;
#if defined(SO_BINDTODEVICE)
optname = SO_BINDTODEVICE;
#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
sockaddr_storage addrStorage;
......
......@@ -30,10 +30,6 @@
#include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h>
#if defined(__linux__) && !FOLLY_MOBILE
#define FOLLY_HAVE_BIND_OPTIONS_IFNAME
#endif
namespace folly {
/**
......@@ -177,9 +173,7 @@ class AsyncUDPSocket : public EventHandler {
BindOptions() noexcept {}
// Whether IPV6_ONLY should be set on the socket.
bool bindV6Only{true};
#ifdef FOLLY_HAVE_BIND_OPTIONS_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