dnslabeltext.cc \
dnsname.cc dnsname.hh \
dumresp.cc \
+ iputils.cc iputils.hh \
logger.cc \
misc.cc misc.hh \
statbag.cc \
}
if (cs->reuseport) {
-#ifdef SO_REUSEPORT
- SSetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 1);
-#else
- if (warn) {
- /* no need to warn again if configured but support is not available, we already did for UDP */
- warnlog("SO_REUSEPORT has been configured on local address '%s' but is not supported", cs->local.toStringWithPort());
+ if (!setReusePort(fd)) {
+ if (warn) {
+ /* no need to warn again if configured but support is not available, we already did for UDP */
+ warnlog("SO_REUSEPORT has been configured on local address '%s' but is not supported", cs->local.toStringWithPort());
+ }
}
-#endif
}
/* Only set this on IPv4 UDP sockets.
static void tcpAcceptor(const ComboAddress local)
{
Socket tcpSocket(local.sin4.sin_family, SOCK_STREAM);
-#ifdef SO_REUSEPORT
- int one=1;
- if(setsockopt(tcpSocket.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
- unixDie("setsockopt for REUSEPORT");
-#endif
-
+ setReusePort(tcpSocket.getHandle());
tcpSocket.bind(local);
tcpSocket.listen(1024);
}
Socket s(local.sin4.sin_family, SOCK_DGRAM);
-#ifdef SO_REUSEPORT
- int one=1;
- if(setsockopt(s.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
- unixDie("setsockopt for REUSEPORT");
-#endif
-
+ setReusePort(s.getHandle());
s.bind(local);
cout<<"Bound to UDP "<<local.toStringWithPort()<<endl;
#endif /* defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) */
}
+bool setReusePort(int sockfd)
+{
+#if defined(SO_REUSEPORT_LB)
+ try {
+ SSetsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT_LB, 1);
+ return true;
+ }
+ catch (const std::exception& e) {
+ return false;
+ }
+#elif defined(SO_REUSEPORT)
+ try {
+ SSetsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, 1);
+ return true;
+ }
+ catch (const std::exception& e) {
+ return false;
+ }
+#endif
+ return false;
+}
+
bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv)
{
#ifdef SO_TIMESTAMP
int SListen(int sockfd, int limit);
int SSetsockopt(int sockfd, int level, int opname, int value);
void setSocketIgnorePMTU(int sockfd);
+bool setReusePort(int sockfd);
#if defined(IP_PKTINFO)
#define GEN_IP_PKTINFO IP_PKTINFO
}
}
-#ifdef SO_REUSEPORT
- if( d_can_reuseport )
- if( setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) )
- d_can_reuseport = false;
-#endif
+ if (d_can_reuseport) {
+ if (!setReusePort(s)) {
+ d_can_reuseport = false;
+ }
+ }
if( ::arg().mustDo("non-local-bind") )
Utility::setBindAny(locala.sin4.sin_family, s);
UDPNameserver::UDPNameserver( bool additional_socket )
{
-#ifdef SO_REUSEPORT
d_can_reuseport = ::arg().mustDo("reuseport");
-#endif
// Are we the main socket (false) or a rebinding using SO_REUSEPORT ?
d_additional_socket = additional_socket;
bool receive(DNSPacket& packet, std::string& buffer); //!< call this in a while or for(;;) loop to get packets
void send(DNSPacket&); //!< send a DNSPacket. Will call DNSPacket::truncate() if over 512 bytes
inline bool canReusePort() {
-#ifdef SO_REUSEPORT
return d_can_reuseport;
-#else
- return false;
-#endif
};
private:
bool d_additional_socket;
-#ifdef SO_REUSEPORT
- bool d_can_reuseport;
-#endif
+ bool d_can_reuseport{false};
vector<int> d_sockets;
void bindAddresses();
vector<pollfd> d_rfds;
if( ::arg().mustDo("non-local-bind") )
Utility::setBindAny(AF_INET, fd);
-#ifdef SO_REUSEPORT
- if(g_reusePort) {
- if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &tmp, sizeof(tmp)) < 0)
- throw PDNSException("SO_REUSEPORT: "+stringerror());
- }
+ if (g_reusePort) {
+#if defined(SO_REUSEPORT_LB)
+ try {
+ SSetsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, 1);
+ }
+ catch (const std::exception& e) {
+ throw PDNSException(std::string("SO_REUSEPORT_LB: ") + e.what());
+ }
+#elif defined(SO_REUSEPORT)
+ try {
+ SSetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 1);
+ }
+ catch (const std::exception& e) {
+ throw PDNSException(std::string("SO_REUSEPORT: ") + e.what());
+ }
#endif
+ }
if (::arg().asNum("tcp-fast-open") > 0) {
#ifdef TCP_FASTOPEN
sin.sin4.sin_port = htons(st.port);
-#ifdef SO_REUSEPORT
- if(g_reusePort) {
- if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
- throw PDNSException("SO_REUSEPORT: "+stringerror());
- }
+ if (g_reusePort) {
+#if defined(SO_REUSEPORT_LB)
+ try {
+ SSetsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, 1);
+ }
+ catch (const std::exception& e) {
+ throw PDNSException(std::string("SO_REUSEPORT_LB: ") + e.what());
+ }
+#elif defined(SO_REUSEPORT)
+ try {
+ SSetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 1);
+ }
+ catch (const std::exception& e) {
+ throw PDNSException(std::string("SO_REUSEPORT: ") + e.what());
+ }
#endif
+ }
if (sin.isIPv4()) {
try {