| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2016-2020 Joelle Maslak | ||||||||||||||||||||||||||||
| 5 | # All Rights Reserved - See License | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package IP::Random; | ||||||||||||||||||||||||||||
| 9 | 7 | $IP::Random::VERSION = '1.200230'; | |||||||||||||||||||||||||||
| 10 | # ABSTRACT: Generate IP Addresses Randomly | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Some boilerplate | ||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 7 | @IP::Random::ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 25 | 7 | @IP::Random::EXPORT_OK = qw(random_ipv4 in_ipv4_subnet default_ipv4_exclude); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # We need a version of List::Util with uniq in it | ||||||||||||||||||||||||||||
| 28 | 7 | ▶ | use List::Util 1.50 qw(any none notall pairs uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | use Socket qw(inet_aton); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 7 | my $IPV4_EXCLUDE = { | |||||||||||||||||||||||||||
| 32 | '0.0.0.0/8' => [ 'default', 'rfc1122' ], | ||||||||||||||||||||||||||||
| 33 | '10.0.0.0/8' => [ 'default', 'rfc1918' ], | ||||||||||||||||||||||||||||
| 34 | '100.64.0.0/10' => [ 'default', 'rfc6598' ], | ||||||||||||||||||||||||||||
| 35 | '127.0.0.0/8' => [ 'default', 'rfc1122' ], | ||||||||||||||||||||||||||||
| 36 | '169.254.0.0/16' => [ 'default', 'rfc3927' ], | ||||||||||||||||||||||||||||
| 37 | '172.16.0.0/12' => [ 'default', 'rfc1918' ], | ||||||||||||||||||||||||||||
| 38 | '192.0.0.0/24' => [ 'default', 'rfc5736' ], | ||||||||||||||||||||||||||||
| 39 | '192.0.2.0/24' => [ 'default', 'rfc5737' ], | ||||||||||||||||||||||||||||
| 40 | '192.88.99.0/24' => [ 'default', 'rfc3068' ], | ||||||||||||||||||||||||||||
| 41 | '192.168.0.0/16' => [ 'default', 'rfc1918' ], | ||||||||||||||||||||||||||||
| 42 | '198.18.0.0/15' => [ 'default', 'rfc2544' ], | ||||||||||||||||||||||||||||
| 43 | '198.51.100.0/24' => [ 'default', 'rfc5737' ], | ||||||||||||||||||||||||||||
| 44 | '203.0.113.0/24' => [ 'default', 'rfc5737' ], | ||||||||||||||||||||||||||||
| 45 | '224.0.0.0/4' => [ 'default', 'rfc3171' ], | ||||||||||||||||||||||||||||
| 46 | '240.0.0.0/4' => [ 'default', 'rfc1112' ], | ||||||||||||||||||||||||||||
| 47 | '255.255.255.255/32' => [ 'default', 'rfc919' ], | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Build cache of valid types | ||||||||||||||||||||||||||||
| 51 | 7 | my %VALID_TYPES = map { $_, 1 } uniq sort map { @$_ } values %$IPV4_EXCLUDE; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 13299 | ▶ | sub random_ipv4 ( %args ) { | ||||||||||||||||||||||||||
|
Subroutine
random_ipv4: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 13299 | ▶ | $args{rand} //= sub { int( rand( shift() + 1 ) ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{"rand"} //= sub {
int rand((shift()) + 1);
}
MC/DC0%0/2
$args{"rand"} //= sub {
int rand((shift()) + 1);
}
$args{"rand"}
sub {
int rand((shift()) + 1);
}
Truth table66%2/3
A //= B
A= $args{"rand"}B= sub {
int rand((shift()) + 1);
}
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Can't have exclude and additional_types_allowed both existing | ||||||||||||||||||||||||||||
| 58 | 13299 | ▶ | if ( exists( $args{exclude} ) && exists( $args{additional_types_allowed} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"exclude"} and exists $args{"additional_types_allowed"}
MC/DC0%0/2
exists $args{"exclude"} and exists $args{"additional_types_allowed"}
exists $args{"exclude"}
exists $args{"additional_types_allowed"}
Truth table66%2/3
A and B
A= exists $args{"exclude"}B= exists $args{"additional_types_allowed"}
| |||||||||||||||||||||||||||||
| 59 | 0 | croak( "Cannot define both 'exclude' and " . "'additional_types_allowed' parameters" ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # This saves us some later branches | ||||||||||||||||||||||||||||
| 63 | # Define defaults | ||||||||||||||||||||||||||||
| 64 | 13299 | ▶ | $args{additional_types_allowed} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"additional_types_allowed"} //= []
MC/DC100%1/1
$args{"additional_types_allowed"} //= []
$args{"additional_types_allowed"}
Truth table100%2/2
A
A= $args{"additional_types_allowed"}
| |||||||||||||||||||||||||||||
| 65 | 13299 | ▶ | $args{additional_exclude} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"additional_exclude"} //= []
MC/DC100%1/1
$args{"additional_exclude"} //= []
$args{"additional_exclude"}
Truth table100%2/2
A
A= $args{"additional_exclude"}
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # What are valid option names? | ||||||||||||||||||||||||||||
| 68 | 13299 | my $optre = qr/\A(?:rand|exclude|additional_(?:types_allowed|exclude))\z/; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Make sure all options are valid | ||||||||||||||||||||||||||||
| 71 | 13299 | ▶ | if ( notall { m/$optre/ } keys %args ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | my (@bad) = grep { !m/$optre/ } keys %args; | |||||||||||||||||||||||||||
| 73 | 0 | croak( "unknown named argument passed to random_ipv4: " . $bad[0] ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Get default excludes | ||||||||||||||||||||||||||||
| 77 | 13299 | ▶ | if ( !defined( $args{exclude} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | $args{exclude} = | ||||||||||||||||||||||||||||
| 79 | 9200 | _get_ipv4_excludes( $args{additional_types_allowed} ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Expand out tags in exclude list | ||||||||||||||||||||||||||||
| 83 | 71465 | my (@exclude_cidrs) = grep { m/^\d+\.\d+\.\d+\.\d+(:?\/\d+)$/ } @{ $args{exclude} }, | |||||||||||||||||||||||||||
| 84 | 13298 | @{ $args{additional_exclude} }; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 71465 | my (@exclude_tags) = grep { !m/^\d+\.\d+\.\d+\.\d+(:?\/\d+)$/ } @{ $args{exclude} }, | |||||||||||||||||||||||||||
| 87 | 13298 | @{ $args{additional_exclude} }; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 13298 | my (@exclude_expanded) = ( @exclude_cidrs, map { @{ _get_ipv4_excludes( $args{additional_types_allowed}, $_ ) } } @exclude_tags ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 13296 | my (@exclude_all) = uniq sort @exclude_expanded; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Build a closure for checking to see if an address is excluded | ||||||||||||||||||||||||||||
| 94 | 15739 | ▶ | my $is_not_excluded = sub($addr) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 15739 | ▶ | none { in_ipv4_subnet( $_, $addr ) } @exclude_all; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 13296 | }; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 13296 | my $addr; | |||||||||||||||||||||||||||
| 99 | 13296 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 15739 | my @parts; | |||||||||||||||||||||||||||
| 101 | 15739 | for my $octet ( 1 .. 4 ) { | |||||||||||||||||||||||||||
| 102 | 62956 | push @parts, $args{rand}->( 255, $octet ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 15739 | $addr = join '.', @parts; | |||||||||||||||||||||||||||
| 105 | } until $is_not_excluded->($addr); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 13296 | return $addr; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Private sub to build the default list of excludes, when passed a list | ||||||||||||||||||||||||||||
| 111 | # of additional types allowed | ||||||||||||||||||||||||||||
| 112 | # | ||||||||||||||||||||||||||||
| 113 | # Returns a list ref | ||||||||||||||||||||||||||||
| 114 | 17395 | ▶ | sub _get_ipv4_excludes ( $addl_types, $tag = 'default' ) { | ||||||||||||||||||||||||||
|
Subroutine
_get_ipv4_excludes: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 115 | 17395 | foreach my $t (@$addl_types) { | |||||||||||||||||||||||||||
| 116 | 57349 | ▶ | if ( !exists( $VALID_TYPES{$t} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | confess("Type '$t' is not a valid type"); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 17394 | ▶ | if ( !exists( $VALID_TYPES{$tag} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | confess("Type '$tag' is not a valid type"); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 17392 | my @ret; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | NEXT_EXCLUDE: | ||||||||||||||||||||||||||||
| 127 | 17392 | foreach my $default_exclude ( keys %$IPV4_EXCLUDE ) { | |||||||||||||||||||||||||||
| 128 | 278272 | ▶ | if ( none { $_ eq $tag } @{ $IPV4_EXCLUDE->{$default_exclude} } ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 118784 | next NEXT_EXCLUDE; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 159488 | foreach my $checktype ( @{ $IPV4_EXCLUDE->{$default_exclude} } ) { | |||||||||||||||||||||||||||
| 133 | 318976 | ▶ | if ( any { $_ eq $checktype } @$addl_types ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | # Not excluded. | ||||||||||||||||||||||||||||
| 135 | 83978 | next NEXT_EXCLUDE; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 75510 | push @ret, $default_exclude; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 17392 | return \@ret; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 101325 | ▶ | sub in_ipv4_subnet ( $sub_cidr, $ip ) { | ||||||||||||||||||||||||||
|
Subroutine
in_ipv4_subnet: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( !defined($sub_cidr) ) { confess("subnet_cidr is not defined"); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( !defined($ip) ) { confess("ip is not defined"); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 101325 | ▶ | if ( $sub_cidr !~ m/\A(?:[0-9\.]+)(?:\/(?:[0-9]+))?\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 1 | confess("$sub_cidr is not in the format A.B.C.D/N"); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 101324 | my ( $sub_net, $sub_mask ) = $sub_cidr =~ m/\A([0-9\.]+)(?:\/([0-9]+))?\z/ms; | |||||||||||||||||||||||||||
| 153 | 101324 | ▶ | $sub_mask //= 32; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sub_mask //= 32
MC/DC0%0/1
$sub_mask //= 32
$sub_mask
Truth table50%1/2
A
A= $sub_mask
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 101324 | my $addr = unpack( 'N', inet_aton($ip) ); | |||||||||||||||||||||||||||
| 156 | 101324 | my $sub = unpack( 'N', inet_aton($sub_net) ); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 101324 | my $mask = 0; | |||||||||||||||||||||||||||
| 159 | 101324 | for ( 1 .. $sub_mask ) { | |||||||||||||||||||||||||||
| 160 | 1223511 | $mask = $mask >> 1; | |||||||||||||||||||||||||||
| 161 | 1223511 | $mask = $mask | ( 1 << 31 ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 101324 | ▶ | if ( ( $addr & $mask ) == ( $sub & $mask ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 2447 | return 1; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 98877 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | sub default_ipv4_exclude() { | ||||||||||||||||||||||||||
|
Subroutine
default_ipv4_exclude: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 1 | return _get_ipv4_excludes( [] ); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 7 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||