blib/lib/Net/IP.pm

filter: Statementstmt 58.4% 411 / 703 Branchbran 52.6% 182 / 346 Conditioncond 48.9% 23 / 47 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 86.7% 59 / 68 Podpod 90.4% 57 / 63 totaltotal 58.7% 739 / 1257 CC 203 SCAR 29.7
CC
203
Cov
56%
ip_is_overlap
65.7
ip_normalize
65.1
ip_prefix_to_range
42.0
CRAP 19.5
↑ Summary
1 # Copyright (c) 1999 - 2002 RIPE NCC
2 #
3 # All Rights Reserved
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose and without fee is hereby granted,
7 # provided that the above copyright notice appear in all copies and that
8 # both that copyright notice and this permission notice appear in
9 # supporting documentation, and that the name of the author not be
10 # used in advertising or publicity pertaining to distribution of the
11 # software without specific, written prior permission.
12 #
13 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
14 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
15 # AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
17 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  
20 #------------------------------------------------------------------------------
21 # Module Header
22 # Filename : IP.pm
23 # Purpose : Provide functions to manipulate IPv4/v6 addresses
24 # Author : Manuel Valente <manuel.valente@gmail.com>
25 # Date : 19991124
26 # Description :
27 # Language Version : Perl 5
28 # OSs Tested : BSDI 3.1 - Linux
29 # Command Line : ipcount
30 # Input Files :
31 # Output Files :
32 # External Programs : Math::BigInt.pm
33 # Problems :
34 # To Do :
35 # Comments : Based on ipv4pack.pm (Monica) and iplib.pm (Lee)
36 # Math::BigInt is only loaded if int functions are used
37 # $Id: IP.pm,v 1.23 2003/02/18 16:13:01 manuel Exp $
38 #------------------------------------------------------------------------------
39  
40 package Net::IP;
41  
42 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # Global Variables definition
46 2 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $ERROR $ERRNO
Subroutine BEGIN: called CC=1 SCAR=0.0
47 %IPv4ranges %IPv6ranges $useBigInt
48 2 $IP_NO_OVERLAP $IP_PARTIAL_OVERLAP $IP_A_IN_B_OVERLAP $IP_B_IN_A_OVERLAP $IP_IDENTICAL);
49  
50 2 $VERSION = '1.26';
51  
52 2 require Exporter;
53  
54 2 @ISA = qw(Exporter);
55  
56 # Functions and variables exported in all cases
57 2 @EXPORT = qw(&Error &Errno
58 $IP_NO_OVERLAP $IP_PARTIAL_OVERLAP $IP_A_IN_B_OVERLAP $IP_B_IN_A_OVERLAP $IP_IDENTICAL
59 );
60  
61 # Functions exported on demand (with :PROC)
62 2 @EXPORT_OK = qw(&Error &Errno &ip_iptobin &ip_bintoip &ip_bintoint &ip_inttobin
63 &ip_get_version &ip_is_ipv4 &ip_is_ipv6 &ip_expand_address &ip_get_mask
64 &ip_last_address_bin &ip_splitprefix &ip_prefix_to_range
65 &ip_is_valid_mask &ip_bincomp &ip_binadd &ip_get_prefix_length
66 &ip_range_to_prefix &ip_compress_address &ip_is_overlap
67 &ip_get_embedded_ipv4 &ip_aggregate &ip_iptype &ip_check_prefix
68 &ip_reverse &ip_normalize &ip_normal_range &ip_iplengths
69 $IP_NO_OVERLAP $IP_PARTIAL_OVERLAP $IP_A_IN_B_OVERLAP $IP_B_IN_A_OVERLAP $IP_IDENTICAL
70 );
71  
72 2 %EXPORT_TAGS = (PROC => [@EXPORT_OK],);
73  
74 # Definition of the Ranges for IPv4 IPs
75 2 %IPv4ranges = (
76 '00000000' => 'PRIVATE', # 0/8
77 '00001010' => 'PRIVATE', # 10/8
78 '0110010001' => 'SHARED', # 100.64/10
79 '01111111' => 'LOOPBACK', # 127.0/8
80 '1010100111111110' => 'LINK-LOCAL', # 169.254/16
81 '101011000001' => 'PRIVATE', # 172.16/12
82 '110000000000000000000000' => 'RESERVED', # 192.0.0/24
83 '110000000000000000000010' => 'TEST-NET', # 192.0.2/24
84 '110000000101100001100011' => '6TO4-RELAY', # 192.88.99.0/24
85 '1100000010101000' => 'PRIVATE', # 192.168/16
86 '110001100001001' => 'RESERVED', # 198.18/15
87 '110001100011001101100100' => 'TEST-NET', # 198.51.100/24
88 '110010110000000001110001' => 'TEST-NET', # 203.0.113/24
89 '1110' => 'MULTICAST', # 224/4
90 '1111' => 'RESERVED', # 240/4
91 '11111111111111111111111111111111' => 'BROADCAST', # 255.255.255.255/32
92 );
93  
94 # Definition of the Ranges for Ipv6 IPs
95 2 %IPv6ranges = (
96 '00000000' => 'RESERVED', # ::/8
97 ('0' x 128) => 'UNSPECIFIED', # ::/128
98 ('0' x 127) . '1' => 'LOOPBACK', # ::1/128
99 ('0' x 80) . ('1' x 16) => 'IPV4MAP', # ::FFFF:0:0/96
100 '00000001' => 'RESERVED', # 0100::/8
101 '0000000100000000' . ('0' x 48) => 'DISCARD', # 0100::/64
102 '0000001' => 'RESERVED', # 0200::/7
103 '000001' => 'RESERVED', # 0400::/6
104 '00001' => 'RESERVED', # 0800::/5
105 '0001' => 'RESERVED', # 1000::/4
106 '001' => 'GLOBAL-UNICAST', # 2000::/3
107 '0010000000000001' . ('0' x 16) => 'TEREDO', # 2001::/32
108 '00100000000000010000000000000010' . ('0' x 16) => 'BMWG', # 2001:0002::/48
109 '00100000000000010000110110111000' => 'DOCUMENTATION', # 2001:DB8::/32
110 '0010000000000001000000000001' => 'ORCHID', # 2001:10::/28
111 '0010000000000010' => '6TO4', # 2002::/16
112 '010' => 'RESERVED', # 4000::/3
113 '011' => 'RESERVED', # 6000::/3
114 '100' => 'RESERVED', # 8000::/3
115 '101' => 'RESERVED', # A000::/3
116 '110' => 'RESERVED', # C000::/3
117 '1110' => 'RESERVED', # E000::/4
118 '11110' => 'RESERVED', # F000::/5
119 '111110' => 'RESERVED', # F800::/6
120 '1111110' => 'UNIQUE-LOCAL-UNICAST', # FC00::/7
121 '111111100' => 'RESERVED', # FE00::/9
122 '1111111010' => 'LINK-LOCAL-UNICAST', # FE80::/10
123 '1111111011' => 'RESERVED', # FEC0::/10
124 '11111111' => 'MULTICAST', # FF00::/8
125 );
126  
127 # Overlap constants
128 2 $IP_NO_OVERLAP = 0;
129 2 $IP_PARTIAL_OVERLAP = 1;
130 2 $IP_A_IN_B_OVERLAP = -1;
131 2 $IP_B_IN_A_OVERLAP = -2;
132 2 $IP_IDENTICAL = -3;
133  
134 # ----------------------------------------------------------
135 # OVERLOADING
136  
137 2 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
138 '+' => 'ip_add_num',
139 36023 ▶ 'bool' => sub { @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
140 2 );
141  
142 #------------------------------------------------------------------------------
143 # Subroutine ip_num_add
144 # Purpose : Add an integer to an IP
145 # Params : Number to add
146 # Returns : New object or undef
147 # Note : Used by overloading - returns undef when
148 # the end of the range is reached
149  
150 0 ▶sub ip_add_num {
Subroutine ip_add_num: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my $self = shift;
152  
153 0 my ($value) = @_;
154
155 0 my $ip = $self->intip + $value;
156
157 0 my $last = $self->last_int;
158  
159 # Reached the end of the range ?
160 0 ▶ if ($ip > $self->last_int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip > $self->last_int) 0 0 0
161 0 return;
162 }
163  
164 0 my $newb = ip_inttobin($ip, $self->version);
165 0 $newb = ip_bintoip($newb, $self->version);
166  
167 0 my $newe = ip_inttobin($last, $self->version);
168 0 $newe = ip_bintoip($newe, $self->version);
169  
170 0 my $new = new Net::IP("$newb - $newe");
171  
172 0 return ($new);
173 }
174  
175 # -----------------------------------------------------------------------------
176  
177 #------------------------------------------------------------------------------
178 # Subroutine new
179 # Purpose : Create an instance of an IP object
180 # Params : Class, IP prefix, IP version
181 # Returns : Object reference or undef
182 # Note : New just allocates a new object - set() does all the work
183 36021 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
184 36021 my ($class, $data, $ipversion) = (@_);
185  
186 # Allocate new object
187 36021 my $self = {};
188  
189 36021 bless($self, $class);
190  
191 # Pass everything to set()
192 36021 ▶ unless ($self->set($data, $ipversion)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->set($data, $ipversion)) 0 36021 36021
193 0 return;
194 }
195  
196 36021 return $self;
197 }
198  
199 #------------------------------------------------------------------------------
200 # Subroutine set
201 # Purpose : Set the IP for an IP object
202 # Params : Data, IP type
203 # Returns : 1 (success) or undef (failure)
204 36039 ▶sub set {
Subroutine set: called CC=16 SCAR=32.9
Pod documented
205 36039 my $self = shift;
206  
207 36039 my ($data, $ipversion) = @_;
208  
209 # Normalize data as received - this should return 2 IPs
210 36039 ▶ my ($begin, $end) = ip_normalize($data, $ipversion) or do {
Branch: 1 branch
BranchTrueFalseTotal
() = ip_normalize($data, $ipversion) or do { $self->{"error"} = $Net::IP::ERROR; $self->{"errno"} = $Net::IP::ERRNO; return } 36036 3 36039
211 3 $self->{error} = $ERROR;
212 3 $self->{errno} = $ERRNO;
213 3 return;
214 };
215  
216 # Those variables are set when the object methods are called
217 # We need to reset everything
218 36036 for (
219 qw(ipversion errno prefixlen binmask reverse_ip last_ip iptype
220 binip error ip intformat hexformat mask last_bin last_int prefix is_prefix)
221 )
222 {
223 612612 delete($self->{$_});
224 }
225  
226 # Determine IP version for this object
227 36036 ▶ return unless ($self->{ipversion} = $ipversion || ip_get_version($begin));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ipversion"} = $ipversion || ip_get_version($begin) 0 36036 36036
Condition66%2/3
$ipversion || ip_get_version($begin)
l!l&&r!l&&!r
4360320
MC/DC0%0/2
$ipversion || ip_get_version($begin)
$ipversion ip_get_version($begin)
Truth table66%2/3
A || B
A= $ipversionB= ip_get_version($begin)
ABresult
1X1
011
000
228  
229 # Set begin IP address
230 36036 $self->{ip} = $begin;
231  
232 # Set Binary IP address
233 return
234 36036 ▶ unless ($self->{binip} = ip_iptobin($self->ip(), $self->version()));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"binip"} = ip_iptobin($self->ip, $self->version) 0 36036 36036
235  
236 36036 $self->{is_prefix} = 0;
237  
238 # Set end IP address
239 # If single IP: begin and end IPs are identical
240 36036 ▶ $end ||= $begin;
Condition66%2/3
$end ||= $begin
l!l&&r!l&&!r
20360160
MC/DC0%0/2
$end ||= $begin
$end $begin
Truth table66%2/3
A ||= B
A= $endB= $begin
ABresult
1X1
011
000
241 36036 $self->{last_ip} = $end;
242  
243 # Try to determine the IP version
244 36036 ▶ my $ver = ip_get_version($end) || return;
Condition50%1/2
ip_get_version($end) || (return)
l!l
360360
MC/DC0%0/1
ip_get_version($end) || (return)
ip_get_version($end)
Truth table50%1/2
A
A= ip_get_version($end)
Aresult
11
00
245  
246 # Check if begin and end addresses have the same version
247 36036 ▶ if ($ver != $self->version()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver != $self->version) 0 36036 36036
248 0 $ERRNO = 201;
249 0 $ERROR =
250 "Begin and End addresses have different IP versions - $begin - $end";
251 0 $self->{errno} = $ERRNO;
252 0 $self->{error} = $ERROR;
253 0 return;
254 }
255  
256 # Get last binary address
257 return
258 unless ($self->{last_bin} =
259 36036 ▶ ip_iptobin($self->last_ip(), $self->version()));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"last_bin"} = ip_iptobin($self->last_ip, $self->version) 0 36036 36036
260  
261 # Check that End IP >= Begin IP
262 36036 ▶ unless (ip_bincomp($self->binip(), 'le', $self->last_bin())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ip_bincomp($self->binip, "le", $self->last_bin)) 0 36036 36036
263 0 $ERRNO = 202;
264 0 $ERROR = "Begin address is greater than End address $begin - $end";
265 0 $self->{errno} = $ERRNO;
266 0 $self->{error} = $ERROR;
267 0 return;
268 }
269  
270 # Find all prefixes (eg:/24) in the current range
271 36036 ▶ my @prefixes = $self->find_prefixes() or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@prefixes) = $self->find_prefixes or return 36036 0 36036
272  
273 # If there is only one prefix:
274 36036 ▶ if (scalar(@prefixes) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @prefixes == 1) 36034 2 36036
275  
276 # Get length of prefix
277 return
278 36034 ▶ unless ((undef, $self->{prefixlen}) = ip_splitprefix($prefixes[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless (undef, $self->{"prefixlen"}) = ip_splitprefix($prefixes[0]) 0 36034 36034
279  
280 # Set prefix boolean var
281 # This value is 1 if the IP range only contains a single /nn prefix
282 36034 $self->{is_prefix} = 1;
283 }
284  
285 # If the range is a single prefix:
286 36036 ▶ if ($self->{is_prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_prefix"}) 36034 2 36036
287  
288 # Set mask property
289 36034 $self->{binmask} = ip_get_mask($self->prefixlen(), $self->version());
290  
291 # Check that the mask is valid
292 36034 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (ip_check_prefix($self->binip, $self->prefixlen, $self->version)) 0 36034 36034
293 ip_check_prefix(
294 $self->binip(), $self->prefixlen(), $self->version()
295 )
296 )
297 {
298 0 $self->{error} = $ERROR;
299 0 $self->{errno} = $ERRNO;
300 0 return;
301 }
302 }
303  
304 36036 return ($self);
305 }
306  
307 36009 ▶sub print {
Subroutine print: called CC=2 SCAR=7.6
Pod documented
308 36009 my $self = shift;
309  
310 36009 ▶ if ($self->{is_prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_prefix'}) { } 36009 0 36009
311 36009 return ($self->short() . '/' . $self->prefixlen());
312 }
313 else {
314 0 return (sprintf("%s - %s", $self->ip(), $self->last_ip()));
315 }
316 }
317  
318 #------------------------------------------------------------------------------
319 # Subroutine error
320 # Purpose : Return the current error message
321 # Returns : Error string
322 36058 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
323 36058 my $self = shift;
324 36058 return $self->{error};
325 }
326  
327 #------------------------------------------------------------------------------
328 # Subroutine errno
329 # Purpose : Return the current error number
330 # Returns : Error number
331 3 ▶sub errno {
Subroutine errno: called CC=1 SCAR=0.0
Pod documented
332 3 my $self = shift;
333 3 return $self->{errno};
334 }
335  
336 #------------------------------------------------------------------------------
337 # Subroutine binip
338 # Purpose : Return the IP as a binary string
339 # Returns : binary string
340 144142 ▶sub binip {
Subroutine binip: called CC=1 SCAR=0.0
Pod documented
341 144142 my $self = shift;
342 144142 return $self->{binip};
343 }
344  
345 #------------------------------------------------------------------------------
346 # Subroutine prefixlen
347 # Purpose : Get the IP prefix length
348 # Returns : prefix length
349 116086 ▶sub prefixlen {
Subroutine prefixlen: called CC=1 SCAR=0.0
Pod documented
350 116086 my $self = shift;
351 116086 return $self->{prefixlen};
352 }
353  
354 #------------------------------------------------------------------------------
355 # Subroutine version
356 # Purpose : Return the IP version
357 # Returns : IP version
358 316256 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
359 316256 my $self = shift;
360 316256 return $self->{ipversion};
361 }
362  
363 #------------------------------------------------------------------------------
364 # Subroutine version
365 # Purpose : Return the IP in quad format
366 # Returns : IP string
367 72054 ▶sub ip {
Subroutine ip: called CC=1 SCAR=0.0
Pod documented
368 72054 my $self = shift;
369 72054 return $self->{ip};
370 }
371  
372 #------------------------------------------------------------------------------
373 # Subroutine is_prefix
374 # Purpose : Check if range of IPs is a prefix
375 # Returns : boolean
376 4 ▶sub is_prefix {
Subroutine is_prefix: called CC=1 SCAR=0.0
Pod undocumented
377 4 my $self = shift;
378 4 return $self->{is_prefix};
379 }
380  
381 #------------------------------------------------------------------------------
382 # Subroutine binmask
383 # Purpose : Return the binary mask of an IP prefix
384 # Returns : Binary mask (as string)
385 5 ▶sub binmask {
Subroutine binmask: called CC=1 SCAR=0.0
Pod documented
386 5 my $self = shift;
387 5 return $self->{binmask};
388 }
389  
390 #------------------------------------------------------------------------------
391 # Subroutine size
392 # Purpose : Return the number of addresses contained in an IP object
393 # Returns : Number of addresses
394 2 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
395 2 my $self = shift;
396  
397 2 my $size = new Math::BigInt($self->last_int);
398 2 $size->badd(1);
399
400 2 $size->bsub($self->intip);
401 }
402
403 # All the following functions work the same way: the method is just a frontend
404 # to the real function. When the real function is called, the output is cached
405 # so that next time the same function is called,the frontend function directly
406 # returns the result.
407  
408 #------------------------------------------------------------------------------
409 # Subroutine intip
410 # Purpose : Return the IP in integer format
411 # Returns : Integer
412 8 ▶sub intip {
Subroutine intip: called CC=3 SCAR=11.8
Pod documented
413 8 my $self = shift;
414  
415 8 ▶ return ($self->{intformat}) if defined($self->{intformat});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"intformat"} 3 5 8
416  
417 5 my $int = ip_bintoint($self->binip());
418  
419 5 ▶ if (!$int) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($int) 0 5 5
420 0 $self->{error} = $ERROR;
421 0 $self->{errno} = $ERRNO;
422 0 return;
423 }
424  
425 5 $self->{intformat} = $int;
426  
427 5 return ($int);
428 }
429  
430 #------------------------------------------------------------------------------
431 # Subroutine hexip
432 # Purpose : Return the IP in hex format
433 # Returns : hex string
434 4 ▶sub hexip {
Subroutine hexip: called CC=2 SCAR=7.0
Pod documented
435 4 my $self = shift;
436 4 ▶ return $self->{'hexformat'} if(defined($self->{'hexformat'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"hexformat"} 0 4 4
437 4 $self->{'hexformat'} = $self->intip->as_hex();
438 4 return $self->{'hexformat'};
439 }
440  
441 #------------------------------------------------------------------------------
442 # Subroutine hexmask
443 # Purpose : Return the mask back in hex
444 # Returns : hex string
445 2 ▶sub hexmask {
Subroutine hexmask: called CC=2 SCAR=7.0
Pod documented
446 2 my $self = shift;
447  
448 2 ▶ return $self->{hexmask} if(defined($self->{hexmask}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"hexmask"} 0 2 2
449
450 2 my $intmask = ip_bintoint($self->binmask);
451
452 2 $self->{'hexmask'} = $intmask->as_hex();
453
454 2 return ($self->{'hexmask'});
455 }
456  
457 #------------------------------------------------------------------------------
458 # Subroutine prefix
459 # Purpose : Return the Prefix (n.n.n.n/s)
460 # Returns : IP Prefix
461 0 ▶sub prefix {
Subroutine prefix: not called CC=4 SCAR=30.0
Pod documented
462 0 my $self = shift;
463  
464 0 ▶ if (not $self->is_prefix()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_prefix) 0 0 0
465 0 $self->{error} = "IP range $self->{ip} is not a Prefix.";
466 0 $self->{errno} = 209;
467 0 return;
468 }
469  
470 0 ▶ return ($self->{prefix}) if defined($self->{prefix});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prefix"} 0 0 0
471  
472 0 my $prefix = $self->ip() . '/' . $self->prefixlen();
473  
474 0 ▶ if (!$prefix) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix) 0 0 0
475 0 $self->{error} = $ERROR;
476 0 $self->{errno} = $ERRNO;
477 0 return;
478 }
479  
480 0 $self->{prefix} = $prefix;
481  
482 0 return ($prefix);
483 }
484  
485 #------------------------------------------------------------------------------
486 # Subroutine mask
487 # Purpose : Return the IP mask in quad format
488 # Returns : Mask (string)
489 2 ▶sub mask {
Subroutine mask: called CC=4 SCAR=17.4
Pod documented
490 2 my $self = shift;
491  
492 2 ▶ if (not $self->is_prefix()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_prefix) 0 2 2
493 0 $self->{error} = "IP range $self->{ip} is not a Prefix.";
494 0 $self->{errno} = 209;
495 0 return;
496 }
497  
498 2 ▶ return ($self->{mask}) if defined($self->{mask});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mask"} 0 2 2
499  
500 2 my $mask = ip_bintoip($self->binmask(), $self->version());
501  
502 2 ▶ if (!$mask) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask) 0 2 2
503 0 $self->{error} = $ERROR;
504 0 $self->{errno} = $ERRNO;
505 0 return;
506 }
507  
508 2 $self->{mask} = $mask;
509  
510 2 return ($mask);
511 }
512  
513 #------------------------------------------------------------------------------
514 # Subroutine short
515 # Purpose : Get the short format of an IP address or a Prefix
516 # Returns : short format IP or undef
517 36011 ▶sub short {
Subroutine short: called CC=3 SCAR=11.7
Pod documented
518 36011 my $self = shift;
519  
520 36011 my $r;
521  
522 36011 ▶ if ($self->version == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version == 6) { } 28008 8003 36011
523 28008 $r = ip_compress_address($self->ip(), $self->version());
524 }
525 else {
526 8003 $r = ip_compress_v4_prefix($self->ip(), $self->prefixlen());
527 }
528  
529 36011 ▶ if (!defined($r)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r) 0 36011 36011
530 0 $self->{error} = $ERROR;
531 0 $self->{errno} = $ERRNO;
532 0 return;
533 }
534  
535 36011 return ($r);
536 }
537  
538 #------------------------------------------------------------------------------
539 # Subroutine iptype
540 # Purpose : Return the type of an IP
541 # Returns : Type or undef (failure)
542 36010 ▶sub iptype {
Subroutine iptype: called CC=3 SCAR=12.6
Pod documented
543 36010 my ($self) = shift;
544  
545 36010 ▶ return ($self->{iptype}) if defined($self->{iptype});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"iptype"} 0 36010 36010
546  
547 36010 my $type = ip_iptype($self->binip(), $self->version());
548  
549 36010 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 36010 36010
550 0 $self->{error} = $ERROR;
551 0 $self->{errno} = $ERRNO;
552 0 return;
553 }
554  
555 36010 $self->{iptype} = $type;
556  
557 36010 return ($type);
558 }
559  
560 #------------------------------------------------------------------------------
561 # Subroutine reverse_ip
562 # Purpose : Return the Reverse IP
563 # Returns : Reverse IP or undef(failure)
564 2 ▶sub reverse_ip {
Subroutine reverse_ip: called CC=4 SCAR=17.4
Pod documented
565 2 my ($self) = shift;
566  
567 2 ▶ if (not $self->is_prefix()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_prefix) 0 2 2
568 0 $self->{error} = "IP range $self->{ip} is not a Prefix.";
569 0 $self->{errno} = 209;
570 0 return;
571 }
572  
573 2 ▶ return ($self->{reverse_ip}) if defined($self->{reverse_ip});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"reverse_ip"} 0 2 2
574  
575 2 my $rev = ip_reverse($self->ip(), $self->prefixlen(), $self->version());
576  
577 2 ▶ if (!$rev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rev) 0 2 2
578 0 $self->{error} = $ERROR;
579 0 $self->{errno} = $ERRNO;
580 0 return;
581 }
582  
583 2 $self->{reverse_ip} = $rev;
584  
585 2 return ($rev);
586 }
587  
588 #------------------------------------------------------------------------------
589 # Subroutine last_bin
590 # Purpose : Get the last IP of a range in binary format
591 # Returns : Last binary IP or undef (failure)
592 72086 ▶sub last_bin {
Subroutine last_bin: called CC=4 SCAR=25.8
Pod documented
593 72086 my ($self) = shift;
594  
595 72086 ▶ return ($self->{last_bin}) if defined($self->{last_bin});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"last_bin"} 72086 0 72086
596  
597 0 my $last;
598  
599 0 ▶ if ($self->is_prefix()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_prefix) { } 0 0 0
600 0 $last =
601 ip_last_address_bin($self->binip(), $self->prefixlen(),
602 $self->version());
603 }
604 else {
605 0 $last = ip_iptobin($self->last_ip(), $self->version());
606 }
607  
608 0 ▶ if (!$last) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last) 0 0 0
609 0 $self->{error} = $ERROR;
610 0 $self->{errno} = $ERRNO;
611 0 return;
612 }
613  
614 0 $self->{last_bin} = $last;
615  
616 0 return ($last);
617 }
618  
619 #------------------------------------------------------------------------------
620 # Subroutine last_int
621 # Purpose : Get the last IP of a range in integer format
622 # Returns : Last integer IP or undef (failure)
623 2 ▶sub last_int {
Subroutine last_int: called CC=4 SCAR=14.5
Pod documented
624 2 my ($self) = shift;
625  
626 2 ▶ return ($self->{last_int}) if defined($self->{last_int});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"last_int"} 0 2 2
627  
628 2 ▶ my $last_bin = $self->last_bin() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $last_bin = $self->last_bin or return 2 0 2
629  
630 2 ▶ my $last_int = ip_bintoint($last_bin, $self->version()) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $last_int = ip_bintoint($last_bin, $self->version) or return 2 0 2
631  
632 2 $self->{last_int} = $last_int;
633  
634 2 return ($last_int);
635 }
636  
637 #------------------------------------------------------------------------------
638 # Subroutine last_ip
639 # Purpose : Get the last IP of a prefix in IP format
640 # Returns : IP or undef (failure)
641 36038 ▶sub last_ip {
Subroutine last_ip: called CC=3 SCAR=19.6
Pod documented
642 36038 my ($self) = shift;
643  
644 36038 ▶ return ($self->{last_ip}) if defined($self->{last_ip});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"last_ip"} 36038 0 36038
645  
646 0 my $last = ip_bintoip($self->last_bin(), $self->version());
647  
648 0 ▶ if (!$last) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last) 0 0 0
649 0 $self->{error} = $ERROR;
650 0 $self->{errno} = $ERRNO;
651 0 return;
652 }
653  
654 0 $self->{last_ip} = $last;
655  
656 0 return ($last);
657 }
658  
659 #------------------------------------------------------------------------------
660 # Subroutine find_prefixes
661 # Purpose : Get all prefixes in the range defined by two IPs
662 # Params : IP
663 # Returns : List of prefixes or undef (failure)
664 36039 ▶sub find_prefixes {
Subroutine find_prefixes: called CC=2 SCAR=8.5
Pod documented
665 36039 my ($self) = @_;
666  
667 36039 my @list =
668 ip_range_to_prefix($self->binip(), $self->last_bin(), $self->version());
669  
670 36039 ▶ if (!scalar(@list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @list) 0 36039 36039
671 0 $self->{error} = $ERROR;
672 0 $self->{errno} = $ERRNO;
673 0 return;
674 }
675  
676 36039 return (@list);
677 }
678  
679 #------------------------------------------------------------------------------
680 # Subroutine bincomp
681 # Purpose : Compare two IPs
682 # Params : Operation, IP to compare
683 # Returns : 1 (True), 0 (False) or undef (problem)
684 # Comments : Operation can be lt, le, gt, ge
685 2 ▶sub bincomp {
Subroutine bincomp: called CC=2 SCAR=8.5
Pod documented
686 2 my ($self, $op, $other) = @_;
687  
688 2 my $a = ip_bincomp($self->binip(), $op, $other->binip());
689  
690 2 ▶ unless (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 0 2 2
691 0 $self->{error} = $ERROR;
692 0 $self->{errno} = $ERRNO;
693 0 return;
694 }
695  
696 2 return ($a);
697 }
698  
699 #------------------------------------------------------------------------------
700 # Subroutine binadd
701 # Purpose : Add two IPs
702 # Params : IP to add
703 # Returns : New IP object or undef (failure)
704 2 ▶sub binadd {
Subroutine binadd: called CC=3 SCAR=13.0
Pod documented
705 2 my ($self, $other) = @_;
706  
707 2 my $ip = ip_binadd($self->binip(), $other->binip());
708  
709 2 ▶ if (!$ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 2 2
710 0 $self->{error} = $ERROR;
711 0 $self->{errno} = $ERRNO;
712 0 return;
713 }
714  
715 2 ▶ my $new = new Net::IP(ip_bintoip($ip, $self->version())) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $new = "Net::IP"->new(ip_bintoip($ip, $self->version)) or return 2 0 2
716  
717 2 return ($new);
718 }
719  
720 #------------------------------------------------------------------------------
721 # Subroutine aggregate
722 # Purpose : Aggregate (append) two IPs
723 # Params : IP to add
724 # Returns : New IP object or undef (failure)
725 2 ▶sub aggregate {
Subroutine aggregate: called CC=2 SCAR=8.5
Pod documented
726 2 my ($self, $other) = @_;
727  
728 2 my $r = ip_aggregate(
729 $self->binip(), $self->last_bin(),
730 $other->binip(), $other->last_bin(),
731 $self->version()
732 );
733  
734 2 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 2 2
735 0 $self->{error} = $ERROR;
736 0 $self->{errno} = $ERRNO;
737 0 return;
738 }
739  
740 2 return (new Net::IP($r));
741 }
742  
743 #------------------------------------------------------------------------------
744 # Subroutine overlaps
745 # Purpose : Check if two prefixes overlap
746 # Params : Prefix to compare
747 # Returns : $NO_OVERLAP (no overlap)
748 # $IP_PARTIAL_OVERLAP (overlap)
749 # $IP_A_IN_B_OVERLAP (range1 is included in range2)
750 # $IP_B_IN_A_OVERLAP (range2 is included in range1)
751 # $IP_IDENTICAL (range1 == range2)
752 # or undef (problem)
753  
754 2 ▶sub overlaps {
Subroutine overlaps: called CC=2 SCAR=8.5
Pod documented
755 2 my ($self, $other) = @_;
756  
757 2 my $r = ip_is_overlap(
758 $self->binip(), $self->last_bin(),
759 $other->binip(), $other->last_bin()
760 );
761  
762 2 ▶ if (!defined($r)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r) 0 2 2
763 0 $self->{error} = $ERROR;
764 0 $self->{errno} = $ERRNO;
765 0 return;
766 }
767  
768 2 return ($r);
769 }
770  
771 #------------------------------------------------------------------------------
772 # Subroutine auth
773 # Purpose : Return Authority information from IP::Authority
774 # Params : IP object
775 # Returns : Authority Source
776  
777 0 ▶sub auth {
Subroutine auth: not called CC=3 SCAR=24.8
Pod documented
778 0 my ($self) = shift;
779  
780 0 ▶ return ($self->{auth}) if defined($self->{auth});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"auth"} 0 0 0
781  
782 0 my $auth = ip_auth($self->ip, $self->version);
783  
784 0 ▶ if (!$auth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth) 0 0 0
785 0 $self->{error} = $ERROR;
786 0 $self->{errno} = $ERRNO;
787 0 return;
788 }
789  
790 0 $self->{auth} = $auth;
791  
792 0 return ($self->{auth});
793 }
794  
795 #------------------------------ PROCEDURAL INTERFACE --------------------------
796 #------------------------------------------------------------------------------
797 # Subroutine Error
798 # Purpose : Return the ERROR string
799 # Returns : string
800 1 ▶sub Error {
Subroutine Error: called CC=1 SCAR=0.0
Pod documented
801 1 return ($ERROR);
802 }
803  
804 #------------------------------------------------------------------------------
805 # Subroutine Error
806 # Purpose : Return the ERRNO value
807 # Returns : number
808 1 ▶sub Errno {
Subroutine Errno: called CC=1 SCAR=0.0
Pod documented
809 1 return ($ERRNO);
810 }
811  
812 #------------------------------------------------------------------------------
813 # Subroutine ip_iplengths
814 # Purpose : Get the length in bits of an IP from its version
815 # Params : IP version
816 # Returns : Number of bits
817  
818 180294 ▶sub ip_iplengths {
Subroutine ip_iplengths: called CC=3 SCAR=11.3
Pod undocumented
819 180294 my ($version) = @_;
820  
821 180294 ▶ if ($version == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 4) { } 40166 140128 180294
elsif ($version == 6) { } 140128 0 140128
822 40166 return (32);
823 }
824 elsif ($version == 6) {
825 140128 return (128);
826 }
827 else {
828 0 return;
829 }
830 }
831  
832 #------------------------------------------------------------------------------
833 # Subroutine ip_iptobin
834 # Purpose : Transform an IP address into a bit string
835 # Params : IP address, IP version
836 # Returns : bit string on success, undef otherwise
837 72090 ▶sub ip_iptobin {
Subroutine ip_iptobin: called CC=3 SCAR=11.8
Pod documented
838 72090 my ($ip, $ipversion) = @_;
839  
840 # v4 -> return 32-bit array
841 72090 ▶ if ($ipversion == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipversion == 4) 16050 56040 72090
842 16050 return unpack('B32', pack('C4C4C4C4', split(/\./, $ip)));
843 }
844  
845 # Strip ':'
846 56040 $ip =~ s/://g;
847  
848 # Check size
849 56040 ▶ unless (length($ip) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $ip == 32) 0 56040 56040
850 0 $ERROR = "Bad IP address $ip";
851 0 $ERRNO = 102;
852 0 return;
853 }
854  
855 # v6 -> return 128-bit array
856 56040 return unpack('B128', pack('H32', $ip));
857 }
858  
859 #------------------------------------------------------------------------------
860 # Subroutine ip_bintoip
861 # Purpose : Transform a bit string into an IP address
862 # Params : bit string, IP version
863 # Returns : IP address on success, undef otherwise
864 36076 ▶sub ip_bintoip {
Subroutine ip_bintoip: called CC=3 SCAR=11.7
Pod documented
865 36076 my ($binip, $ip_version) = @_;
866  
867 # Define normal size for address
868 36076 my $len = ip_iplengths($ip_version);
869  
870 36076 ▶ if ($len < length($binip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < length $binip) 0 36076 36076
871 0 $ERROR = "Invalid IP length for binary IP $binip\n";
872 0 $ERRNO = 189;
873 0 return;
874 }
875  
876 # Prepend 0s if address is less than normal size
877 36076 $binip = '0' x ($len - length($binip)) . $binip;
878  
879 # IPv4
880 36076 ▶ if ($ip_version == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_version == 4) 8043 28033 36076
881 8043 return join '.', unpack('C4C4C4C4', pack('B32', $binip));
882 }
883  
884 # IPv6
885 28033 return join(':', unpack('H4H4H4H4H4H4H4H4', pack('B128', $binip)));
886 }
887  
888 #------------------------------------------------------------------------------
889 # Subroutine ip_bintoint
890 # Purpose : Transform a bit string into an Integer
891 # Params : bit string
892 # Returns : BigInt
893 9 ▶sub ip_bintoint {
Subroutine ip_bintoint: called CC=3 SCAR=11.0
Pod documented
894 9 my $binip = shift;
895  
896 # $n is the increment, $dec is the returned value
897 9 my ($n, $dec) = (Math::BigInt->new(1), Math::BigInt->new(0));
898  
899  
900 # Reverse the bit string
901 9 foreach (reverse(split '', $binip)) {
902  
903 # If the nth bit is 1, add 2**n to $dec
904 480 ▶ $_ and $dec += $n;
Branch: 1 branch
BranchTrueFalseTotal
$_ and $dec += $n 155 325 480
905 480 $n *= 2;
906 }
907  
908 # Strip leading + sign
909 9 $dec =~ s/^\+//;
910 9 return $dec;
911 }
912  
913 #------------------------------------------------------------------------------
914 # Subroutine ip_inttobin
915 # Purpose : Transform a BigInt into a bit string
916 # Comments : sets warnings (-w) off.
917 # This is necessary because Math::BigInt is not compliant
918 # Params : BigInt, IP version
919 # Returns : bit string
920 0 ▶sub ip_inttobin {
Subroutine ip_inttobin: not called CC=2 SCAR=17.9
Pod documented
921  
922 0 my $dec = Math::BigInt->new(shift);
923  
924 # Find IP version
925 0 my $ip_version = shift;
926  
927 0 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 0 0
928 0 $ERROR = "Cannot determine IP version for $dec";
929 0 $ERRNO = 101;
930 0 return;
931 }
932  
933 0 my $binip = $dec->as_bin();
934 0 $binip =~ s/^0b//;
935  
936 # Define normal size for address
937 0 my $len = ip_iplengths($ip_version);
938
939 # Prepend 0s if result is less than normal size
940 0 $binip = '0' x ($len - length($binip)) . $binip;
941  
942
943 0 return $binip;
944  
945 }
946  
947 #------------------------------------------------------------------------------
948 # Subroutine ip_get_version
949 # Purpose : Get an IP version
950 # Params : IP address
951 # Returns : 4, 6, 0(don't know)
952 108107 ▶sub ip_get_version {
Subroutine ip_get_version: called CC=4 SCAR=13.9
Pod documented
953 108107 my $ip = shift;
954  
955 # If the address does not contain any ':', maybe it's IPv4
956 108107 ▶ $ip !~ /:/ and ip_is_ipv4($ip) and return '4';
Branch: 1 branch
BranchTrueFalseTotal
not $ip =~ /:/ and ip_is_ipv4($ip) and return 4 24056 84051 108107
Condition66%2/3
not $ip =~ /:/ and ip_is_ipv4($ip)
!ll&&!rl&&r
84051024056
MC/DC50%1/2
not $ip =~ /:/ and ip_is_ipv4($ip)
not $ip =~ /:/ ip_is_ipv4($ip)
Truth table66%2/3
not(A) and B
A= not $ip =~ /:/B= ip_is_ipv4($ip)
ABresult
0X0
100
111
957  
958 # Is it IPv6 ?
959 84051 ▶ ip_is_ipv6($ip) and return '6';
Branch: 1 branch
BranchTrueFalseTotal
ip_is_ipv6($ip) and return 6 84050 1 84051
960  
961 1 return;
962 }
963  
964 #------------------------------------------------------------------------------
965 # Subroutine ip_is_ipv4
966 # Purpose : Check if an IP address is version 4
967 # Params : IP address
968 # Returns : 1 (yes) or 0 (no)
969 24056 ▶sub ip_is_ipv4 {
Subroutine ip_is_ipv4: called CC=12 SCAR=35.7
Pod documented
970 24056 my $ip = shift;
971  
972 # Check for invalid chars
973 24056 ▶ unless ($ip =~ m/^[\d\.]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /^[\d\.]+$/) 0 24056 24056
974 0 $ERROR = "Invalid chars in IP $ip";
975 0 $ERRNO = 107;
976 0 return 0;
977 }
978  
979 24056 ▶ if ($ip =~ m/^\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^\./) 0 24056 24056
980 0 $ERROR = "Invalid IP $ip - starts with a dot";
981 0 $ERRNO = 103;
982 0 return 0;
983 }
984  
985 24056 ▶ if ($ip =~ m/\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\.$/) 0 24056 24056
986 0 $ERROR = "Invalid IP $ip - ends with a dot";
987 0 $ERRNO = 104;
988 0 return 0;
989 }
990  
991 # Single Numbers are considered to be IPv4
992 24056 ▶ if ($ip =~ m/^(\d+)$/ and $1 < 256) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^(\d+)$/ and $1 < 256) 1 24055 24056
Condition66%2/3
$ip =~ /^(\d+)$/ and $1 < 256
!ll&&!rl&&r
2405501
MC/DC50%1/2
$ip =~ /^(\d+)$/ and $1 < 256
$ip =~ /^(\d+)$/ $1 < 256
Truth table66%2/3
A and B
A= $ip =~ /^(\d+)$/B= $1 < 256
ABresult
0X0
100
111
993  
994 # Count quads
995 24055 my $n = ($ip =~ tr/\./\./);
996  
997 # IPv4 must have from 1 to 4 quads
998 24055 ▶ unless ($n >= 0 and $n < 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n >= 0 and $n < 4) 0 24055 24055
Condition33%1/3
$n >= 0 and $n < 4
!ll&&!rl&&r
0024055
MC/DC0%0/2
$n >= 0 and $n < 4
$n >= 0 $n < 4
Truth table33%1/3
A and B
A= $n >= 0B= $n < 4
ABresult
0X0
100
111
999 0 $ERROR = "Invalid IP address $ip";
1000 0 $ERRNO = 105;
1001 0 return 0;
1002 }
1003  
1004 # Check for empty quads
1005 24055 ▶ if ($ip =~ m/\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\.\./) 0 24055 24055
1006 0 $ERROR = "Empty quad in IP address $ip";
1007 0 $ERRNO = 106;
1008 0 return 0;
1009 }
1010  
1011 24055 foreach (split /\./, $ip) {
1012  
1013 # Check for invalid quads
1014 96214 ▶ unless ($_ >= 0 and $_ < 256) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ >= 0 and $_ < 256) 0 96214 96214
Condition33%1/3
$_ >= 0 and $_ < 256
!ll&&!rl&&r
0096214
MC/DC0%0/2
$_ >= 0 and $_ < 256
$_ >= 0 $_ < 256
Truth table33%1/3
A and B
A= $_ >= 0B= $_ < 256
ABresult
0X0
100
111
1015 0 $ERROR = "Invalid quad in IP address $ip - $_";
1016 0 $ERRNO = 107;
1017 0 return 0;
1018 }
1019 }
1020 24055 return 1;
1021 }
1022  
1023 #------------------------------------------------------------------------------
1024 # Subroutine ip_is_ipv6
1025 # Purpose : Check if an IP address is version 6
1026 # Params : IP address
1027 # Returns : 1 (yes) or 0 (no)
1028 84055 ▶sub ip_is_ipv6 {
Subroutine ip_is_ipv6: called CC=13 SCAR=32.1
Pod documented
1029 84055 my $ip = shift;
1030  
1031 # Count octets
1032 84055 my $n = ($ip =~ tr/:/:/);
1033 84055 ▶ return 0 unless ($n > 0 and $n < 8);
Branch: 1 branch
BranchTrueFalseTotal
unless $n > 0 and $n < 8 0 84055 84055
Condition33%1/3
$n > 0 and $n < 8
!ll&&!rl&&r
0084055
MC/DC0%0/2
$n > 0 and $n < 8
$n > 0 $n < 8
Truth table33%1/3
A and B
A= $n > 0B= $n < 8
ABresult
0X0
100
111
1034  
1035 # $k is a counter
1036 84055 my $k;
1037  
1038 84055 foreach (split /:/, $ip) {
1039 672370 $k++;
1040  
1041 # Empty octet ?
1042 672370 ▶ next if ($_ eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 11 672359 672370
1043  
1044 # Normal v6 octet ?
1045 672359 ▶ next if (/^[a-f\d]{1,4}$/i);
Branch: 1 branch
BranchTrueFalseTotal
if /^[a-f\d]{1,4}$/i 672359 0 672359
1046  
1047 # Last octet - is it IPv4 ?
1048 0 ▶ if ( ($k == $n + 1) && ip_is_ipv4($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == $n + 1 and ip_is_ipv4($_)) 0 0 0
Condition0%0/3
$k == $n + 1 and ip_is_ipv4($_)
!ll&&!rl&&r
000
MC/DC0%0/2
$k == $n + 1 and ip_is_ipv4($_)
$k == $n + 1 ip_is_ipv4($_)
Truth table0%0/3
A and B
A= $k == $n + 1B= ip_is_ipv4($_)
ABresult
0X0
100
111
1049 0 $n++; # ipv4 is two octets
1050 0 next;
1051 }
1052  
1053 0 $ERROR = "Invalid IP address $ip";
1054 0 $ERRNO = 108;
1055 0 return 0;
1056 }
1057  
1058 # Does the IP address start with : ?
1059 84055 ▶ if ($ip =~ m/^:[^:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^:[^:]/) 1 84054 84055
1060 1 $ERROR = "Invalid address $ip (starts with :)";
1061 1 $ERRNO = 109;
1062 1 return 0;
1063 }
1064  
1065 # Does the IP address finish with : ?
1066 84054 ▶ if ($ip =~ m/[^:]:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /[^:]:$/) 0 84054 84054
1067 0 $ERROR = "Invalid address $ip (ends with :)";
1068 0 $ERRNO = 110;
1069 0 return 0;
1070 }
1071  
1072 # Does the IP address have more than one '::' pattern ?
1073 84054 ▶ if ($ip =~ s/:(?=:)/:/g > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ s/:(?=:)/:/g > 1) 0 84054 84054
1074 0 $ERROR = "Invalid address $ip (More than one :: pattern)";
1075 0 $ERRNO = 111;
1076 0 return 0;
1077 }
1078  
1079 # number of octets
1080 84054 ▶ if ($n != 7 && $ip !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != 7 and not $ip =~ /::/) 2 84052 84054
Condition100%3/3
$n != 7 and not $ip =~ /::/
!ll&&!rl&&r
84041112
MC/DC100%2/2
$n != 7 and not $ip =~ /::/
$n != 7 not $ip =~ /::/
Truth table100%3/3
A and not(B)
A= $n != 7B= not $ip =~ /::/
ABresult
0X0
100
111
1081 2 $ERROR = "Invalid number of octets $ip";
1082 2 $ERRNO = 112;
1083 2 return 0;
1084 }
1085
1086 # valid IPv6 address
1087 84052 return 1;
1088 }
1089  
1090 #------------------------------------------------------------------------------
1091 # Subroutine ip_expand_address
1092 # Purpose : Expand an address from compact notation
1093 # Params : IP address, IP version
1094 # Returns : expanded IP address or undef on failure
1095 64055 ▶sub ip_expand_address {
Subroutine ip_expand_address: called CC=12 SCAR=26.9
Pod documented
1096 64055 my ($ip, $ip_version) = @_;
1097  
1098 64055 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 64055 64055
1099 0 $ERROR = "Cannot determine IP version for $ip";
1100 0 $ERRNO = 101;
1101 0 return;
1102 }
1103  
1104 # v4 : add .0 for missing quads
1105 64055 ▶ if ($ip_version == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_version == 4) 8024 56031 64055
1106 8024 my @quads = split /\./, $ip;
1107  
1108 # check number of quads
1109 8024 ▶ if (scalar(@quads) > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @quads > 4) 0 8024 8024
1110 0 $ERROR = "Not a valid IPv address $ip";
1111 0 $ERRNO = 102;
1112 0 return;
1113 }
1114 8024 my @clean_quads = (0, 0, 0, 0);
1115  
1116 8024 foreach my $q (reverse @quads) {
1117
1118 #check quad data
1119 32084 ▶ if ($q !~ m/^\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q =~ /^\d{1,3}$/) 1 32083 32084
1120 1 $ERROR = "Not a valid IPv4 address $ip";
1121 1 $ERRNO = 102;
1122 1 return;
1123 }
1124
1125 # build clean ipv4
1126 32083 unshift(@clean_quads, $q + 1 - 1);
1127 }
1128  
1129 8023 return (join '.', @clean_quads[ 0 .. 3 ]);
1130 }
1131  
1132 # Keep track of ::
1133 56031 my $num_of_double_colon = ($ip =~ s/::/:!:/g);
1134 56031 ▶ if ($num_of_double_colon > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_of_double_colon > 1) 1 56030 56031
1135 1 $ERROR = "Too many :: in ip";
1136 1 $ERRNO = 102;
1137 1 return;
1138 }
1139  
1140 # IP as an array
1141 56030 my @ip = split /:/, $ip;
1142  
1143 # Number of octets
1144 56030 my $num = scalar(@ip);
1145  
1146 56030 foreach (0 .. (scalar(@ip) - 1)) {
1147  
1148 # Embedded IPv4
1149 448192 ▶ if ($ip[$_] =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip[$_] =~ /\./) 0 448192 448192
1150  
1151 # Expand Ipv4 address
1152 # Convert into binary
1153 # Convert into hex
1154 # Keep the last two octets
1155  
1156 0 $ip[$_] = substr( ip_bintoip( ip_iptobin( ip_expand_address($ip[$_], 4), 4), 6), -9);
1157  
1158 # Has an error occured here ?
1159 0 ▶ return unless (defined($ip[$_]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip[$_] 0 0 0
1160  
1161 # $num++ because we now have one more octet:
1162 # IPv4 address becomes two octets
1163 0 $num++;
1164 0 next;
1165 }
1166  
1167 # Add missing trailing 0s
1168 448192 $ip[$_] = ('0' x (4 - length($ip[$_]))) . $ip[$_];
1169 }
1170  
1171 # Now deal with '::' ('000!')
1172 56030 foreach (0 .. (scalar(@ip) - 1)) {
1173  
1174 # Find the pattern
1175 448180 ▶ next unless ($ip[$_] eq '000!');
Branch: 1 branch
BranchTrueFalseTotal
unless $ip[$_] eq "000!" 448168 12 448180
1176  
1177 # @empty is the IP address 0
1178 12 my @empty = map { $_ = '0' x 4 } (0 .. 7);
1179  
1180 # Replace :: with $num '0000' octets
1181 12 $ip[$_] = join ':', @empty[ 0 .. 8 - $num ];
1182 12 last;
1183 }
1184  
1185 56030 return (lc(join ':', @ip));
1186 }
1187  
1188 #------------------------------------------------------------------------------
1189 # Subroutine ip_get_mask
1190 # Purpose : Get IP mask from prefix length.
1191 # Params : Prefix length, IP version
1192 # Returns : Binary Mask
1193 36034 ▶sub ip_get_mask {
Subroutine ip_get_mask: called CC=2 SCAR=8.5
Pod documented
1194 36034 my ($len, $ip_version) = @_;
1195  
1196 36034 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 36034 36034
1197 0 $ERROR = "Cannot determine IP version";
1198 0 $ERRNO = 101;
1199 0 return;
1200 }
1201  
1202 36034 my $size = ip_iplengths($ip_version);
1203  
1204 # mask is $len 1s plus the rest as 0s
1205 36034 return (('1' x $len) . ('0' x ($size - $len)));
1206 }
1207  
1208 #------------------------------------------------------------------------------
1209 # Subroutine ip_last_address_bin
1210 # Purpose : Return the last binary address of a range
1211 # Params : First binary IP, prefix length, IP version
1212 # Returns : Binary IP
1213 17 ▶sub ip_last_address_bin {
Subroutine ip_last_address_bin: called CC=2 SCAR=8.1
Pod documented
1214 17 my ($binip, $len, $ip_version) = @_;
1215  
1216 17 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 17 17
1217 0 $ERROR = "Cannot determine IP version";
1218 0 $ERRNO = 101;
1219 0 return;
1220 }
1221  
1222 17 my $size = ip_iplengths($ip_version);
1223  
1224 # Find the part of the IP address which will not be modified
1225 17 $binip = substr($binip, 0, $len);
1226  
1227 # Fill with 1s the variable part
1228 17 return ($binip . ('1' x ($size - length($binip))));
1229 }
1230  
1231 #------------------------------------------------------------------------------
1232 # Subroutine ip_splitprefix
1233 # Purpose : Split a prefix into IP and prefix length
1234 # Comments : If it was passed a simple IP, it just returns it
1235 # Params : Prefix
1236 # Returns : IP, optionnaly length of prefix
1237 36034 ▶sub ip_splitprefix {
Subroutine ip_splitprefix: called CC=3 SCAR=11.3
Pod documented
1238 36034 my $prefix = shift;
1239  
1240 # Find the '/'
1241 36034 ▶ return unless ($prefix =~ m!^([^/]+?)(/\d+)?$!);
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix =~ m[^([^/]+?)(/\d+)?$] 0 36034 36034
1242  
1243 36034 my ($ip, $len) = ($1, $2);
1244  
1245 36034 ▶ defined($len) and $len =~ s!/!!;
Branch: 1 branch
BranchTrueFalseTotal
defined $len and $len =~ s[/][] 36034 0 36034
1246  
1247 36034 return ($ip, $len);
1248 }
1249  
1250 #------------------------------------------------------------------------------
1251 # Subroutine ip_prefix_to_range
1252 # Purpose : Get a range from a prefix
1253 # Params : IP, Prefix length, IP version
1254 # Returns : First IP, last IP
1255 0 ▶sub ip_prefix_to_range {
Subroutine ip_prefix_to_range: not called CC=6 SCAR=37.4
Pod documented
1256 0 my ($ip, $len, $ip_version) = @_;
1257  
1258 0 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 0 0
1259 0 $ERROR = "Cannot determine IP version";
1260 0 $ERRNO = 101;
1261 0 return;
1262 }
1263  
1264 # Expand the first IP address
1265 0 $ip = ip_expand_address($ip, $ip_version);
1266  
1267 # Turn into a binary
1268 # Get last address
1269 # Turn into an IP
1270 0 ▶ my $binip = ip_iptobin($ip, $ip_version) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $binip = ip_iptobin($ip, $ip_version) or return 0 0 0
1271  
1272 0 ▶ return unless (ip_check_prefix($binip, $len, $ip_version));
Branch: 1 branch
BranchTrueFalseTotal
unless ip_check_prefix($binip, $len, $ip_version) 0 0 0
1273  
1274 0 ▶ my $lastip = ip_last_address_bin($binip, $len, $ip_version) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lastip = ip_last_address_bin($binip, $len, $ip_version) or return 0 0 0
1275 0 ▶ return unless ($lastip = ip_bintoip($lastip, $ip_version));
Branch: 1 branch
BranchTrueFalseTotal
unless $lastip = ip_bintoip($lastip, $ip_version) 0 0 0
1276  
1277 0 return ($ip, $lastip);
1278 }
1279  
1280 #------------------------------------------------------------------------------
1281 # Subroutine ip_is_valid_mask
1282 # Purpose : Check the validity of an IP mask (11110000)
1283 # Params : Mask
1284 # Returns : 1 or undef (invalid)
1285 0 ▶sub ip_is_valid_mask {
Subroutine ip_is_valid_mask: not called CC=4 SCAR=30.0
Pod undocumented
1286 0 my ($mask, $ip_version) = @_;
1287  
1288 0 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 0 0
1289 0 $ERROR = "Cannot determine IP version for $mask";
1290 0 $ERRNO = 101;
1291 0 return;
1292 }
1293  
1294 0 my $len = ip_iplengths($ip_version);
1295  
1296 0 ▶ if (length($mask) != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mask != $len) 0 0 0
1297 0 $ERROR = "Invalid mask length for $mask";
1298 0 $ERRNO = 150;
1299 0 return;
1300 }
1301  
1302 # The mask should be of the form 111110000000
1303 0 ▶ unless ($mask =~ m/^1*0*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask =~ /^1*0*$/) 0 0 0
1304 0 $ERROR = "Invalid mask $mask";
1305 0 $ERRNO = 151;
1306 0 return;
1307 }
1308  
1309 0 return 1;
1310 }
1311  
1312 #------------------------------------------------------------------------------
1313 # Subroutine ip_bincomp
1314 # Purpose : Compare binary Ips with <, >, <=, >=
1315 # Comments : Operators are lt(<), le(<=), gt(>), and ge(>=)
1316 # Params : First binary IP, operator, Last binary Ip
1317 # Returns : 1 (yes), 0 (no), or undef (problem)
1318 185798 ▶sub ip_bincomp {
Subroutine ip_bincomp: called CC=9 SCAR=23.5
Pod documented
1319 185798 my ($begin, $op, $end) = @_;
1320  
1321 185798 my ($b, $e);
1322  
1323 185798 ▶ if ($op =~ /^l[te]$/) # Operator is lt or le
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /^l[te]$/) { } 185796 2 185798
elsif ($op =~ /^g[te]$/) { } 2 0 2
1324 {
1325 185796 ($b, $e) = ($end, $begin);
1326 }
1327 elsif ($op =~ /^g[te]$/) # Operator is gt or ge
1328 {
1329 2 ($b, $e) = ($begin, $end);
1330 }
1331 else {
1332 0 $ERROR = "Invalid Operator $op\n";
1333 0 $ERRNO = 131;
1334 0 return;
1335 }
1336  
1337 # le or ge -> return 1 if IPs are identical
1338 185798 ▶ return (1) if ($op =~ /e/ and ($begin eq $end));
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ /e/ and $begin eq $end 108081 77717 185798
Condition100%3/3
$op =~ /e/ and $begin eq $end
!ll&&!rl&&r
777710108081
MC/DC100%2/2
$op =~ /e/ and $begin eq $end
$op =~ /e/ $begin eq $end
Truth table100%3/3
A and B
A= $op =~ /e/B= $begin eq $end
ABresult
0X0
100
111
1339  
1340 # Check IP sizes
1341 77717 ▶ unless (length($b) eq length($e)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $b eq length $e) 0 77717 77717
1342 0 $ERROR = "IP addresses of different length\n";
1343 0 $ERRNO = 130;
1344 0 return;
1345 }
1346  
1347 77717 my $c;
1348  
1349 # Foreach bit
1350 77717 for (0 .. length($b) - 1) {
1351  
1352 # substract the two bits
1353 7958538 $c = substr($b, $_, 1) - substr($e, $_, 1);
1354  
1355 # Check the result
1356 7958538 ▶ return (1) if ($c == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $c == 1 73 7958465 7958538
1357 7958465 ▶ return (0) if ($c == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $c == -1 77644 7880821 7958465
1358 }
1359  
1360 # IPs are identical
1361 0 return 0;
1362 }
1363  
1364 #------------------------------------------------------------------------------
1365 # Subroutine ip_binadd
1366 # Purpose : Add two binary IPs
1367 # Params : First binary IP, Last binary Ip
1368 # Returns : Binary sum or undef (problem)
1369 36059 ▶sub ip_binadd {
Subroutine ip_binadd: called CC=4 SCAR=14.2
Pod documented
1370 36059 my ($b, $e) = @_;
1371  
1372 # Check IP length
1373 36059 ▶ unless (length($b) eq length($e)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $b eq length $e) 0 36059 36059
1374 0 $ERROR = "IP addresses of different length\n";
1375 0 $ERRNO = 130;
1376 0 return;
1377 }
1378  
1379 # Reverse the two IPs
1380 36059 $b = scalar(reverse $b);
1381 36059 $e = scalar(reverse $e);
1382  
1383 36059 my ($carry, $result, $c) = (0);
1384  
1385 # Foreach bit (reversed)
1386 36059 for (0 .. length($b) - 1) {
1387  
1388 # add the two bits plus the carry
1389 3844480 $c = substr($b, $_, 1) + substr($e, $_, 1) + $carry;
1390 3844480 $carry = 0;
1391  
1392 # sum = 0 => $c = 0, $carry = 0
1393 # sum = 1 => $c = 1, $carry = 0
1394 # sum = 2 => $c = 0, $carry = 1
1395 # sum = 3 => $c = 1, $carry = 1
1396 3844480 ▶ if ($c > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 1) 40124 3804356 3844480
1397 40124 $c -= 2;
1398 40124 $carry = 1;
1399 }
1400  
1401 3844480 $result .= $c;
1402 }
1403  
1404 # Reverse result
1405 36059 return scalar(reverse($result));
1406 }
1407  
1408 #------------------------------------------------------------------------------
1409 # Subroutine ip_get_prefix_length
1410 # Purpose : Get the prefix length for a given range of IPs
1411 # Params : First binary IP, Last binary IP
1412 # Returns : Length of prefix or undef (problem)
1413 36055 ▶sub ip_get_prefix_length {
Subroutine ip_get_prefix_length: called CC=4 SCAR=15.5
Pod documented
1414 36055 my ($bin1, $bin2) = @_;
1415  
1416 # Check length of IPs
1417 36055 ▶ unless (length($bin1) eq length($bin2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $bin1 eq length $bin2) 0 36055 36055
1418 0 $ERROR = "IP addresses of different length\n";
1419 0 $ERRNO = 130;
1420 0 return;
1421 }
1422  
1423 # reverse IPs
1424 36055 $bin1 = scalar(reverse $bin1);
1425 36055 $bin2 = scalar(reverse $bin2);
1426  
1427 # foreach bit
1428 36055 for (0 .. length($bin1) - 1) {
1429  
1430 # If bits are equal it means we have reached the longest prefix
1431 36577 ▶ return ("$_") if (substr($bin1, $_, 1) eq substr($bin2, $_, 1));
Branch: 1 branch
BranchTrueFalseTotal
if substr($bin1, $_, 1) eq substr($bin2, $_, 1) 36055 522 36577
1432  
1433 }
1434  
1435 # Return 32 (IPv4) or 128 (IPv6)
1436 0 return length($bin1);
1437 }
1438  
1439 #------------------------------------------------------------------------------
1440 # Subroutine ip_range_to_prefix
1441 # Purpose : Return all prefixes between two IPs
1442 # Params : First IP, Last IP, IP version
1443 # Returns : List of Prefixes or undef (problem)
1444 36041 ▶sub ip_range_to_prefix {
Subroutine ip_range_to_prefix: called CC=7 SCAR=20.1
Pod documented
1445 36041 my ($binip, $endbinip, $ip_version) = @_;
1446  
1447 36041 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 36041 36041
1448 0 $ERROR = "Cannot determine IP version";
1449 0 $ERRNO = 101;
1450 0 return;
1451 }
1452  
1453 36041 ▶ unless (length($binip) eq length($endbinip)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $binip eq length $endbinip) 0 36041 36041
1454 0 $ERROR = "IP addresses of different length\n";
1455 0 $ERRNO = 130;
1456 0 return;
1457 }
1458  
1459 36041 my ($len, $nbits, $current, $add, @prefix);
1460  
1461 # 1 in binary
1462 36041 my $one = ('0' x (ip_iplengths($ip_version) - 1)) . '1';
1463  
1464 # While we have not reached the last IP
1465 36041 ▶ while (ip_bincomp($binip, 'le', $endbinip) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ip_bincomp($binip, "le", $endbinip) == 1) 36055 36038 72093
1466  
1467 # Find all 0s at the end
1468 36055 ▶ if ($binip =~ m/(0+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binip =~ /(0+)$/) { } 18244 17811 36055
1469  
1470 # nbits = nb of 0 bits
1471 18244 $nbits = length($1);
1472 }
1473 else {
1474 17811 $nbits = 0;
1475 }
1476  
1477 36055 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
ip_bincomp($current, "le", $endbinip) != 1 and do { do { $current = $binip; $add = 1 x $nbits; $current =~ s/0{$nbits}$/$add/; --$nbits } } 41603 36055 77658
1478 77658 $current = $binip;
1479 77658 $add = '1' x $nbits;
1480  
1481 # Replace $nbits 0s with 1s
1482 77658 $current =~ s/0{$nbits}$/$add/;
1483 77658 $nbits--;
1484  
1485 # Decrease $nbits if $current >= $endbinip
1486 } while (ip_bincomp($current, 'le', $endbinip) != 1);
1487  
1488 # Find Prefix length
1489 36055 $len =
1490 (ip_iplengths($ip_version)) - ip_get_prefix_length($binip, $current);
1491  
1492 # Push prefix in list
1493 36055 push(@prefix, ip_bintoip($binip, $ip_version) . "/$len");
1494  
1495 # Add 1 to current IP
1496 36055 $binip = ip_binadd($current, $one);
1497  
1498 # Exit if IP is 32/128 1s
1499 36055 ▶ last if ($current =~ m/^1+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $current =~ /^1+$/ 3 36052 36055
1500 }
1501  
1502 36041 return (@prefix);
1503 }
1504  
1505 #------------------------------------------------------------------------------
1506 # Subroutine ip_compress_v4_prefix
1507 # Purpose : Compress an IPv4 Prefix
1508 # Params : IP, Prefix length
1509 # Returns : Compressed IP - ie: 194.5
1510 8003 ▶sub ip_compress_v4_prefix {
Subroutine ip_compress_v4_prefix: called CC=2 SCAR=7.0
Pod documented
1511 8003 my ($ip, $len) = @_;
1512  
1513 8003 my @quads = split /\./, $ip;
1514  
1515 8003 my $qlen = int(($len - 1) / 8);
1516  
1517 8003 ▶ $qlen = 0 if ($qlen < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $qlen < 0 0 8003 8003
1518  
1519 8003 my $newip = join '.', @quads[ 0 .. $qlen ];
1520  
1521 8003 return ($newip);
1522 }
1523  
1524 #------------------------------------------------------------------------------
1525 # Subroutine ip_compress_address
1526 # Purpose : Compress an IPv6 address
1527 # Params : IP, IP version
1528 # Returns : Compressed IP or undef (problem)
1529 28009 ▶sub ip_compress_address {
Subroutine ip_compress_address: called CC=6 SCAR=18.5
Pod documented
1530 28009 my ($ip, $ip_version) = @_;
1531  
1532 28009 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 28009 28009
1533 0 $ERROR = "Cannot determine IP version for $ip";
1534 0 $ERRNO = 101;
1535 0 return;
1536 }
1537  
1538 # Just return if IP is IPv4
1539 28009 ▶ return ($ip) if ($ip_version == 4);
Branch: 1 branch
BranchTrueFalseTotal
if $ip_version == 4 0 28009 28009
1540  
1541 # already compressed addresses must be expanded first
1542 28009 $ip = ip_expand_address( $ip, $ip_version);
1543
1544 # Remove leading 0s: 0034 -> 34; 0000 -> 0
1545 28009 $ip =~ s/
1546 (^|:) # Find beginning or ':' -> $1
1547 0+ # 1 or several 0s
1548 (?= # Look-ahead
1549 [a-fA-F\d]+ # One or several Hexs
1550 (?::|$)) # ':' or end
1551 /$1/gx;
1552  
1553 28009 my $reg = '';
1554  
1555 # Find the longuest :0:0: sequence
1556 28009 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ / ((?:^|:) # Find beginning or ':' -> $1 0(?::0)+ # 0 followed by 1 or several ':0' (?::|$)) # ':' or end /gx) 2037 28009 30046
1557 $ip =~ m/
1558 ((?:^|:) # Find beginning or ':' -> $1
1559 0(?::0)+ # 0 followed by 1 or several ':0'
1560 (?::|$)) # ':' or end
1561 /gx
1562 )
1563 {
1564 2037 ▶ $reg = $1 if (length($reg) < length($1));
Branch: 1 branch
BranchTrueFalseTotal
if length $reg < length $1 2036 1 2037
1565 }
1566  
1567 # Replace sequence by '::'
1568 28009 ▶ $ip =~ s/$reg/::/ if ($reg ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $reg ne "" 2035 25974 28009
1569  
1570 28009 return $ip;
1571 }
1572  
1573 #------------------------------------------------------------------------------
1574 # Subroutine ip_is_overlap
1575 # Purpose : Check if two ranges overlap
1576 # Params : Four binary IPs (begin of range 1,end1,begin2,end2)
1577 # Returns : $NO_OVERLAP (no overlap)
1578 # $IP_PARTIAL_OVERLAP (overlap)
1579 # $IP_A_IN_B_OVERLAP (range1 is included in range2)
1580 # $IP_B_IN_A_OVERLAP (range2 is included in range1)
1581 # $IP_IDENTICAL (range1 == range2)
1582 # or undef (problem)
1583  
1584 2 ▶sub ip_is_overlap {
Subroutine ip_is_overlap: called CC=16 SCAR=41.8
Pod documented
1585 2 my ($b1, $e1, $b2, $e2) = (@_);
1586  
1587 2 my $swap;
1588 2 $swap = 0;
1589  
1590 2 ▶ unless ((length($b1) eq length($e1))
Branch: 1 branch
BranchTrueFalseTotal
unless (length $b1 eq length $e1 and length $b2 eq length $e2 and length $b1 eq length $b2) 0 2 2
Condition33%2/6
length $b1 eq length $e1 and length $b2 eq length $e2 and length $b1 eq length $b2
!ll&&!rl&&r
002
length $b1 eq length $e1 and length $b2 eq length $e2
!ll&&!rl&&r
002
MC/DC0%0/3
length $b1 eq length $e1 and length $b2 eq length $e2 and length $b1 eq length $b2
length $b1 eq length $e1 length $b2 eq length $e2 length $b1 eq length $b2
Truth table25%1/4
A and B and C
A= length $b1 eq length $e1B= length $b2 eq length $e2C= length $b1 eq length $b2
ABCresult
0XX0
10X0
1100
1111
1591 and (length($b2) eq length($e2))
1592 and (length($b1) eq length($b2)))
1593 {
1594 0 $ERROR = "IP addresses of different length\n";
1595 0 $ERRNO = 130;
1596 0 return;
1597 }
1598  
1599 # begin1 <= end1 ?
1600 2 ▶ unless (ip_bincomp($b1, 'le', $e1) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ip_bincomp($b1, "le", $e1) == 1) 0 2 2
1601 0 $ERROR = "Invalid range $b1 - $e1";
1602 0 $ERRNO = 140;
1603 0 return;
1604 }
1605  
1606 # begin2 <= end2 ?
1607 2 ▶ unless (ip_bincomp($b2, 'le', $e2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ip_bincomp($b2, "le", $e2) == 1) 0 2 2
1608 0 $ERROR = "Invalid range $b2 - $e2";
1609 0 $ERRNO = 140;
1610 0 return;
1611 }
1612  
1613 # b1 == b2 ?
1614 2 ▶ if ($b1 eq $b2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b1 eq $b2) 0 2 2
1615  
1616 # e1 == e2
1617 0 ▶ return ($IP_IDENTICAL) if ($e1 eq $e2);
Branch: 1 branch
BranchTrueFalseTotal
if $e1 eq $e2 0 0 0
1618  
1619 # e1 < e2 ?
1620 return (
1621 0 ▶ ip_bincomp($e1, 'lt', $e2)
Branch: 1 branch
BranchTrueFalseTotal
ip_bincomp($e1, 'lt', $e2) ? : 0 0 0
1622 ? $IP_A_IN_B_OVERLAP
1623 : $IP_B_IN_A_OVERLAP
1624 );
1625 }
1626  
1627 # e1 == e2 ?
1628 2 ▶ if ($e1 eq $e2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e1 eq $e2) 0 2 2
1629  
1630 # b1 < b2
1631 return (
1632 0 ▶ ip_bincomp($b1, 'lt', $b2)
Branch: 1 branch
BranchTrueFalseTotal
ip_bincomp($b1, 'lt', $b2) ? : 0 0 0
1633 ? $IP_B_IN_A_OVERLAP
1634 : $IP_A_IN_B_OVERLAP
1635 );
1636 }
1637  
1638 # b1 < b2
1639 2 ▶ if ((ip_bincomp($b1, 'lt', $b2) == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ip_bincomp($b1, 'lt', $b2) == 1) { } 1 1 2
1640  
1641 # e1 < b2
1642 1 ▶ return ($IP_NO_OVERLAP) if (ip_bincomp($e1, 'lt', $b2) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if ip_bincomp($e1, "lt", $b2) == 1 0 1 1
1643  
1644 # e1 < e2 ?
1645 return (
1646 1 ▶ ip_bincomp($e1, 'lt', $e2)
Branch: 1 branch
BranchTrueFalseTotal
ip_bincomp($e1, 'lt', $e2) ? : 0 1 1
1647 ? $IP_PARTIAL_OVERLAP
1648 : $IP_B_IN_A_OVERLAP
1649 );
1650 }
1651 else # b1 > b2
1652 {
1653  
1654 # e2 < b1
1655 1 ▶ return ($IP_NO_OVERLAP) if (ip_bincomp($e2, 'lt', $b1) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if ip_bincomp($e2, "lt", $b1) == 1 1 0 1
1656  
1657 # e2 < e1 ?
1658 return (
1659 0 ▶ ip_bincomp($e2, 'lt', $e1)
Branch: 1 branch
BranchTrueFalseTotal
ip_bincomp($e2, 'lt', $e1) ? : 0 0 0
1660 ? $IP_PARTIAL_OVERLAP
1661 : $IP_A_IN_B_OVERLAP
1662 );
1663 }
1664 }
1665  
1666 #------------------------------------------------------------------------------
1667 # Subroutine get_embedded_ipv4
1668 # Purpose : Get an IPv4 embedded in an IPv6 address
1669 # Params : IPv6
1670 # Returns : IPv4 or undef (not found)
1671 0 ▶sub ip_get_embedded_ipv4 {
Subroutine ip_get_embedded_ipv4: not called CC=3 SCAR=24.8
Pod documented
1672 0 my $ipv6 = shift;
1673  
1674 0 my @ip = split /:/, $ipv6;
1675  
1676 # Bugfix by Norbert Koch
1677 0 ▶ return unless (@ip);
Branch: 1 branch
BranchTrueFalseTotal
unless @ip 0 0 0
1678  
1679 # last octet should be ipv4
1680 0 ▶ return ($ip[-1]) if (ip_is_ipv4($ip[-1]));
Branch: 1 branch
BranchTrueFalseTotal
if ip_is_ipv4($ip[-1]) 0 0 0
1681  
1682 0 return;
1683 }
1684  
1685 #------------------------------------------------------------------------------
1686 # Subroutine aggregate
1687 # Purpose : Aggregate 2 ranges
1688 # Params : 1st range (1st IP, Last IP), last range (1st IP, last IP),
1689 # IP version
1690 # Returns : prefix or undef (invalid)
1691 2 ▶sub ip_aggregate {
Subroutine ip_aggregate: called CC=5 SCAR=21.4
Pod documented
1692 2 my ($binbip1, $bineip1, $binbip2, $bineip2, $ip_version) = @_;
1693  
1694 2 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 2 2
1695 0 $ERROR = "Cannot determine IP version for $binbip1";
1696 0 $ERRNO = 101;
1697 0 return;
1698 }
1699  
1700 # Bin 1
1701 2 my $one = (('0' x (ip_iplengths($ip_version) - 1)) . '1');
1702  
1703 # $eip1 + 1 = $bip2 ?
1704 2 ▶ unless (ip_binadd($bineip1, $one) eq $binbip2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ip_binadd($bineip1, $one) eq $binbip2) 0 2 2
1705 0 $ERROR = "Ranges not contiguous - $bineip1 - $binbip2";
1706 0 $ERRNO = 160;
1707 0 return;
1708 }
1709  
1710 # Get ranges
1711 2 my @prefix = ip_range_to_prefix($binbip1, $bineip2, $ip_version);
1712  
1713 # There should be only one range
1714 2 ▶ return if scalar(@prefix) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @prefix < 1 0 2 2
1715  
1716 2 ▶ if (scalar(@prefix) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @prefix > 1) 0 2 2
1717 0 $ERROR = "$binbip1 - $bineip2 is not a single prefix";
1718 0 $ERRNO = 161;
1719 0 return;
1720 }
1721 2 return ($prefix[0]);
1722  
1723 }
1724  
1725 #------------------------------------------------------------------------------
1726 # Subroutine ip_iptype
1727 # Purpose : Return the type of an IP (Public, Private, Reserved)
1728 # Params : IP to test, IP version
1729 # Returns : type or undef (invalid)
1730 36010 ▶sub ip_iptype {
Subroutine ip_iptype: called CC=3 SCAR=12.7
Pod documented
1731 36010 my ($ip, $ip_version) = @_;
1732  
1733 # handle known ip versions
1734 36010 ▶ return ip_iptypev4($ip) if $ip_version == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_version == 4 8003 28007 36010
1735 28007 ▶ return ip_iptypev6($ip) if $ip_version == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_version == 6 28007 0 28007
1736  
1737 # unsupported ip version
1738 0 $ERROR = "IP version $ip not supported";
1739 0 $ERRNO = 180;
1740 0 return;
1741 }
1742  
1743 #------------------------------------------------------------------------------
1744 # Subroutine ip_iptypev4
1745 # Purpose : Return the type of an IP (Public, Private, Reserved)
1746 # Params : IP to test, IP version
1747 # Returns : type or undef (invalid)
1748 8003 ▶sub ip_iptypev4 {
Subroutine ip_iptypev4: called CC=4 SCAR=15.0
Pod documented
1749 8003 my ($ip) = @_;
1750  
1751 # check ip
1752 8003 ▶ if ($ip !~ m/^[01]{1,32}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /^[01]{1,32}$/) 0 8003 8003
1753 0 $ERROR = "$ip is not a binary IPv4 address $ip";
1754 0 $ERRNO = 180;
1755 0 return;
1756 }
1757
1758 # see if IP is listed
1759 8003 foreach (sort { length($b) <=> length($a) } keys %IPv4ranges) {
1760 114932 ▶ return ($IPv4ranges{$_}) if ($ip =~ m/^$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /^$_/ 4102 110830 114932
1761 }
1762  
1763 # not listed means IP is public
1764 3901 return 'PUBLIC';
1765 }
1766  
1767 #------------------------------------------------------------------------------
1768 # Subroutine ip_iptypev6
1769 # Purpose : Return the type of an IP (Public, Private, Reserved)
1770 # Params : IP to test, IP version
1771 # Returns : type or undef (invalid)
1772 28007 ▶sub ip_iptypev6 {
Subroutine ip_iptypev6: called CC=4 SCAR=17.3
Pod documented
1773 28007 my ($ip) = @_;
1774  
1775 # check ip
1776 28007 ▶ if ($ip !~ m/^[01]{1,128}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /^[01]{1,128}$/) 0 28007 28007
1777 0 $ERROR = "$ip is not a binary IPv6 address";
1778 0 $ERRNO = 180;
1779 0 return;
1780 }
1781
1782 28007 foreach (sort { length($b) <=> length($a) } keys %IPv6ranges) {
1783 451035 ▶ return ($IPv6ranges{$_}) if ($ip =~ m/^$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /^$_/ 28007 423028 451035
1784 }
1785  
1786 # How did we get here? All IPv6 addresses should match
1787 0 $ERROR = "Cannot determine type for $ip";
1788 0 $ERRNO = 180;
1789 0 return;
1790 }
1791  
1792 #------------------------------------------------------------------------------
1793 # Subroutine ip_check_prefix
1794 # Purpose : Check the validity of a prefix
1795 # Params : binary IP, length of prefix, IP version
1796 # Returns : 1 or undef (invalid)
1797 36052 ▶sub ip_check_prefix {
Subroutine ip_check_prefix: called CC=4 SCAR=15.9
Pod documented
1798 36052 my ($binip, $len, $ipversion) = (@_);
1799  
1800 # Check if len is longer than IP
1801 36052 ▶ if ($len > length($binip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $binip) 0 36052 36052
1802 0 $ERROR =
1803 "Prefix length $len is longer than IP address ("
1804 . length($binip) . ")";
1805 0 $ERRNO = 170;
1806 0 return;
1807 }
1808  
1809 36052 my $rest = substr($binip, $len);
1810  
1811 # Check if last part of the IP (len part) has only 0s
1812 36052 ▶ unless ($rest =~ /^0*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rest =~ /^0*$/) 1 36051 36052
1813 1 $ERROR = "Invalid prefix $binip/$len";
1814 1 $ERRNO = 171;
1815 1 return;
1816 }
1817  
1818 # Check if prefix length is correct
1819 36051 ▶ unless (length($rest) + $len == ip_iplengths($ipversion)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($rest) + $len == ip_iplengths($ipversion)) 0 36051 36051
1820 0 $ERROR = "Invalid prefix length /$len";
1821 0 $ERRNO = 172;
1822 0 return;
1823 }
1824  
1825 36051 return 1;
1826 }
1827  
1828 #------------------------------------------------------------------------------
1829 # Subroutine ip_reverse
1830 # Purpose : Get a reverse name from a prefix
1831 # Comments : From Lee's iplib.pm
1832 # Params : IP, length of prefix, IP version
1833 # Returns : Reverse name or undef (error)
1834 4 ▶sub ip_reverse {
Subroutine ip_reverse: called CC=9 SCAR=23.3
Pod documented
1835 4 my ($ip, $len, $ip_version) = (@_);
1836  
1837 4 ▶ $ip_version ||= ip_get_version($ip);
Condition33%1/3
$ip_version ||= ip_get_version($ip)
l!l&&r!l&&!r
400
MC/DC0%0/2
$ip_version ||= ip_get_version($ip)
$ip_version ip_get_version($ip)
Truth table33%1/3
A ||= B
A= $ip_versionB= ip_get_version($ip)
ABresult
1X1
011
000
1838 4 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 4 4
1839 0 $ERROR = "Cannot determine IP version for $ip";
1840 0 $ERRNO = 101;
1841 0 return;
1842 }
1843  
1844 4 ▶ if ($ip_version == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ip_version == 4) { } 1 3 4
elsif ($ip_version == 6) { } 3 0 3
1845 1 my @quads = split /\./, $ip;
1846 1 my $no_quads = ($len / 8);
1847  
1848 1 my @reverse_quads = reverse @quads;
1849  
1850 1 ▶ while (@reverse_quads and $reverse_quads[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reverse_quads and $reverse_quads[0] == 0) 1 1 2
Condition66%2/3
@reverse_quads and $reverse_quads[0] == 0
!ll&&!rl&&r
011
MC/DC50%1/2
@reverse_quads and $reverse_quads[0] == 0
@reverse_quads $reverse_quads[0] == 0
Truth table66%2/3
A and B
A= @reverse_quadsB= $reverse_quads[0] == 0
ABresult
0X0
100
111
1851 1 shift(@reverse_quads);
1852 }
1853  
1854 1 return join '.', @reverse_quads, 'in-addr', 'arpa.';
1855 }
1856 elsif ($ip_version == 6) {
1857 3 my @rev_groups = reverse split /:/, ip_expand_address($ip, 6);
1858 3 my @result;
1859  
1860 3 foreach (@rev_groups) {
1861 24 my @revhex = reverse split //;
1862 24 push @result, @revhex;
1863 }
1864  
1865 # This takes the zone above if it's not exactly on a nibble
1866 3 ▶ my $first_nibble_index = $len ? 32 - (int($len / 4)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 3 0 3
1867 3 return join '.', @result[ $first_nibble_index .. $#result ], 'ip6',
1868 'arpa.';
1869 }
1870 }
1871  
1872 #------------------------------------------------------------------------------
1873 # Subroutine ip_normalize
1874 # Purpose : Normalize data to a range of IP addresses
1875 # Params : IP or prefix or range
1876 # Returns : ip1, ip2 (if range) or undef (error)
1877 36039 ▶sub ip_normalize {
Subroutine ip_normalize: called CC=24 SCAR=41.8
Pod documented
1878 36039 my ($data) = shift;
1879  
1880 36039 my $ipversion;
1881  
1882 36039 my ($len, $ip, $ip2, $real_len, $first, $last, $curr_bin, $addcst, $clen);
1883  
1884 # Prefix
1885 36039 ▶ if ($data =~ m!^(\S+?)(/\S+)$!) {
Branch: 3 branches
BranchTrueFalseTotal
if ($data =~ m[^(\S+?)(/\S+)$]) { } 19 36020 36039
elsif ($data =~ /^(.+?)\s*\-\s*(.+)$/) { } 4 36016 36020
elsif ($data =~ /^(.+?)\s+\+\s+(.+)$/) { } 0 36016 36016
1886 19 ($ip, $len) = ($1, $2);
1887  
1888 19 ▶ return unless ($ipversion = ip_get_version($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless $ipversion = ip_get_version($ip) 1 18 19
1889 18 ▶ return unless ($ip = ip_expand_address($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = ip_expand_address($ip, $ipversion) 0 18 18
1890 18 ▶ return unless ($curr_bin = ip_iptobin($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $curr_bin = ip_iptobin($ip, $ipversion) 0 18 18
1891  
1892 18 my $one = '0' x (ip_iplengths($ipversion) - 1) . '1';
1893  
1894 18 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 18 17 35
1895 18 ▶ last unless ($len =~ s!^/(\d+)(\,|$)!!);
Branch: 1 branch
BranchTrueFalseTotal
unless $len =~ s[^/(\d+)(\,|$)][] 0 18 18
1896  
1897 18 $clen = $1;
1898 18 $addcst = length($2) > 0;
1899  
1900 18 ▶ return unless (ip_check_prefix($curr_bin, $clen, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless ip_check_prefix($curr_bin, $clen, $ipversion) 1 17 18
1901  
1902 return
1903 17 ▶ unless ($curr_bin =
Branch: 1 branch
BranchTrueFalseTotal
unless $curr_bin = ip_last_address_bin($curr_bin, $clen, $ipversion) 0 17 17
1904 ip_last_address_bin($curr_bin, $clen, $ipversion));
1905  
1906 17 ▶ if ($addcst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addcst) 0 17 17
1907 0 ▶ return unless ($curr_bin = ip_binadd($curr_bin, $one));
Branch: 1 branch
BranchTrueFalseTotal
unless $curr_bin = ip_binadd($curr_bin, $one) 0 0 0
1908 }
1909 }
1910  
1911 17 return ($ip, ip_bintoip($curr_bin, $ipversion));
1912 }
1913  
1914 # Range
1915 elsif ($data =~ /^(.+?)\s*\-\s*(.+)$/) {
1916 4 ($ip, $ip2) = ($1, $2);
1917  
1918 4 ▶ return unless ($ipversion = ip_get_version($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless $ipversion = ip_get_version($ip) 0 4 4
1919  
1920 4 ▶ return unless ($ip = ip_expand_address($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = ip_expand_address($ip, $ipversion) 0 4 4
1921 4 ▶ return unless ($ip2 = ip_expand_address($ip2, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip2 = ip_expand_address($ip2, $ipversion) 1 3 4
1922  
1923 3 return ($ip, $ip2);
1924 }
1925  
1926 # IP + Number
1927 elsif ($data =~ /^(.+?)\s+\+\s+(.+)$/) {
1928 0 ($ip, $len) = ($1, $2);
1929  
1930 0 ▶ return unless ($ipversion = ip_get_version($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless $ipversion = ip_get_version($ip) 0 0 0
1931 0 ▶ return unless ($ip = ip_expand_address($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = ip_expand_address($ip, $ipversion) 0 0 0
1932  
1933 0 my ($bin_ip);
1934 0 ▶ return unless ($bin_ip = ip_iptobin($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $bin_ip = ip_iptobin($ip, $ipversion) 0 0 0
1935  
1936 0 ▶ return unless ($len = ip_inttobin($len, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $len = ip_inttobin($len, $ipversion) 0 0 0
1937  
1938 0 ▶ return unless ($ip2 = ip_binadd($bin_ip, $len));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip2 = ip_binadd($bin_ip, $len) 0 0 0
1939 0 ▶ return unless ($ip2 = ip_bintoip($ip2, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip2 = ip_bintoip($ip2, $ipversion) 0 0 0
1940  
1941 0 return ($ip, $ip2);
1942 }
1943  
1944 # Single IP
1945 else {
1946 36016 $ip = $data;
1947  
1948 36016 ▶ return unless ($ipversion = ip_get_version($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless $ipversion = ip_get_version($ip) 0 36016 36016
1949  
1950 36016 ▶ return unless ($ip = ip_expand_address($ip, $ipversion));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = ip_expand_address($ip, $ipversion) 0 36016 36016
1951  
1952 36016 return $ip;
1953 }
1954 }
1955  
1956 #------------------------------------------------------------------------------
1957 # Subroutine normal_range
1958 # Purpose : Return the normalized format of a range
1959 # Params : IP or prefix or range
1960 # Returns : "ip1 - ip2" or undef (error)
1961 0 ▶sub ip_normal_range {
Subroutine ip_normal_range: not called CC=3 SCAR=24.8
Pod undocumented
1962 0 my ($data) = shift;
1963  
1964 0 my ($ip1, $ip2) = ip_normalize($data);
1965  
1966 0 ▶ return unless ($ip1);
Branch: 1 branch
BranchTrueFalseTotal
unless $ip1 0 0 0
1967  
1968 0 ▶ $ip2 ||= $ip1;
Condition0%0/3
$ip2 ||= $ip1
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip2 ||= $ip1
$ip2 $ip1
Truth table0%0/3
A ||= B
A= $ip2B= $ip1
ABresult
1X1
011
000
1969  
1970 0 return ("$ip1 - $ip2");
1971 }
1972  
1973 #------------------------------------------------------------------------------
1974 # Subroutine ip_auth
1975 # Purpose : Get Authority information from IP::Authority Module
1976 # Comments : Requires IP::Authority
1977 # Params : IP, length of prefix
1978 # Returns : Reverse name or undef (error)
1979 0 ▶sub ip_auth {
Subroutine ip_auth: not called CC=3 SCAR=24.8
Pod documented
1980 0 my ($ip, $ip_version) = (@_);
1981  
1982 0 ▶ unless ($ip_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip_version) 0 0 0
1983 0 $ERROR = "Cannot determine IP version for $ip";
1984 0 $ERRNO = 101;
1985 0 die;
1986 0 return;
1987 }
1988  
1989 0 ▶ if ($ip_version != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_version != 4) 0 0 0
1990  
1991 0 $ERROR = "Cannot get auth information: Not an IPv4 address";
1992 0 $ERRNO = 308;
1993 0 die;
1994 0 return;
1995 }
1996  
1997 0 require IP::Authority;
1998  
1999 0 my $reg = new IP::Authority;
2000  
2001 0 return ($reg->inet_atoauth($ip));
2002 }
2003  
2004 2 1;
2005  
2006 __END__
↑ Summary