6 years and counting – my Net::Patricia patch
16 01 2009Wow. It’s been more than 6 years since I reported (and provided a patch) a bug in Net::Patricia, and I’m still getting requests for the patch. It occurred to me that rather than search my email every time I get the request, why not just post it here?
I don’t think I could even find the original email thread now, but the bug is pretty well documented in the CPAN page for Net::Patricia:
The match_string method ignores the mask bits/width, if specified, in its argument. So, if you add two prefixes with the same base address but different mask widths, this module will match the most-specific prefix even if that prefix doesn't wholly cotain the prefix specified by the match argument. For example:
use Net::Patricia;
my $pt = new Net::Patricia;
$pt->add_string('192.168.0.0/25');
$pt->add_string('192.168.0.0/16');
print $pt->match_string('192.168.0.0/24'), "\n";
prints "192.168.0.0/25", just as if you had called:
print $pt->match_string('192.168.0.0'), "\n";
This issue was reported to me by John Payne, who also provided a candidate patch, but I have not applied it since I hesitate to change this behavior which was inherited from MRT. Consequently, this module might seem to violate the principle of least surprise if you specific the mask bits when trying to find the best match.
The fix is not even one line. The patch is below.
*** libpatricia/patricia.c~ Wed Oct 4 16:41:35 2000
--- libpatricia/patricia.c Tue Mar 5 11:42:23 2002
***************
*** 614,620 ****
#endif /* PATRICIA_DEBUG */
if (comp_with_mask (prefix_tochar (node->prefix),
prefix_tochar (prefix),
! node->prefix->bitlen)) {
#ifdef PATRICIA_DEBUG
fprintf (stderr, "patricia_search_best: found %s/%d\n",
prefix_toa (node->prefix), node->prefix->bitlen);
--- 614,620 ----
#endif /* PATRICIA_DEBUG */
if (comp_with_mask (prefix_tochar (node->prefix),
prefix_tochar (prefix),
! node->prefix->bitlen) && node->prefix->bitlen <= bitlen) {
#ifdef PATRICIA_DEBUG
fprintf (stderr, "patricia_search_best: found %s/%d\n",
prefix_toa (node->prefix), node->prefix->bitlen);
Categories : Hacks





