From f402f38820edffdf62c94f4eac7cccb1d2d929dc Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Thu, 18 Nov 2021 17:01:32 +0100 Subject: [PATCH] auth,rec: Refactor the setSocket*Buffer functions --- pdns/calidns.cc | 49 +++++++++++++++---------------------------- pdns/dnsreplay.cc | 40 +++++++++++------------------------ pdns/iputils.cc | 24 +++++++++++++++++++++ pdns/iputils.hh | 6 ++++++ pdns/pdns_recursor.cc | 43 ++++++++++++------------------------- 5 files changed, 72 insertions(+), 90 deletions(-) diff --git a/pdns/calidns.cc b/pdns/calidns.cc index 973e530953..89886e3a70 100644 --- a/pdns/calidns.cc +++ b/pdns/calidns.cc @@ -117,36 +117,6 @@ static void* recvThread(const vector>* sockets) return 0; } -static void setSocketBuffer(int fd, int optname, uint32_t size) -{ - uint32_t psize=0; - socklen_t len=sizeof(psize); - - if(!getsockopt(fd, SOL_SOCKET, optname, (char*)&psize, &len) && psize > size) { - if (!g_quiet) { - cerr<<"Not decreasing socket buffer size from "<(dest.sin4.sin_family, SOCK_DGRAM); // sock->connect(dest); - setSocketSendBuffer(sock->getHandle(), 2000000); - setSocketReceiveBuffer(sock->getHandle(), 2000000); + try { + setSocketSendBuffer(sock->getHandle(), 2000000); + } + catch (const std::exception& e) { + if (!g_quiet) { + cerr<getHandle(), 2000000); + } + catch (const std::exception& e) { + if (!g_quiet) { + cerr< size) { - cerr<<"Not decreasing socket buffer size from "<().empty()) s_socket->bind(ComboAddress(g_vm["source-ip"].as(), g_vm["source-port"].as())); - setSocketReceiveBuffer(s_socket->getHandle(), 2000000); - setSocketSendBuffer(s_socket->getHandle(), 2000000); + try { + setSocketReceiveBuffer(s_socket->getHandle(), 2000000); + } + catch (const std::exception& e) { + cerr<getHandle(), 2000000); + } + catch (const std::exception& e) { + cerr<(), g_vm["target-port"].as()); diff --git a/pdns/iputils.cc b/pdns/iputils.cc index 05eed1245b..1c042289a9 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -519,3 +519,27 @@ ComboAddress parseIPAndPort(const std::string& input, uint16_t port) return ComboAddress(input, port); } } + +void setSocketBuffer(int fd, int optname, uint32_t size) +{ + uint32_t psize = 0; + socklen_t len = sizeof(psize); + + if (!getsockopt(fd, SOL_SOCKET, optname, &psize, &len) && psize > size) { + throw std::runtime_error("Not decreasing socket buffer size from " + std::to_string(psize) + " to " + std::to_string(size)); + } + + if (setsockopt(fd, SOL_SOCKET, optname, &size, sizeof(size)) < 0) { + throw std::runtime_error("Unable to raise socket buffer size to " + std::to_string(size) + ": " + stringerror()); + } +} + +void setSocketReceiveBuffer(int fd, uint32_t size) +{ + setSocketBuffer(fd, SO_RCVBUF, size); +} + +void setSocketSendBuffer(int fd, uint32_t size) +{ + setSocketBuffer(fd, SO_SNDBUF, size); +} diff --git a/pdns/iputils.hh b/pdns/iputils.hh index b36f0e2772..23b200b1a5 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1649,3 +1649,9 @@ bool isTCPSocketUsable(int sock); extern template class NetmaskTree; ComboAddress parseIPAndPort(const std::string& input, uint16_t port); + +/* These functions throw if the value was already set to a higher value, + or on error */ +void setSocketBuffer(int fd, int optname, uint32_t size); +void setSocketReceiveBuffer(int fd, uint32_t size); +void setSocketSendBuffer(int fd, uint32_t size); diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index f3ac0a08dc..362be3973b 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -597,34 +597,6 @@ PacketBuffer GenUDPQueryResponse(const ComboAddress& dest, const string& query) static void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t&); -static void setSocketBuffer(int fd, int optname, uint32_t size) -{ - uint32_t psize=0; - socklen_t len=sizeof(psize); - - if(!getsockopt(fd, SOL_SOCKET, optname, (char*)&psize, &len) && psize > size) { - g_log<& tcp throw PDNSException("Binding TCP server socket for "+ st.host +": "+stringerror()); setNonBlocking(fd); - setSocketSendBuffer(fd, 65000); + try { + setSocketSendBuffer(fd, 65000); + } + catch (const std::exception& e) { + g_log<