Paranoid/Network.pm

filter: Statementstmt 96.9% 126 / 130 Branchbran 77.5% 31 / 40 Conditioncond 47.6% 10 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 84.5% 191 / 226 CC 33 SCAR 20.6
CC
33
Cov
87%
ipInNetworks
13.9
extractIPs
9.3
netIntersect
7.6
CRAP 7.9
« Paranoid/IO/FileMultiplexer/Block.pm ↑ Summary Paranoid/Debug.pm »
1 # Paranoid::Network -- Network functions for paranoid programs
2 #
3 # $Id: lib/Paranoid/Network.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;
33  
34 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Paranoid::Network::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Paranoid::Network::IPv4 qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Paranoid::Network::IPv6 qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
47  
48 3 @EXPORT = qw(ipInNetworks hostInDomains extractIPs netIntersect);
49 3 @EXPORT_OK = ( @EXPORT, qw(NETMATCH HOSTNAME_REGEX) );
50 3 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
51  
52 3 ▶use constant HOSTNAME_REGEX =>
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 qr#(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?#s;
54  
55 #####################################################################
56 #
57 # Module code follows
58 #
59 #####################################################################
60  
61 {
62  
63 3 my $lmatch;
64  
65 33 ▶ sub NETMATCH : lvalue {
Subroutine NETMATCH: called CC=1 SCAR=0.0
Pod documented
66 33 $lmatch;
67 }
68  
69 }
70  
71 3 ▶sub ipInNetworks {
Subroutine ipInNetworks: called CC=13 SCAR=26.3
Pod documented
72  
73 # Purpose: Checks to see if the IP occurs in the passed list of IPs and
74 # networks
75 # Returns: True (1) if the IP occurs, False (0) otherwise
76 # Usage: $rv = ipInNetworks($ip, @networks);
77  
78 15 my $ip = shift;
79 15 my @networks = grep {defined} @_;
80 15 my $rv = 0;
81 15 my ( $family, @tmp );
82  
83 15 subPreamble( PDLEVEL1, '$@', $ip, @networks );
84  
85 15 NETMATCH = undef;
86  
87 # Validate arguments
88 15 ▶ if ( defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ip) 15 0 15
89  
90 # Extract IPv4 address from IPv6 encoding
91 15 $ip =~ s/^::ffff:(@{[ IPV4REGEX ]})$/$1/sio;
92  
93 # Check for IPv6 support
94 15 ▶ if ( has_ipv6() or $] >= 5.012 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_ipv6() or $] >= 5.012) { } 15 0 15
Condition33%1/3
has_ipv6() or $] >= "5.012"
l!l&&r!l&&!r
1500
MC/DC0%0/2
has_ipv6() or $] >= "5.012"
has_ipv6() $] >= "5.012"
Truth table33%1/3
A or B
A= has_ipv6()B= $] >= "5.012"
ABresult
1X1
011
000
95  
96 15 pdebug( 'Found IPv4/IPv6 support', PDLEVEL2 );
97 15 ▶ $family =
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /^@{[\'(?^s:(?:(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?))'];}$/so ? : 7 8 15
98 1 ▶ $ip =~ m/^@{[ IPV4REGEX ]}$/so ? AF_INET()
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /^@{[\"(?^six:\n :(?::[abcdef\\d]{1,4}){1,7} | \n \\b[abcdef\\d]{1,4}(?:::?[abcdef\\d]{1,4}){1,7} | \n (?:\\b[abcdef\\d]{1,4}:){1,7}: \n )"];}$/so ? : 7 1 8
99 1 : $ip =~ m/^@{[ IPV6REGEX ]}$/so ? AF_INET6()
100 : undef;
101  
102 } else {
103  
104 0 pdebug( 'Found only IPv4 support', PDLEVEL2 );
105 0 ▶ $family = AF_INET()
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ m[^@{[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];}$]so 0 0 0
106 0 if $ip =~ m/^@{[ IPV4REGEX ]}$/so;
107 }
108 }
109  
110 15 ▶ if ( defined $ip and defined $family ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ip and defined $family) 14 1 15
Condition66%2/3
defined $ip and defined $family
!ll&&!rl&&r
0114
MC/DC50%1/2
defined $ip and defined $family
defined $ip defined $family
Truth table66%2/3
A and B
A= defined $ipB= defined $family
ABresult
0X0
100
111
111  
112 # Filter out non-family data from @networks
113 @networks = grep {
114 14 ▶ $family == AF_INET()
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 ? : 10 10 20
115 1 ? m#^(?:@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$#so
116 1 : m#^(?:@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})$#so
117 } @networks;
118  
119 14 pdebug( 'networks to compare: %s', PDLEVEL2, @networks );
120  
121 # Start comparisons
122 14 foreach (@networks) {
123 13 ▶ if ($family == AF_INET()
Branch: 2 branches
BranchTrueFalseTotal
if ($family == 2 ? ipv4NetIntersect($ip, $_) : ipv6NetIntersect($ip, $_)) 10 3 13
$family == 2 ? : 6 7 13
124 ? ipv4NetIntersect( $ip, $_ )
125 : ipv6NetIntersect( $ip, $_ )
126 ) {
127 10 NETMATCH = $_;
128 10 $rv = 1;
129 10 last;
130 }
131 }
132 }
133  
134 15 subPostamble( PDLEVEL1, '$', $rv );
135  
136 15 return $rv;
137 }
138  
139 5 ▶sub hostInDomains {
Subroutine hostInDomains: called CC=7 SCAR=19.5
Pod documented
140  
141 # Purpose: Checks to see if the host occurs in the list of domains
142 # Returns: True (1) if the host occurs, False (0) otherwise
143 # Usage: $rv = hostInDomains($hostname, @domains);
144  
145 5 my $host = shift;
146 5 my @domains = @_;
147 5 my $rv = 0;
148 5 my $domain;
149  
150 5 subPreamble( PDLEVEL1, '$@', $host, @domains );
151  
152 5 NETMATCH = undef;
153  
154 5 ▶ if ( defined $host and $host =~ /^@{[ HOSTNAME_REGEX ]}$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $host and $host =~ m[^@{[qr/(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?/s];}$]so) 4 1 5
Condition66%2/3
defined $host and $host =~ m[^@{[qr/(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?/s];}$]so
!ll&&!rl&&r
014
MC/DC50%1/2
defined $host and $host =~ m[^@{[qr/(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?/s];}$]so
defined $host $host =~ m[^@{[qr/(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?/s];}$]so
Truth table66%2/3
A and B
A= defined $hostB= $host =~ m[^@{[qr/(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?/s];}$]so
ABresult
0X0
100
111
155  
156 # Filter out non-domains
157 @domains =
158 4 ▶ grep { defined $_ && m/^@{[ HOSTNAME_REGEX ]}$/so } @domains;
Condition66%2/3
defined $_ && /^@{[\'(?^s:(?:[a-z0-9][a-z0-9\\-]*)(?:\\.[a-z0-9][a-z0-9\\-]*)*\\.?)'];}$/so
!ll&&!rl&&r
014
MC/DC50%1/2
defined $_ && /^@{[\'(?^s:(?:[a-z0-9][a-z0-9\\-]*)(?:\\.[a-z0-9][a-z0-9\\-]*)*\\.?)'];}$/so
defined $_ /^@{[\'(?^s:(?:[a-z0-9][a-z0-9\\-]*)(?:\\.[a-z0-9][a-z0-9\\-]*)*\\.?)'];}$/so
Truth table66%2/3
A && B
A= defined $_B= /^@{[\'(?^s:(?:[a-z0-9][a-z0-9\\-]*)(?:\\.[a-z0-9][a-z0-9\\-]*)*\\.?)'];}$/so
ABresult
0X0
100
111
159  
160 # Start the comparison
161 4 ▶ if (@domains) {
Branch: 1 branch
BranchTrueFalseTotal
if (@domains) 3 1 4
162 3 foreach $domain (@domains) {
163 4 ▶ if ( $host =~ /^(?:[\w\-]+\.)*\Q$domain\E$/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^(?:[\w\-]+\.)*\Q$domain\E$/is) 3 1 4
164 3 NETMATCH = $domain;
165 3 $rv = 1;
166 3 last;
167 }
168 }
169 }
170 }
171  
172 5 subPostamble( PDLEVEL1, '$', $rv );
173  
174 5 return $rv;
175 }
176  
177 8 ▶sub extractIPs {
Subroutine extractIPs: called CC=9 SCAR=22.3
Pod documented
178  
179 # Purpose: Extracts IPv4/IPv6 addresses from arbitrary text.
180 # Returns: List containing extracted IP addresses
181 # Usage: @ips = extractIPs($string1, $string2);
182  
183 8 my @strings = @_;
184 8 my ( $string, @ips, $ip, @tmp, @rv );
185  
186 8 subPreamble( PDLEVEL1, '@', @strings );
187  
188 8 foreach $string (@strings) {
189 10 ▶ next unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 10 10
190  
191 # Look for IPv4 addresses
192 10 @ips = ( $string =~ /(@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})/sog );
193  
194 # Validate them by filtering through inet_aton
195 10 foreach $ip (@ips) {
196 28 @tmp = split m#/#s, $ip;
197 28 ▶ push @rv, $ip if defined inet_aton( $tmp[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined inet_aton($tmp[0]) 28 0 28
198 }
199  
200 # If Socket6 is present or we have Perl 5.14 or higher we'll check
201 # for IPv6 addresses
202 10 ▶ if ( has_ipv6() or $] >= 5.012 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_ipv6() or $] >= "5.012") 10 0 10
Condition33%1/3
has_ipv6() or $] >= "5.012"
l!l&&r!l&&!r
1000
MC/DC0%0/2
has_ipv6() or $] >= "5.012"
has_ipv6() $] >= "5.012"
Truth table33%1/3
A or B
A= has_ipv6()B= $] >= "5.012"
ABresult
1X1
011
000
203  
204 10 @ips =
205 1 ( $string =~ m/(@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})/sogix );
206  
207 # Filter out addresses with more than one ::
208 10 @ips = grep { scalar(m/(::)/sg) <= 1 } @ips;
209  
210 # Validate remaining addresses with inet_pton
211 10 foreach $ip (@ips) {
212 45 @tmp = split m#/#s, $ip;
213 45 ▶ push @rv, $ip
Branch: 1 branch
BranchTrueFalseTotal
if defined inet_pton(10, $tmp[0]) 15 30 45
214 if defined inet_pton( AF_INET6(), $tmp[0] );
215 }
216 }
217 }
218  
219 8 subPostamble( PDLEVEL1, '@', @rv );
220  
221 8 return @rv;
222 }
223  
224 6 ▶sub netIntersect {
Subroutine netIntersect: called CC=7 SCAR=20.3
Pod documented
225  
226 # Purpose: Tests whether network address ranges intersect
227 # Returns: Integer, denoting whether an intersection exists, and what
228 # kind:
229 #
230 # -1: destination range encompasses target range
231 # 0: both ranges do not intersect at all
232 # 1: target range encompasses destination range
233 #
234 # Usage: $rv = netIntersect( $cidr1, $cidr2 );
235  
236 6 my $target = shift;
237 6 my $dest = shift;
238 6 my $rv = 0;
239  
240 6 subPreamble( PDLEVEL1, '$$', $target, $dest );
241  
242 6 ▶ if ( defined $target and defined $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target and defined $dest) { } 6 0 6
Condition33%1/3
defined $target and defined $dest
!ll&&!rl&&r
006
MC/DC0%0/2
defined $target and defined $dest
defined $target defined $dest
Truth table33%1/3
A and B
A= defined $targetB= defined $dest
ABresult
0X0
100
111
243 6 ▶ if ( $target =~ m/^(?:@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target =~ m[^(?:@{[\'(?^s:(?^s:(?:(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?))/(?:(?:3[0-2]|[12]?\\d)|(?^s:(?:(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?))))'];}|@{[\'(?^s:(?:(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4][0-9]|1?\\d\\d?))'];})$]s) { } 3 3 6
elsif ($target =~ m[^(?:@{[\"(?^s:(?^six:\n :(?::[abcdef\\d]{1,4}){1,7} | \n \\b[abcdef\\d]{1,4}(?:::?[abcdef\\d]{1,4}){1,7} | \n (?:\\b[abcdef\\d]{1,4}:){1,7}: \n )/(?:1(?:[01]\\d|2[0-8])|\\d\\d?))"];}|@{[\"(?^six:\n :(?::[abcdef\\d]{1,4}){1,7} | \n \\b[abcdef\\d]{1,4}(?:::?[abcdef\\d]{1,4}){1,7} | \n (?:\\b[abcdef\\d]{1,4}:){1,7}: \n )"];})$]is) { } 2 1 3
244 3 $rv = ipv4NetIntersect( $target, $dest );
245 3 } elsif ( $target =~ m/^(?:@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})$/si )
246 {
247 2 ▶ $rv = ipv6NetIntersect( $target, $dest )
Branch: 1 branch
BranchTrueFalseTotal
if has_ipv6() or $] >= "5.012" 2 0 2
Condition33%1/3
has_ipv6() or $] >= "5.012"
l!l&&r!l&&!r
200
MC/DC0%0/2
has_ipv6() or $] >= "5.012"
has_ipv6() $] >= "5.012"
Truth table33%1/3
A or B
A= has_ipv6()B= $] >= "5.012"
ABresult
1X1
011
000
248 if has_ipv6()
249 or $] >= 5.012;
250 } else {
251 1 pdebug(
252 'target string (%s) doesn\'t seem to match '
253 . 'an IP/network address',
254 PDLEVEL1, $target
255 );
256 }
257 } else {
258 0 pdebug( 'one or both arguments are not defined', PDLEVEL1 );
259 }
260  
261 6 subPostamble( PDLEVEL1, '$', $rv );
262  
263 6 return $rv;
264 }
265  
266 3 1;
267  
268 __END__
« Paranoid/IO/FileMultiplexer/Block.pm ↑ Summary Paranoid/Debug.pm »