| 1 | package App::Tel::HostRange; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | require Exporter; | |||||||||||||||||||||||||||
| 8 | 8 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | 8 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 10 | 8 | our @EXPORT_OK = qw ( check_hostrange ); | |||||||||||||||||||||||||||
| 11 | 8 | our $_have_netaddr; # can't set a default because this happens after the BEGIN block | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # needed because CPAN won't index undef since it's a lower version number | ||||||||||||||||||||||||||||
| 14 | 8 | our $VERSION = '0.201503'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 17 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 18 | 8 | ▶ | if (eval { Module::Load::load NetAddr::IP; 1; }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 8 | $_have_netaddr=1; | |||||||||||||||||||||||||||
| 20 | } else { | ||||||||||||||||||||||||||||
| 21 | 0 | $_have_netaddr=0; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | App::Tel::HostRange - Support for HostRanges | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | if (check_hostrange($_, $host)); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Searches an IPv4 or IPv6 range to see if it contains a particular IP address. | ||||||||||||||||||||||||||||
| 34 | Returns true if the host is contained in the range, false if it is not. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Robert Drake, C<< <rdrake at cpan.org> >> | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Copyright 2015 Robert Drake, all rights reserved. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 45 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 check_hostrange | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | if (check_hostrange($rangelist, $host)); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Searches an IPv4 or IPv6 range to see if it contains a particular IP address. | ||||||||||||||||||||||||||||
| 54 | Returns true if the host is contained in the range, false if it is not. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | This does no validation, leaving it all up to NetAddr:IP and the calling | ||||||||||||||||||||||||||||
| 57 | function. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | This should support the following types of ranges: | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # 1. 192.168.13.17-192.168.32.128 | ||||||||||||||||||||||||||||
| 62 | # 2. 192.168.13.17-22 | ||||||||||||||||||||||||||||
| 63 | # 3. fe80::1-fe80::256 | ||||||||||||||||||||||||||||
| 64 | # 4. 192.168.13.0/24 | ||||||||||||||||||||||||||||
| 65 | # 5. fe80::/64 | ||||||||||||||||||||||||||||
| 66 | # 6. 192.168.13.17-192.168.32.128,172.16.0.2-172.16.0.13,172.28.0.0/24 | ||||||||||||||||||||||||||||
| 67 | # 7. 192.168.13.12 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 22 | ▶ | sub check_hostrange { | ||||||||||||||||||||||||||
|
Subroutine
check_hostrange: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 22 | my ($rangelist, $host) = @_; | |||||||||||||||||||||||||||
| 74 | 22 | ▶ | return 0 if (!$_have_netaddr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 22 | ▶ | $host = NetAddr::IP->new($host) || return 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
'NetAddr::IP'->new($host) || (return 0)
MC/DC100%1/1
'NetAddr::IP'->new($host) || (return 0)
'NetAddr::IP'->new($host)
Truth table100%2/2
A
A= 'NetAddr::IP'->new($host)
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 21 | for(split(/,/,$rangelist)) { | |||||||||||||||||||||||||||
| 78 | # if it's a cidr pass it into NetAddr::IP directly | ||||||||||||||||||||||||||||
| 79 | 30 | ▶ | if ($_ =~ qr#/#) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 13 | ▶ | my $range = NetAddr::IP->new($_) || return 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
'NetAddr::IP'->new($_) || (return 0)
MC/DC0%0/1
'NetAddr::IP'->new($_) || (return 0)
'NetAddr::IP'->new($_)
Truth table50%1/2
A
A= 'NetAddr::IP'->new($_)
| |||||||||||||||||||||||||||||
| 81 | 13 | ▶ | return 1 if ($range->contains($host)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | 17 | my ($host1, $host2) = split(/-/); | |||||||||||||||||||||||||||
| 84 | 17 | ▶ | $host1 = NetAddr::IP->new($host1) || return 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
'NetAddr::IP'->new($host1) || (return 0)
MC/DC0%0/1
'NetAddr::IP'->new($host1) || (return 0)
'NetAddr::IP'->new($host1)
Truth table50%1/2
A
A= 'NetAddr::IP'->new($host1)
| |||||||||||||||||||||||||||||
| 85 | # if it's a single IP, like #7 | ||||||||||||||||||||||||||||
| 86 | 17 | ▶ | if (!defined($host2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | return $host == $host1 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | # if they only supplied the last octet like #2 | ||||||||||||||||||||||||||||
| 90 | 15 | ▶ | if ($host2 =~ /^[\da-f]+$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 4 | my $tmp = $host1->addr; | |||||||||||||||||||||||||||
| 92 | # drop the last octet | ||||||||||||||||||||||||||||
| 93 | 4 | $tmp =~ s/([:\.])[\da-f]+$/$1/; | |||||||||||||||||||||||||||
| 94 | 4 | $host2 = $tmp . $host2; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 15 | ▶ | $host2 = NetAddr::IP->new($host2) || return 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
'NetAddr::IP'->new($host2) || (return 0)
MC/DC0%0/1
'NetAddr::IP'->new($host2) || (return 0)
'NetAddr::IP'->new($host2)
Truth table50%1/2
A
A= 'NetAddr::IP'->new($host2)
| |||||||||||||||||||||||||||||
| 97 | 15 | ▶ | return 1 if ($host >= $host1 && $host <= $host2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$host >= $host1 and $host <= $host2
MC/DC100%2/2
$host >= $host1 and $host <= $host2
$host >= $host1
$host <= $host2
Truth table100%3/3
A and B
A= $host >= $host1B= $host <= $host2
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 9 | return 0; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 8 | 1; | |||||||||||||||||||||||||||