| 1 | package Tie::NetAddr::IP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Tie::NetAddr::IP - Implements a Hash where the key is a subnet | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Tie::NetAddr::IP; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my %WhereIs; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | tie %WhereIs, Tie::NetAddr::IP; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $WhereIs{"10.0.10.0/24"} = "Lab, First Floor"; | ||||||||||||||||||||||||||||
| 18 | $WhereIs{"10.0.20.0/24"} = "Datacenter, Second Floor"; | ||||||||||||||||||||||||||||
| 19 | $WhereIs{"10.0.30.0/27"} = "Remote location"; | ||||||||||||||||||||||||||||
| 20 | $WhereIs{"0.0.0.0/0"} = "God knows where"; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | foreach $host ("10.0.10.1", "10.0.20.15", "10.0.32.17", "10.10.0.1") { | ||||||||||||||||||||||||||||
| 23 | print "Host $host is in ", $WhereIs{$host}, "\n"; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | foreach $subnet (keys %WhereIs) { | ||||||||||||||||||||||||||||
| 27 | print "Network ", $subnet, " is used in ", | ||||||||||||||||||||||||||||
| 28 | $WhereIs{$subnet}, "\n"; | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | untie %WhereIs; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | This module overloads hashes so that the key can be a subnet as in | ||||||||||||||||||||||||||||
| 36 | B<NetAddr::IP>. When looking values up, an interpretation will be made | ||||||||||||||||||||||||||||
| 37 | to find the given key B<within> the subnets specified in the hash. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | The code sample provided on the B<SYNOPSIS> would print out the | ||||||||||||||||||||||||||||
| 40 | locations of every machine in the C<foreach> loop. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Care must be taken, as only strings that can be parsed as an IP | ||||||||||||||||||||||||||||
| 43 | address by B<NetAddr::IP> can be used as keys for this hash. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Iterators on the hash such as C<foreach>, C<each>, C<keys> and | ||||||||||||||||||||||||||||
| 46 | C<values> will only see the actual subnets provided as keys to the | ||||||||||||||||||||||||||||
| 47 | hash. When looking up a value such as in C<$hash{$ipaddress}> this IP | ||||||||||||||||||||||||||||
| 48 | address will be looked up among the subnets existing as keys within | ||||||||||||||||||||||||||||
| 49 | the hash. The matching subnet with the longest mask (ie, the most | ||||||||||||||||||||||||||||
| 50 | specific subnet) will win and its associated value will be returned. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | This code can be distributed freely according to the terms set forth | ||||||||||||||||||||||||||||
| 53 | in the PERL license provided that proper credit is maintained. Please | ||||||||||||||||||||||||||||
| 54 | send bug reports and feedback to the author for further improvement. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | use NetAddr::IP 3.00; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | $VERSION = '1.51'; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | TIEHASH(shift); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 71 | 5 | my $self = [ ]; | |||||||||||||||||||||||||||
| 72 | 5 | bless $self, $class; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 27 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 76 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 77 | 27 | my $where = shift; | |||||||||||||||||||||||||||
| 78 | 27 | my $ip = new NetAddr::IP $where; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 27 | ▶ | if ($ip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 27 | my @fles = reverse @$self; | |||||||||||||||||||||||||||
| 82 | 27 | for my $item (@fles) { | |||||||||||||||||||||||||||
| 83 | 2103 | ▶ | next unless ref $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1984 | for my $a (keys %{$item}) { | |||||||||||||||||||||||||||
| 85 | 68 | ▶ | if ($item->{$a}->{where}->contains($ip)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 26 | return $item->{$a}->{what}; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 0 | croak "$where is not a valid NetAddr::IP specification"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | return; # None of the networks matched the spec | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 23 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 98 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 23 | my $where = shift; | |||||||||||||||||||||||||||
| 100 | 23 | my $what = shift; | |||||||||||||||||||||||||||
| 101 | 23 | my $ip = new NetAddr::IP $where; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 23 | ▶ | if ($ip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 23 | $self->[$ip->masklen]->{$ip->addr} = { | |||||||||||||||||||||||||||
| 105 | where => $ip, | ||||||||||||||||||||||||||||
| 106 | what => $what, | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 0 | croak "$where is not a valid IP address specification"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 114 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 2 | my $where = shift; | |||||||||||||||||||||||||||
| 116 | 2 | my $ip = new NetAddr::IP $where; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | if ($ip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 2 | return exists $self->[$ip->masklen]->{$ip->addr}; | |||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||
| 121 | 0 | croak "$where is not a valid NetAddr::IP specification"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | return; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 5 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||
| 127 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 128 | 5 | my $where = shift; | |||||||||||||||||||||||||||
| 129 | 5 | my $ip = new NetAddr::IP $where; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | if ($ip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 5 | my $mask = $ip->masklen; | |||||||||||||||||||||||||||
| 133 | 5 | my $addr = $ip->addr; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 5 | return delete $self->[$mask]->{$addr}; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 0 | croak "$where is not a valid NetAddr::IP specification"; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 2 | splice(@$self, 0, $#{$self}); | |||||||||||||||||||||||||||
| 146 | 2 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 37 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||
| 150 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 37 | my $last = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 37 | ▶ | if (defined $last) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 27 | my $l_ip = new NetAddr::IP $last; | |||||||||||||||||||||||||||
| 155 | 27 | ▶ | return undef unless $l_ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 27 | my $found = 0; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 27 | for my $bits ($l_ip->masklen .. 128) { | |||||||||||||||||||||||||||
| 160 | 1155 | for my $a (keys %{$self->[$bits]}) { | |||||||||||||||||||||||||||
| 161 | 77 | ▶ | if ($a eq $l_ip->addr and $bits == $l_ip->masklen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$a eq $l_ip->addr and $bits == $l_ip->masklen
MC/DC50%1/2
$a eq $l_ip->addr and $bits == $l_ip->masklen
$a eq $l_ip->addr
$bits == $l_ip->masklen
Truth table66%2/3
A and B
A= $a eq $l_ip->addrB= $bits == $l_ip->masklen
| |||||||||||||||||||||||||||||
| 162 | 27 | $found = 1; | |||||||||||||||||||||||||||
| 163 | 27 | next; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 50 | ▶ | if ($found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 18 | my $r = $self->[$bits]->{$a}->{where}->cidr; | |||||||||||||||||||||||||||
| 167 | 18 | ▶ | return wantarray ? ($r) : $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } else { | ||||||||||||||||||||||||||||
| 172 | 10 | for my $bits (0 .. 128) { | |||||||||||||||||||||||||||
| 173 | 162 | for my $a (keys %{$self->[$bits]}) { | |||||||||||||||||||||||||||
| 174 | 9 | my $r = $self->[$bits]->{$a}->{where}->cidr; | |||||||||||||||||||||||||||
| 175 | 9 | ▶ | return wantarray ? ($r) : $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 10 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 10 | ▶ | sub FIRSTKEY { NEXTKEY $_[0], undef; } | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 6 | 1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||