| 1 | # IPv4Addr.pm - Perl module to manipulate IPv4 addresses. | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Author: Francis J. Lacoste <francis@Contre.COM> | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations Inc. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 8 | # it under the terms as perl itself. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package Network::IPv4Addr; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 17 | 1 | require AutoLoader; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | @ISA = qw(Exporter AutoLoader); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 22 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 23 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 24 | 1 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 27 | all => [qw{ ipv4_parse ipv4_checkip | ||||||||||||||||||||||||||||
| 28 | ipv4_network ipv4_broadcast | ||||||||||||||||||||||||||||
| 29 | ipv4_cidr2msk ipv4_msk2cidr | ||||||||||||||||||||||||||||
| 30 | ipv4_in_network ipv4_dflt_netmask | ||||||||||||||||||||||||||||
| 31 | } ], | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | Exporter::export_ok_tags('all'); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Functions to manipulate IPV4 address | ||||||||||||||||||||||||||||
| 44 | 1 | my $ip_rgx = "\\d+\\.\\d+\\.\\d+\\.\\d+"; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Given an IPv4 address in host, ip/netmask or cidr format | ||||||||||||||||||||||||||||
| 47 | # returns a ip / cidr pair. | ||||||||||||||||||||||||||||
| 48 | 49 | ▶ | sub ipv4_parse($;$) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_parse: called CC=10 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 49 | my ($ip,$msk); | |||||||||||||||||||||||||||
| 50 | # Called with 2 args, assume first is IP address | ||||||||||||||||||||||||||||
| 51 | 49 | ▶ | if ( defined $_[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 12 | $ip = $_[0]; | |||||||||||||||||||||||||||
| 53 | 12 | $msk= $_[1]; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 37 | ($ip) = $_[0] =~ /($ip_rgx)/o; | |||||||||||||||||||||||||||
| 56 | 37 | ($msk) = $_[0] =~ m!/(.+)!o; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Remove white spaces | ||||||||||||||||||||||||||||
| 60 | 49 | ▶ | $ip = ipv4_chkip( $ip ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | croak __PACKAGE__, ": invalid IPv4 address: ", $ip, "\n"; | ||||||||||||||||||||||||||||
| 62 | 49 | ▶ | $msk =~ s/\s//g if defined $msk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Check Netmask to see if it is a CIDR or Network | ||||||||||||||||||||||||||||
| 65 | 49 | ▶ | if (defined $msk ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 24 | ▶ | if ($msk =~ /^\d{1,2}$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | # Check cidr | ||||||||||||||||||||||||||||
| 68 | 20 | ▶ | croak __PACKAGE__, ": invalid cidr: ", $msk, "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$msk < 0 or $msk > 32
MC/DC0%0/2
$msk < 0 or $msk > 32
$msk < 0
$msk > 32
Truth table33%1/3
A or B
A= $msk < 0B= $msk > 32
| |||||||||||||||||||||||||||||
| 69 | if $msk < 0 or $msk > 32; | ||||||||||||||||||||||||||||
| 70 | } elsif ($msk =~ /^$ip_rgx$/o ) { | ||||||||||||||||||||||||||||
| 71 | 4 | $msk = ipv4_msk2cidr($msk); | |||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||
| 73 | 0 | croak __PACKAGE__, ": invalid netmask specification: ", $msk, "\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | # Host | ||||||||||||||||||||||||||||
| 77 | 25 | return $ip; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 24 | ▶ | wantarray ? ($ip,$msk) : "$ip/$msk"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | sub ipv4_dflt_netmask($) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_dflt_netmask: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 6 | my ($ip) = ipv4_parse($_[0]); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | my ($b1) = split /\./, $ip; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | return "255.0.0.0" if $b1 <= 127; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | return "255.255.0.0" if $b1 <= 191; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | return "255.255.255.0"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Check form a valid IPv4 address. | ||||||||||||||||||||||||||||
| 93 | 62 | ▶ | sub ipv4_chkip($) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_chkip: called CC=5 SCAR=17.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 62 | my ($ip) = $_[0] =~ /($ip_rgx)/o; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 62 | ▶ | return undef unless $ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Check that bytes are in range | ||||||||||||||||||||||||||||
| 99 | 62 | for (split /\./, $ip ) { | |||||||||||||||||||||||||||
| 100 | 248 | ▶ | return undef if $_ < 0 or $_ > 255; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ < 0 or $_ > 255
MC/DC0%0/2
$_ < 0 or $_ > 255
$_ < 0
$_ > 255
Truth table33%1/3
A or B
A= $_ < 0B= $_ > 255
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 62 | return $ip; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Transform a netmask in a CIDR mask length | ||||||||||||||||||||||||||||
| 106 | 13 | ▶ | sub ipv4_msk2cidr($) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_msk2cidr: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 13 | ▶ | my $msk = ipv4_chkip( $_[0] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | or croak __PACKAGE__, ": invalid netmask: ", $_[0], "\n"; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 13 | my @bytes = split /\./, $msk; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 13 | my $cidr = 0; | |||||||||||||||||||||||||||
| 113 | 13 | for (@bytes) { | |||||||||||||||||||||||||||
| 114 | 52 | my $bits = unpack( "B*", pack( "C", $_ ) ); | |||||||||||||||||||||||||||
| 115 | 52 | $cidr += $bits =~ tr /1/1/; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 13 | return $cidr; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Transform a CIDR mask length in a netmask | ||||||||||||||||||||||||||||
| 121 | 6 | ▶ | sub ipv4_cidr2msk($) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_cidr2msk: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 6 | my $cidr = shift; | |||||||||||||||||||||||||||
| 123 | 6 | ▶ | croak __PACKAGE__, ": invalid cidr: ", $cidr, "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cidr < 0 or $cidr > 32
MC/DC0%0/2
$cidr < 0 or $cidr > 32
$cidr < 0
$cidr > 32
Truth table33%1/3
A or B
A= $cidr < 0B= $cidr > 32
| |||||||||||||||||||||||||||||
| 124 | if $cidr < 0 or $cidr > 32; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 6 | my $bits = "1" x $cidr . "0" x (32 - $cidr); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 6 | return join ".", (unpack 'CCCC', pack("B*", $bits )); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Return the network address of | ||||||||||||||||||||||||||||
| 132 | # an IPv4 address | ||||||||||||||||||||||||||||
| 133 | 13 | ▶ | sub ipv4_network($;$) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_network: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 13 | my ($ip,$cidr) = ipv4_parse( $_[0], $_[1] ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # If only an host is given, use the default netmask | ||||||||||||||||||||||||||||
| 137 | 13 | ▶ | unless ($cidr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 2 | $cidr = ipv4_msk2cidr( ipv4_dflt_netmask($ip) ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 13 | my $u32 = unpack "N", pack "CCCC", split /\./, $ip; | |||||||||||||||||||||||||||
| 141 | 13 | my $bits = "1" x $cidr . "0" x (32 - $cidr ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 13 | my $msk = unpack "N", pack "B*", $bits; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 13 | my $net = join ".", unpack "CCCC", pack "N", $u32 & $msk; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 13 | ▶ | wantarray ? ( $net, $cidr) : "$net/$cidr"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | ▶ | sub ipv4_broadcast($;$) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_broadcast: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 3 | my ($ip,$cidr) = ipv4_parse( $_[0], $_[1] ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # If only an host is given, use the default netmask | ||||||||||||||||||||||||||||
| 154 | 3 | ▶ | unless ($cidr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | $cidr = ipv4_msk2cidr( ipv4_dflt_netmask($ip) ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | my $u32 = unpack "N", pack "CCCC", split /\./, $ip; | |||||||||||||||||||||||||||
| 159 | 3 | my $bits = "1" x $cidr . "0" x (32 - $cidr ); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | my $msk = unpack "N", pack "B*", $bits; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 3 | my $broadcast = join ".", unpack "CCCC", pack "N", $u32 | ~$msk; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 3 | $broadcast; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 11 | ▶ | sub ipv4_in_network($$;$$) { | ||||||||||||||||||||||||||
|
Subroutine
ipv4_in_network: called CC=9 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 11 | my ($ip1,$cidr1,$ip2,$cidr2); | |||||||||||||||||||||||||||
| 170 | 11 | ▶ | if ( @_ >= 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ($ip1,$cidr1) = ipv4_parse( $_[0], $_[1] ); | |||||||||||||||||||||||||||
| 172 | 0 | ($ip2,$cidr2) = ipv4_parse( $_[2], $_[3] ); | |||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 11 | ($ip1,$cidr1) = ipv4_parse( $_[0]); | |||||||||||||||||||||||||||
| 175 | 11 | ($ip2,$cidr2) = ipv4_parse( $_[1]); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Check for magic addresses. | ||||||||||||||||||||||||||||
| 179 | 11 | ▶ | return 1 if $ip1 eq "255.255.255.255" or $ip1 eq "0.0.0.0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ip1 eq "255.255.255.255" or $ip1 eq "0.0.0.0"
MC/DC100%2/2
$ip1 eq "255.255.255.255" or $ip1 eq "0.0.0.0"
$ip1 eq "255.255.255.255"
$ip1 eq "0.0.0.0"
Truth table100%3/3
A or B
A= $ip1 eq "255.255.255.255"B= $ip1 eq "0.0.0.0"
| |||||||||||||||||||||||||||||
| 180 | 9 | ▶ | return 1 if $ip2 eq "255.255.255.255" or $ip2 eq "0.0.0.0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ip2 eq "255.255.255.255" or $ip2 eq "0.0.0.0"
MC/DC100%2/2
$ip2 eq "255.255.255.255" or $ip2 eq "0.0.0.0"
$ip2 eq "255.255.255.255"
$ip2 eq "0.0.0.0"
Truth table100%3/3
A or B
A= $ip2 eq "255.255.255.255"B= $ip2 eq "0.0.0.0"
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Case where first argument is really an host | ||||||||||||||||||||||||||||
| 183 | 7 | ▶ | return $ip1 eq $ip2 unless ($cidr1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Case where second argument is an host | ||||||||||||||||||||||||||||
| 186 | 5 | ▶ | if ( not defined $cidr2) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 187 | 5 | return ipv4_network( $ip1, $cidr1) eq ipv4_network( $ip2, $cidr1 ); | |||||||||||||||||||||||||||
| 188 | } elsif ( $cidr2 > $cidr1 ) { | ||||||||||||||||||||||||||||
| 189 | # Netmask 2 is more specific than netmask 1 | ||||||||||||||||||||||||||||
| 190 | 0 | return ipv4_network( $ip1, $cidr2) eq ipv4_network( $ip2, $cidr2); | |||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||
| 192 | # Netmask 1 is more specific than netmask 2 | ||||||||||||||||||||||||||||
| 193 | 0 | return ipv4_network( $ip1, $cidr1) eq ipv4_network( $ip2, $cidr1); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 1 | 1; | |||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||