From: Otto Moerbeek Date: Fri, 6 Jan 2023 14:45:46 +0000 (+0100) Subject: Be more careful saving errno in makeClientSocket() and closesocket() X-Git-Tag: dnsdist-1.8.0-rc1~131^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F12392%2Fhead;p=thirdparty%2Fpdns.git Be more careful saving errno in makeClientSocket() and closesocket() --- diff --git a/pdns/misc.cc b/pdns/misc.cc index 7e5cb07063..cdf86a7d60 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -984,13 +984,16 @@ bool setReceiveSocketErrors(int sock, int af) } // Closes a socket. -int closesocket( int socket ) +int closesocket(int socket) { - int ret=::close(socket); - if(ret < 0 && errno == ECONNRESET) // see ticket 192, odd BSD behaviour + int ret = ::close(socket); + if(ret < 0 && errno == ECONNRESET) { // see ticket 192, odd BSD behaviour return 0; - if(ret < 0) - throw PDNSException("Error closing socket: "+stringerror()); + } + if (ret < 0) { + int err = errno; + throw PDNSException("Error closing socket: " + stringerror(err)); + } return ret; } diff --git a/pdns/recursordist/pdns_recursor.cc b/pdns/recursordist/pdns_recursor.cc index 6e9c4f9ac1..2bc8729958 100644 --- a/pdns/recursordist/pdns_recursor.cc +++ b/pdns/recursordist/pdns_recursor.cc @@ -154,7 +154,8 @@ int UDPClientSocks::makeClientSocket(int family) return ret; } if (ret < 0) { - throw PDNSException("Making a socket for resolver (family = " + std::to_string(family) + "): " + stringerror()); + int err = errno; + throw PDNSException("Making a socket for resolver (family = " + std::to_string(family) + "): " + stringerror(err)); } // The loop below runs the body with [tries-1 tries-2 ... 1]. Last iteration with tries == 1 is special: it uses a kernel @@ -165,7 +166,7 @@ int UDPClientSocks::makeClientSocket(int family) int tries = 2; // hit the reliable kernel random case for OpenBSD immediately (because it will match tries==1 below), using sysctl net.inet.udp.baddynamic to exclude ports #endif ComboAddress sin; - while (--tries) { + while (--tries != 0) { in_port_t port; if (tries == 1) { // last iteration: fall back to kernel 'random' @@ -174,17 +175,20 @@ int UDPClientSocks::makeClientSocket(int family) else { do { port = g_minUdpSourcePort + dns_random(g_maxUdpSourcePort - g_minUdpSourcePort + 1); - } while (g_avoidUdpSourcePorts.count(port)); + } while (g_avoidUdpSourcePorts.count(port) != 0); } sin = pdns::getQueryLocalAddress(family, port); // does htons for us - if (::bind(ret, reinterpret_cast(&sin), sin.getSocklen()) >= 0) + if (::bind(ret, reinterpret_cast(&sin), sin.getSocklen()) >= 0) { break; + } } - if (!tries) { + int err = errno; + + if (tries == 0) { closesocket(ret); - throw PDNSException("Resolver binding to local query client socket on " + sin.toString() + ": " + stringerror()); + throw PDNSException("Resolver binding to local query client socket on " + sin.toString() + ": " + stringerror(err)); } try {