Commit a082c773 authored by Huapeng Zhou's avatar Huapeng Zhou Committed by Facebook GitHub Bot

folly: add freebind to async udp socket

Summary:
This is useful if we want to bind to a nonlocal address or an address that doesn't exist yet.

       IP_FREEBIND (since Linux 2.4)
              If enabled, this boolean option allows binding to an IP
              address that is nonlocal or does not (yet) exist.  This
              permits listening on a socket, without requiring the
              underlying network interface or the specified dynamic IP
              address to be up at the time that the application is
              trying to bind to it.  This option is the per-socket
              equivalent of the ip_nonlocal_bind /proc interface
              described below.

Reviewed By: yfeldblum, mjoras

Differential Revision: D30903919

fbshipit-source-id: 09058cac1c14a4003525b0a7c5381ea301ca28da
parent 83a6ce73
...@@ -140,6 +140,26 @@ void AsyncUDPSocket::init(sa_family_t family, BindOptions bindOptions) { ...@@ -140,6 +140,26 @@ void AsyncUDPSocket::init(sa_family_t family, BindOptions bindOptions) {
} }
} }
if (freeBind_) {
int optname = 0;
#if defined(IP_FREEBIND)
optname = IP_FREEBIND;
#endif
if (!optname) {
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN, "IP_FREEBIND is not supported");
}
// put the socket in free bind mode
int value = 1;
if (netops::setsockopt(
socket, IPPROTO_IP, optname, &value, sizeof(value)) != 0) {
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
"failed to put socket in free bind mode",
errno);
}
}
if (busyPollUs_ > 0) { if (busyPollUs_ > 0) {
int optname = 0; int optname = 0;
#if defined(SO_BUSY_POLL) #if defined(SO_BUSY_POLL)
......
...@@ -336,6 +336,12 @@ class AsyncUDPSocket : public EventHandler { ...@@ -336,6 +336,12 @@ class AsyncUDPSocket : public EventHandler {
return fd_; return fd_;
} }
/**
* Set IP_FREEBIND to allow binding to an address that is nonlocal or doesn't
* exist yet.
*/
virtual void setFreeBind(bool freeBind) { freeBind_ = freeBind; }
/** /**
* Set reuse port mode to call bind() on the same address multiple times * Set reuse port mode to call bind() on the same address multiple times
*/ */
...@@ -551,6 +557,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -551,6 +557,7 @@ class AsyncUDPSocket : public EventHandler {
bool reuseAddr_{false}; bool reuseAddr_{false};
bool reusePort_{false}; bool reusePort_{false};
bool freeBind_{false};
int rcvBuf_{0}; int rcvBuf_{0};
int sndBuf_{0}; int sndBuf_{0};
int busyPollUs_{0}; int busyPollUs_{0};
......
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