9 uint32_t burtleCI(const unsigned char* k, uint32_t lengh, uint32_t init);
12 // #include "logger.hh"
14 //#include <ext/vstring.h>
17 accept escaped ascii presentations of DNS names and store them "natively"
18 accept a DNS packet with an offset, and extract a DNS name from it
19 build up DNSNames with prepend and append of 'raw' unescaped labels
21 Be able to turn them into ASCII and "DNS name in a packet" again on request
23 Provide some common operators for comparison, detection of being part of another domain
25 NOTE: For now, everything MUST be . terminated, otherwise it is an error
32 DNSName() {} //!< Constructs an *empty* DNSName, NOT the root!
33 explicit DNSName(const char* p); //!< Constructs from a human formatted, escaped presentation
34 explicit DNSName(const std::string& str) : DNSName(str.c_str()) {} //!< Constructs from a human formatted, escaped presentation
35 DNSName(const char* p, int len, int offset, bool uncompress, uint16_t* qtype=0, uint16_t* qclass=0, unsigned int* consumed=0); //!< Construct from a DNS Packet, taking the first question if offset=12
37 bool isPartOf(const DNSName& rhs) const; //!< Are we part of the rhs name?
38 bool operator==(const DNSName& rhs) const; //!< DNS-native comparison (case insensitive) - empty compares to empty
39 bool operator!=(const DNSName& other) const { return !(*this == other); }
41 std::string toString(const std::string& separator=".", const bool trailing=true) const; //!< Our human-friendly, escaped, representation
42 std::string toStringNoDot() const { return toString(".", false); }
43 std::string toDNSString() const; //!< Our representation in DNS native format
44 void appendRawLabel(const std::string& str); //!< Append this unescaped label
45 void prependRawLabel(const std::string& str); //!< Prepend this unescaped label
46 std::vector<std::string> getRawLabels() const; //!< Individual raw unescaped labels
47 bool chopOff(); //!< Turn www.powerdns.com. into powerdns.com., returns false for .
48 DNSName makeRelative(const DNSName& zone) const;
49 void makeUsRelative(const DNSName& zone);
50 DNSName labelReverse() const;
51 bool isWildcard() const;
52 unsigned int countLabels() const;
53 size_t wirelength() const; //!< Number of total bytes in the name
54 bool empty() const { return d_storage.empty(); }
55 bool isRoot() const { return d_storage.size()==1 && d_storage[0]==0; }
56 void clear() { d_storage.clear(); }
57 void trimToLabels(unsigned int);
60 return burtleCI((const unsigned char*)d_storage.c_str(), d_storage.size(), 0);
62 DNSName& operator+=(const DNSName& rhs)
64 if(d_storage.size() + rhs.d_storage.size() > 256) // reserve one byte for the root label
65 throw std::range_error("name too long");
70 d_storage+=rhs.d_storage;
72 d_storage.replace(d_storage.length()-1, rhs.d_storage.length(), rhs.d_storage);
77 bool operator<(const DNSName& rhs) const // this delivers _some_ kind of ordering, but not one useful in a DNS context. Really fast though.
79 return std::lexicographical_compare(d_storage.rbegin(), d_storage.rend(),
80 rhs.d_storage.rbegin(), rhs.d_storage.rend(),
81 [](const char& a, const char& b) {
82 return tolower(a) < tolower(b);
83 }); // note that this is case insensitive, including on the label lengths
86 template<class Archive>
87 void serialize(Archive &ar, const unsigned int version)
92 inline bool canonCompare(const DNSName& rhs) const;
93 bool slowCanonCompare(const DNSName& rhs) const;
95 //typedef __gnu_cxx::__sso_string string_t;
96 typedef std::string string_t;
100 void packetParser(const char* p, int len, int offset, bool uncompress, uint16_t* qtype=0, uint16_t* qclass=0, unsigned int* consumed=0, int depth=0);
101 static std::string escapeLabel(const std::string& orig);
102 static std::string unescapeLabel(const std::string& orig);
105 size_t hash_value(DNSName const& d);
107 inline char dns2_tolower(char c)
115 inline bool DNSName::canonCompare(const DNSName& rhs) const
118 // us: 1a3www4ds9a2nl
119 // rhs: 3www6online3com
120 // to compare, we start at the back, is nl < com? no -> done
125 uint8_t ourpos[64], rhspos[64];
126 uint8_t ourcount=0, rhscount=0;
127 //cout<<"Asked to compare "<<toString()<<" to "<<rhs.toString()<<endl;
128 for(const unsigned char* p = (const unsigned char*)d_storage.c_str(); p < (const unsigned char*)d_storage.c_str() + d_storage.size() && *p && ourcount < sizeof(ourpos); p+=*p+1)
129 ourpos[ourcount++]=(p-(const unsigned char*)d_storage.c_str());
130 for(const unsigned char* p = (const unsigned char*)rhs.d_storage.c_str(); p < (const unsigned char*)rhs.d_storage.c_str() + rhs.d_storage.size() && *p && rhscount < sizeof(rhspos); p+=*p+1)
131 rhspos[rhscount++]=(p-(const unsigned char*)rhs.d_storage.c_str());
133 if(ourcount == sizeof(ourpos) || rhscount==sizeof(rhspos)) {
134 return slowCanonCompare(rhs);
138 if(ourcount == 0 && rhscount != 0)
140 if(ourcount == 0 && rhscount == 0)
142 if(ourcount !=0 && rhscount == 0)
147 bool res=std::lexicographical_compare(
148 d_storage.c_str() + ourpos[ourcount] + 1,
149 d_storage.c_str() + ourpos[ourcount] + 1 + *(d_storage.c_str() + ourpos[ourcount]),
150 rhs.d_storage.c_str() + rhspos[rhscount] + 1,
151 rhs.d_storage.c_str() + rhspos[rhscount] + 1 + *(rhs.d_storage.c_str() + rhspos[rhscount]),
152 [](const char& a, const char& b) {
153 return dns2_tolower(a) < dns2_tolower(b);
156 // cout<<"Forward: "<<res<<endl;
160 res=std::lexicographical_compare( rhs.d_storage.c_str() + rhspos[rhscount] + 1,
161 rhs.d_storage.c_str() + rhspos[rhscount] + 1 + *(rhs.d_storage.c_str() + rhspos[rhscount]),
162 d_storage.c_str() + ourpos[ourcount] + 1,
163 d_storage.c_str() + ourpos[ourcount] + 1 + *(d_storage.c_str() + ourpos[ourcount]),
164 [](const char& a, const char& b) {
165 return dns2_tolower(a) < dns2_tolower(b);
167 // cout<<"Reverse: "<<res<<endl;
175 struct CanonDNSNameCompare: public std::binary_function<DNSName, DNSName, bool>
177 bool operator()(const DNSName&a, const DNSName& b) const
179 return a.canonCompare(b);
183 inline DNSName operator+(const DNSName& lhs, const DNSName& rhs)
190 /* Quest in life: serve as a rapid block list. If you add a DNSName to a root SuffixMatchNode,
191 anything part of that domain will return 'true' in check */
192 struct SuffixMatchNode
194 SuffixMatchNode(const std::string& name_="", bool endNode_=false) : name(name_), endNode(endNode_)
198 mutable std::set<SuffixMatchNode> children;
199 mutable bool endNode;
200 bool operator<(const SuffixMatchNode& rhs) const
202 return strcasecmp(name.c_str(), rhs.name.c_str()) < 0;
205 void add(const DNSName& name)
208 d_human.append(", ");
209 d_human += name.toString();
210 add(name.getRawLabels());
213 void add(std::vector<std::string> labels) const
215 if(labels.empty()) { // this allows insertion of the root
218 else if(labels.size()==1) {
219 children.insert(SuffixMatchNode(*labels.begin(), true));
222 auto res=children.insert(SuffixMatchNode(*labels.rbegin(), false));
224 res.first->add(labels);
228 bool check(const DNSName& name) const
230 if(children.empty()) // speed up empty set
232 return check(name.getRawLabels());
235 bool check(std::vector<std::string> labels) const
237 if(labels.empty()) // optimization
240 SuffixMatchNode smn(*labels.rbegin());
241 auto child = children.find(smn);
242 if(child == children.end())
245 return child->check(labels);
248 std::string toString() const
255 std::ostream & operator<<(std::ostream &os, const DNSName& d);