From: bert hubert Date: Thu, 7 Jan 2016 13:01:47 +0000 (+0100) Subject: turns out we weren't gathering the local address for TCP at all, and for UDP we did... X-Git-Tag: dnsdist-1.0.0-alpha2~119^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6147cd2632a34b3535fe978c8e98873f063b29c;p=thirdparty%2Fpdns.git turns out we weren't gathering the local address for TCP at all, and for UDP we did not do the work for ipfilter except when bound to 0.0.0.0. Now we do it right in the other case too AND gather the port number. --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 83991f7c97..483e3d77c6 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -606,8 +606,6 @@ void startDoResolve(void *p) maxanswersize = min(edo.d_packetsize, g_udpTruncationThreshold); haveEDNS=true; } - ComboAddress local; - listenSocketsAddresses_t::const_iterator lociter; vector ret; vector packet; @@ -661,16 +659,6 @@ void startDoResolve(void *p) if(!dc->d_mdp.d_header.rd) sr.setCacheOnly(); - local.sin4.sin_family = dc->d_remote.sin4.sin_family; - - lociter = g_listenSocketsAddresses.find(dc->d_socket); - if(lociter != g_listenSocketsAddresses.end()) { - local = lociter->second; - } - else { - socklen_t len = local.getSocklen(); - getsockname(dc->d_socket, (sockaddr*)&local, &len); // if this fails, we're ok with it - } // if there is a RecursorLua active, and it 'took' the query in preResolve, we don't launch beginResolve @@ -714,7 +702,7 @@ void startDoResolve(void *p) } - if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) { + if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) { try { res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); } @@ -772,13 +760,13 @@ void startDoResolve(void *p) if(i->d_type == dc->d_mdp.d_qtype && i->d_place == DNSResourceRecord::ANSWER) break; if(i == ret.cend()) - (*t_pdl)->nodata(dc->d_remote,local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); + (*t_pdl)->nodata(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); } else if(res == RCode::NXDomain) - (*t_pdl)->nxdomain(dc->d_remote,local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); + (*t_pdl)->nxdomain(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); - (*t_pdl)->postresolve(dc->d_remote,local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); + (*t_pdl)->postresolve(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer); } } @@ -1057,6 +1045,13 @@ void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var) dc->setSocket(conn->getFD()); // this is the only time a copy is made of the actual fd dc->d_tcp=true; dc->setRemote(&conn->d_remote); + ComboAddress dest; + memset(&dest, 0, sizeof(dest)); + dest.sin4.sin_family = conn->d_remote.sin4.sin_family; + socklen_t len = dest.getSocklen(); + getsockname(conn->getFD(), (sockaddr*)&dest, &len); // if this fails, we're ok with it + dc->setLocal(dest); + if(dc->d_mdp.d_header.qr) { delete dc; g_stats.ignoredCount++; @@ -1216,7 +1211,6 @@ string* doProcessUDPQuestion(const std::string& question, const ComboAddress& fr dc->setSocket(fd); dc->setRemote(&fromaddr); dc->setLocal(destaddr); - dc->d_tcp=false; MT->makeThread(startDoResolve, (void*) dc); // deletes dc return 0; @@ -1274,7 +1268,22 @@ void handleNewUDPQuestion(int fd, FDMultiplexer::funcparam_t& var) HarvestTimestamp(&msgh, &tv); ComboAddress dest; memset(&dest, 0, sizeof(dest)); // this makes sure we igore this address if not returned by recvmsg above - HarvestDestinationAddress(&msgh, &dest); + auto loc = rplookup(g_listenSocketsAddresses, fd); + if(HarvestDestinationAddress(&msgh, &dest)) { + // but.. need to get port too + if(loc) + dest.sin4.sin_port = loc->sin4.sin_port; + } + else { + if(loc) { + dest = *loc; + } + else { + dest.sin4.sin_family = fromaddr.sin4.sin_family; + socklen_t len = dest.getSocklen(); + getsockname(fd, (sockaddr*)&dest, &len); // if this fails, we're ok with it + } + } if(g_weDistributeQueries) distributeAsyncFunction(question, boost::bind(doProcessUDPQuestion, question, fromaddr, dest, tv, fd)); else @@ -1412,7 +1421,6 @@ void makeUDPServerSockets() L<