8 // #include <boost/container/string.hpp>
10 uint32_t burtleCI(const unsigned char* k, uint32_t lengh, uint32_t init);
13 // #include "logger.hh"
15 //#include <ext/vstring.h>
18 accept escaped ascii presentations of DNS names and store them "natively"
19 accept a DNS packet with an offset, and extract a DNS name from it
20 build up DNSNames with prepend and append of 'raw' unescaped labels
22 Be able to turn them into ASCII and "DNS name in a packet" again on request
24 Provide some common operators for comparison, detection of being part of another domain
26 NOTE: For now, everything MUST be . terminated, otherwise it is an error
29 inline char dns2_tolower(char c)
39 DNSName() {} //!< Constructs an *empty* DNSName, NOT the root!
40 explicit DNSName(const char* p); //!< Constructs from a human formatted, escaped presentation
41 explicit DNSName(const std::string& str) : DNSName(str.c_str()) {} //!< Constructs from a human formatted, escaped presentation
42 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
44 bool isPartOf(const DNSName& rhs) const; //!< Are we part of the rhs name?
45 bool operator==(const DNSName& rhs) const; //!< DNS-native comparison (case insensitive) - empty compares to empty
46 bool operator!=(const DNSName& other) const { return !(*this == other); }
48 std::string toString(const std::string& separator=".", const bool trailing=true) const; //!< Our human-friendly, escaped, representation
49 std::string toStringNoDot() const { return toString(".", false); }
50 std::string toStringRootDot() const { if(isRoot()) return "."; else return toString(".", false); }
51 std::string toDNSString() const; //!< Our representation in DNS native format
52 std::string toDNSStringLC() const; //!< Our representation in DNS native format, lower cased
53 void appendRawLabel(const std::string& str); //!< Append this unescaped label
54 void appendRawLabel(const char* start, unsigned int length); //!< Append this unescaped label
55 void prependRawLabel(const std::string& str); //!< Prepend this unescaped label
56 std::vector<std::string> getRawLabels() const; //!< Individual raw unescaped labels
57 bool chopOff(); //!< Turn www.powerdns.com. into powerdns.com., returns false for .
58 DNSName makeRelative(const DNSName& zone) const;
59 void makeUsRelative(const DNSName& zone);
60 DNSName labelReverse() const;
61 bool isWildcard() const;
62 unsigned int countLabels() const;
63 size_t wirelength() const; //!< Number of total bytes in the name
64 bool empty() const { return d_storage.empty(); }
65 bool isRoot() const { return d_storage.size()==1 && d_storage[0]==0; }
66 void clear() { d_storage.clear(); }
67 void trimToLabels(unsigned int);
68 size_t hash(size_t init=0) const
70 return burtleCI((const unsigned char*)d_storage.c_str(), d_storage.size(), init);
72 DNSName& operator+=(const DNSName& rhs)
74 if(d_storage.size() + rhs.d_storage.size() > 256) // reserve one byte for the root label
75 throw std::range_error("name too long");
80 d_storage+=rhs.d_storage;
82 d_storage.replace(d_storage.length()-1, rhs.d_storage.length(), rhs.d_storage);
87 bool operator<(const DNSName& rhs) const // this delivers _some_ kind of ordering, but not one useful in a DNS context. Really fast though.
89 return std::lexicographical_compare(d_storage.rbegin(), d_storage.rend(),
90 rhs.d_storage.rbegin(), rhs.d_storage.rend(),
91 [](const char& a, const char& b) {
92 return dns2_tolower(a) < dns2_tolower(b);
93 }); // note that this is case insensitive, including on the label lengths
96 inline bool canonCompare(const DNSName& rhs) const;
97 bool slowCanonCompare(const DNSName& rhs) const;
99 // typedef boost::container::string string_t;
100 typedef std::string string_t;
104 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);
105 static std::string escapeLabel(const std::string& orig);
106 static std::string unescapeLabel(const std::string& orig);
109 size_t hash_value(DNSName const& d);
112 inline bool DNSName::canonCompare(const DNSName& rhs) const
115 // us: 1a3www4ds9a2nl
116 // rhs: 3www6online3com
117 // to compare, we start at the back, is nl < com? no -> done
122 uint8_t ourpos[64], rhspos[64];
123 uint8_t ourcount=0, rhscount=0;
124 //cout<<"Asked to compare "<<toString()<<" to "<<rhs.toString()<<endl;
125 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)
126 ourpos[ourcount++]=(p-(const unsigned char*)d_storage.c_str());
127 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)
128 rhspos[rhscount++]=(p-(const unsigned char*)rhs.d_storage.c_str());
130 if(ourcount == sizeof(ourpos) || rhscount==sizeof(rhspos)) {
131 return slowCanonCompare(rhs);
135 if(ourcount == 0 && rhscount != 0)
137 if(ourcount == 0 && rhscount == 0)
139 if(ourcount !=0 && rhscount == 0)
144 bool res=std::lexicographical_compare(
145 d_storage.c_str() + ourpos[ourcount] + 1,
146 d_storage.c_str() + ourpos[ourcount] + 1 + *(d_storage.c_str() + ourpos[ourcount]),
147 rhs.d_storage.c_str() + rhspos[rhscount] + 1,
148 rhs.d_storage.c_str() + rhspos[rhscount] + 1 + *(rhs.d_storage.c_str() + rhspos[rhscount]),
149 [](const char& a, const char& b) {
150 return dns2_tolower(a) < dns2_tolower(b);
153 // cout<<"Forward: "<<res<<endl;
157 res=std::lexicographical_compare( rhs.d_storage.c_str() + rhspos[rhscount] + 1,
158 rhs.d_storage.c_str() + rhspos[rhscount] + 1 + *(rhs.d_storage.c_str() + rhspos[rhscount]),
159 d_storage.c_str() + ourpos[ourcount] + 1,
160 d_storage.c_str() + ourpos[ourcount] + 1 + *(d_storage.c_str() + ourpos[ourcount]),
161 [](const char& a, const char& b) {
162 return dns2_tolower(a) < dns2_tolower(b);
164 // cout<<"Reverse: "<<res<<endl;
172 struct CanonDNSNameCompare: public std::binary_function<DNSName, DNSName, bool>
174 bool operator()(const DNSName&a, const DNSName& b) const
176 return a.canonCompare(b);
180 inline DNSName operator+(const DNSName& lhs, const DNSName& rhs)
187 /* Quest in life: serve as a rapid block list. If you add a DNSName to a root SuffixMatchNode,
188 anything part of that domain will return 'true' in check */
189 struct SuffixMatchNode
191 SuffixMatchNode(const std::string& name_="", bool endNode_=false) : name(name_), endNode(endNode_)
195 mutable std::set<SuffixMatchNode> children;
196 mutable bool endNode;
197 bool operator<(const SuffixMatchNode& rhs) const
199 return strcasecmp(name.c_str(), rhs.name.c_str()) < 0;
202 void add(const DNSName& name)
205 d_human.append(", ");
206 d_human += name.toString();
207 add(name.getRawLabels());
210 void add(std::vector<std::string> labels) const
212 if(labels.empty()) { // this allows insertion of the root
215 else if(labels.size()==1) {
216 children.insert(SuffixMatchNode(*labels.begin(), true));
219 auto res=children.insert(SuffixMatchNode(*labels.rbegin(), false));
221 res.first->add(labels);
225 bool check(const DNSName& name) const
227 if(children.empty()) // speed up empty set
229 return check(name.getRawLabels());
232 bool check(std::vector<std::string> labels) const
234 if(labels.empty()) // optimization
237 SuffixMatchNode smn(*labels.rbegin());
238 auto child = children.find(smn);
239 if(child == children.end())
242 return child->check(labels);
245 std::string toString() const
252 std::ostream & operator<<(std::ostream &os, const DNSName& d);
255 struct hash<DNSName> {
256 size_t operator () (const DNSName& dn) const { return dn.hash(0); }