File Coverage

blib/lib/Net/IP/Match.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1 1     1   23659 use 5.008;
  1         4  
  1         38  
2 1     1   6 use strict;
  1         2  
  1         32  
3 1     1   4 use warnings;
  1         2  
  1         56  
4              
5             package Net::IP::Match;
6             BEGIN {
7 1     1   33 $Net::IP::Match::VERSION = '1.101700';
8             }
9              
10             # ABSTRACT: Efficiently match IP addresses against IP ranges
11 1     1   1113 use Filter::Simple;
  1         31456  
  1         9  
12             FILTER sub {
13             s[\b __MATCH_IP \s* \( (.*?) \s* , \s* (.*?) \s* \) ]
14             [
15             my @n = eval $2;
16             my @t;
17             for (@n) {
18             my ($quad, $bits) = m!^(\d+\.\d+\.\d+\.\d+)(?:/(\d+))?!g;
19             my $matchbits = 32 - ($bits || 32);
20             my $int = unpack("N", pack("C4", split(/\./, $quad)));
21             my $mask = $int >> $matchbits;
22             push @t => { mask => $mask, bits => $matchbits };
23             }
24              
25             my $unpack_code = qq!unpack("N", pack("C4", split(/\\./, $1)))!;
26              
27             # if there's only one ip range to match against, we don't need
28             # the temp variable and the do-block, so it's even faster
29              
30             if (@t == 1) {
31             local $_ = shift @t;
32             "($_->{mask} == $unpack_code" .
33             ($_->{bits} ? " >> $_->{bits}" : "") . ")"
34             } else {
35             my $var = '$__tmp_match_ip';
36             my $cond = join ' || ' => map { "$_->{mask} == $var" .
37             ($_->{bits} ? " >> $_->{bits}" : "") } @t;
38             qq!do { my $var = $unpack_code; $cond }!
39             }
40              
41             ]gsex;
42             print if $::debug;
43             };
44             1;
45              
46              
47             __END__