2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
31 // #include <netinet/in.h>
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/tuple/tuple_comparison.hpp>
37 #include "dnswriter.hh"
39 #include "pdnsexception.hh"
41 /** DNS records have three representations:
43 2) parsed in a class, ready for use
46 We should implement bidirectional transitions between 1&2 and 2&3.
47 Currently we have: 1 -> 2
50 We can add: 2 -> 1 easily by reversing the packetwriter
51 And we might be able to reverse 2 -> 3 as well
54 #include "namespaces.hh"
55 #include "namespaces.hh"
57 class MOADNSException : public runtime_error
60 MOADNSException(const string& str) : runtime_error(str)
70 PacketReader(const vector<uint8_t>& content)
71 : d_pos(0), d_startrecordpos(0), d_content(content)
73 if(content.size() > std::numeric_limits<uint16_t>::max())
74 throw std::out_of_range("packet too large");
76 d_recordlen = (uint16_t) content.size();
80 uint32_t get32BitInt();
81 uint16_t get16BitInt();
84 void xfr48BitInt(uint64_t& val);
86 void xfr32BitInt(uint32_t& val)
91 void xfrIP(uint32_t& val)
97 void xfrIP6(std::string &val) {
101 void xfrTime(uint32_t& val)
107 void xfr16BitInt(uint16_t& val)
112 void xfrType(uint16_t& val)
118 void xfr8BitInt(uint8_t& val)
124 void xfrName(DNSName &name, bool compress=false, bool noDot=false)
129 void xfrText(string &text, bool multi=false, bool lenField=true)
131 text=getText(multi, lenField);
134 void xfrUnquotedText(string &text, bool lenField){
135 text=getUnquotedText(lenField);
138 void xfrBlob(string& blob);
139 void xfrBlobNoSpaces(string& blob, int len);
140 void xfrBlob(string& blob, int length);
141 void xfrHexBlob(string& blob, bool keepReading=false);
143 static uint16_t get16BitInt(const vector<unsigned char>&content, uint16_t& pos);
145 void getDnsrecordheader(struct dnsrecordheader &ah);
146 void copyRecord(vector<unsigned char>& dest, uint16_t len);
147 void copyRecord(unsigned char* dest, uint16_t len);
150 string getText(bool multi, bool lenField);
151 string getUnquotedText(bool lenField);
155 bool eof() { return true; };
158 uint16_t d_startrecordpos; // needed for getBlob later on
159 uint16_t d_recordlen; // ditto
160 uint16_t not_used; // Alighns the whole class on 8-byte boundries
161 const vector<uint8_t>& d_content;
166 class DNSRecordContent
169 static DNSRecordContent* mastermake(const DNSRecord &dr, PacketReader& pr);
170 static DNSRecordContent* mastermake(const DNSRecord &dr, PacketReader& pr, uint16_t opcode);
171 static DNSRecordContent* mastermake(uint16_t qtype, uint16_t qclass, const string& zone);
172 static std::unique_ptr<DNSRecordContent> makeunique(uint16_t qtype, uint16_t qclass, const string& content);
174 virtual std::string getZoneRepresentation(bool noDot=false) const = 0;
175 virtual ~DNSRecordContent() {}
176 virtual void toPacket(DNSPacketWriter& pw)=0;
177 virtual string serialize(const DNSName& qname, bool canonic=false, bool lowerCase=false) // it would rock if this were const, but it is too hard
179 vector<uint8_t> packet;
181 DNSPacketWriter pw(packet, empty, 1);
186 pw.setLowercase(true);
188 pw.startRecord(qname, this->getType());
193 pw.getRecords(record);
197 static shared_ptr<DNSRecordContent> unserialize(const DNSName& qname, uint16_t qtype, const string& serialized);
199 void doRecordCheck(const struct DNSRecord&){}
201 typedef DNSRecordContent* makerfunc_t(const struct DNSRecord& dr, PacketReader& pr);
202 typedef DNSRecordContent* zmakerfunc_t(const string& str);
204 static void regist(uint16_t cl, uint16_t ty, makerfunc_t* f, zmakerfunc_t* z, const char* name)
207 getTypemap()[make_pair(cl,ty)]=f;
209 getZmakermap()[make_pair(cl,ty)]=z;
211 getT2Namemap().insert(make_pair(make_pair(cl,ty), name));
212 getN2Typemap().insert(make_pair(name, make_pair(cl,ty)));
215 static void unregist(uint16_t cl, uint16_t ty)
217 pair<uint16_t, uint16_t> key=make_pair(cl, ty);
218 getTypemap().erase(key);
219 getZmakermap().erase(key);
222 static uint16_t TypeToNumber(const string& name)
224 n2typemap_t::const_iterator iter = getN2Typemap().find(toUpper(name));
225 if(iter != getN2Typemap().end())
226 return iter->second.second;
228 if(boost::starts_with(name, "TYPE") || boost::starts_with(name, "type"))
229 return (uint16_t) pdns_stou(name.substr(4));
231 throw runtime_error("Unknown DNS type '"+name+"'");
234 static const string NumberToType(uint16_t num, uint16_t classnum=1)
236 t2namemap_t::const_iterator iter = getT2Namemap().find(make_pair(classnum, num));
237 if(iter == getT2Namemap().end())
238 return "TYPE" + std::to_string(num);
239 // throw runtime_error("Unknown DNS type with numerical id "+std::to_string(num));
243 virtual uint16_t getType() const = 0;
246 typedef std::map<std::pair<uint16_t, uint16_t>, makerfunc_t* > typemap_t;
247 typedef std::map<std::pair<uint16_t, uint16_t>, zmakerfunc_t* > zmakermap_t;
248 typedef std::map<std::pair<uint16_t, uint16_t>, string > t2namemap_t;
249 typedef std::map<string, std::pair<uint16_t, uint16_t> > n2typemap_t;
250 static typemap_t& getTypemap();
251 static t2namemap_t& getT2Namemap();
252 static n2typemap_t& getN2Typemap();
253 static zmakermap_t& getZmakermap();
260 d_class = QClass::IN;
263 d_place = DNSResourceRecord::ANSWER;
265 explicit DNSRecord(const DNSResourceRecord& rr);
267 std::shared_ptr<DNSRecordContent> d_content;
272 DNSResourceRecord::Place d_place;
274 bool operator<(const DNSRecord& rhs) const
276 if(tie(d_name, d_type, d_class, d_ttl) < tie(rhs.d_name, rhs.d_type, rhs.d_class, rhs.d_ttl))
279 if(tie(d_name, d_type, d_class, d_ttl) != tie(rhs.d_name, rhs.d_type, rhs.d_class, rhs.d_ttl))
284 lzrp=toLower(d_content->getZoneRepresentation());
286 rzrp=toLower(rhs.d_content->getZoneRepresentation());
291 // this orders in canonical order and keeps the SOA record on top
292 static bool prettyCompare(const DNSRecord& a, const DNSRecord& b)
294 auto aType = (a.d_type == QType::SOA) ? 0 : a.d_type;
295 auto bType = (b.d_type == QType::SOA) ? 0 : b.d_type;
297 if(a.d_name.canonCompare(b.d_name))
299 if(b.d_name.canonCompare(a.d_name))
302 if(tie(aType, a.d_class, a.d_ttl) < tie(bType, b.d_class, b.d_ttl))
305 if(tie(aType, a.d_class, a.d_ttl) != tie(bType, b.d_class, b.d_ttl))
310 lzrp=toLower(a.d_content->getZoneRepresentation());
312 rzrp=toLower(b.d_content->getZoneRepresentation());
318 bool operator==(const DNSRecord& rhs) const
322 lzrp=toLower(d_content->getZoneRepresentation());
324 rzrp=toLower(rhs.d_content->getZoneRepresentation());
326 string llabel=toLower(d_name.toString());
327 string rlabel=toLower(rhs.d_name.toString());
330 tie(llabel, d_type, d_class, lzrp) ==
331 tie(rlabel, rhs.d_type, rhs.d_class, rzrp);
335 //! This class can be used to parse incoming packets, and is copyable
336 class MOADNSParser : public boost::noncopyable
339 //! Parse from a string
340 MOADNSParser(const string& buffer) : d_tsigPos(0)
342 init(buffer.c_str(), (unsigned int)buffer.size());
345 //! Parse from a pointer and length
346 MOADNSParser(const char *packet, unsigned int len) : d_tsigPos(0)
352 uint16_t d_qclass, d_qtype;
356 typedef vector<pair<DNSRecord, uint16_t > > answers_t;
358 //! All answers contained in this packet
361 shared_ptr<PacketReader> getPacketReader(uint16_t offset)
363 shared_ptr<PacketReader> pr(new PacketReader(d_content));
368 uint16_t getTSIGPos()
373 void getDnsrecordheader(struct dnsrecordheader &ah);
374 void init(const char *packet, unsigned int len);
375 vector<uint8_t> d_content;
379 string simpleCompress(const string& label, const string& root="");
380 void ageDNSPacket(char* packet, size_t length, uint32_t seconds);
381 void ageDNSPacket(std::string& packet, uint32_t seconds);
382 uint32_t getDNSPacketMinTTL(const char* packet, size_t length);
383 uint32_t getDNSPacketLength(const char* packet, size_t length);
384 uint16_t getRecordsOfTypeCount(const char* packet, size_t length, uint8_t section, uint16_t type);
387 std::shared_ptr<T> getRR(const DNSRecord& dr)
389 return std::dynamic_pointer_cast<T>(dr.d_content);