blib/lib/Tie/NetAddr/IP.pm

filter: Statementstmt 91.2% 73 / 80 Branchbran 70.8% 17 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 1 totaltotal 85.3% 105 / 123 CC 20 SCAR 18.5
CC
20
Cov
86%
NEXTKEY
12.2
FETCH
6.0
EXISTS
2.1
CRAP 6.4
↑ Summary
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
BranchTrueFalseTotal
if ($ip) { } 27 0 27
81 27 my @fles = reverse @$self;
82 27 for my $item (@fles) {
83 2103 ▶ next unless ref $item;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item 119 1984 2103
84 1984 for my $a (keys %{$item}) {
85 68 ▶ if ($item->{$a}->{where}->contains($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{$a}{"where"}->contains($ip)) 26 42 68
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
BranchTrueFalseTotal
if ($ip) { } 23 0 23
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
BranchTrueFalseTotal
if ($ip) { } 2 0 2
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
BranchTrueFalseTotal
if ($ip) { } 5 0 5
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
BranchTrueFalseTotal
if (defined $last) { } 27 10 37
154 27 my $l_ip = new NetAddr::IP $last;
155 27 ▶ return undef unless $l_ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $l_ip 0 27 27
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
BranchTrueFalseTotal
if ($a eq $l_ip->addr and $bits == $l_ip->masklen) 27 50 77
Condition66%2/3
$a eq $l_ip->addr and $bits == $l_ip->masklen
!ll&&!rl&&r
50027
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
ABresult
0X0
100
111
162 27 $found = 1;
163 27 next;
164 }
165 50 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 18 32 50
166 18 my $r = $self->[$bits]->{$a}->{where}->cidr;
167 18 ▶ return wantarray ? ($r) : $r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 18 18
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
BranchTrueFalseTotal
wantarray ? : 0 9 9
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__
↑ Summary