IP.pm

filter: Statementstmt 95.0% 133 / 140 Branchbran 90.0% 54 / 60 Conditioncond 72.4% 21 / 29 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 96.5% 28 / 29 Podpod 100% 15 / 15 totaltotal 89.1% 254 / 285 CC 41 SCAR 16.4
CC
41
Cov
91%
BEGIN
11.0
getaddrs
8.1
getname
7.3
CRAP 5.2
↑ Summary Util.pm »
1 package Net::IPAM::IP;
2  
3 10 our $VERSION = '4.01';
4  
5 10 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Net::IPAM::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 our @EXPORT_OK = qw(sort_ip);
16  
17 10 ▶use constant Is4in6_Prefix => "\x00" x 10 . "\xff\xff";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Net::IPAM::IP - A library for reading, formatting, sorting and converting IP-addresses.
22  
23 =head1 SYNOPSIS
24  
25 use Net::IPAM::IP;
26  
27 # parse and normalize
28 $ip1 = Net::IPAM::IP->new('1.2.3.4') // die 'wrong format,';
29 $ip2 = Net::IPAM::IP->new('fe80::1') // die 'wrong format,';
30  
31 $ip3 = $ip2->incr // die 'overflow,';
32  
33 say $ip1; # 1.2.3.4
34 say $ip2; # fe80::1
35 say $ip3; # fe80::2
36  
37 $ip3 = $ip2->decr // die 'underflow,';
38  
39 say $ip1; # 1.2.3.4
40 say $ip2; # fe80::1
41 say $ip3; # fe80::0
42  
43 say $ip1->cmp($ip2); # -1
44  
45 say $ip2->expand; # fe80:0000:0000:0000:0000:0000:0000:0001
46 say $ip2->reverse; # 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f
47  
48 $ip = Net::IPAM::IP->new_from_bytes( pack( 'C4', 192, 168, 0, 1 ) ); # 192.168.0.1
49 $ip = Net::IPAM::IP->new_from_bytes( pack( 'n8', 0x2001, 0xdb8, 0, 0, 0, 0, 0, 1, ) ); # 2001:db8::1
50  
51 @ips = Net::IPAM::IP->getaddrs('dns.google.');
52 say "@ips"; # 8.8.8.8 8.8.4.4 2001:4860:4860::8844 2001:4860:4860::8888
53  
54 =cut
55  
56 =head1 CONSTRUCTORS
57  
58 =head2 new
59  
60 $ip = Net::IPAM::IP->new("::1");
61  
62 Parse the input string as IPv4/IPv6 address and returns the IP address object.
63  
64 Returns undef on illegal input.
65  
66 =cut
67  
68 189 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
69 189 my $self = bless( {}, $_[0] );
70 189 ▶ my $input = $_[1] // Carp::croak 'missing argument';
Condition100%2/2
$_[1] // Carp::croak("missing argument")
l!l
1881
MC/DC100%1/1
$_[1] // Carp::croak("missing argument")
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
71  
72 # IPv4
73 188 ▶ if ( index( $input, ':' ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($input, ":") == -1) 49 139 188
74 49 my $n = Socket::inet_pton( Socket::AF_INET, $input );
75 49 ▶ return unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 17 32 49
76  
77 32 $self->{version} = 4;
78 32 $self->{binary} = 4 . $n;
79 32 return $self;
80 }
81  
82 # IPv4-mapped-IPv6
83 139 ▶ if ( index( $input, '.' ) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($input, ".") != -1) 40 99 139
84 # allow only IPv4-mapped-IPv6 in mixed mode
85 40 ▶ return unless $input =~ m/^::ffff:/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $input =~ /^::ffff:/m 16 24 40
86 }
87  
88 # IPv6 address
89 123 my $n = Socket::inet_pton( Socket::AF_INET6, $input );
90 123 ▶ return unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 54 69 123
91  
92 69 $self->{version} = 6;
93 69 $self->{binary} = 6 . $n;
94 69 return $self;
95 }
96  
97 =head2 new_from_bytes
98  
99 $ip = Net::IPAM::IP->new_from_bytes("\x0a\x00\x00\x01")
100  
101 Parse the input as packed IPv4/IPv6/IPv4-mapped-IPv6 address and returns the IP address object.
102  
103 Croaks on illegal input.
104  
105 Can be used for cloning the object:
106  
107 $clone = $obj->new_from_bytes($obj->bytes);
108  
109 =cut
110  
111 30 ▶sub new_from_bytes {
Subroutine new_from_bytes: called CC=5 SCAR=16.1
Pod documented
112 30 ▶ my $self = bless( {}, ref $_[0] || $_[0] );
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
12180
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
113 30 my $n = $_[1];
114 30 ▶ Carp::croak('missing argument') unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 29 30
115  
116 29 ▶ if ( length($n) == 4 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $n == 4) { } 11 18 29
elsif (length $n == 16) { } 16 2 18
117 11 $self->{version} = 4;
118 11 $self->{binary} = 4 . $n;
119 11 return $self;
120 }
121 elsif ( length($n) == 16 ) {
122 16 $self->{version} = 6;
123 16 $self->{binary} = 6 . $n;
124 16 return $self;
125 }
126  
127 2 Carp::croak 'illegal input';
128 }
129  
130 =head2 getaddrs($name, [$error_cb])
131  
132 Returns a list of ip objects for a given $name or undef if there is no RR record for $name.
133  
134 my @ips = Net::IPAM::IP->getaddrs('dns.google.');
135 say "@ips"; # 8.8.8.8 8.8.4.4 2001:4860:4860::8844 2001:4860:4860::8888
136  
137 L</"getaddrs"> calls the L<Socket> function C<< getaddrinfo() >> under the hood.
138  
139 With no error callback L</getaddrs> just calls C<< carp() >> with underlying Socket errors.
140  
141 For granular error handling use your own error callback:
142  
143 my $my_error_cb = sub {
144 my $error = shift;
145 # check the $error and do what you want
146 ...
147 }
148  
149 my @ips = Net::IPAM::IP->getaddrs( $name, $my_error_cb );
150  
151 or shut up the default error handler with:
152  
153 my @ips = Net::IPAM::IP->getaddrs( $name, sub { } );
154  
155 ANNOTATION: This constructor could also be named C<< new_from_name >> but it behaves differently
156 because it returns a B<list> of objects and supports an optional argument as error callback,
157 reporting underlying Socket errors.
158  
159 =cut
160  
161 # heuristic detection of ip addrs as input
162 10 my $v4_rx = qr/^[0-9.]+$/;
163 10 my $v6_rx = qr/^[a-fA-F0-9:]+$/;
164 10 my $v4mapv6_rx = qr/^::[a-fA-F]+:[0-9.]+$/;
165 10 my $v4compv6_rx = qr/^::[0-9.]+$/;
166  
167 10 my $ip_rx = qr/$v4_rx|$v6_rx|$v4mapv6_rx|$v4compv6_rx/;
168  
169 24 ▶sub getaddrs {
Subroutine getaddrs: called CC=8 SCAR=20.9
Pod documented
170 24 my ( $class, $name, $error_cb ) = @_;
171 24 ▶ Carp::croak('missing argument') unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 23 24
172  
173 23 ▶ $error_cb = \&Carp::carp unless defined $error_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_cb 22 1 23
174  
175 # just ip address as input param, don't rely on (buggy) Socket getaddrinfo
176 23 ▶ return $class->new($name) if $name =~ $ip_rx;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$ip_rx/ 20 3 23
177  
178 # resolve name
179 3 my ( $err, @res ) =
180 Socket::getaddrinfo( $name, "", { socktype => Socket::SOCK_RAW, family => Socket::AF_UNSPEC } );
181  
182 3 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 2 1 3
183  
184 # no error, just no resolveable name
185 2 ▶ return if $err == Socket::EAI_NONAME;
Branch: 1 branch
BranchTrueFalseTotal
if $err == -2 2 0 2
186  
187 0 $error_cb->("getaddrinfo($name): $err");
188 0 return;
189 }
190  
191 # unpack sockaddr struct
192 1 my @ips;
193 1 ▶ while ( my $ai = shift @res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ai = shift @res) 4 1 5
194 4 my $n;
195 4 ▶ if ( $ai->{family} == Socket::AF_INET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai->{'family'} == 2) { } 2 2 4
196 2 $n = substr( $ai->{addr}, 4, 4 );
197 }
198 else {
199 2 $n = substr( $ai->{addr}, 8, 16 );
200 }
201 4 push @ips, $class->new_from_bytes($n);
202 }
203  
204 1 return @ips;
205 }
206  
207 =head1 METHODS
208  
209 L<Net::IPAM::IP> implements the following methods:
210  
211 =head2 cmp
212  
213 Compare IP objects, returns -1, 0, +1
214  
215 $this->cmp($other)
216  
217 @sorted_ips = sort { $a->cmp($b) } @unsorted_ips;
218  
219 Fast bytewise lexical comparison of the binary representation in network byte order.
220  
221 For even faster sorting import L</sort_ip>.
222  
223 =cut
224  
225 # the first byte is the version
226 # use fast builtin cmp
227 # IPv4 is sorted before IPv6
228 25 ▶sub cmp {
Subroutine cmp: called CC=1 SCAR=0.0
Pod documented
229 25 $_[0]->{binary} cmp $_[1]->{binary};
230 }
231  
232 =head2 version
233  
234 $v = Net::IPAM::IP->new('fe80::1')->version # 6
235  
236 Returns 4 or 6.
237  
238 =cut
239  
240 5 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
241 5 $_[0]->{version};
242 }
243  
244 =head2 to_string
245  
246 Returns the input string in canonical form.
247  
248 lower case hexadecimal characters
249 zero compression
250 remove leading zeros
251  
252 say Net::IPAM::IP->new('Fe80::0001')->to_string; # fe80::1
253  
254 Stringification is overloaded with L</"to_string">
255  
256 my $ip = Net::IPAM::IP->new('Fe80::0001') // die 'wrong format';
257 say $ip; # fe80::1
258  
259 =cut
260  
261 # without inet_ntop bug it would be easy, sic
262 #sub to_string {
263 # return $_[0]->{as_string} if exists $_[0]->{as_string};
264 # my ( $v, $n ) = unpack( 'C a*', $_[0]->{binary} );
265 # return $_[0]->{as_string} = Socket::inet_ntop( $v, $n );
266 #}
267  
268 # circumvent IPv4-compatible-IPv6 bug in Socket::inet_ntop
269 74 ▶sub to_string {
Subroutine to_string: called CC=5 SCAR=16.1
Pod documented
270 74 ▶ return $_[0]->{as_string} if exists $_[0]->{as_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"as_string"} 11 63 74
271  
272 63 my $n = substr( $_[0]->{binary}, 1, );
273  
274 # no bug in Socket::inet_ntop for IPv4, just return
275 63 ▶ if ( $_[0]->{version} == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"version"} == 4) 26 37 63
276 26 return $_[0]->{as_string} = Socket::inet_ntop( Socket::AF_INET, $n );
277 }
278  
279 # IPv6 case
280 # handle bug in Socket::inet_ntop for deprecated IPv4-compatible-IPv6 addresses
281 # ::aaaa:bbbb are returned as ::hex(aa).hex(aa).hex(bb).hex(bb) = ::170.170.187.187
282 # e.g: ::cafe:affe => ::202.254.175.254
283  
284 # handle IPv4MappedIPv6 address special
285 37 ▶ if ( substr( $n, 0, 12 ) eq Is4in6_Prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($n, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377") 8 29 37
286 # concat ::ffff:1.2.3.4
287 8 return $_[0]->{as_string} = '::ffff:' . Socket::inet_ntop( Socket::AF_INET, substr( $n, -4 ) );
288 }
289  
290 29 my $str = Socket::inet_ntop( Socket::AF_INET6, $n );
291  
292 # first handle normal case, no dot '.'
293 29 ▶ if ( index( $str, '.' ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, ".") == -1) 28 1 29
294 28 return $_[0]->{as_string} = $str;
295 }
296  
297 # handle the bug, use our pure perl inet_ntop_pp
298 1 return $_[0]->{as_string} = Net::IPAM::Util::inet_ntop_pp( Socket::AF_INET6, $n );
299 }
300  
301 =head2 TO_JSON
302  
303 helper method for JSON serialization, just calls $ip->to_string.
304 See also L<JSON/OBJECT SERIALISATION>.
305  
306 =cut
307  
308 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
309 0 $_[0]->to_string;
310 }
311  
312 =head2 incr
313  
314 Returns the next IP address, returns undef on overflow.
315  
316 $next_ip = Net::IPAM::IP->new('fe80::1')->incr // die 'overflow,';
317 say $next_ip; # fe80::2
318  
319 =cut
320  
321 7 ▶sub incr {
Subroutine incr: called CC=2 SCAR=6.9
Pod documented
322 7 my $n_plus1 = Net::IPAM::Util::incr_n( $_[0]->bytes );
323  
324 # overflow?
325 7 ▶ return unless defined $n_plus1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n_plus1 2 5 7
326  
327 # sort of cloning
328 5 $_[0]->new_from_bytes($n_plus1);
329 }
330  
331 =head2 decr
332  
333 Returns the previous IP address, returns undef on underflow.
334  
335 $prev_ip = Net::IPAM::IP->new('fe80::1')->decr // die 'underflow,';
336 say $prev_ip; # fe80::
337  
338 =cut
339  
340 6 ▶sub decr {
Subroutine decr: called CC=2 SCAR=6.9
Pod documented
341 6 my $n_minus1 = Net::IPAM::Util::decr_n( $_[0]->bytes );
342  
343 # underflow?
344 6 ▶ return unless defined $n_minus1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n_minus1 2 4 6
345  
346 # sort of cloning
347 4 $_[0]->new_from_bytes($n_minus1);
348 }
349  
350 =head2 expand
351  
352 Expand IP address into canonical form, useful for C<< grep >>, aligned output and lexical C<< sort >>
353  
354 Net::IPAM::IP->new('1.2.3.4')->expand; # '001.002.003.004'
355 Net::IPAM::IP->new('fe80::1')->expand; # 'fe80:0000:0000:0000:0000:0000:0000:0001'
356  
357 =cut
358  
359 5 ▶sub expand {
Subroutine expand: called CC=3 SCAR=11.0
Pod documented
360 5 ▶ return $_[0]->{expand} if exists $_[0]->{expand};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"expand"} 1 4 5
361  
362 4 my $n = substr( $_[0]->{binary}, 1, );
363  
364 4 ▶ if ( $_[0]->{version} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"version"} == 6) 2 2 4
365 2 my @hextets = unpack( 'H4' x 8, $n );
366  
367 # cache it and return
368 2 return $_[0]->{expand} = join( ':', @hextets );
369 }
370  
371 # IPv4
372 2 my @octets = unpack( 'C4', $n );
373  
374 # cache it and return
375 2 return $_[0]->{expand} = sprintf( "%03d.%03d.%03d.%03d", @octets );
376 }
377  
378 =head2 reverse
379  
380 Reverse IP address, needed for PTR entries in DNS zone files.
381  
382 Net::IPAM::IP->new('fe80::1')->reverse; # '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f'
383 Net::IPAM::IP->new('1.2.3.4')->reverse; # '4.3.2.1'
384  
385 =cut
386  
387 5 ▶sub reverse {
Subroutine reverse: called CC=3 SCAR=11.0
Pod documented
388 5 ▶ return $_[0]->{reverse} if exists $_[0]->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"reverse"} 1 4 5
389  
390 # unpack to version and network byte order (from Socket::inet_pton)
391 # my ( $v, $n ) = unpack( 'C a*', $_[0]->{binary} );
392 # substr() ist faster
393 4 my $n = substr( $_[0]->{binary}, 1, );
394  
395 4 ▶ if ( $_[0]->{version} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"version"} == 6) 3 1 4
396 3 my $hex_str = unpack( 'H*', $n );
397 3 my @nibbles = unpack( 'A' x 32, $hex_str );
398  
399 # cache it and return
400 3 return $_[0]->{reverse} = join( '.', reverse @nibbles );
401 }
402  
403 # IPv4
404 1 my @octets = unpack( 'C4', $n );
405  
406 # cache it and return
407 1 return $_[0]->{reverse} = join( '.', reverse @octets );
408 }
409  
410 =head2 getname([$error_cb])
411  
412 Returns the DNS name for the ip object or undef if there is no PTR RR.
413  
414 say Net::IPAM::IP->new('2001:4860:4860::8888')->getname; # dns.google.
415  
416 L</"getname"> calls the L<Socket> function C<< getnameinfo() >> under the hood.
417  
418 With no error callback L</getname> just calls C<< carp() >> with underlying Socket errors.
419  
420 =head3 LIMITATION:
421  
422 Returns just one name even if the IP has more than one PTR RR. This is a limitation
423 of Socket::getnameinfo. If you need all names for IPs with more than one PTR RR then you should
424 use L<Net::DNS> or similar modules.
425  
426 =cut
427  
428 1 ▶sub getname {
Subroutine getname: called CC=5 SCAR=19.8
Pod documented
429 1 my ( $self, $error_cb ) = @_;
430 1 ▶ $error_cb = \&Carp::carp unless defined $error_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_cb 1 0 1
431  
432 1 my $sock_addr;
433 1 ▶ if ( $self->{version} == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 1 0 1
434 1 $sock_addr = Socket::pack_sockaddr_in( 0, $self->bytes );
435 }
436 else {
437 0 $sock_addr = Socket::pack_sockaddr_in6( 0, $self->bytes );
438 }
439  
440 1 my ( $err, $name ) = Socket::getnameinfo( $sock_addr, Socket::NI_NAMEREQD, Socket::NIx_NOSERV );
441  
442 1 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 1 1
443  
444 # no error, just no resolveable name
445 0 ▶ return if $err == Socket::EAI_NONAME;
Branch: 1 branch
BranchTrueFalseTotal
if $err == -2 0 0 0
446  
447 0 $error_cb->("getnameinfo($self): $err");
448 0 return;
449 }
450  
451 1 $name;
452 }
453  
454 =head2 bytes
455  
456 $ip = Net::IPAM::IP->new('fe80::');
457 $bytes = $ip->bytes; # "\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
458  
459 $ip = Net::IPAM::IP->new('10.0.0.1');
460 $bytes = $ip->bytes; # "\x0a\x00\x00\x01"
461  
462 Returns the packed IP address as byte-string. It's the opposite to L</"new_from_bytes">
463  
464 =cut
465  
466 # drop first byte (version) and return the packed IP address,
467 20 ▶sub bytes {
Subroutine bytes: called CC=1 SCAR=0.0
Pod documented
468 20 substr( $_[0]->{binary}, 1 );
469 }
470  
471 =head2 is4in6
472  
473 $ip = Net::IPAM::IP->new('::ffff:1.2.3.4')
474 if ( $ip->is4in6 ) {
475 ...
476 }
477  
478 Returns true if the IP address is a IPv4-mapped IPv6 address.
479  
480 =cut
481  
482 3 ▶sub is4in6 {
Subroutine is4in6: called CC=1 SCAR=0.0
Pod documented
483 3 return substr( $_[0]->{binary}, 1, 12 ) eq Is4in6_Prefix;
484 }
485  
486 =head1 FUNCTIONS
487  
488 =head2 sort_ip
489  
490 use Net::IPAM::IP 'sort_ip';
491  
492 @sorted_ips = sort_ip @unsorted_ips;
493  
494 Faster sort implemention (Schwartzian transform) as explcit sort function:
495  
496 @sorted_ips = sort { $a->cmp($b) } @unsorted_ips;
497  
498 =cut
499  
500 1 ▶sub sort_ip {
Subroutine sort_ip: called CC=1 SCAR=0.0
Pod documented
501 11 return map { $_->[0] }
502 25 sort { $a->[1] cmp $b->[1] }
503 1 map { [ $_, $_->{binary} ] } @_;
504 }
505  
506 =head1 OPERATORS
507  
508 L<Net::IPAM::IP> overloads the following operators.
509  
510 =head2 bool
511  
512 my $bool = !!$ip;
513  
514 Always true.
515  
516 =head2 stringify
517  
518 my $str = "$ip";
519  
520 Alias for L</"to_string">.
521  
522 =cut
523  
524 use overload
525 55 ▶ '""' => sub { shift->to_string },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
526 32 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
527 10 fallback => 1;
528  
529 =head1 WARNING
530  
531 Some Socket::inet_XtoY implementations are hopelessly buggy.
532  
533 Tests are made during loading and in case of errors, these functions are redefined
534 with a (slower) pure-perl implementation.
535  
536 =cut
537  
538 # On some platforms, inet_pton accepts various forms of invalid input or discards valid input.
539 # In this case use a (slower) pure-perl implementation for Socket::inet_pton.
540 # and also for Socket::inet_ntop, I don't trust that too.
541 10 ▶BEGIN {
Subroutine BEGIN: called CC=10 SCAR=24.0
542 10 ▶ if ( # wrong valid
Branch: 1 branch
BranchTrueFalseTotal
if (defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::") or not defined Socket::inet_pton(10, "caFe::") or not defined Socket::inet_pton(10, "::") or not defined Socket::inet_pton(2, "0.0.0.0")) 2 8 10
Condition70%17/24
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::") or not defined Socket::inet_pton(10, "caFe::") or not defined Socket::inet_pton(10, "::") or not defined Socket::inet_pton(2, "0.0.0.0")
l!l&&r!l&&!r
208
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::") or not defined Socket::inet_pton(10, "caFe::") or not defined Socket::inet_pton(10, "::")
l!l&&r!l&&!r
208
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::") or not defined Socket::inet_pton(10, "caFe::")
l!l&&r!l&&!r
118
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::")
l!l&&r!l&&!r
109
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:")
l!l&&r!l&&!r
109
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::")
l!l&&r!l&&!r
109
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::")
l!l&&r!l&&!r
109
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1")
l!l&&r!l&&!r
109
MC/DC22%2/9
defined Socket::inet_pton(2, "010.0.0.1") or defined Socket::inet_pton(2, "10.000.0.1") or defined Socket::inet_pton(10, "cafe:::") or defined Socket::inet_pton(10, "cafe::1::") or defined Socket::inet_pton(10, "cafe::1:") or defined Socket::inet_pton(10, ":cafe::") or not defined Socket::inet_pton(10, "caFe::") or not defined Socket::inet_pton(10, "::") or not defined Socket::inet_pton(2, "0.0.0.0")
defined Socket::inet_pton(2, "010.0.0.1") defined Socket::inet_pton(2, "10.000.0.1") defined Socket::inet_pton(10, "cafe:::") defined Socket::inet_pton(10, "cafe::1::") defined Socket::inet_pton(10, "cafe::1:") defined Socket::inet_pton(10, ":cafe::") not defined Socket::inet_pton(10, "caFe::") not defined Socket::inet_pton(10, "::") not defined Socket::inet_pton(2, "0.0.0.0")
Truth table30%3/10
A or B or C or D or E or F or not(G) or not(H) or not(I)
A= defined Socket::inet_pton(2, "010.0.0.1")B= defined Socket::inet_pton(2, "10.000.0.1")C= defined Socket::inet_pton(10, "cafe:::")D= defined Socket::inet_pton(10, "cafe::1::")E= defined Socket::inet_pton(10, "cafe::1:")F= defined Socket::inet_pton(10, ":cafe::")G= not defined Socket::inet_pton(10, "caFe::")H= not defined Socket::inet_pton(10, "::")I= not defined Socket::inet_pton(2, "0.0.0.0")
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
543 defined Socket::inet_pton( Socket::AF_INET, '010.0.0.1' )
544 || defined Socket::inet_pton( Socket::AF_INET, '10.000.0.1' )
545 || defined Socket::inet_pton( Socket::AF_INET6, 'cafe:::' )
546 || defined Socket::inet_pton( Socket::AF_INET6, 'cafe::1::' )
547 || defined Socket::inet_pton( Socket::AF_INET6, 'cafe::1:' )
548 || defined Socket::inet_pton( Socket::AF_INET6, ':cafe::' )
549  
550 # wrong invalid
551 || !defined Socket::inet_pton( Socket::AF_INET6, 'caFe::' )
552 || !defined Socket::inet_pton( Socket::AF_INET6, '::' )
553 || !defined Socket::inet_pton( Socket::AF_INET, '0.0.0.0' )
554 )
555 {
556 10 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
557 2 *Socket::inet_pton = \&Net::IPAM::Util::inet_pton_pp;
558 2 *Socket::inet_ntop = \&Net::IPAM::Util::inet_ntop_pp;
559 }
560 }
561  
562 =head1 AUTHOR
563  
564 Karl Gaissmaier, C<< <karl.gaissmaier(at)uni-ulm.de> >>
565  
566 =head1 BUGS
567  
568 Please report any bugs or feature requests to C<bug-net-ipam-ip at rt.cpan.org>, or through
569 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-IPAM-IP>. I will be notified, and then you'll
570 automatically be notified of progress on your bug as I make changes.
571  
572 =head1 SUPPORT
573  
574 You can find documentation for this module with the perldoc command.
575  
576 perldoc Net::IPAM::IP
577  
578  
579 You can also look for information at:
580  
581 =over 4
582  
583 =item * on github
584  
585 TODO
586  
587 =back
588  
589 =head1 SEE ALSO
590  
591 L<Net::IPAM::Util>
592 L<Net::IPAM::Block>
593 L<Net::IPAM::Tree>
594  
595 =head1 LICENSE AND COPYRIGHT
596  
597 This software is copyright (c) 2020-2022 by Karl Gaissmaier.
598  
599 This is free software; you can redistribute it and/or modify it under
600 the same terms as the Perl 5 programming language system itself.
601  
602  
603 =cut
604  
605 10 1; # End of Net::IPAM::IP
↑ Summary Util.pm »