10 // #include "logger.hh"
12 // #include <ext/vstring.h>
15 accept escaped ascii presentations of DNS names and store them "natively"
16 accept a DNS packet with an offset, and extract a DNS name from it
17 build up DNSNames with prepend and append of 'raw' unescaped labels
19 Be able to turn them into ASCII and "DNS name in a packet" again on request
21 Provide some common operators for comparison, detection of being part of another domain
23 NOTE: For now, everything MUST be . terminated, otherwise it is an error
30 DNSName() : d_empty(true), d_recurse(0) {} //!< Don't constructs the root name
31 explicit DNSName(const char* p); //!< Constructs from a human formatted, escaped presentation
32 explicit DNSName(const std::string& str) : DNSName(str.c_str()) {} //!< Constructs from a human formatted, escaped presentation
33 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
35 bool isPartOf(const DNSName& rhs) const; //!< Are we part of the rhs name?
36 bool operator==(const DNSName& rhs) const; //!< DNS-native comparison (case insensitive)
37 bool operator!=(const DNSName& other) const { return !(*this == other); }
39 std::string toString(const std::string& separator=".", const bool trailing=true) const; //!< Our human-friendly, escaped, representation
40 std::string toStringNoDot() const { return toString(".", false); }
41 std::string toDNSString() const; //!< Our representation in DNS native format
42 void appendRawLabel(const std::string& str); //!< Append this unescaped label
43 void prependRawLabel(const std::string& str); //!< Prepend this unescaped label
44 std::vector<std::string> getRawLabels() const; //!< Individual raw unescaped labels
45 bool chopOff(); //!< Turn www.powerdns.com. into powerdns.com., returns false for .
46 DNSName makeRelative(const DNSName& zone) const;
47 DNSName labelReverse() const;
48 bool isWildcard() const;
49 unsigned int countLabels() const;
50 size_t length() const; // FIXME400 remove me?
51 bool empty() const { return d_empty; }
52 bool isRoot() const { return !d_empty && d_storage.empty(); }
53 void clear() { d_storage.clear(); d_empty=true; }
54 void trimToLabels(unsigned int);
55 DNSName& operator+=(const DNSName& rhs)
57 if(d_storage.size() + rhs.d_storage.size() > 254) // reserve one byte for the root label
58 throw std::range_error("name too long");
60 d_storage+=rhs.d_storage;
65 bool operator<(const DNSName& rhs) const // this delivers _some_ kind of ordering, but not one useful in a DNS context. Really fast though.
67 return std::lexicographical_compare(d_storage.rbegin(), d_storage.rend(),
68 rhs.d_storage.rbegin(), rhs.d_storage.rend(),
69 [](const char& a, const char& b) {
70 return tolower(a) < tolower(b);
71 }); // note that this is case insensitive, including on the label lengths
74 template<class Archive>
75 void serialize(Archive &ar, const unsigned int version)
81 inline bool canonCompare(const DNSName& rhs) const;
84 // typedef __gnu_cxx::__sso_string string_t;
85 typedef std::string string_t;
86 bool slowCanonCompare(const DNSName& rhs) const;
91 void packetParser(const char* p, int len, int offset, bool uncompress, uint16_t* qtype=0, uint16_t* qclass=0, unsigned int* consumed=0);
92 static std::string escapeLabel(const std::string& orig);
93 static std::string unescapeLabel(const std::string& orig);
96 size_t hash_value(DNSName const& d);
98 inline char dns2_tolower(char c)
106 inline bool DNSName::canonCompare(const DNSName& rhs) const
109 // us: 1a3www4ds9a2nl
110 // rhs: 3www6online3com
111 // to compare, we start at the back, is nl < com? no -> done
116 uint8_t ourpos[64], rhspos[64];
117 uint8_t ourcount=0, rhscount=0;
118 //cout<<"Asked to compare "<<toString()<<" to "<<rhs.toString()<<endl;
119 for(const char* p = d_storage.c_str(); p < d_storage.c_str() + d_storage.size() && ourcount < sizeof(ourpos); p+=*p+1)
120 ourpos[ourcount++]=(p-d_storage.c_str());
121 for(const char* p = rhs.d_storage.c_str(); p < rhs.d_storage.c_str() + rhs.d_storage.size() && rhscount < sizeof(rhspos); p+=*p+1)
122 rhspos[rhscount++]=(p-rhs.d_storage.c_str());
124 if(ourcount == sizeof(ourpos) || rhscount==sizeof(rhspos)) {
125 return slowCanonCompare(rhs);
129 if(ourcount == 0 && rhscount != 0)
131 if(ourcount == 0 && rhscount == 0)
133 if(ourcount !=0 && rhscount == 0)
139 cout<<"Going to compare: '"<<string(d_storage.c_str() + ourpos[ourcount] + 1,
140 d_storage.c_str() + ourpos[ourcount] + 1 + *(d_storage.c_str() + ourpos[ourcount]))<<"'"<<endl;
141 cout<<"Against: '"<<string(rhs.d_storage.c_str() + rhspos[rhscount] + 1,
142 rhs.d_storage.c_str() + rhspos[rhscount] + 1 + *(rhs.d_storage.c_str() + rhspos[rhscount]))<<"'"<<endl;
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);