From: Remi Gacogne Date: Mon, 5 Mar 2018 09:44:41 +0000 (+0100) Subject: dnsdist: If at least one (re-)connection failed, close all sockets X-Git-Tag: dnsdist-1.3.0~35^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38069e7ea3ad11b1cc055469cf3378531f2e7239;p=pdns dnsdist: If at least one (re-)connection failed, close all sockets --- diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 5061e6336..71698f21a 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -384,12 +384,12 @@ static bool sendUDPResponse(int origFD, char* response, uint16_t responseLen, in } -static int pickBackendFD(DownstreamState* state) +static int pickBackendSocketForSending(DownstreamState* state) { return state->fds[state->fdOffset++ % state->fds.size()]; } -static int selectBackendFD(const std::shared_ptr& state) +static int pickBackendSocketForReceiving(const std::shared_ptr& state) { if (state->fds.size() == 1) { return state->fds[0]; @@ -431,7 +431,7 @@ try { dnsheader* dh = reinterpret_cast(packet); bool outstandingDecreased = false; try { - int fd = selectBackendFD(dss); + int fd = pickBackendSocketForReceiving(dss); ssize_t got = recv(fd, packet, sizeof(packet), 0); char * response = packet; size_t responseSize = sizeof(packet); @@ -589,6 +589,20 @@ void DownstreamState::reconnect() } catch(const std::runtime_error& error) { infolog("Error connecting to new server with address %s: %s", remote.toStringWithPort(), error.what()); + connected = false; + break; + } + } + } + + /* if at least one (re-)connection failed, close all sockets */ + if (!connected) { + for (auto& fd : fds) { + if (fd != -1) { + /* shutdown() is needed to wake up recv() in the responderThread */ + shutdown(fd, SHUT_RDWR); + close(fd); + fd = -1; } } } @@ -1498,7 +1512,7 @@ static void processUDPQuery(ClientState& cs, LocalHolders& holders, const struct dh->id = idOffset; - int fd = pickBackendFD(ss); + int fd = pickBackendSocketForSending(ss); ssize_t ret = udpClientSendRequestToBackend(ss, fd, query, dq.len); if(ret < 0) {