From: Kees Monshouwer Date: Fri, 17 Apr 2015 07:23:39 +0000 (+0000) Subject: switch from getLabelFromContent() to DNSName() X-Git-Tag: dnsdist-1.0.0-alpha1~248^2~88^2~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d926c0da2c4631a6c357a5a6db1825b9b7962906;p=thirdparty%2Fpdns.git switch from getLabelFromContent() to DNSName() --- diff --git a/modules/remotebackend/Makefile.am b/modules/remotebackend/Makefile.am index 34f0468471..4f832d44df 100644 --- a/modules/remotebackend/Makefile.am +++ b/modules/remotebackend/Makefile.am @@ -98,6 +98,7 @@ libtestremotebackend_la_SOURCES = \ ../../pdns/base64.cc \ ../../pdns/dnsbackend.hh ../../pdns/dnsbackend.cc \ ../../pdns/dnslabeltext.cc \ + ../../pdns/dnsname.cc ../../pdns/dnsname.hh \ ../../pdns/dnspacket.cc \ ../../pdns/dnsparser.cc \ ../../pdns/dnsrecords.cc \ diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index 3aa0a0ed82..275a10077a 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -24,7 +24,7 @@ dns_random.o pubsuffix.o ext/polarssl/library/aes.o ext/polarssl/library/base64. lua-pdns.o lua-recursor.o randomhelper.o recpacketcache.o dns.o \ reczones.o base32.o nsecrecords.o json.o ws-recursor.o ws-api.o \ version.o responsestats.o webserver.o ext/yahttp/yahttp/reqresp.o ext/yahttp/yahttp/router.o \ -rec-carbon.o secpoll-recursor.o lua-iputils.o iputils.o +rec-carbon.o secpoll-recursor.o lua-iputils.o iputils.o dnsname.o REC_CONTROL_OBJECTS=rec_channel.o rec_control.o arguments.o misc.o \ unix_utility.o logger.o qtype.o diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 33dd7bba63..9a3571d594 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -138,6 +138,7 @@ pdns_server_SOURCES = \ dns_random.cc dns_random.hh \ dnsbackend.cc dnsbackend.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnspacket.cc dnspacket.hh \ dnsparser.cc \ dnsproxy.cc dnsproxy.hh \ @@ -255,6 +256,7 @@ pdnssec_SOURCES = \ dnsbackend.cc \ dns.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnspacket.cc \ dnsparser.cc dnsparser.hh \ dns_random.cc \ @@ -342,6 +344,7 @@ zone2sql_SOURCES = \ bindparserclasses.hh \ dns.cc \ dns_random.cc \ + dnsname.cc dnsname.hh \ dnslabeltext.cc \ dnsparser.cc \ dnsrecords.cc \ @@ -369,6 +372,7 @@ zone2json_SOURCES = \ bindparser.yy \ bindparserclasses.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc \ dnsrecords.cc \ dnswriter.cc \ @@ -402,6 +406,7 @@ zone2ldap_SOURCES = \ bindparser.yy \ bindparserclasses.hh \ dns_random.cc \ + dnsname.cc dnsname.hh \ dnslabeltext.cc \ dnsparser.cc \ dnsrecords.cc \ @@ -433,6 +438,7 @@ zone2lmdb_SOURCES = \ dns.cc \ dns_random.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc \ dnsrecords.cc \ dnswriter.cc \ @@ -456,6 +462,7 @@ sdig_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnswriter.cc dnswriter.hh \ @@ -477,6 +484,7 @@ calidns_SOURCES = \ base64.cc base64.hh \ calidns.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnswriter.cc dnswriter.hh \ @@ -507,6 +515,7 @@ saxfr_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnssecinfra.cc \ @@ -533,6 +542,7 @@ dnstcpbench_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnstcpbench.cc \ @@ -585,6 +595,7 @@ dnsdist_LDADD = \ nsec3dig_SOURCES = \ base32.cc \ base64.cc base64.hh \ + dnsname.cc dnsname.hh \ dnslabeltext.cc \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ @@ -612,6 +623,7 @@ toysdig_SOURCES = \ base32.cc \ base64.cc base64.hh \ dns_random.cc \ + dnsname.cc dnsname.hh \ dnslabeltext.cc \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ @@ -638,6 +650,7 @@ tsig_tests_SOURCES = \ dns.cc \ dns_random.cc dns_random.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnssecinfra.cc \ @@ -666,6 +679,7 @@ speedtest_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnswriter.cc dnswriter.hh \ @@ -697,6 +711,7 @@ dnsbulktest_SOURCES = \ base64.cc \ dnsbulktest.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc \ dnsrecords.cc \ dnswriter.cc \ @@ -722,6 +737,7 @@ dnsscan_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnspcap.cc dnspcap.hh \ dnsrecords.cc \ @@ -744,6 +760,7 @@ dnsreplay_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnspcap.cc dnspcap.hh \ dnsrecords.cc \ @@ -771,6 +788,7 @@ nproxy_SOURCES = \ base32.cc \ base64.cc base64.hh \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnswriter.cc dnswriter.hh \ @@ -800,6 +818,7 @@ notify_SOURCES = \ base64.cc base64.hh \ dns.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnsrecords.cc \ dnswriter.cc dnswriter.hh \ @@ -828,6 +847,7 @@ dnsscope_SOURCES = \ base64.cc base64.hh \ dns.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnspcap.cc dnspcap.hh \ dnsrecords.cc \ @@ -856,6 +876,7 @@ dnsgram_SOURCES = \ base64.cc base64.hh \ dnsgram.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnspcap.cc dnspcap.hh \ dnsrecords.cc \ @@ -877,6 +898,7 @@ dnsdemog_SOURCES = \ base64.cc base64.hh \ dnsdemog.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc dnsparser.hh \ dnspcap.cc dnspcap.hh \ dnsrecords.cc \ @@ -980,6 +1002,7 @@ pdns_recursor_SOURCES = \ dns.cc \ dns_random.cc \ dnslabeltext.cc \ + dnsname.cc dnsname.hh \ dnsparser.cc \ dnsrecords.cc dnsrecords.hh \ dnswriter.cc dnswriter.hh \ diff --git a/pdns/dist-recursor b/pdns/dist-recursor index 1b2ddc326c..2ddfeab9c6 100755 --- a/pdns/dist-recursor +++ b/pdns/dist-recursor @@ -31,7 +31,7 @@ mplexer.hh pubsuffix.hh \ dns_random.hh lua-pdns.hh lua-recursor.hh namespaces.hh \ recpacketcache.hh base32.hh cachecleaner.hh json.hh version.hh \ ws-recursor.hh ws-api.hh secpoll-recursor.hh \ -responsestats.hh webserver.hh" +responsestats.hh webserver.hh dnsname.hh" CFILES="syncres.cc iputils.cc misc.cc unix_utility.cc qtype.cc \ logger.cc arguments.cc lwres.cc pdns_recursor.cc lua-iputils.cc \ @@ -41,7 +41,7 @@ selectmplexer.cc epollmplexer.cc kqueuemplexer.cc portsmplexer.cc pdns_hw.cc \ sillyrecords.cc pubsuffix.cc lua-pdns.cc lua-recursor.cc randomhelper.cc \ devpollmplexer.cc recpacketcache.cc dns.cc reczones.cc base32.cc nsecrecords.cc \ dnslabeltext.cc json.cc ws-recursor.cc ws-api.cc version.cc dns_random.cc \ -responsestats.cc webserver.cc rec-carbon.cc secpoll-recursor.cc" +responsestats.cc webserver.cc rec-carbon.cc secpoll-recursor.cc dnsname.cc" ./mkpubsuffixcc diff --git a/pdns/dnsparser.cc b/pdns/dnsparser.cc index 354daa9bc0..ecc5c4a4f2 100644 --- a/pdns/dnsparser.cc +++ b/pdns/dnsparser.cc @@ -399,11 +399,15 @@ uint8_t PacketReader::get8BitInt() return d_content.at(d_pos++); } -string PacketReader::getLabel(unsigned int recurs) +string PacketReader::getLabel() { - string ret; - ret.reserve(40); - getLabelFromContent(d_content, d_pos, ret, recurs++); + unsigned int consumed; + vector content(d_content); + content.insert(content.begin(), sizeof(dnsheader), 0); + + string ret = DNSName((const char*) content.data(), content.size(), d_pos + sizeof(dnsheader), true /* uncompress */, 0 /* qtype */, 0 /* qclass */, &consumed).toString(); + + d_pos+=consumed; return ret; } @@ -452,49 +456,6 @@ string PacketReader::getText(bool multi) return ret; } - -void PacketReader::getLabelFromContent(const vector& content, uint16_t& frompos, string& ret, int recurs) -{ - if(recurs > 1000) // the forward reference-check below should make this test 100% obsolete - throw MOADNSException("Loop"); - // it is tempting to call reserve on ret, but it turns out it creates a malloc/free storm in the loop - for(;;) { - unsigned char labellen=content.at(frompos++); - - if(!labellen) { - if(ret.empty()) - ret.append(1,'.'); - break; - } - else if((labellen & 0xc0) == 0xc0) { - uint16_t offset=256*(labellen & ~0xc0) + (unsigned int)content.at(frompos++) - sizeof(dnsheader); - // cout<<"This is an offset, need to go to: "<= frompos-2) - throw MOADNSException("forward reference during label decompression"); - return getLabelFromContent(content, offset, ret, ++recurs); - } - else if(labellen > 63) - throw MOADNSException("Overly long label during label decompression ("+lexical_cast((unsigned int)labellen)+")"); - else { - // XXX FIXME THIS MIGHT BE VERY SLOW! - - for(string::size_type n = 0 ; n < labellen; ++n, frompos++) { - if(content.at(frompos)=='.' || content.at(frompos)=='\\') { - ret.append(1, '\\'); - ret.append(1, content[frompos]); - } - else if(content.at(frompos)==' ') { - ret+="\\032"; - } - else - ret.append(1, content[frompos]); - } - ret.append(1,'.'); - } - } -} - void PacketReader::xfrBlob(string& blob) try { @@ -534,7 +495,7 @@ void PacketReader::xfrHexBlob(string& blob, bool keepReading) string simpleCompress(const string& elabel, const string& root) { string label=elabel; - // FIXME: this relies on the semi-canonical escaped output from getLabelFromContent + // FIXME: this relies on the semi-canonical escaped output from getLabel if(strchr(label.c_str(), '\\')) { boost::replace_all(label, "\\.", "."); boost::replace_all(label, "\\032", " "); diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index 5dedb9a1d8..5f0de674b2 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -37,6 +37,8 @@ #include #include "dns.hh" #include "dnswriter.hh" +#include "dnsname.hh" +#include "pdnsexception.hh" /** DNS records have three representations: 1) in the packet @@ -133,13 +135,12 @@ public: void xfrHexBlob(string& blob, bool keepReading=false); static uint16_t get16BitInt(const vector&content, uint16_t& pos); - static void getLabelFromContent(const vector& content, uint16_t& frompos, string& ret, int recurs); void getDnsrecordheader(struct dnsrecordheader &ah); void copyRecord(vector& dest, uint16_t len); void copyRecord(unsigned char* dest, uint16_t len); - string getLabel(unsigned int recurs=0); + string getLabel(); string getText(bool multi); uint16_t d_pos; diff --git a/pdns/dnswriter.cc b/pdns/dnswriter.cc index e38055ba5d..d6fb037437 100644 --- a/pdns/dnswriter.cc +++ b/pdns/dnswriter.cc @@ -246,7 +246,7 @@ void DNSPacketWriter::xfrLabel(const string& Label, bool compress) if(unescaped) { string part(label.c_str() + i -> first, i->second - i->first); - // FIXME: this relies on the semi-canonical escaped output from getLabelFromContent + // FIXME: this relies on the semi-canonical escaped output from getLabel boost::replace_all(part, "\\.", "."); boost::replace_all(part, "\\032", " "); boost::replace_all(part, "\\\\", "\\");