diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 52f18d595763..27b4b81d1130 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -537,6 +537,7 @@ dumresp_SOURCES = \ dnslabeltext.cc \ dnsname.cc dnsname.hh \ dumresp.cc \ + iputils.cc iputils.hh \ logger.cc \ misc.cc misc.hh \ statbag.cc \ diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 610387ca7ae5..8bf331ca365e 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -1839,14 +1839,12 @@ static void setUpLocalBind(std::unique_ptr& cs) } 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. diff --git a/pdns/dumresp.cc b/pdns/dumresp.cc index 5d457dc47f6f..96565a025ef5 100644 --- a/pdns/dumresp.cc +++ b/pdns/dumresp.cc @@ -105,12 +105,7 @@ catch(const std::exception& e) { 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); @@ -187,12 +182,7 @@ try } 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 "< d_sockets; void bindAddresses(); vector d_rfds; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 29e0c05c6be6..c2cab50064bc 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -2905,12 +2905,23 @@ static void makeTCPServerSockets(deferredAdd_t& deferredAdds, std::set& tcp 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 @@ -2998,12 +3009,23 @@ static void makeUDPServerSockets(deferredAdd_t& deferredAdds) 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 {