Skip to content

Commit

Permalink
actually truncate packet when setting TC, closes #1092
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter van Dijk committed Nov 4, 2013
1 parent c6f9468 commit add935a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pdns/dnswriter.cc
Expand Up @@ -46,6 +46,7 @@ DNSPacketWriter::DNSPacketWriter(vector<uint8_t>& content, const string& qname,

d_stuff=0xffff;
d_labelmap.reserve(16);
d_truncatemarker=d_content.size();
}

dnsheader* DNSPacketWriter::getHeader()
Expand Down Expand Up @@ -294,6 +295,15 @@ void DNSPacketWriter::rollback()
d_stuff=0;
}

void DNSPacketWriter::truncate()
{
d_content.resize(d_truncatemarker);
d_record.clear();
d_stuff=0;
dnsheader* dh=reinterpret_cast<dnsheader*>( &*d_content.begin());
dh->ancount = dh->nscount = dh->arcount = 0;
}

void DNSPacketWriter::commit()
{
if(d_stuff==0xffff && (d_content.size()!=d_sor || !d_record.empty()))
Expand Down
4 changes: 4 additions & 0 deletions pdns/dnswriter.hh
Expand Up @@ -61,6 +61,9 @@ public:
/** Should the packet have grown too big for the writer's liking, rollback removes the record currently being written */
void rollback();

/** Discard all content except the question section */
void truncate();

void xfr48BitInt(uint64_t val);
void xfr32BitInt(uint32_t val);
void xfr16BitInt(uint16_t val);
Expand Down Expand Up @@ -120,6 +123,7 @@ private:
uint16_t d_stuff;
uint16_t d_sor;
uint16_t d_rollbackmarker; // start of last complete packet, for rollback
uint16_t d_truncatemarker; // end of header, for truncate
Place d_recordplace;
bool d_canonic, d_lowerCase;
};
Expand Down
3 changes: 3 additions & 0 deletions pdns/pdns_recursor.cc
Expand Up @@ -591,7 +591,10 @@ void startDoResolve(void *p)
if(pw.size() > maxanswersize) {
pw.rollback();
if(i->d_place==DNSResourceRecord::ANSWER) // only truncate if we actually omitted parts of the answer
{
pw.getHeader()->tc=1;
pw.truncate();
}
goto sendit; // need to jump over pw.commit
}
}
Expand Down
44 changes: 44 additions & 0 deletions regression-tests.recursor/config.sh
Expand Up @@ -95,6 +95,50 @@ ns2.arthur.example.net. 3600 IN A $PREFIX.13
www.arthur.example.net. 3600 IN A 192.0.2.2
www2.arthur.example.net. 3600 IN A 192.0.2.6
mail.arthur.example.net. 3600 IN A 192.0.2.3
big.arthur.example.net. 3600 IN TXT "the quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "The quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THe quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE Quick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUick brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUIck brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICk brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK Brown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BRown fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROwn fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWn fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN Fox jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOx jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX Jumps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUmps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMps over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPs over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS Over the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVer the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVEr the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER the lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER The lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THe lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE Lazy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAzy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZy dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZY dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZY dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZY Dog"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOg"
big.arthur.example.net. 3600 IN TXT "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
EOF

mkdir $PREFIX.13
Expand Down
2 changes: 2 additions & 0 deletions regression-tests.recursor/truncate-empty/command
@@ -0,0 +1,2 @@
#!/bin/sh
cleandig big.arthur.example.net txt | sed 's/\(.*\tIN\t[A-Z0-9]\+\t\)\([0-9]\+\)/\13600/'
1 change: 1 addition & 0 deletions regression-tests.recursor/truncate-empty/description
@@ -0,0 +1 @@
Verify that truncated messages are empty.
2 changes: 2 additions & 0 deletions regression-tests.recursor/truncate-empty/expected_result
@@ -0,0 +1,2 @@
Rcode: 0, RD: 1, QR: 1, TC: 1, AA: 0, opcode: 0
Reply to question for qname='big.arthur.example.net.', qtype=TXT

0 comments on commit add935a

Please sign in to comment.