blib/lib/IPv6/Address.pm

filter: Statementstmt 93.0% 226 / 243 Branchbran 71.4% 80 / 112 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.3% 62 / 65 Podpod 93.5% 29 / 31 totaltotal 87.4% 403 / 461 CC 61 SCAR 15.0
CC
61
Cov
86%
increment
13.5
addr_string
10.0
new
8.1
CRAP 4.5
↑ Summary Build »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IPv6::Address;
5 1 $IPv6::Address::VERSION = '0.207';
6  
7 =head1 NAME
8  
9 IPv6::Address - IPv6 Address Manipulation Library
10  
11 =head1 VERSION
12  
13 version 0.207
14  
15 =for html
16 <a href="https://travis-ci.org/aduitsis/IPv6-Address"><img src="https://travis-ci.org/aduitsis/IPv6-Address.svg?branch=master"></a>
17 <a href='https://coveralls.io/r/aduitsis/IPv6-Address?branch=master'><img src='https://coveralls.io/repos/aduitsis/IPv6-Address/badge.svg?branch=master' alt='Coverage Status' /></a>
18  
19  
20 =head1 SYNOPSIS
21  
22 use IPv6::Address;
23  
24 my $ipv6 = IPv6::Address->new('2001:648:2000::/48');
25  
26 $ipv6->contains('2001:648:2000::/64'); #true
27  
28 say $ipv6->to_string;
29 say $ipv6->string; # Same as previous
30 say $ipv6; # Same as previous
31  
32 say $ipv6->string(nocompress=>1); # do not compress using the :: notation
33 say $ipv6->string(ipv4=>1); #print the last 32 bits as an IPv4 address
34
35 $ipv6->addr_string; # Returns '2001:648:2000::'
36
37 $ipv6->split(4); # Split the prefix into 2^4 smaller prefixes. Returns a list.
38  
39 $ipv6->apply_mask; # Apply the mask to the address. All bits beyond the mask length become 0.
40  
41 $ipv6->first_address;
42  
43 $ipv6->last_address;
44  
45 $a->enumerate_with_offset( 5 , 64 ); #returns 2001:648:2000:4::/64
46  
47 =head1 DESCRIPTION
48  
49 A pure Perl IPv6 address manipulation library. Emphasis on manipulation of
50 prefixes and addresses. Very easy to understand and modify. The internal
51 representation of an IPv6::Address is a blessed hash with two keys, a prefix
52 length (0-128 obviously) and a 128-bit string. A multitude of methods to do
53 various tasks is provided.
54  
55  
56 =head2 Methods
57  
58 =over 12
59  
60 =cut
61  
62 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 use overload
69 1 '""' => \&to_string,
70 '<=>' => \&n_cmp,
71 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
72
73 1 my $DEBUG = 0;
74  
75 sub debug {
76 107 ▶ $DEBUG&&print STDERR $_[0];
Subroutine debug: called CC=3 SCAR=12.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR $_[0] 0 107 107
77 107 ▶ $DEBUG&&print STDERR "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR "\n" 0 107 107
78
79 }
80  
81 =item C<new( ipv6_string )>
82  
83 Takes a string representation of an IPv6 address and creates a corresponding
84 IPv6::Address object.
85  
86 =cut
87  
88 #takes a normal address as argument. Example 2001:648:2000::/48
89 sub new {
90 41 ▶ my $class = shift(@_) or croak "incorrect call to new";
Subroutine new: called CC=8 SCAR=20.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift @_ or croak("incorrect call to new") 0 41 41
91 41 ▶ my $ipv6_string = shift(@_) or croak "Cannot use an empty string as argument";
Branch: 1 branch
BranchTrueFalseTotal
my $ipv6_string = shift @_ or croak("Cannot use an empty string as argument") 0 41 41
92 41 my ($ipv6,$prefixlen) = ( $ipv6_string =~ /([0-9A-Fa-f:]+)\/(\d+)/ );
93 41 ▶ croak "IPv6 address part not parsable" if (!defined($ipv6));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipv6 0 41 41
94 41 ▶ croak "IPv6 prefix length part not parsable" if (!defined($prefixlen));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefixlen 0 41 41
95 41 debug("ipv6 is $ipv6, length is $prefixlen");
96 41 my @arr;
97 41 my @_parts = ( $ipv6 =~ /([0-9A-Fa-f]+)/g );
98 41 my $nparts = scalar @_parts;
99 41 ▶ if ($nparts != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nparts != 8) 33 8 41
100 33 ▶ for(my $i=1;$i<=(8-$nparts);$i++) { push @arr,hex "0000" };
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 8 - $nparts) 194 33 227
101 }
102  
103 41 ▶ my @parts = map { ($_ eq '::')? @arr : hex $_ } ( $ipv6 =~ /((?:[0-9A-Fa-f]+)|(?:::))/g );
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '::' ? : 33 134 167
104
105 41 debug(join(":",map { sprintf "%04x",$_ } @parts));
106  
107 41 my $bitstr = pack 'n8',@parts;
108
109 41 return bless {
110 bitstr => $bitstr,
111 prefixlen => $prefixlen,
112 },$class;
113 }
114  
115 =item C<raw_new( bitstr, length )>
116  
117 Creates a new IPv6::Address out of a bitstring and a prefix length. The
118 bitstring must be binary, please do not use a '0' or '1' character string.
119  
120 =cut
121  
122 #takes a bitstr (0101010101111010010....) and a prefix length as arguments
123 sub raw_new {
124 34 ▶ my $class = $_[0];
Subroutine raw_new: called CC=1 SCAR=0.0
Pod documented
125 34 return bless {
126 bitstr => $_[1],
127 prefixlen => $_[2],
128 },$class;
129 }
130  
131 =item C<get_bitstr>
132  
133 Returns the bitstr of the object.
134  
135 =cut
136  
137 #returns the bitstr (11010111011001....)
138 sub get_bitstr {
139 215 ▶ return $_[0]->{bitstr};
Subroutine get_bitstr: called CC=1 SCAR=0.0
Pod documented
140 }
141  
142  
143 =item C<get_prefixlen>
144  
145 Returns the prefix length of the address.
146  
147 =cut
148  
149 #returns the length of the IPv6 address prefix
150 sub get_prefixlen {
151 215 ▶ return $_[0]->{prefixlen};
Subroutine get_prefixlen: called CC=1 SCAR=0.0
Pod documented
152 }
153  
154 =item C<get_mask_bitstr(length)>
155  
156 Returns a 128-bit string with the first prefix-length bits equal
157 to 1, rest equal to 0. Essentially takes the prefix length of the object and
158 returns a corresponding bit mask.
159  
160 =cut
161  
162 #returns a 1111100000 corresponding to the prefix length
163 sub get_mask_bitstr {
164 3 ▶ generate_bitstr( $_[0]->get_prefixlen )
Subroutine get_mask_bitstr: called CC=1 SCAR=0.0
Pod documented
165 }
166  
167 =item C<get_masked_address_bitstr>
168  
169 Returns the bitstring, after zeroing out all the bits after the prefix length.
170 Essentially applies the prefix mask to the address.
171  
172 =cut
173 sub get_masked_address_bitstr {
174 8 ▶ generate_bitstr( $_[0]->get_prefixlen ) & $_[0]->get_bitstr;
Subroutine get_masked_address_bitstr: called CC=1 SCAR=0.0
Pod documented
175 }
176  
177 =item C<generate_bitstr( number )>
178  
179 Not a method, returns 128-bit string, first n-items are 1, rest is 0.
180  
181 =cut
182  
183 sub generate_bitstr {
184 #TODO trick bellow is stupid ... fix
185 19 ▶ pack 'B128',join('',( ( map { '1' } ( 1 .. $_[0] ) ) , ( map { '0' } ( 1 .. 128-$_[0] ) ) ));
Subroutine generate_bitstr: called CC=1 SCAR=0.0
Pod documented
186 }
187  
188 =item C<bitstr_and( bitstr1 , bitstr2 )>
189  
190 Not a method, AND's two bitstrings, returns result.
191  
192 =cut
193 #takes two bitstrs as arguments and returns their logical or as bitstr
194 sub bitstr_and {
195 1 ▶ return $_[0] & $_[1]
Subroutine bitstr_and: called CC=1 SCAR=0.0
Pod documented
196 }
197  
198 =item C<bitstr_or( bitstr1 , bitstr2)>
199  
200 Not a method, OR's two bitstrings, returns result.
201  
202 =cut
203 #takes two bitstrs as arguments and returns their logical or as bitstr
204 sub bitstr_or {
205 1 ▶ return $_[0] | $_[1]
Subroutine bitstr_or: called CC=1 SCAR=0.0
Pod documented
206 }
207  
208 =item C<bitstr_not( bitstr )>
209  
210 Not a method, inverts a bitstring.
211  
212 =cut
213 #takes a bitstr and inverts it
214 sub bitstr_not {
215 1 ▶ return ~ $_[0]
Subroutine bitstr_not: called CC=1 SCAR=0.0
Pod documented
216 }
217  
218 =item C<from_str( string_bitstring )>
219  
220 Not a method, takes a string of characters 0 or 1, returns corresponding binary
221 bitstring. Please do not use more than 128 characters, rest will be ignored.
222  
223 =cut
224  
225 #converts a bitstr (111010010010....) to a binary string
226 sub from_str {
227 32 ▶ my $str = shift(@_);
Subroutine from_str: called CC=1 SCAR=0.0
Pod documented
228 32 return pack("B128",$str);
229 }
230  
231 =item C<to_str( bitstring )>
232  
233 Not a method, takes a binary bitstring, returns a string composed of 0's and
234 1's. Please supply bitstrings of max. 128 bits, rest of the bits will be
235 ignored.
236  
237 =cut
238  
239 #converts from binary to literal bitstr
240 sub to_str {
241 41 ▶ my $bitstr = shift(@_);
Subroutine to_str: called CC=1 SCAR=0.0
Pod documented
242 41 return join('',unpack("B128",$bitstr));
243 }
244  
245 =item C<contains( other_address )>
246  
247 This method takes an argument which is either an IPv6::Address or a plain string
248 that can be promoted to a valid IPv6::Address, and tests whether the object
249 contains it. Obviously returns true or false.
250  
251 =cut
252  
253 sub contains {
254 9 ▶ defined( my $self = shift(@_) ) or die 'incorrect call';
Subroutine contains: called CC=6 SCAR=18.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined(my $self = shift @_) or die "incorrect call" 0 9 9
255 9 ▶ defined( my $other = shift(@_) ) or die 'incorrect call';
Branch: 1 branch
BranchTrueFalseTotal
defined(my $other = shift @_) or die "incorrect call" 0 9 9
256 9 ▶ if (ref($other) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq "") 9 0 9
257 9 $other = __PACKAGE__->new($other);
258 }
259 9 ▶ return if ($self->get_prefixlen > $other->get_prefixlen);
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_prefixlen > $other->get_prefixlen 1 8 9
260 8 ▶ return 1 if $self->get_masked_address_bitstr eq ( generate_bitstr( $self->get_prefixlen ) & $other->get_bitstr );
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_masked_address_bitstr eq (generate_bitstr($self->get_prefixlen) & $other->get_bitstr) 7 1 8
261 #return 1 if (substr($self->get_bitstr,0,$self->get_prefixlen) eq substr($other->get_bitstr,0,$self->get_prefixlen));
262 1 return;
263 }
264  
265 =item C<addr_string>
266  
267 Returns the address part of the IPv6::Address. Using the option ipv4=>1 like
268  
269 $a->addr_string(ipv4=>1)
270  
271 will make the last 32-bits appear as an IPv4 address. Also, using nocompress=>1
272 like
273  
274 $a->addr_string( nocompress => 1 )
275  
276 will prevent the string from containing a '::' part. So it will be 8 parts
277 separated by ':' colons.
278  
279 =cut
280  
281 #returns the address part (2001:648:2000:0000:0000....)
282 sub addr_string {
283 54 ▶ my $self = shift(@_);
Subroutine addr_string: called CC=10 SCAR=23.0
Pod documented
284 54 my $str = join(':',map { sprintf("%x",$_) } (unpack("nnnnnnnn",$self->get_bitstr)) );
285 54 my $str2 = join(':',map { sprintf("%04x",$_) } (unpack("nnnnnnnn",$self->get_bitstr)) );
286 #print Dumper(@_);
287 54 my %option = (@_) ;
288 #print Dumper(\%option);
289 54 ▶ if (defined($option{ipv4}) && $option{ipv4}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{"ipv4"} and $option{"ipv4"}) 3 51 54
Condition66%2/3
defined $option{"ipv4"} and $option{"ipv4"}
!ll&&!rl&&r
5103
MC/DC50%1/2
defined $option{"ipv4"} and $option{"ipv4"}
defined $option{"ipv4"} $option{"ipv4"}
Truth table66%2/3
A and B
A= defined $option{"ipv4"}B= $option{"ipv4"}
ABresult
0X0
100
111
290 ###print "string:",$str,"\n";
291 3 $str = join(':',map { sprintf("%x",$_) } (unpack("nnnnnn",$self->get_bitstr)) ).':'.join('.', map {sprintf("%d",hex $_)} ($str2 =~ /([0-9A-Fa-f]{2})([0-9A-Fa-f]{2}):([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/));
292 #print STDERR $ipv4,"\n";
293
294 }
295 #print 'DEBUG:' . $str,"\n";
296 54 ▶ return $str2 if $option{full};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"full"} 1 53 54
297 53 ▶ return $str if $option{nocompress};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"nocompress"} 2 51 53
298 51 ▶ return '::' if($str eq '0:0:0:0:0:0:0:0');
Branch: 1 branch
BranchTrueFalseTotal
if $str eq "0:0:0:0:0:0:0:0" 3 48 51
299 48 ▶ for(my $i=7;$i>1;$i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 181 1 182
300 181 my $zerostr = join(':',split('','0'x$i));
301 ###print "DEBUG: $str $zerostr \n";
302 181 ▶ if($str =~ /:$zerostr$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /:$zerostr$/) { } 34 147 181
elsif ($str =~ /:$zerostr:/) { } 9 138 147
elsif ($str =~ /^$zerostr:/) { } 4 134 138
303 34 $str =~ s/:$zerostr$/::/;
304 34 return $str;
305 }
306 elsif ($str =~ /:$zerostr:/) {
307 9 $str =~ s/:$zerostr:/::/;
308 9 return $str;
309 }
310 elsif ($str =~ /^$zerostr:/) {
311 4 $str =~ s/^$zerostr:/::/;
312 4 return $str;
313 }
314 }
315 1 return $str;
316 }
317  
318 =item C<string>
319  
320 Returns the full IPv6 address, with the prefix in its end.
321  
322 =cut
323  
324 #returns the full IPv6 address
325 sub string {
326 48 ▶ my $self = shift(@_);
Subroutine string: called CC=1 SCAR=0.0
Pod documented
327 48 return $self->addr_string(@_).'/'.$self->get_prefixlen;
328 }
329  
330 =item C<to_string>
331  
332 Used internally by the overload module.
333  
334 =cut
335 #to be used by the overload module
336 sub to_string {
337 41 ▶ return $_[0]->string();
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
338 }
339  
340 =item C<split( exponent , target_length )>
341  
342 Splits the address to the order of two of the number given as first argument.
343 Example: if argument is 3, 2^3=8, address is split into 8 parts. The final parts
344 have prefix length equal to the target_length specified in the second argument.
345  
346 =cut
347 sub split {
348 0 ▶ my $self = shift(@_);
Subroutine split: not called CC=2 SCAR=17.9
Pod documented
349 0 my $split_length = shift(@_);#example: 3
350 0 my $networks = 2**$split_length;#2**3 equals 8 prefixes
351 0 my @bag = ();
352 0 ▶ for(my $i=0;$i<$networks;$i++) { #from 0 to 7
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $networks) 0 0 0
353 0 my $b_str = sprintf("%0${split_length}b",$i); # 001,010,011 and so on util 111 (7)
354 0 my $addr_str = $self->get_bitstr; #get the original bitstring of the address
355 0 substr($addr_str,$self->get_prefixlen,$split_length) = $b_str; #replace the correct 3 bits with $b_str
356 0 debug $addr_str,"\n";
357 0 push @bag,(__PACKAGE__->raw_new($addr_str,$self->get_prefixlen + $split_length)); #create and store the new addr
358 }
359 0 return @bag;
360 }
361  
362
363 =item C<apply_mask>
364  
365 Applies the prefix length mask to the address. Does not return anything. Works on $self.
366 B<WARNING:>This will alter the object.
367  
368 =cut
369 sub apply_mask {
370 0 ▶ my $self = shift(@_);
Subroutine apply_mask: not called CC=1 SCAR=6.9
Pod documented
371 0 $self->{bitstr} = bitstr_and($self->get_bitstr,$self->get_mask_bitstr);
372 }
373  
374 =item C<first_address>
375  
376 Returns the first address of the prefix that is represented by the object. E.g.
377 consider 2001:648:2000::1234/64. First address will be 2001:648:2000::/64.
378  
379 =cut
380  
381 sub first_address {
382 1 ▶ my $bitstr = bitstr_and( $_[0]->get_bitstr , $_[0]->get_mask_bitstr );
Subroutine first_address: called CC=1 SCAR=0.0
Pod documented
383 1 IPv6::Address->raw_new( $bitstr, $_[0]->get_prefixlen);
384 }
385  
386 =item C<last_address>
387  
388 Returns the last address of the prefix that is represented by the object. E.g.
389 consider 2001:648:2000::1234/64. Last address will be
390 2001:648:2000::ffff:ffff:ffff:ffff/64.
391  
392 =cut
393 sub last_address {
394 1 ▶ my $bitstr = bitstr_or( $_[0]->get_bitstr , bitstr_not( $_[0]->get_mask_bitstr ) );
Subroutine last_address: called CC=1 SCAR=0.0
Pod documented
395 1 IPv6::Address->raw_new( $bitstr, $_[0]->get_prefixlen);
396 }
397
398  
399 =item C<is_unspecified> , C<is_loopback> , C<is_multicast>
400  
401 Returns true or false depending on whether the address falls into the
402 corresponding category stated by the method name. E.g.
403  
404 IPv6::Address->new('::1')->is_loopback # returns true
405  
406 =cut
407  
408 1 my %patterns = (
409 unspecified => "^::\$",
410 loopback => "^::1\$",
411 multicast => "^ff",
412 );
413 #@TODO: implement this
414 1 my %binary_patterns = (
415 "link-local unicast" => "^",
416 );
417  
418  
419 1 for my $item (keys %patterns) {
420 Sub::Install::install_sub({
421 code => sub {
422 6 ▶ return ( shift(@_)->addr_string =~ /$patterns{$item}/i )? 1 : 0;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
shift(@_)->addr_string =~ /$patterns{$item}/i ? : 5 1 6
423 },
424 3 into => __PACKAGE__,
425 as => 'is_'.$item,
426 });
427 }
428  
429 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
430  
431 =item C<ipv4_to_binarray>
432  
433 Not a method, takes an IPv4 address, returns a character string consisting of 32
434 characters that are 0 or 1. Used internally, not too useful for the end user.
435  
436 =cut
437 sub ipv4_to_binarray {
438 1 ▶ defined( my $ipv4 = shift ) or die 'Missing IPv4 address argument';
Subroutine ipv4_to_binarray: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined(my $ipv4 = shift()) or die "Missing IPv4 address argument" 0 1 1
439 1 my @parts = ( split('\.',$ipv4) );
440 1 my @binarray = split('',join('',map { sprintf "%08b",$_ } @parts));
441 #debug(Dumper(\@binarray));
442 1 return @binarray;
443 }
444  
445  
446  
447 =item C<enumerate_with_IPv4( ipv4, mask )>
448  
449 Takes an IPv4 address and uses a part of it to enumerate inside the Ipv6 prefix
450 of the object. E.g.
451  
452 IPv6::Address->new('2001:648:2001::/48')->enumerate_with_IPv4('0.0.0.1',0x0000ffff) #will yield 2001:648::2001:0001::/64
453  
454 The return value will be a new IPv6::Address object, so the original object
455 remains intact. The part that will be used as an offset is extracted from the
456 ipv4 by using the mask.
457  
458 =cut
459  
460 sub enumerate_with_IPv4 {
461 1 ▶ my ($self,$IPv4,$mask) = (@_) or die 'Incorrect call';
Subroutine enumerate_with_IPv4: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my($self, $IPv4, $mask) = or die "Incorrect call" 0 1 1
462 1 my $binmask = sprintf "%032b",$mask;
463
464 1 my @IPv4 = ipv4_to_binarray($IPv4);
465 1 my $binary = '';
466 1 ▶ for(my $i=0;$i<32;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 32) 32 1 33
467 #debug("$i ".substr($binmask,$i,1));
468 32 ▶ $binary = $binary.$IPv4[$i] if substr($binmask,$i,1) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if substr($binmask, $i, 1) == 1 15 17 32
469 }
470 1 debug($binary);
471 1 my $new_prefixlen = $self->get_prefixlen + length($binary);
472 1 my $new_bitstr = to_str( $self->get_bitstr );
473 1 debug($new_bitstr);
474 1 substr($new_bitstr, ($self->get_prefixlen), length($binary)) = $binary;
475 1 debug("old bitstring is ".$self->get_bitstr);
476 1 debug("new bitstring is $new_bitstr");
477 1 debug($new_prefixlen);
478
479 1 return __PACKAGE__->raw_new(from_str($new_bitstr),$new_prefixlen);
480 }
481  
482 =item C<enumerate_with_offset( offset, desired_length )>
483  
484 Takes a non-negative integer offset and returns a prefix whose relative position
485 inside the object is defined by the offset. The prefix length of the result is
486 defined by the second argument. E.g.
487  
488 IPv6::Address->new('2001:648:2000::/48')->enumerate_with_offset( 5 , 64 ) #2001:648:2000:4::/64
489  
490 =cut
491  
492 sub enumerate_with_offset {
493 20 ▶ my ($self,$offset,$desired_length) = (@_) or die 'Incorrect call';
Subroutine enumerate_with_offset: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my($self, $offset, $desired_length) = or die "Incorrect call" 0 20 20
494 20 my $to_replace_len = $desired_length - $self->get_prefixlen;
495 20 my $new_bitstr = to_str( $self->get_bitstr );
496 20 my $offset_bitstr = sprintf("%0*b",$to_replace_len,$offset);
497 20 debug("offset number is $offset (or: $offset_bitstr)");
498 #consistency check
499 20 ▶ die "Tried to replace $to_replace_len bits, but for $offset, ".length($offset_bitstr)." bits are required"
Branch: 1 branch
BranchTrueFalseTotal
if length $offset_bitstr > $to_replace_len 2 18 20
500 if(length($offset_bitstr) > $to_replace_len);
501 18 substr($new_bitstr, ($self->get_prefixlen), length($offset_bitstr) ) = $offset_bitstr;
502 18 return __PACKAGE__->raw_new(from_str($new_bitstr),$desired_length);
503 }
504  
505 =item C<increment( offset )>
506  
507 Increments the IPv6::Address object by offset. Offsets larger than 2^32-1 are
508 not acceptable. This method is probably not too useful, but is provided for
509 completeness.
510  
511 =cut
512  
513 sub increment {
514 15 ▶ my ( $self , $offset ) = (@_) or die 'Incorrect call';
Subroutine increment: called CC=12 SCAR=26.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my($self, $offset) = or die "Incorrect call" 0 15 15
515  
516 15 my $max_int = 2**32-1;
517 15 ▶ die 'Sorry, offsets beyond 2^32-1 are not acceptable' if( $offset > $max_int );
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $max_int 0 15 15
518 15 ▶ die 'Sorry, cannot offset a /0 prefix. ' if ( $self->get_prefixlen == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_prefixlen == 0 1 14 15
519  
520 14 my $new_bitstr = to_str( $self->get_bitstr ); #will use it to store the new bitstr
521  
522 14 ▶ $DEBUG && print STDERR "Original bitstring is $new_bitstr\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR "Original bitstring is $new_bitstr\n" 0 14 14
523  
524 # 0..127
525 14 ▶ my $start = ($self->get_prefixlen>=32)? $self->get_prefixlen - 32 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_prefixlen >= 32 ? : 14 0 14
526 14 my $len = $self->get_prefixlen - $start;
527  
528 14 ▶ $DEBUG && print STDERR "will replace from pos $start (from 0) and for $len len\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR "will replace from pos $start (from 0) and for $len len\n" 0 14 14
529  
530 # extract start..start+len part, 0-pad to 32 bits, pack into a network byte order $n
531 14 my $n = unpack('N',pack('B32',sprintf("%0*s",32,substr($new_bitstr, $start , $len ))));
532  
533 14 ▶ $DEBUG && print STDERR "Original n=".$n."\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR 'Original n=' . $n . "\n" 0 14 14
534 14 $n += $offset;
535 14 ▶ $DEBUG && print STDERR "Result n=".$n."\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR 'Result n=' . $n . "\n" 0 14 14
536  
537 14 ▶ die "Sorry, address part exceeded $max_int" if( $n > $max_int ); #just a precaution
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max_int 1 13 14
538  
539 # repack the $n into a 32bit network ordered integer, convert into "1000101010101..." string
540 13 my $bstr = unpack( "B32", pack( 'N' , $n ) );
541  
542 13 ▶ $DEBUG && print STDERR "Replacement bitstr is $bstr\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR "Replacement bitstr is $bstr\n" 0 13 13
543 13 ▶ die 'internal error. Address should be 32-bits long' unless (length($bstr) == 32); #another precaution
Branch: 1 branch
BranchTrueFalseTotal
unless length $bstr == 32 0 13 13
544
545 #replace into new_bitstr from start and for len with bstr up for len bytes counting from the *end*
546 13 substr( $new_bitstr , $start , $len ) = substr( $bstr, - $len);
547  
548 # result is ready, return it
549 13 return __PACKAGE__->raw_new(from_str($new_bitstr),$self->get_prefixlen);
550 }
551  
552 =item C<nxx_parts(unpack_format)>
553  
554 Takes the bitstring of the address and unpacks it using the first argument.
555 Internal use mostly.
556  
557 =cut
558  
559 sub nxx_parts {
560 44 ▶ unpack($_[1],$_[0]->get_bitstr)
Subroutine nxx_parts: called CC=1 SCAR=0.0
Pod documented
561 }
562  
563 =item C<n16_parts>
564  
565 Splits the address into an 8-item array of unsigned short integers. Network byte
566 order is implied, a short integer is 16-bits long.
567  
568 =cut
569  
570 #@TODO add tests for this method
571 sub n16_parts {
572 0 ▶ ( $_[0]->nxx_parts('nnnnnnnn') )
Subroutine n16_parts: not called CC=1 SCAR=6.9
Pod documented
573 }
574  
575 =item C<n16_parts>
576  
577 Splits the address into an 4-item array of unsigned long integers. Network byte
578 order is implied, a long integer is 32-bits long.
579  
580 =cut
581 #@TODO add tests for this method
582 sub n32_parts {
583 44 ▶ ( $_[0]->nxx_parts('NNNN') )
Subroutine n32_parts: called CC=1 SCAR=0.0
Pod undocumented
584 }
585  
586 =item C<n_cmp( a , b )>
587  
588 Takes two 128-bit bitstr arguments, compares them and returns the result as -1,
589 0 or 1. The semantics are the same as that of the spaceship operator <=>.
590  
591 This method will overload the <=> operator for IPv6::Address objects, so
592 comparing IPv6::Address objects like they were integers produces the correct
593 results.
594  
595 =cut
596  
597 #@TODO add tests for this method
598 sub n_cmp {
599 22 ▶ my @a = $_[0]->n32_parts;
Subroutine n_cmp: called CC=3 SCAR=11.0
Pod documented
600 22 my @b = $_[1]->n32_parts;
601 22 for ( 0 .. 3 ) {
602 64 my $cmp = ( $a[$_] <=> $b[$_] );
603 64 ▶ return $cmp if ( $cmp != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cmp != 0 12 52 64
604 }
605 10 return 0;
606 }
607  
608 =item C<n_sort( array )>
609  
610 Sorts an array of bitstrs using the n_cmp function.
611  
612 =cut
613  
614 sub n_sort {
615 1 ▶ sort { $a <=> $b } @_;
Subroutine n_sort: called CC=1 SCAR=0.0
Pod documented
616 }
617  
618 =item C<radius_string>
619  
620 Returns a string suitable to be returned as an IPv6 Radius AV-pair. See RFC 3162
621 for an explanation of the format.
622  
623 =back
624 =cut
625  
626 sub radius_string {
627 6 ▶ defined(my $self = shift) or die 'Missing argument';
Subroutine radius_string: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined(my $self = shift()) or die "Missing argument" 0 6 6
628 #Framed-IPv6-Prefix := 0x0040200106482001beef
629 6 my $partial_bitstr = substr(to_str( $self->get_bitstr ),0,$self->get_prefixlen);
630 6 my $remain = $self->get_prefixlen % 8;
631 6 ▶ if($remain > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain > 0) 2 4 6
632 2 $partial_bitstr = $partial_bitstr . '0'x(8 - $remain);
633 }
634 6 return '0x00'.sprintf("%02x",$self->get_prefixlen).join('',map {unpack("H",pack("B4",$_))} ($partial_bitstr =~ /([01]{4})/g) );
635 }
636  
637 package IPv4Subnet;
638 1 $IPv4Subnet::VERSION = '0.207';
639 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
640 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
641 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
642 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
643 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
644  
645  
646 sub new {
647 35 ▶ defined ( my $class = shift ) or die "missing class";
Subroutine new: called CC=4 SCAR=14.9
Branch: 1 branch
BranchTrueFalseTotal
defined(my $class = shift()) or die "missing class" 0 35 35
648 35 ▶ defined ( my $str = shift ) or die "missing string";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $str = shift()) or die "missing string" 0 35 35
649 35 ▶ my ( $ip , $length_n ) = ( $str =~ /^(\d+\.\d+\.\d+\.\d+)\/(\d+)$/ ) or croak "Cannot parse $str";
Branch: 1 branch
BranchTrueFalseTotal
() = $str =~ m[^(\d+\.\d+\.\d+\.\d+)/(\d+)$] or croak("Cannot parse $str") 0 35 35
650 35 bless { ip_n => my_aton($ip) , length_n => $length_n } , $class ;
651 }
652  
653 sub new_from_start_stop {
654 1 ▶ $_[0]->new( $_[1].'/'.(32 - log( ( my_aton($_[1]) ^ my_aton($_[2]) ) + 1)/log(2)))
Subroutine new_from_start_stop: called CC=1 SCAR=0.0
655 }
656  
657 sub to_string {
658 1 ▶ $_[0]->get_start_ip . '/' . $_[0]->get_length_n
Subroutine to_string: called CC=1 SCAR=0.0
659 }
660  
661 sub get_ip_n {
662 314 ▶ return $_[0]->{ip_n} ;
Subroutine get_ip_n: called CC=1 SCAR=0.0
663 }
664  
665 sub get_start {
666 314 ▶ return $_[0]->get_ip_n & $_[0]->get_mask_n;
Subroutine get_start: called CC=1 SCAR=0.0
667 }
668  
669 sub get_stop {
670 262 ▶ return $_[0]->get_start + $_[0]->get_length - 1;
Subroutine get_stop: called CC=1 SCAR=0.0
671 }
672  
673 sub get_start_ip {
674 4 ▶ return my_ntoa($_[0]->get_start);
Subroutine get_start_ip: called CC=1 SCAR=0.0
675 }
676  
677 sub get_stop_ip {
678 3 ▶ return my_ntoa($_[0]->get_stop);
Subroutine get_stop_ip: called CC=1 SCAR=0.0
679 }
680  
681 sub get_length {
682 285 ▶ return 2**(32-$_[0]->get_length_n);
Subroutine get_length: called CC=1 SCAR=0.0
683 }
684  
685 sub enumerate {
686 # in 32-bit systems, this seems to fail with error:
687 # "Range iterator outside integer range"
688 #map { my_ntoa( $_ ) } ($_[0]->get_start .. $_[0]->get_stop)
689 1 ▶ my @ret;
Subroutine enumerate: called CC=2 SCAR=6.9
690 1 ▶ for( my $i = $_[0]->get_start ; $i <= $_[0]->get_stop ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $_[0]->get_stop) 256 1 257
691 256 push @ret,my_ntoa( $i )
692 }
693 return @ret
694 1 }
695  
696 sub get_length_n {
697 928 ▶ return $_[0]->{length_n};
Subroutine get_length_n: called CC=1 SCAR=0.0
698 }
699  
700 sub get_mask_n {
701 326 ▶ ($_[0]->get_length_n == 0 )?
Subroutine get_mask_n: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->get_length_n == 0 ? : 10 316 326
702 0 : hex('0xffffffff') << ( 32 - $_[0]->get_length_n ) ;
703 }
704  
705 sub get_mask {
706 6 ▶ my_ntoa( $_[0]->get_mask_n );
Subroutine get_mask: called CC=1 SCAR=0.0
707 }
708  
709 sub get_wildcard {
710 6 ▶ my_ntoa( ~ $_[0]->get_mask_n );
Subroutine get_wildcard: called CC=1 SCAR=0.0
711 }
712  
713 sub my_aton {
714 82 ▶ defined ( my $aton_str = inet_aton( $_[0] ) ) or croak '$_[0] cannot be fed to inet_aton';
Subroutine my_aton: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined(my $aton_str = inet_aton($_[0])) or croak("\$_[0] cannot be fed to inet_aton") 0 82 82
715 82 return unpack('N',$aton_str);
716 }
717  
718 sub my_ntoa {
719 275 ▶ return inet_ntoa(pack('N',$_[0]));
Subroutine my_ntoa: called CC=1 SCAR=0.0
720 }
721  
722 sub position {
723 45 ▶ my $self = shift;
Subroutine position: called CC=3 SCAR=11.3
724 45 ▶ defined ( my $arg = shift ) or die "Incorrect call";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $arg = shift()) or die "Incorrect call" 0 45 45
725 45 my $number = my_aton($arg);
726 45 ▶ $DEBUG && print STDERR "number is ",my_ntoa($number)," and start is ",my_ntoa($self->get_start)," and stop is ",my_ntoa($self->get_stop),"\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print STDERR "number is ", my_ntoa($number), " and start is ", my_ntoa($self->get_start), " and stop is ", my_ntoa($self->get_stop), "\n" 0 45 45
727 45 return $number - $self->get_start;
728 }
729  
730 sub contains {
731 19 ▶ return ( ($_[0]->position($_[1]) < $_[0]->get_length) && ( $_[0]->position($_[1]) >= 0 ) )? 1 : 0;
Subroutine contains: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->position($_[1]) < $_[0]->get_length && $_[0]->position($_[1]) >= 0 ? : 16 3 19
Condition66%2/3
$_[0]->position($_[1]) < $_[0]->get_length and $_[0]->position($_[1]) >= 0
!ll&&!rl&&r
3016
MC/DC50%1/2
$_[0]->position($_[1]) < $_[0]->get_length and $_[0]->position($_[1]) >= 0
$_[0]->position($_[1]) < $_[0]->get_length $_[0]->position($_[1]) >= 0
Truth table66%2/3
A and B
A= $_[0]->position($_[1]) < $_[0]->get_lengthB= $_[0]->position($_[1]) >= 0
ABresult
0X0
100
111
732 }
733  
734 sub calculate_compound_offset {
735 10 ▶ defined( my $address = shift ) or die 'missing address';
Subroutine calculate_compound_offset: called CC=5 SCAR=17.1
Branch: 1 branch
BranchTrueFalseTotal
defined(my $address = shift()) or die "missing address" 0 10 10
736 10 ▶ defined( my $blocks = shift ) or die 'missing block reference';
Branch: 1 branch
BranchTrueFalseTotal
defined(my $blocks = shift()) or die "missing block reference" 0 10 10
737
738 10 my $offset = 0;
739 10 for my $block (@{$blocks}) {
740 12 my $subnet = IPv4Subnet->new($block);
741 12 ▶ if ($subnet->contains($address)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subnet->contains($address)) { } 10 2 12
742 10 return ( $subnet->position($address) + $offset );
743 }
744 else {
745 2 $offset = $offset + $subnet->get_length;
746 }
747 }
748 0 die "Address $address does not belong to range:",join(',',@{$blocks});
749 0 return;
750 }
751  
752 =head1 AUTHOR
753  
754 Athanasios Douitsis C<< <aduitsis@cpan.org> >>
755  
756 =head1 SUPPORT
757  
758 Please open a ticket at L<https://github.com/aduitsis/IPv6-Address>.
759  
760 =head1 COPYRIGHT & LICENSE
761
762 Copyright 2008-2015 Athanasios Douitsis, all rights reserved.
763
764 This program is free software; you can use it
765 under the terms of Artistic License 2.0 which can be found at
766 http://www.perlfoundation.org/artistic_license_2_0
767
768 =cut
769  
770 1 1;
771
772
↑ Summary Build »