File Coverage

blib/lib/FormValidator/Simple/Plugin/NetAddr/IP.pm
Criterion Covered Total %
statement 24 24 100.0
branch 11 12 91.6
condition 3 6 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package FormValidator::Simple::Plugin::NetAddr::IP;
2              
3 3     3   791462 use strict;
  3         7  
  3         117  
4 3     3   969 use NetAddr::IP;
  3         85039  
  3         28  
5 3     3   1293 use FormValidator::Simple::Constants;
  3         9  
  3         1432  
6              
7             our $VERSION = '0.01';
8             our @CARP_NOT = qw(NetAddr::IP);
9              
10             =head1 NAME
11              
12             FormValidator::Simple::Plugin::NetAddr::IP - IP Address validation
13              
14             =head1 SYNOPSIS
15              
16             use FormValidator::Simple qw/NetAddr::IP/;
17              
18             my $result = FormValidator::Simple->check( $req => [
19             ip => [ 'NOT_BLANK', 'NETADDR_IPV4HOST' ],
20             ] );
21              
22             =head1 DESCRIPTION
23              
24             This module adds IP Address validation commands to FormValidator::Simple.
25             It uses NetAddr::IP to do the validation. There are other modules that may
26             do IP Address validation with less overhead, but NetAddr::IP was already
27             being used in the project that this was written for.
28              
29             =head1 VALIDATION COMMANDS
30              
31             =over 4
32              
33             =item NETADDR_IP4HOST
34              
35             Checks for a single IPv4 address. Address supplied must be in dotted
36             quad or CIDR format. Does not accept DNS names.
37              
38             =cut
39              
40             sub NETADDR_IP4HOST {
41 26     26 1 38566 my ($self, $params, $args) = @_;
42 26         44 my $data = $params->[0];
43              
44 26         73 my $ip = $self->_getaddr($data);
45              
46 26 100       134 return FALSE unless ( ref($ip) eq 'NetAddr::IP' );
47 12 100 66     47 return ( $ip->version == 4 && $ip->masklen == 32 ) ? TRUE : FALSE;
48             }
49              
50             =item NETADDR_IP4NET
51              
52             Checks for a IPv4 network block. Address supplied must be in dotted
53             quad or CIDR format. Does not accept DNS names. A /32 is accepted
54             as a network.
55              
56             =cut
57              
58             sub NETADDR_IP4NET {
59 26     26 1 40094 my ($self, $params, $args) = @_;
60 26         51 my $data = $params->[0];
61              
62 26         72 my $ip = $self->_getaddr($data);
63              
64 26 100       143 return FALSE unless ( ref($ip) eq 'NetAddr::IP' );
65 12 50 33     45 return ( $ip->version == 4 && $ip->masklen <= 32 ) ? TRUE : FALSE;
66             }
67              
68             sub _getaddr {
69 52     52   73 my ($self, $data) = @_;
70              
71             # Do not allow DNS resolution or partial addresses
72             # even though NetAddr would do it.
73             # Speeds things up quite a bit.
74 52 100       412 return FALSE if $data =~ qr([\\a-zA-Z]);
75 48 100       501 return FALSE unless $data =~ qr(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3});
76              
77 30         177 my $ip = NetAddr::IP->new($data);
78              
79 30         3242 return ($ip);
80             }
81              
82             1;
83             #__END__
84             # Below is stub documentation for your module. You'd better edit it!
85              
86             =back
87              
88             =head1 SEE ALSO
89              
90             L
91              
92             L
93              
94             L for which this module was needed.
95              
96             =head1 AUTHOR
97              
98             Eric Hacker Ehacker at cpan.orgE
99              
100             =head1 BUGS
101              
102             None known at this time.
103              
104             =head1 LICENSE
105              
106             Copyright (c) 2007, Alcatel Lucent, All rights resevred.
107              
108             This package is free software; you may redistribute it
109             and/or modify it under the same terms as Perl itself.
110              
111             =cut