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