File Coverage

blib/lib/Net/IP/Resolver.pm
Criterion Covered Total %
statement 29 29 100.0
branch 7 10 70.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 46 49 93.8


line stmt bran cond sub pod time code
1             package Net::IP::Resolver;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Net::IP::Resolver - Resolve IPs to a particular network
8              
9             =head1 SYNOPSIS
10              
11             # Create the resolver and add some networks
12             my $resolver = Net::IP::Resolve->new;
13             $resolver->add( 'Comcast' => '123.0.0.0/8', '124.128.0.0/10' );
14             $resolver->add( 'Foobar' => [ '1.2.3.0/24', '1.2.4.0/24' ] );
15            
16             # Check an IP
17             my $ip = '123.123.123.123';
18             my $network = $resolver->find_first( $ip );
19             print "IP $ip is in network $network";
20            
21             # prints... "IP 123.123.123.123 is in network Comcast";
22              
23             =head1 DESCRIPTION
24              
25             C provides a mechanism for registering a number of
26             different networks (specified by a set of ip ranges), and then finding
27             the network for a given IP based on this specification.
28              
29             The identifier for a network can be any defined value that you wish.
30              
31             Thus you can resolve to numeric identifiers, names, or even to objects
32             representing the networks.
33              
34             =head1 METHODS
35              
36             =cut
37              
38 2     2   25810 use strict;
  2         6  
  2         75  
39 2     2   1958 use Net::IP::Match::XS ();
  2         1464  
  2         58  
40              
41 2     2   24 use vars qw{$VERSION};
  2         4  
  2         124  
42             BEGIN {
43 2     2   578 $VERSION = '0.02';
44             }
45              
46              
47              
48              
49              
50             #####################################################################
51             # Constructor and Accessors
52              
53             =pod
54              
55             =head2 new
56              
57             The C constructor takes no arguments, and create a new and empty
58             resolver.
59              
60             Returns a new C object.
61              
62             =cut
63              
64             sub new {
65 1 50   1 1 14 my $class = ref $_[0] ? ref shift : shift;
66              
67 1         6 my $self = bless {
68             networks => [],
69             results => [],
70             }, $class;
71              
72 1         3 $self;
73             }
74              
75              
76              
77              
78              
79             #####################################################################
80             # Net::IP::Resolver Interface
81              
82             =pod
83              
84             =head2 add $network, $range, ...
85              
86             The C method adds a network to the resolver. It takes as argument
87             an identifier for the network, which can be C defined value, including
88             an object of any type, followed by a set of 1 or more IP ranges, in the
89             format used by L (which this class uses for the actual
90             ip matching).
91              
92             Returns true if the network was added, or C if passed incorrect
93             arguments.
94              
95             =cut
96              
97             sub add {
98 2     2 1 805 my $self = shift;
99 2 50       8 my $result = defined $_[0] ? shift : return undef;
100 2 50       10 my $network = @_ ? [ @_ ] : return undef;
101              
102             # Add the result and ranges
103 2         4 push @{$self->{networks}}, $network;
  2         9  
104 2         4 push @{$self->{results}}, $result;
  2         5  
105              
106 2         13 1;
107             }
108              
109             =pod
110              
111             =head2 find_first $ip
112              
113             The C method takes an IP address as argument, and checks
114             it against each network to find the first one that matches.
115              
116             The assumption made by C is that each network in the resolver
117             occupies a unique and non-overlapping set of ranges, and thus only any ip
118             can only ever resolve to one network
119              
120             Returns the network identifier as originally provided, or C if the
121             ip is not provided, or the resolver cannot match it to any network.
122              
123             =cut
124              
125             sub find_first {
126 5     5 1 12 my $self = shift;
127 5 100       17 my $ip = defined $_[0] ? shift : return undef;
128              
129 4         6 foreach my $i ( 0 .. $#{ $self->{networks} } ) {
  4         16  
130 6         11 my $network = $self->{networks}->[$i];
131 6 100       25 if ( Net::IP::Match::XS::match_ip( $ip, @$network ) ) {
132 3         17 return $self->{results}->[$i];
133             }
134             }
135              
136 1         5 return undef;
137             }
138              
139             1;
140              
141             =pod
142              
143             =head1 SUPPORT
144              
145             All bugs should be filed via the bug tracker at
146              
147             L
148              
149             For other issues, or commercial enhancement and support, contact the author
150              
151             =head1 AUTHORS
152              
153             Adam Kennedy Eadamk@cpan.orgE
154              
155             =head1 SEE ALSO
156              
157             L, L, L
158              
159             =head1 COPYRIGHT
160              
161             Copyright 2005 - 2006 Adam Kennedy.
162              
163             This program is free software; you can redistribute
164             it and/or modify it under the same terms as Perl itself.
165              
166             The full text of the license can be found in the
167             LICENSE file included with this module.
168              
169             =cut