]>
Commit | Line | Data |
---|---|---|
12471842 PL |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
b8cd1d8f PL |
25 | #include <boost/version.hpp> |
26 | #if BOOST_VERSION >= 105400 | |
8c0139b0 | 27 | #include <boost/container/static_vector.hpp> |
b8cd1d8f | 28 | #endif |
a0a276c2 BH |
29 | #include "dnswriter.hh" |
30 | #include "misc.hh" | |
31 | #include "dnsparser.hh" | |
8c0139b0 | 32 | |
a6c51664 | 33 | #include <limits.h> |
a0a276c2 | 34 | |
e636cab2 | 35 | /* d_content: <---- d_stuff ----> |
36 | v d_truncatemarker | |
37 | dnsheader | qname | qtype | qclass | {recordname| dnsrecordheader | record } | |
38 | ^ d_rollbackmarker ^ d_sor | |
39 | ||
40 | ||
41 | */ | |
42 | ||
43 | ||
c2f3be9d | 44 | DNSPacketWriter::DNSPacketWriter(vector<uint8_t>& content, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint8_t opcode) |
fea4599a | 45 | : d_content(content), d_qname(qname), d_canonic(false), d_lowerCase(false) |
a0a276c2 BH |
46 | { |
47 | d_content.clear(); | |
48 | dnsheader dnsheader; | |
950cfe0f | 49 | |
a0a276c2 | 50 | memset(&dnsheader, 0, sizeof(dnsheader)); |
8e97e9a3 | 51 | dnsheader.id=0; |
a0a276c2 | 52 | dnsheader.qdcount=htons(1); |
88c1bc50 | 53 | dnsheader.opcode=opcode; |
950cfe0f | 54 | |
a0a276c2 | 55 | const uint8_t* ptr=(const uint8_t*)&dnsheader; |
790e7c1b BH |
56 | uint32_t len=d_content.size(); |
57 | d_content.resize(len + sizeof(dnsheader)); | |
58 | uint8_t* dptr=(&*d_content.begin()) + len; | |
950cfe0f | 59 | |
790e7c1b | 60 | memcpy(dptr, ptr, sizeof(dnsheader)); |
fea4599a | 61 | d_namepositions.reserve(16); |
ad8fa726 | 62 | xfrName(qname, false); |
e636cab2 | 63 | xfr16BitInt(qtype); |
64 | xfr16BitInt(qclass); | |
950cfe0f | 65 | |
add935a2 | 66 | d_truncatemarker=d_content.size(); |
0eabbd4b AT |
67 | d_sor = 0; |
68 | d_rollbackmarker = 0; | |
a0a276c2 BH |
69 | } |
70 | ||
5a57d2ea | 71 | dnsheader* DNSPacketWriter::getHeader() |
8e97e9a3 | 72 | { |
b346905c | 73 | return reinterpret_cast<dnsheader*>(&*d_content.begin()); |
8e97e9a3 BH |
74 | } |
75 | ||
e636cab2 | 76 | |
e693ff5a | 77 | void DNSPacketWriter::startRecord(const DNSName& name, uint16_t qtype, uint32_t ttl, uint16_t qclass, DNSResourceRecord::Place place, bool compress) |
a0a276c2 | 78 | { |
e636cab2 | 79 | commit(); |
10321a98 | 80 | d_rollbackmarker=d_content.size(); |
bca6643b | 81 | |
e636cab2 | 82 | if(compress && !name.empty() && d_qname==name) { // don't do the whole label compression thing if we *know* we can get away with "see question" - except when compressing the root |
6f724e8b PD |
83 | static unsigned char marker[2]={0xc0, 0x0c}; |
84 | d_content.insert(d_content.end(), (const char *) &marker[0], (const char *) &marker[2]); | |
a2ce25e4 BH |
85 | } |
86 | else { | |
e636cab2 | 87 | xfrName(name, compress); |
a2ce25e4 | 88 | } |
e636cab2 | 89 | xfr16BitInt(qtype); |
90 | xfr16BitInt(qclass); | |
91 | xfr32BitInt(ttl); | |
92 | xfr16BitInt(0); // this will be the record size | |
93 | d_recordplace = place; | |
94 | d_sor=d_content.size(); // this will remind us where to stuff the record size | |
878435ce | 95 | } |
a0a276c2 | 96 | |
3ed1d0ec | 97 | void DNSPacketWriter::addOpt(uint16_t udpsize, uint16_t extRCode, int Z, const vector<pair<uint16_t,string> >& options, uint8_t version) |
878435ce BH |
98 | { |
99 | uint32_t ttl=0; | |
705f31ae | 100 | |
009f9f55 | 101 | EDNS0Record stuff; |
878435ce | 102 | |
bf269e28 | 103 | stuff.version=version; |
878435ce | 104 | stuff.Z=htons(Z); |
7f7b8d55 | 105 | |
f1db0de2 PL |
106 | /* RFC 6891 section 4 on the Extended RCode wire format |
107 | * EXTENDED-RCODE | |
108 | * Forms the upper 8 bits of extended 12-bit RCODE (together with the | |
109 | * 4 bits defined in [RFC1035]. Note that EXTENDED-RCODE value 0 | |
110 | * indicates that an unextended RCODE is in use (values 0 through 15). | |
111 | */ | |
112 | stuff.extRCode = extRCode>>4; | |
d0983bff | 113 | if (extRCode != 0) { // As this trumps the existing RCODE |
f1db0de2 PL |
114 | getHeader()->rcode = extRCode; |
115 | } | |
116 | ||
a683e8bd | 117 | static_assert(sizeof(EDNS0Record) == sizeof(ttl), "sizeof(EDNS0Record) must match sizeof(ttl)"); |
878435ce | 118 | memcpy(&ttl, &stuff, sizeof(stuff)); |
8a63d3ce | 119 | |
878435ce | 120 | ttl=ntohl(ttl); // will be reversed later on |
950cfe0f | 121 | |
12c06211 | 122 | startRecord(g_rootdnsname, QType::OPT, ttl, udpsize, DNSResourceRecord::ADDITIONAL, false); |
7f7b8d55 BH |
123 | for(optvect_t::const_iterator iter = options.begin(); iter != options.end(); ++iter) { |
124 | xfr16BitInt(iter->first); | |
125 | xfr16BitInt(iter->second.length()); | |
126 | xfrBlob(iter->second); | |
950cfe0f | 127 | } |
a0a276c2 BH |
128 | } |
129 | ||
341930bb BH |
130 | void DNSPacketWriter::xfr48BitInt(uint64_t val) |
131 | { | |
132 | unsigned char bytes[6]; | |
a6e93c0f | 133 | uint16_t theLeft = htons((val >> 32)&0xffffU); |
0407751c | 134 | uint32_t theRight = htonl(val & 0xffffffffU); |
a683e8bd RG |
135 | memcpy(bytes, (void*)&theLeft, sizeof(theLeft)); |
136 | memcpy(bytes+2, (void*)&theRight, sizeof(theRight)); | |
341930bb | 137 | |
e636cab2 | 138 | d_content.insert(d_content.end(), bytes, bytes + sizeof(bytes)); |
341930bb BH |
139 | } |
140 | ||
141 | ||
a0a276c2 BH |
142 | void DNSPacketWriter::xfr32BitInt(uint32_t val) |
143 | { | |
ea634573 BH |
144 | int rval=htonl(val); |
145 | uint8_t* ptr=reinterpret_cast<uint8_t*>(&rval); | |
e636cab2 | 146 | d_content.insert(d_content.end(), ptr, ptr+4); |
a0a276c2 BH |
147 | } |
148 | ||
149 | void DNSPacketWriter::xfr16BitInt(uint16_t val) | |
150 | { | |
96aed220 | 151 | uint16_t rval=htons(val); |
ea634573 | 152 | uint8_t* ptr=reinterpret_cast<uint8_t*>(&rval); |
e636cab2 | 153 | d_content.insert(d_content.end(), ptr, ptr+2); |
a0a276c2 BH |
154 | } |
155 | ||
156 | void DNSPacketWriter::xfr8BitInt(uint8_t val) | |
157 | { | |
e636cab2 | 158 | d_content.push_back(val); |
a0a276c2 BH |
159 | } |
160 | ||
bac8f21b | 161 | |
950cfe0f | 162 | /* input: |
84e1142d | 163 | if lenField is true |
bac8f21b BH |
164 | "" -> 0 |
165 | "blah" -> 4blah | |
166 | "blah" "blah" -> output 4blah4blah | |
167 | "verylongstringlongerthan256....characters" \xffverylongstring\x23characters (autosplit) | |
168 | "blah\"blah" -> 9blah"blah | |
169 | "blah\97" -> 5blahb | |
84e1142d PL |
170 | |
171 | if lenField is false | |
172 | "blah" -> blah | |
173 | "blah\"blah" -> blah"blah | |
bac8f21b | 174 | */ |
84e1142d | 175 | void DNSPacketWriter::xfrText(const string& text, bool, bool lenField) |
a0a276c2 | 176 | { |
bac8f21b | 177 | if(text.empty()) { |
e636cab2 | 178 | d_content.push_back(0); |
bac8f21b BH |
179 | return; |
180 | } | |
181 | vector<string> segments = segmentDNSText(text); | |
ef7cd021 | 182 | for(const string& str : segments) { |
84e1142d | 183 | if(lenField) |
e636cab2 | 184 | d_content.push_back(str.length()); |
185 | d_content.insert(d_content.end(), str.c_str(), str.c_str() + str.length()); | |
ef6a78d5 | 186 | } |
a0a276c2 BH |
187 | } |
188 | ||
948a927f PL |
189 | void DNSPacketWriter::xfrUnquotedText(const string& text, bool lenField) |
190 | { | |
191 | if(text.empty()) { | |
e636cab2 | 192 | d_content.push_back(0); |
948a927f PL |
193 | return; |
194 | } | |
195 | if(lenField) | |
e636cab2 | 196 | d_content.push_back(text.length()); |
197 | d_content.insert(d_content.end(), text.c_str(), text.c_str() + text.length()); | |
948a927f PL |
198 | } |
199 | ||
802a93d0 | 200 | |
201 | static constexpr bool l_verbose=false; | |
202 | uint16_t DNSPacketWriter::lookupName(const DNSName& name, uint16_t* matchLen) | |
a2ce25e4 | 203 | { |
802a93d0 | 204 | // iterate over the written labels, see if we find a match |
205 | const auto& raw = name.getStorage(); | |
206 | ||
207 | /* name might be a.root-servers.net, we need to be able to benefit from finding: | |
208 | b.root-servers.net, or even: | |
209 | b\xc0\x0c | |
210 | */ | |
211 | unsigned int bestpos=0; | |
212 | *matchLen=0; | |
b8cd1d8f | 213 | #if BOOST_VERSION >= 105400 |
802a93d0 | 214 | boost::container::static_vector<uint16_t, 34> nvect, pvect; |
b8cd1d8f PL |
215 | #else |
216 | vector<uint16_t> nvect, pvect; | |
217 | #endif | |
802a93d0 | 218 | |
98cda0a9 | 219 | try { |
220 | for(auto riter= raw.cbegin(); riter < raw.cend(); ) { | |
221 | if(!*riter) | |
222 | break; | |
223 | nvect.push_back(riter - raw.cbegin()); | |
224 | riter+=*riter+1; | |
225 | } | |
226 | } | |
227 | catch(std::bad_alloc& ba) { | |
228 | if(l_verbose) | |
229 | cout<<"Domain "<<name<<" too large to compress"<<endl; | |
230 | return 0; | |
802a93d0 | 231 | } |
232 | ||
233 | if(l_verbose) { | |
234 | cout<<"Input vector for lookup "<<name<<": "; | |
235 | for(const auto n : nvect) | |
236 | cout << n<<" "; | |
237 | cout<<endl; | |
238 | cout<<makeHexDump(string(raw.c_str(), raw.c_str()+raw.size()))<<endl; | |
239 | } | |
a2ce25e4 | 240 | |
802a93d0 | 241 | if(l_verbose) |
fea4599a | 242 | cout<<"Have "<<d_namepositions.size()<<" to ponder"<<endl; |
802a93d0 | 243 | int counter=1; |
fea4599a | 244 | for(auto p : d_namepositions) { |
802a93d0 | 245 | if(l_verbose) { |
e636cab2 | 246 | cout<<"Pos: "<<p<<", "<<d_content.size()<<endl; |
247 | DNSName pname((const char*)&d_content[0], d_content.size(), p, true); // only for debugging | |
248 | cout<<"Looking at '"<<pname<<"' in packet at position "<<p<<"/"<<d_content.size()<<", option "<<counter<<"/"<<d_namepositions.size()<<endl; | |
802a93d0 | 249 | ++counter; |
250 | } | |
251 | // memcmp here makes things _slower_ | |
252 | pvect.clear(); | |
98cda0a9 | 253 | try { |
254 | for(auto iter = d_content.cbegin() + p; iter < d_content.cend();) { | |
255 | uint8_t c=*iter; | |
802a93d0 | 256 | if(l_verbose) |
98cda0a9 | 257 | cout<<"Found label length: "<<(int)c<<endl; |
258 | if(c & 0xc0) { | |
259 | uint16_t npos = 0x100*(c & (~0xc0)) + *++iter; | |
260 | iter = d_content.begin() + npos; | |
261 | if(l_verbose) | |
262 | cout<<"Is compressed label to newpos "<<npos<<", going there"<<endl; | |
263 | // check against going forward here | |
264 | continue; | |
265 | } | |
266 | if(!c) | |
267 | break; | |
268 | pvect.push_back(iter - d_content.cbegin()); | |
269 | iter+=*iter+1; | |
802a93d0 | 270 | } |
98cda0a9 | 271 | } |
272 | catch(std::bad_alloc& ba) { | |
273 | if(l_verbose) | |
274 | cout<<"Domain "<<name<<" too large to compress"<<endl; | |
275 | continue; | |
802a93d0 | 276 | } |
277 | if(l_verbose) { | |
278 | cout<<"Packet vector: "<<endl; | |
279 | for(const auto n : pvect) | |
280 | cout << n<<" "; | |
281 | cout<<endl; | |
282 | } | |
283 | auto niter=nvect.crbegin(), piter=pvect.crbegin(); | |
284 | unsigned int cmatchlen=1; | |
285 | for(; niter != nvect.crend() && piter != pvect.crend(); ++niter, ++piter) { | |
286 | // niter is an offset in raw, pvect an offset in packet | |
e636cab2 | 287 | uint8_t nlen = raw[*niter], plen=d_content[*piter]; |
802a93d0 | 288 | if(l_verbose) |
289 | cout<<"nlnen="<<(int)nlen<<", plen="<<(int)plen<<endl; | |
290 | if(nlen != plen) | |
291 | break; | |
e636cab2 | 292 | if(strncasecmp(raw.c_str()+*niter+1, (const char*)&d_content[*piter]+1, nlen)) { |
802a93d0 | 293 | if(l_verbose) |
e636cab2 | 294 | cout<<"Mismatch: "<<string(raw.c_str()+*niter+1, raw.c_str()+*niter+nlen+1)<< " != "<<string((const char*)&d_content[*piter]+1, (const char*)&d_content[*piter]+nlen+1)<<endl; |
802a93d0 | 295 | break; |
296 | } | |
297 | cmatchlen+=nlen+1; | |
298 | if(cmatchlen == raw.length()) { // have matched all of it, can't improve | |
299 | if(l_verbose) | |
300 | cout<<"Stopping search, matched whole name"<<endl; | |
301 | *matchLen = cmatchlen; | |
302 | return *piter; | |
303 | } | |
304 | } | |
305 | if(piter != pvect.crbegin() && *matchLen < cmatchlen) { | |
306 | *matchLen = cmatchlen; | |
307 | bestpos=*--piter; | |
308 | } | |
309 | } | |
310 | return bestpos; | |
311 | } | |
950cfe0f | 312 | // this is the absolute hottest function in the pdns recursor |
f21fc0aa | 313 | void DNSPacketWriter::xfrName(const DNSName& name, bool compress, bool) |
38e655b6 | 314 | { |
802a93d0 | 315 | if(l_verbose) |
316 | cout<<"Wants to write "<<name<<", compress="<<compress<<", canonic="<<d_canonic<<", LC="<<d_lowerCase<<endl; | |
317 | if(d_canonic || d_lowerCase) // d_lowerCase implies canonic | |
f3f4938f BH |
318 | compress=false; |
319 | ||
802a93d0 | 320 | if(name.empty() || name.isRoot()) { // for speed |
e636cab2 | 321 | d_content.push_back(0); |
c1d02c0d BH |
322 | return; |
323 | } | |
950cfe0f | 324 | |
802a93d0 | 325 | uint16_t li=0; |
326 | uint16_t matchlen=0; | |
327 | if(compress && (li=lookupName(name, &matchlen))) { | |
328 | const auto& dns=name.getStorage(); | |
329 | if(l_verbose) | |
330 | cout<<"Found a substring of "<<matchlen<<" bytes from the back, offset: "<<li<<", dnslen: "<<dns.size()<<endl; | |
331 | // found a substring, if www.powerdns.com matched powerdns.com, we get back matchlen = 13 | |
332 | ||
e636cab2 | 333 | unsigned int pos=d_content.size(); |
802a93d0 | 334 | if(pos < 16384 && matchlen != dns.size()) { |
335 | if(l_verbose) | |
336 | cout<<"Inserting pos "<<pos<<" for "<<name<<" for compressed case"<<endl; | |
fea4599a | 337 | d_namepositions.push_back(pos); |
bca6643b | 338 | } |
2f4c3abb | 339 | |
802a93d0 | 340 | if(l_verbose) |
341 | cout<<"Going to write unique part: '"<<makeHexDump(string(dns.c_str(), dns.c_str() + dns.size() - matchlen)) <<"'"<<endl; | |
e636cab2 | 342 | d_content.insert(d_content.end(), (const unsigned char*)dns.c_str(), (const unsigned char*)dns.c_str() + dns.size() - matchlen); |
802a93d0 | 343 | uint16_t offset=li; |
344 | offset|=0xc000; | |
a2ce25e4 | 345 | |
e636cab2 | 346 | d_content.push_back((char)(offset >> 8)); |
347 | d_content.push_back((char)(offset & 0xff)); | |
bca6643b | 348 | } |
802a93d0 | 349 | else { |
e636cab2 | 350 | unsigned int pos=d_content.size(); |
802a93d0 | 351 | if(l_verbose) |
352 | cout<<"Found nothing, we are at pos "<<pos<<", inserting whole name"<<endl; | |
353 | if(pos < 16384) { | |
354 | if(l_verbose) | |
355 | cout<<"Inserting pos "<<pos<<" for "<<name<<" for uncompressed case"<<endl; | |
fea4599a | 356 | d_namepositions.push_back(pos); |
802a93d0 | 357 | } |
5be843df | 358 | |
802a93d0 | 359 | std::unique_ptr<DNSName> lc; |
360 | if(d_lowerCase) | |
361 | lc = make_unique<DNSName>(name.makeLowerCase()); | |
a0a276c2 | 362 | |
802a93d0 | 363 | const DNSName::string_t& raw = (lc ? *lc : name).getStorage(); |
364 | if(l_verbose) | |
365 | cout<<"Writing out the whole thing "<<makeHexDump(string(raw.c_str(), raw.c_str() + raw.length()))<<endl; | |
e636cab2 | 366 | d_content.insert(d_content.end(), raw.c_str(), raw.c_str() + raw.size()); |
802a93d0 | 367 | } |
a0a276c2 BH |
368 | } |
369 | ||
06ffdc52 | 370 | void DNSPacketWriter::xfrBlob(const string& blob, int ) |
8c1c9170 BH |
371 | { |
372 | const uint8_t* ptr=reinterpret_cast<const uint8_t*>(blob.c_str()); | |
e636cab2 | 373 | d_content.insert(d_content.end(), ptr, ptr+blob.size()); |
8c1c9170 BH |
374 | } |
375 | ||
2fe9d6f7 AT |
376 | void DNSPacketWriter::xfrBlobNoSpaces(const string& blob, int ) |
377 | { | |
378 | xfrBlob(blob); | |
379 | } | |
380 | ||
e4090157 | 381 | void DNSPacketWriter::xfrHexBlob(const string& blob, bool keepReading) |
59a0f653 BH |
382 | { |
383 | xfrBlob(blob); | |
384 | } | |
385 | ||
e636cab2 | 386 | // call __before commit__ |
387 | void DNSPacketWriter::getRecordPayload(string& records) | |
ea634573 BH |
388 | { |
389 | records.assign(d_content.begin() + d_sor, d_content.end()); | |
390 | } | |
a0a276c2 | 391 | |
dffbaa08 | 392 | uint32_t DNSPacketWriter::size() |
10321a98 | 393 | { |
e636cab2 | 394 | return d_content.size(); |
10321a98 BH |
395 | } |
396 | ||
397 | void DNSPacketWriter::rollback() | |
398 | { | |
399 | d_content.resize(d_rollbackmarker); | |
4bb75371 | 400 | d_sor = 0; |
10321a98 BH |
401 | } |
402 | ||
add935a2 PD |
403 | void DNSPacketWriter::truncate() |
404 | { | |
405 | d_content.resize(d_truncatemarker); | |
add935a2 PD |
406 | dnsheader* dh=reinterpret_cast<dnsheader*>( &*d_content.begin()); |
407 | dh->ancount = dh->nscount = dh->arcount = 0; | |
408 | } | |
409 | ||
a0a276c2 BH |
410 | void DNSPacketWriter::commit() |
411 | { | |
e636cab2 | 412 | if(!d_sor) |
413 | return; | |
414 | uint16_t rlen = d_content.size() - d_sor; | |
415 | d_content[d_sor-2]=rlen >> 8; | |
416 | d_content[d_sor-1]=rlen & 0xff; | |
417 | d_sor=0; | |
10321a98 BH |
418 | dnsheader* dh=reinterpret_cast<dnsheader*>( &*d_content.begin()); |
419 | switch(d_recordplace) { | |
e693ff5a AT |
420 | case DNSResourceRecord::QUESTION: |
421 | dh->qdcount = htons(ntohs(dh->qdcount) + 1); | |
422 | break; | |
423 | case DNSResourceRecord::ANSWER: | |
10321a98 BH |
424 | dh->ancount = htons(ntohs(dh->ancount) + 1); |
425 | break; | |
e693ff5a | 426 | case DNSResourceRecord::AUTHORITY: |
10321a98 BH |
427 | dh->nscount = htons(ntohs(dh->nscount) + 1); |
428 | break; | |
e693ff5a | 429 | case DNSResourceRecord::ADDITIONAL: |
10321a98 BH |
430 | dh->arcount = htons(ntohs(dh->arcount) + 1); |
431 | break; | |
432 | } | |
433 | ||
a0a276c2 | 434 | } |