| 1 | package Data::IPV4::Range::Parse; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK $VERSION @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | $VERSION = '1.05'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | require Exporter; | |||||||||||||||||||||||||||
| 11 | 6 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | @EXPORT_OK=qw( | |||||||||||||||||||||||||||
| 14 | ALL_BITS | ||||||||||||||||||||||||||||
| 15 | MAX_CIDR | ||||||||||||||||||||||||||||
| 16 | MIN_CIDR | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sort_quad | ||||||||||||||||||||||||||||
| 19 | sort_notations | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | int_to_ip | ||||||||||||||||||||||||||||
| 22 | ip_to_int | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | parse_ipv4_cidr | ||||||||||||||||||||||||||||
| 25 | parse_ipv4_ip | ||||||||||||||||||||||||||||
| 26 | parse_ipv4_range | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | broadcast_int | ||||||||||||||||||||||||||||
| 29 | base_int | ||||||||||||||||||||||||||||
| 30 | size_from_mask | ||||||||||||||||||||||||||||
| 31 | hostmask | ||||||||||||||||||||||||||||
| 32 | cidr_to_int | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | auto_parse_ipv4_range | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 38 | ALL=>\@EXPORT_OK | ||||||||||||||||||||||||||||
| 39 | ,CONSTANTS=>[qw( | ||||||||||||||||||||||||||||
| 40 | ALL_BITS | ||||||||||||||||||||||||||||
| 41 | MAX_CIDR | ||||||||||||||||||||||||||||
| 42 | MIN_CIDR | ||||||||||||||||||||||||||||
| 43 | )] | ||||||||||||||||||||||||||||
| 44 | ,PARSE_RANGE=>[qw( | ||||||||||||||||||||||||||||
| 45 | parse_ipv4_cidr | ||||||||||||||||||||||||||||
| 46 | parse_ipv4_ip | ||||||||||||||||||||||||||||
| 47 | parse_ipv4_range | ||||||||||||||||||||||||||||
| 48 | auto_parse_ipv4_range | ||||||||||||||||||||||||||||
| 49 | )] | ||||||||||||||||||||||||||||
| 50 | ,PARSE_IP=>[qw( | ||||||||||||||||||||||||||||
| 51 | int_to_ip | ||||||||||||||||||||||||||||
| 52 | ip_to_int | ||||||||||||||||||||||||||||
| 53 | )] | ||||||||||||||||||||||||||||
| 54 | ,SORT=>[qw( | ||||||||||||||||||||||||||||
| 55 | sort_quad | ||||||||||||||||||||||||||||
| 56 | sort_notations | ||||||||||||||||||||||||||||
| 57 | )] | ||||||||||||||||||||||||||||
| 58 | ,COMPUTE_FROM_INT=>[ qw( | ||||||||||||||||||||||||||||
| 59 | broadcast_int | ||||||||||||||||||||||||||||
| 60 | base_int | ||||||||||||||||||||||||||||
| 61 | size_from_mask | ||||||||||||||||||||||||||||
| 62 | hostmask | ||||||||||||||||||||||||||||
| 63 | cidr_to_int | ||||||||||||||||||||||||||||
| 64 | )] | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | use constant ALL_BITS=>0xffffffff; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | use constant MAX_CIDR=>32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | use constant MIN_CIDR=>0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub int_to_ip ($) { shift if $#_>0;join '.',unpack('C4',(pack('N',$_[0]))) } | ||||||||||||||||||||||||||
|
Subroutine
int_to_ip: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 35 | ▶ | sub ip_to_int ($) { shift if $#_>0;unpack('N',pack('C4',split(/\./,$_[0]))) } | ||||||||||||||||||||||||||
|
Subroutine
ip_to_int: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | ▶ | sub sort_quad ($$) { | ||||||||||||||||||||||||||
|
Subroutine
sort_quad: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 4 | my ($ip_a,$ip_b)=@_; | |||||||||||||||||||||||||||
| 76 | 4 | ip_to_int($ip_a) <=> ip_to_int($ip_b) | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub sort_notations ($$) { | ||||||||||||||||||||||||||
|
Subroutine
sort_notations: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 2 | my ($a_start,$a_end,$b_start,$b_end)=map { auto_parse_ipv4_range($_) } @_; | |||||||||||||||||||||||||||
| 81 | 2 | ▶ | croak 'cannot parse notation a or b' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | unless defined($b_end); | ||||||||||||||||||||||||||||
| 83 | 2 | my $ab_cmp=($a_start<=>$b_start); | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | return $ab_cmp if $ab_cmp!=0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | $a_end <=> $b_end | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 8 | ▶ | sub broadcast_int ($$) { | ||||||||||||||||||||||||||
|
Subroutine
broadcast_int: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 8 | ▶ | shift if $#_>1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 8 | base_int($_[0],$_[1]) + hostmask($_[1]) | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 16 | ▶ | sub base_int ($$) { shift if $#_>1;$_[0] & $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
base_int: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | sub size_from_mask ($) { shift if $#_>0;1 + hostmask($_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
size_from_mask: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 12 | ▶ | sub hostmask ($) { shift if $#_>0;ALL_BITS & (~(ALL_BITS & $_[0])) } | ||||||||||||||||||||||||||
|
Subroutine
hostmask: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 39 | ▶ | sub cidr_to_int ($) { | ||||||||||||||||||||||||||
|
Subroutine
cidr_to_int: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 39 | ▶ | shift if $#_>0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 39 | my ($cidr)=@_; | |||||||||||||||||||||||||||
| 99 | 39 | my $shift=MAX_CIDR -$cidr; | |||||||||||||||||||||||||||
| 100 | 39 | ▶ | return undef unless defined($cidr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 39 | ▶ | return undef unless $cidr=~ /^\d{1,2}$/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 39 | ▶ | return undef if $cidr>MAX_CIDR or $cidr<MIN_CIDR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cidr > 32 or $cidr < 0
MC/DC0%0/2
$cidr > 32 or $cidr < 0
$cidr > 32
$cidr < 0
Truth table33%1/3
A or B
A= $cidr > 32B= $cidr < 0
| |||||||||||||||||||||||||||||
| 103 | 39 | ▶ | return 0 if $shift==MAX_CIDR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 38 | ALL_BITS & (ALL_BITS << $shift) | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | ▶ | sub parse_ipv4_cidr { | ||||||||||||||||||||||||||
|
Subroutine
parse_ipv4_cidr: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 6 | my $notation=$_[$#_]; | |||||||||||||||||||||||||||
| 109 | 6 | $notation=~ s/(^\s+|\s+$)//g; | |||||||||||||||||||||||||||
| 110 | return () | ||||||||||||||||||||||||||||
| 111 | 6 | ▶ | unless($notation=~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | ^\d{1,3}(\.\d{1,3}){0,3} | ||||||||||||||||||||||||||||
| 113 | \s*\/\s* | ||||||||||||||||||||||||||||
| 114 | \d{1,3}(\.\d{1,3}){0,3}$ | ||||||||||||||||||||||||||||
| 115 | /x); | ||||||||||||||||||||||||||||
| 116 | 6 | my ($ip,$mask)=split /\s*\/\s*/,$notation; | |||||||||||||||||||||||||||
| 117 | 6 | my $ip_int=ip_to_int($ip); | |||||||||||||||||||||||||||
| 118 | 6 | my $mask_int; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | ▶ | if($mask=~ /\./) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 121 | # we know its quad notation | ||||||||||||||||||||||||||||
| 122 | 0 | $mask_int=ip_to_int($mask); | |||||||||||||||||||||||||||
| 123 | ▶ | } elsif($mask>=MIN_CIDR && $mask<=MAX_CIDR) { | |||||||||||||||||||||||||||
|
Condition33%1/3
$mask >= 0 and $mask <= 32
MC/DC0%0/2
$mask >= 0 and $mask <= 32
$mask >= 0
$mask <= 32
Truth table33%1/3
A and B
A= $mask >= 0B= $mask <= 32
| |||||||||||||||||||||||||||||
| 124 | 6 | $mask_int=cidr_to_int($mask); | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 0 | $mask_int=ip_to_int($mask); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 6 | my $first_int=base_int($ip_int , $mask_int); | |||||||||||||||||||||||||||
| 129 | 6 | my $last_int=broadcast_int( $first_int,$mask_int); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 6 | ($first_int,$last_int) | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | ▶ | sub parse_ipv4_range { | ||||||||||||||||||||||||||
|
Subroutine
parse_ipv4_range: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 5 | my $range=$_[$#_]; | |||||||||||||||||||||||||||
| 136 | 5 | ▶ | return () unless defined($range); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | # lop off start and end spaces | ||||||||||||||||||||||||||||
| 138 | 5 | $range=~ s/(^\s+|\s+$)//g; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 5 | ▶ | return () unless $range=~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | ^\d{1,3}(\.\d{1,3}){0,3} | ||||||||||||||||||||||||||||
| 142 | \s*-\s* | ||||||||||||||||||||||||||||
| 143 | \d{1,3}(\.\d{1,3}){0,3}$ | ||||||||||||||||||||||||||||
| 144 | /x; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 5 | my ($start,$end)=split /\s*-\s*/,$range; | |||||||||||||||||||||||||||
| 147 | 5 | ( ip_to_int($start) ,ip_to_int($end)) | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | ▶ | sub parse_ipv4_ip { | ||||||||||||||||||||||||||
|
Subroutine
parse_ipv4_ip: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 3 | my $ip=$_[$#_]; | |||||||||||||||||||||||||||
| 153 | 3 | ▶ | return () unless defined($ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | ( ip_to_int($ip) ,ip_to_int($ip)) | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 7 | ▶ | sub auto_parse_ipv4_range { | ||||||||||||||||||||||||||
|
Subroutine
auto_parse_ipv4_range: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 7 | my $source=$_[$#_]; | |||||||||||||||||||||||||||
| 160 | 7 | ▶ | return parse_ipv4_cidr($source) if $source=~ /\//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 3 | ▶ | return parse_ipv4_range($source) if $source=~ /-/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | return parse_ipv4_ip($source); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 6 | 1; | |||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||