| 1 | package Net::IPAddress::Filter::IPFilterDat; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A fast IP address filter from ipfilter.dat | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '20121119.02'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Net::IPAddress::Filter 20121117; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use base qw( Net::IPAddress::Filter ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # 000.000.000.000 - 000.255.255.255 , 000 , invalid ip | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | RULE_REGEX => qr{ | |||||||||||||||||||||||||||||||||||||||||
| 19 | \A \s* | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # Start IP address | ||||||||||||||||||||||||||||||||||||||||||
| 21 | \s* - \s* | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # End IP address | ||||||||||||||||||||||||||||||||||||||||||
| 23 | \s* , \s* | ||||||||||||||||||||||||||||||||||||||||||
| 24 | (\d+) # Score | ||||||||||||||||||||||||||||||||||||||||||
| 25 | \s* , \s* | ||||||||||||||||||||||||||||||||||||||||||
| 26 | (.*?) # Label | ||||||||||||||||||||||||||||||||||||||||||
| 27 | \s* \z | ||||||||||||||||||||||||||||||||||||||||||
| 28 | }xms, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | sub load_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_file: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $file = shift || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | my $FH; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # A filehandle can be a GLOB ref, or a blessed ref to one of the IO:: | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # packages. reftype() handles boths cases. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | if ( ref($file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $file and builtin::reftype($file) eq "GLOB" || builtin::reftype(\$file) eq "GLOB"
builtin::reftype($file) eq "GLOB" or builtin::reftype(\$file) eq "GLOB"
MC/DC33%1/3
ref $file and builtin::reftype($file) eq "GLOB" || builtin::reftype(\$file) eq "GLOB"
ref $file
builtin::reftype($file) eq "GLOB"
builtin::reftype(\$file) eq "GLOB"
Truth table50%2/4
A and B or C
A= ref $fileB= builtin::reftype($file) eq "GLOB"C= builtin::reftype(\$file) eq "GLOB"
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | && ( Scalar::Util::reftype($file) eq 'GLOB' || Scalar::Util::reftype( \$file ) eq 'GLOB' ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $FH = $file; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | open $FH, '<', $file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | or die __PACKAGE__ . "::load_file() unable to open $file for reading: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | my $rules_added = 0; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | while ( my $line = <$FH> ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | $rules_added++ if $self->add_rule($line); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | close $FH; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | return $rules_added; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | sub add_rule { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_rule: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | my $rule = shift || return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return 0)
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | if ( my $data = _parse_rule($rule) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | $self->add_range_with_value( $data->{label}, $data->{start_ip}, $data->{end_ip} ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | sub _parse_rule { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_rule: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 13 | my $rule = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # 000.000.000.000 - 000.255.255.255 , 000 , invalid ip | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 13 | ▶ | if ( $rule =~ RULE_REGEX ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | start_ip => $1, | |||||||||||||||||||||||||||||||||||||||||
| 81 | end_ip => $2, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | score => $3, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | label => $4, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||||||||||||