From: Remi Gacogne Date: Wed, 20 Oct 2021 16:19:01 +0000 (+0200) Subject: NetmaskTree: Unify the two existing lookup() methods X-Git-Tag: rec-4.6.0-beta1~39^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5d2c20f745e8340d47c209e7d323165d2ddfef41;p=thirdparty%2Fpdns.git NetmaskTree: Unify the two existing lookup() methods --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 9da0ea07dd..c46c5a8655 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1127,113 +1127,18 @@ public: //left.get(); - else if (value.isIPv6()) - node = d_root->right.get(); - else - throw NetmaskException("invalid address family"); - if (node == nullptr) return nullptr; - - node_type *ret = nullptr; - - int bits = 0; - for(; bits < max_bits; bits++) { - bool vall = value.getBit(-1-bits); - if (bits >= node->d_bits) { - // the end of the current node is reached; continue with the next - // (we keep track of last assigned node) - if (node->assigned && bits == node->node.first.getBits()) - ret = &node->node; - if (vall) { - if (!node->right) - break; - node = node->right.get(); - } else { - if (!node->left) - break; - node = node->left.get(); - } - continue; - } - if (bits >= node->node.first.getBits()) { - // the matching branch ends here - break; - } - bool valr = node->node.first.getBit(-1-bits); - if (vall != valr) { - // the branch matches just upto this point, yet continues in a different - // direction - break; - } - } - // needed if we did not find one in loop - if (node->assigned && bits == node->node.first.getBits()) - ret = &node->node; - - // this can be nullptr. - return ret; - + return lookupImpl(value, max_bits); } // addr_bits) + if (max_bits < 0 || max_bits > addr_bits) { max_bits = addr_bits; - - if (value.isIPv4()) - node = d_root->left.get(); - else if (value.isIPv6()) - node = d_root->right.get(); - else - throw NetmaskException("invalid address family"); - if (node == nullptr) return nullptr; - - node_type *ret = nullptr; - - int bits = 0; - for(; bits < max_bits; bits++) { - bool vall = value.getBit(-1-bits); - if (bits >= node->d_bits) { - // the end of the current node is reached; continue with the next - // (we keep track of last assigned node) - if (node->assigned && bits == node->node.first.getBits()) - ret = &node->node; - if (vall) { - if (!node->right) - break; - node = node->right.get(); - } else { - if (!node->left) - break; - node = node->left.get(); - } - continue; - } - if (bits >= node->node.first.getBits()) { - // the matching branch ends here - break; - } - bool valr = node->node.first.getBit(-1-bits); - if (vall != valr) { - // the branch matches just upto this point, yet continues in a different - // direction - break; - } } - // needed if we did not find one in loop - if (node->assigned && bits == node->node.first.getBits()) - ret = &node->node; - // this can be nullptr. - return ret; + return lookupImpl(key_type(value, max_bits), max_bits); } //left.get(); + else if (value.isIPv6()) + node = d_root->right.get(); + else + throw NetmaskException("invalid address family"); + if (node == nullptr) return nullptr; + + node_type *ret = nullptr; + + int bits = 0; + for(; bits < max_bits; bits++) { + bool vall = value.getBit(-1-bits); + if (bits >= node->d_bits) { + // the end of the current node is reached; continue with the next + // (we keep track of last assigned node) + if (node->assigned && bits == node->node.first.getBits()) + ret = &node->node; + if (vall) { + if (!node->right) + break; + node = node->right.get(); + } else { + if (!node->left) + break; + node = node->left.get(); + } + continue; + } + if (bits >= node->node.first.getBits()) { + // the matching branch ends here + break; + } + bool valr = node->node.first.getBit(-1-bits); + if (vall != valr) { + // the branch matches just upto this point, yet continues in a different + // direction + break; + } + } + // needed if we did not find one in loop + if (node->assigned && bits == node->node.first.getBits()) + ret = &node->node; + + // this can be nullptr. + return ret; + } + unique_ptr d_root; //