| 1 | # -*- mode: coding: utf-8; -*- | ||||||||||||||||||||||||||||
| 2 | package Net::IP::Match::Trie; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Socket qw(inet_aton); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.01_01'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $CIDR_TABLE_BITS = 8; | |||||||||||||||||||||||||||
| 12 | 1 | our $CIDR_TABLE_SIZE = (1 << $CIDR_TABLE_BITS); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # helper | ||||||||||||||||||||||||||||
| 15 | sub itonetmask($) { | ||||||||||||||||||||||||||||
| 16 | 6 | ▶ | my($n, $netmask) = @_; | ||||||||||||||||||||||||||
|
Subroutine
itonetmask: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | return () if ($n < 0 || 32 < $n); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n < 0 or 32 < $n
MC/DC0%0/2
$n < 0 or 32 < $n
$n < 0
32 < $n
Truth table33%1/3
A or B
A= $n < 0B= 32 < $n
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | my $m = 1 << (32 - $n); | |||||||||||||||||||||||||||
| 21 | 6 | --$m; | |||||||||||||||||||||||||||
| 22 | 6 | $netmask = ~$m; | |||||||||||||||||||||||||||
| 23 | 6 | return $netmask & 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub is_leaf($) { | ||||||||||||||||||||||||||||
| 27 | 262175 | ▶ | my($pt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_leaf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 262175 | return $pt->{child}[0] == $pt; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub new_trie_node() { | ||||||||||||||||||||||||||||
| 32 | 14 | ▶ | my $node = { name => "", bits => 0, child => [] }; | ||||||||||||||||||||||||||
|
Subroutine
new_trie_node: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 14 | ▶ | for (my $i = 0; $i < $CIDR_TABLE_SIZE; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 3584 | $node->{child}[$i] = $node; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 14 | return $node; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub digg_trie($) { | ||||||||||||||||||||||||||||
| 40 | 6 | ▶ | my($child) = @_; | ||||||||||||||||||||||||||
|
Subroutine
digg_trie: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 6 | my $parent = new_trie_node; | |||||||||||||||||||||||||||
| 42 | 6 | ▶ | for (my $i = 0; $i < $CIDR_TABLE_SIZE; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1536 | $parent->{child}[$i] = $child; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 6 | return $parent; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub update_leaf($$) { | ||||||||||||||||||||||||||||
| 49 | 1024 | ▶ | my($pt, $leaf) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_leaf: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 1024 | my $used = 0; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1024 | ▶ | for (my $i = 0; $i < $CIDR_TABLE_SIZE; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 262144 | my $next = $pt->{child}[$i]; | |||||||||||||||||||||||||||
| 54 | 262144 | ▶ | if (is_leaf($next)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 261376 | ▶ | if ($next->{bits} < $leaf->{bits}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1003 | $pt->{child}[$i] = $leaf; | |||||||||||||||||||||||||||
| 57 | 1003 | $used = 1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 768 | $used |= &update_leaf($next, $leaf); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1024 | return $used; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub new { | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | my($class, %opt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | my $self = bless { | |||||||||||||||||||||||||||
| 72 | }, $class; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $root = new_trie_node; | |||||||||||||||||||||||||||
| 75 | 1 | $root->{name} = "R"; | |||||||||||||||||||||||||||
| 76 | 1 | my $nullnode = new_trie_node; | |||||||||||||||||||||||||||
| 77 | 1 | ▶ | for (my $i=0; $i < $CIDR_TABLE_SIZE; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 256 | $root->{child}[$i] = $nullnode; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | $self->{root} = $root; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | return $self; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # name => [ cidr1, cidr2, ... ] | ||||||||||||||||||||||||||||
| 86 | sub add { | ||||||||||||||||||||||||||||
| 87 | 4 | ▶ | my($self, $name, $cidrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | my $ad; | |||||||||||||||||||||||||||
| 90 | 4 | my $nm = 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ### name: $name | ||||||||||||||||||||||||||||
| 93 | 4 | for my $cidr (@$cidrs) { | |||||||||||||||||||||||||||
| 94 | 6 | my($ip, $len) = split m{/}, $cidr, 2; | |||||||||||||||||||||||||||
| 95 | 6 | ▶ | $len ||= 32; | ||||||||||||||||||||||||||
|
Condition50%1/2
$len ||= 32
MC/DC0%0/1
$len ||= 32
$len
Truth table50%1/2
A
A= $len
| |||||||||||||||||||||||||||||
| 96 | ### cidr, ip, len: join ', ', $cidr, $ip, $len | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 6 | $ad = unpack "N", inet_aton($ip); | |||||||||||||||||||||||||||
| 99 | 6 | $nm = itonetmask($len); | |||||||||||||||||||||||||||
| 100 | ### ad : sprintf "%08X", $ad | ||||||||||||||||||||||||||||
| 101 | ### nm : sprintf "%08X", $nm | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | $ad = $ad & ($nm & 0xFFFFFFFF); | |||||||||||||||||||||||||||
| 104 | ### ad&nm: sprintf "%08X", $ad | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | my $pt = $self->{root}; | |||||||||||||||||||||||||||
| 107 | 6 | my $p_leaf = new_trie_node; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 6 | $p_leaf->{name} = $name; | |||||||||||||||||||||||||||
| 110 | 6 | $p_leaf->{bits} = $len; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | while ($len > $CIDR_TABLE_BITS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | ### ad : sprintf "%08X", $ad | ||||||||||||||||||||||||||||
| 114 | 10 | my $b = $ad >> (32 - $CIDR_TABLE_BITS); | |||||||||||||||||||||||||||
| 115 | ### b: $b | ||||||||||||||||||||||||||||
| 116 | 10 | my $next = $pt->{child}[$b]; | |||||||||||||||||||||||||||
| 117 | 10 | ▶ | if (is_leaf($next)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 6 | $pt->{child}[$b] = $next = digg_trie($next); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 10 | $pt = $next; | |||||||||||||||||||||||||||
| 121 | 10 | $ad = $ad << $CIDR_TABLE_BITS & 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 122 | 10 | $len -= $CIDR_TABLE_BITS; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 6 | my $bmin = $ad >> (32 - $CIDR_TABLE_BITS); | |||||||||||||||||||||||||||
| 127 | 6 | my $bmax = $bmin + (1 << ($CIDR_TABLE_BITS - $len)); | |||||||||||||||||||||||||||
| 128 | 6 | my $used = 0; | |||||||||||||||||||||||||||
| 129 | 6 | ▶ | for (my $i = $bmin; $i < $bmax; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 21 | my $target = $pt->{child}[$i]; | |||||||||||||||||||||||||||
| 131 | 21 | ▶ | if (is_leaf($target)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 20 | ▶ | if ($target->{bits} < $p_leaf->{bits}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 20 | $pt->{child}[$i] = $p_leaf; | |||||||||||||||||||||||||||
| 134 | 20 | $used = 1; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 1 | ▶ | for (my $j = 0; $j < $CIDR_TABLE_SIZE; $j++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 256 | $used |= update_leaf($target, $p_leaf); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub match_ip { | ||||||||||||||||||||||||||||
| 147 | 10 | ▶ | my($self, $ip) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match_ip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 10 | my @addrs = split /\./, $ip, 4; | |||||||||||||||||||||||||||
| 150 | 10 | return $self->{root}{child}[$addrs[0]]->{child}[$addrs[1]]->{child}[$addrs[2]]->{child}[$addrs[3]]->{name}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub impl { | ||||||||||||||||||||||||||||
| 154 | 1 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
impl: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 1 | return "PP"; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | 1; | |||||||||||||||||||||||||||