Paranoid/Network/IPv4.pm

filter: Statementstmt 98.3% 116 / 118 Branchbran 82.3% 28 / 34 Conditioncond 61.9% 13 / 21 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 21 / 21 Podpod 100% 5 / 5 totaltotal 88.7% 189 / 213 CC 25 SCAR 21.4
CC
25
Cov
91%
ipv4NetConvert
14.7
ipv4NetIntersect
13.2
BEGIN
1.0
CRAP 8.5
« Paranoid/IO/FileMultiplexer/Block/BATHeader.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/FileHeader.pm »
1 # Paranoid::Network::IPv4 -- IPv4-specific network functions
2 #
3 # $Id: lib/Paranoid/Network/IPv4.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Network::IPv4;
33  
34 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Paranoid::Network::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 my @base = qw(ipv4NetConvert ipv4NetIntersect);
45 4 my @constants = qw(MAXIPV4CIDR IPV4REGEX IPV4CIDRRGX IPV4BASE IPV4BRDCST
46 IPV4MASK);
47 4 my @ipv4sort = qw(ipv4NumSort ipv4StrSort ipv4PackedSort);
48  
49 4 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
50 4 @EXPORT = @base;
51 4 @EXPORT_OK = ( @base, @constants, @ipv4sort );
52 4 %EXPORT_TAGS = (
53 all => [@EXPORT_OK],
54 base => [@base],
55 constants => [@constants],
56 ipv4Sort => [@ipv4sort],
57 );
58  
59 4 ▶use constant MAXIPV4CIDR => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 4 ▶use constant IPV4REGEX =>
Subroutine BEGIN: called CC=1 SCAR=0.0
61 4 qr/(?:(?:25[0-5]|2[0-4][0-9]|1?\d\d?)\.){3}(?:25[0-5]|2[0-4][0-9]|1?\d\d?)/s;
62 4 ▶use constant IPV4CIDRRGX =>
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 qr#@{[ IPV4REGEX ]}/(?:(?:3[0-2]|[12]?\d)|@{[ IPV4REGEX ]})#s;
64 4 ▶use constant FULLMASK => 0xffffffff;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 ▶use constant IPV4BASE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 4 ▶use constant IPV4BRDCST => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 4 ▶use constant IPV4MASK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 #####################################################################
70 #
71 # Module code follows
72 #
73 #####################################################################
74  
75 41 ▶sub ipv4NetConvert {
Subroutine ipv4NetConvert: called CC=13 SCAR=26.9
Pod documented
76  
77 # Purpose: Takes a string representation of an IPv4 network
78 # address and returns a list containing the binary
79 # network address, broadcast address, and netmask.
80 # Also allows for a plain IP being passed, in which
81 # case it only returns the binary IP.
82 # Returns: Array, empty on errors
83 # Usage: @network = ipv4NetConvert($netAddr);
84  
85 41 my $netAddr = shift;
86 41 my ( $bnet, $bmask, $t, @rv );
87  
88 41 subPreamble( PDLEVEL1, '$', $netAddr );
89  
90 # Extract net address, mask
91 41 ▶ if ( defined $netAddr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $netAddr) 40 1 41
92 40 ($t) = ( $netAddr =~ m#^(@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$#s )[0];
93 40 ▶ ( $bnet, $bmask ) = split m#/#s, $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 33 7 40
94 }
95  
96 41 ▶ if ( defined $bnet and length $bnet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bnet and length $bnet) { } 33 8 41
Condition66%2/3
defined $bnet and length $bnet
!ll&&!rl&&r
8033
MC/DC50%1/2
defined $bnet and length $bnet
defined $bnet length $bnet
Truth table66%2/3
A and B
A= defined $bnetB= length $bnet
ABresult
0X0
100
111
97  
98 # First, convert $bnet to see if we have a valid IP address
99 33 $bnet = unpack 'N', inet_aton($bnet);
100  
101 33 ▶ if ( defined $bnet and length $bnet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bnet and length $bnet) { } 33 0 33
Condition33%1/3
defined $bnet and length $bnet
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $bnet and length $bnet
defined $bnet length $bnet
Truth table33%1/3
A and B
A= defined $bnetB= length $bnet
ABresult
0X0
100
111
102  
103 # Save our network address
104 33 push @rv, $bnet;
105  
106 33 ▶ if ( defined $bmask and length $bmask ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bmask and length $bmask) 24 9 33
Condition66%2/3
defined $bmask and length $bmask
!ll&&!rl&&r
9024
MC/DC50%1/2
defined $bmask and length $bmask
defined $bmask length $bmask
Truth table66%2/3
A and B
A= defined $bmaskB= length $bmask
ABresult
0X0
100
111
107  
108 # Convert netmask
109 24 ▶ $bmask =
Branch: 2 branches
BranchTrueFalseTotal
!($bmask =~ /^\d+$/s) ? : 2 22 24
$bmask <= 32 ? : 22 0 22
110 $bmask !~ /^\d+$/s ? unpack 'N', inet_aton($bmask)
111 : $bmask <= MAXIPV4CIDR
112 ? FULLMASK - ( ( 2**( MAXIPV4CIDR - $bmask ) ) - 1 )
113 : undef;
114  
115 24 ▶ if ( defined $bmask and length $bmask ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bmask and length $bmask) { } 24 0 24
Condition33%1/3
defined $bmask and length $bmask
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $bmask and length $bmask
defined $bmask length $bmask
Truth table33%1/3
A and B
A= defined $bmaskB= length $bmask
ABresult
0X0
100
111
116  
117 # Apply the mask to the base address
118 24 $rv[IPV4BASE] = $rv[IPV4BASE] & $bmask;
119  
120 # Calculate and save our broadcast address
121 24 push @rv, $bnet | ( $bmask ^ FULLMASK );
122  
123 # Save our mask
124 24 push @rv, $bmask;
125  
126 } else {
127 0 pdebug( 'invalid netmask passed', PDLEVEL1 );
128 }
129 }
130 } else {
131 0 pdebug( 'failed to convert IPv4 address', PDLEVEL1 );
132 }
133 } else {
134 8 pdebug( 'failed to extract an IPv4 address', PDLEVEL1 );
135 }
136  
137 41 subPostamble( PDLEVEL1, '@', @rv );
138  
139 41 return @rv;
140 }
141  
142 18 ▶sub ipv4NetIntersect {
Subroutine ipv4NetIntersect: called CC=13 SCAR=25.8
Pod documented
143  
144 # Purpose: Tests whether network address ranges intersect
145 # Returns: Integer, denoting whether an intersection exists, and what
146 # kind:
147 #
148 # -1: destination range encompasses target range
149 # 0: both ranges do not intersect at all
150 # 1: target range encompasses destination range
151 #
152 # Usage: $rv = ipv4NetIntersect($net1, $net2);
153  
154 18 my $tgt = shift;
155 18 my $dest = shift;
156 18 my $rv = 0;
157 18 my ( @tnet, @dnet );
158  
159 18 subPreamble( PDLEVEL1, '$$', $tgt, $dest );
160  
161 # Bypas if one or both isn't defined -- obviously no intersection
162 18 ▶ unless ( !defined $tgt or !defined $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tgt and defined $dest) 18 0 18
Condition33%1/3
defined $tgt and defined $dest
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $tgt and defined $dest
defined $tgt defined $dest
Truth table33%1/3
A and B
A= defined $tgtB= defined $dest
ABresult
0X0
100
111
163  
164 # Convert addresses (also allows for raw IPs (32bit integers) to be
165 # passed)
166 18 ▶ @tnet = $tgt =~ /^\d+$/s ? ($tgt) : ipv4NetConvert($tgt);
Branch: 1 branch
BranchTrueFalseTotal
$tgt =~ /^\d+$/s ? : 0 18 18
167 18 ▶ @dnet = $dest =~ /^\d+$/s ? ($dest) : ipv4NetConvert($dest);
Branch: 1 branch
BranchTrueFalseTotal
$dest =~ /^\d+$/s ? : 0 18 18
168  
169 # insert bogus numbers for non IP-address info
170 18 ▶ @tnet = (-1) unless scalar @tnet;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @tnet 2 16 18
171 18 ▶ @dnet = (-2) unless scalar @dnet;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @dnet 4 14 18
172  
173 # Dummy up broadcast address for those single IPs passed (in lieu of
174 # network ranges)
175 18 ▶ $tnet[IPV4BRDCST] = $tnet[IPV4BASE] if $#tnet == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#tnet == 0 8 10 18
176 18 ▶ $dnet[IPV4BRDCST] = $dnet[IPV4BASE] if $#dnet == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#dnet == 0 7 11 18
177  
178 18 ▶ if ( $tnet[IPV4BASE] <= $dnet[IPV4BASE]
Branch: 2 branches
BranchTrueFalseTotal
if ($tnet[0] <= $dnet[0] and $tnet[1] >= $dnet[1]) { } 4 14 18
elsif ($tnet[0] >= $dnet[0] and $tnet[1] <= $dnet[1]) { } 7 8 15
Condition100%3/3
$tnet[0] <= $dnet[0] and $tnet[1] >= $dnet[1]
!ll&&!rl&&r
1224
MC/DC100%2/2
$tnet[0] <= $dnet[0] and $tnet[1] >= $dnet[1]
$tnet[0] <= $dnet[0] $tnet[1] >= $dnet[1]
Truth table100%3/3
A and B
A= $tnet[0] <= $dnet[0]B= $tnet[1] >= $dnet[1]
ABresult
0X0
100
111
179 and $tnet[IPV4BRDCST] >= $dnet[IPV4BRDCST] ) {
180  
181 # Target fully encapsulates dest
182 4 $rv = 1;
183  
184 ▶ } elsif ( $tnet[IPV4BASE] >= $dnet[IPV4BASE]
Condition100%3/3
$tnet[0] >= $dnet[0] and $tnet[1] <= $dnet[1]
!ll&&!rl&&r
167
MC/DC100%2/2
$tnet[0] >= $dnet[0] and $tnet[1] <= $dnet[1]
$tnet[0] >= $dnet[0] $tnet[1] <= $dnet[1]
Truth table100%3/3
A and B
A= $tnet[0] >= $dnet[0]B= $tnet[1] <= $dnet[1]
ABresult
0X0
100
111
185 and $tnet[IPV4BRDCST] <= $dnet[IPV4BRDCST] ) {
186  
187 # Dest fully encapsulates target
188 7 $rv = -1;
189  
190 }
191 }
192  
193 18 subPostamble( PDLEVEL1, '$', $rv );
194  
195 18 return $rv;
196 }
197  
198 {
199  
200 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 2 ▶ sub ipv4NumSort {
Subroutine ipv4NumSort: called CC=1 SCAR=0.0
Pod documented
203  
204 # Purpose: Sorts IPv4 addresses represented in numeric form
205 # Returns: -1, 0, 1
206 # Usage: @sorted = sort &ipv4NumSort @ipv4;
207  
208 2 my ($pkg) = caller;
209  
210 2 return ${"${pkg}::a"} <=> ${"${pkg}::b"};
211 }
212  
213 2 ▶ sub ipv4PackedSort {
Subroutine ipv4PackedSort: called CC=1 SCAR=0.0
Pod documented
214  
215 # Purpose: Sorts IPv4 addresses represented in packed strings
216 # Returns: -1, 0, 1
217 # Usage: @sorted = sort &ipv4PackedSort @ipv4;
218  
219 2 my ($pkg) = caller;
220  
221 2 my $a1 = unpack 'N', ${"${pkg}::a"};
222 2 my $b1 = unpack 'N', ${"${pkg}::b"};
223  
224 2 return $a1 <=> $b1;
225 }
226  
227 2 ▶ sub ipv4StrSort {
Subroutine ipv4StrSort: called CC=1 SCAR=0.0
Pod documented
228  
229 # Purpose: Sorts IPv4 addresses represented in string form
230 # Returns: -1, 0, 1
231 # Usage: @sorted = sort &ipv4StrSort @ipv4;
232  
233 2 my ($pkg) = caller;
234  
235 2 my $a1 = ${"${pkg}::a"};
236 2 my $b1 = ${"${pkg}::b"};
237  
238 2 $a1 =~ s#/.+##s;
239 2 $a1 = unpack 'N', inet_aton($a1);
240 2 $b1 =~ s#/.+##s;
241 2 $b1 = unpack 'N', inet_aton($b1);
242  
243 2 return $a1 <=> $b1;
244 }
245 }
246  
247 4 1;
248  
249 __END__
« Paranoid/IO/FileMultiplexer/Block/BATHeader.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/FileHeader.pm »