From: Bert Hubert Date: Thu, 2 Mar 2006 20:19:03 +0000 (+0000) Subject: teach recursor to shuffle, by moving shuffle logic away from dnspacket.cc and into... X-Git-Tag: pdns-2.9.20~21 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e67e250fa3999885da051b44340d0bd191770d78;p=pdns teach recursor to shuffle, by moving shuffle logic away from dnspacket.cc and into misc.cc silence an incorrect warning in dnspacket.cc This closes ticket #54 git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@567 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index a20fb7f58..ba4c11c10 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -91,14 +91,16 @@ pdns_recursor_SOURCES=syncres.cc resolver.hh misc.cc unix_utility.cc qtype.cc \ logger.cc statbag.cc dnspacket.cc arguments.cc lwres.cc pdns_recursor.cc lwres.hh \ mtasker.hh sillyrecords.cc syncres.hh recursor_cache.cc recursor_cache.hh dnsparser.cc \ dnswriter.cc dnswriter.hh dnsrecords.cc dnsrecords.hh rcpgenerator.cc rcpgenerator.hh \ -base64.cc base64.hh zoneparser-tng.cc zoneparser-tng.hh +base64.cc base64.hh zoneparser-tng.cc zoneparser-tng.hh #../modules/gmysqlbackend/smysql.cc pdns_recursor_LDFLAGS= pdns_recursor_LDADD= -pdns_control_SOURCES=dynloader.cc dynmessenger.cc arguments.cc logger.cc statbag.cc misc.cc unix_utility.cc +pdns_control_SOURCES=dynloader.cc dynmessenger.cc arguments.cc logger.cc statbag.cc \ +misc.cc unix_utility.cc qtype.cc + pdns_control_INCLUDES=path.hh pdns_control_LDFLAGS=@THREADFLAGS@ diff --git a/pdns/dnspacket.cc b/pdns/dnspacket.cc index 3ec0499dd..1023c2d32 100644 --- a/pdns/dnspacket.cc +++ b/pdns/dnspacket.cc @@ -544,7 +544,7 @@ void DNSPacket::addSOARecord(const string &domain, const string & content, uint3 uint32_t *i_p=(uint32_t *)piece5; - uint32_t soaoffset; + uint32_t soaoffset=0; if(soadata.serial && (soaoffset=arg().asNum("soa-serial-offset"))) if(soadata.serial::iterator first, second; - for(first=rrs.begin();first!=rrs.end();++first) - if(first->d_place==DNSResourceRecord::ANSWER && first->qtype.getCode() != QType::CNAME) // CNAME must come first - break; - for(second=first;second!=rrs.end();++second) - if(second->d_place!=DNSResourceRecord::ANSWER) - break; - - if(second-first>1) - random_shuffle(first,second); - - // now shuffle the additional records - for(first=second;first!=rrs.end();++first) - if(first->d_place==DNSResourceRecord::ADDITIONAL && first->qtype.getCode() != QType::CNAME) // CNAME must come first - break; - for(second=first;second!=rrs.end();++second) - if(second->d_place!=DNSResourceRecord::ADDITIONAL) - break; - - if(second-first>1) - random_shuffle(first,second); + shuffle(rrs); } d_wrapped=true; diff --git a/pdns/misc.cc b/pdns/misc.cc index d3cab3fc1..dd7932832 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -350,3 +350,31 @@ string makeHexDump(const string& str) } + +// shuffle, maintaining some semblance of order +void shuffle(vector& rrs) +{ + vector::iterator first, second; + for(first=rrs.begin();first!=rrs.end();++first) + if(first->d_place==DNSResourceRecord::ANSWER && first->qtype.getCode() != QType::CNAME) // CNAME must come first + break; + for(second=first;second!=rrs.end();++second) + if(second->d_place!=DNSResourceRecord::ANSWER) + break; + + if(second-first>1) + random_shuffle(first,second); + + // now shuffle the additional records + for(first=second;first!=rrs.end();++first) + if(first->d_place==DNSResourceRecord::ADDITIONAL && first->qtype.getCode() != QType::CNAME) // CNAME must come first + break; + for(second=first;second!=rrs.end();++second) + if(second->d_place!=DNSResourceRecord::ADDITIONAL) + break; + + if(second-first>1) + random_shuffle(first,second); + + // we don't shuffle the rest +} diff --git a/pdns/misc.hh b/pdns/misc.hh index 04b65f319..12f7b2f82 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -22,7 +22,7 @@ #include "utility.hh" - +#include "dns.hh" #ifndef WIN32 # include # include @@ -41,6 +41,8 @@ #include #include #include +#include + using namespace std; bool chopOff(string &domain); bool endsOn(const string &domain, const string &suffix); @@ -228,5 +230,5 @@ inline void unixDie(const string &why) } string makeHexDump(const string& str); - +void shuffle(vector& rrs); #endif diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 5f4b81dfd..00dfefaf7 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -266,6 +266,7 @@ void primeHints(void) RC.replace("", QType(QType::NS), nsset); // and stuff in the cache } + void startDoResolve(void *p) { try { @@ -305,6 +306,7 @@ void startDoResolve(void *p) else { pw.getHeader()->rcode=res; if(ret.size()) { + shuffle(ret); for(vector::const_iterator i=ret.begin();i!=ret.end();++i) { pw.startRecord(i->qname, i->qtype.getCode(), i->ttl, 1, (DNSPacketWriter::Place)i->d_place); shared_ptr drc(DNSRecordContent::mastermake(i->qtype.getCode(), 1, i->content));