From: Remi Gacogne Date: Mon, 12 Dec 2016 16:22:00 +0000 (+0100) Subject: Merge pull request #4541 from rgacogne/fix-suffix-match-tree X-Git-Tag: dnsdist-1.1.0-beta2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2845aad9d2b3d82974a96496c4ca02b55cb80645;p=thirdparty%2Fpdns.git Merge pull request #4541 from rgacogne/fix-suffix-match-tree dnsdist: Fix insertion issues in SuffixMatchTree, move it to dnsname.hh --- 2845aad9d2b3d82974a96496c4ca02b55cb80645 diff --cc pdns/dnsdist.hh index a8d877fbab,dbbfb3d83a..e7851cefd0 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@@ -569,102 -554,7 +569,8 @@@ enum ednsHeaderFlags EDNS_HEADER_FLAG_DO = 32768 }; - /* Quest in life: serve as a rapid block list. If you add a DNSName to a root SuffixMatchNode, - anything part of that domain will return 'true' in check */ - template - struct SuffixMatchTree - { - SuffixMatchTree(const std::string& name_="", bool endNode_=false) : name(name_), endNode(endNode_) - {} - - SuffixMatchTree(const SuffixMatchTree& rhs) - { - name = rhs.name; - d_human = rhs.d_human; - children = rhs.children; - endNode = rhs.endNode; - d_value = rhs.d_value; - } - std::string name; - std::string d_human; - mutable std::set children; - mutable bool endNode; - mutable T d_value; - bool operator<(const SuffixMatchTree& rhs) const - { - return strcasecmp(name.c_str(), rhs.name.c_str()) < 0; - } - typedef SuffixMatchTree value_type; - - template - void visit(const V& v) const { - for(const auto& c : children) - c.visit(v); - if(endNode) - v(*this); - } - - void add(const DNSName& name, const T& t) - { - add(name.getRawLabels(), t); - } - - void add(std::vector labels, const T& value) const - { - if(labels.empty()) { // this allows insertion of the root - endNode=true; - d_value=value; - } - else if(labels.size()==1) { - SuffixMatchTree newChild(*labels.begin(), true); - newChild.d_value=value; - children.insert(newChild); - } - else { - SuffixMatchTree newnode(*labels.rbegin(), false); - auto res=children.insert(newnode); - if(!res.second) { - children.erase(newnode); - res=children.insert(newnode); - } - labels.pop_back(); - res.first->add(labels, value); - } - } - - T* lookup(const DNSName& name) const - { - if(children.empty()) { // speed up empty set - if(endNode) - return &d_value; - return 0; - } - return lookup(name.getRawLabels()); - } - - T* lookup(std::vector labels) const - { - if(labels.empty()) { // optimization - if(endNode) - return &d_value; - return 0; - } - - SuffixMatchTree smn(*labels.rbegin()); - auto child = children.find(smn); - if(child == children.end()) { - if(endNode) - return &d_value; - return 0; - } - labels.pop_back(); - return child->lookup(labels); - } - - }; - extern GlobalStateHolder> g_dynblockSMT; +extern DNSAction::Action g_dynBlockAction; extern GlobalStateHolder > g_carbon; extern GlobalStateHolder g_policy;