Match/CIDR.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 88 CC 10 SCAR 34.7
CC
10
Cov
0%
new
56.0
match
20.0
BEGIN
2.0
CRAP 32.0
1 package Net::Connection::Match::CIDR;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 use Net::CIDR;
7  
8 =head1 NAME
9  
10 Net::Connection::Match::CIDR - Runs a basic CIDR check against a Net::Connection object.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 our $VERSION = '0.0.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Net::Connection::Match::CIDR;
24 use Net::Connection;
25
26 my $connection_args={
27 foreign_host=>'10.0.0.1',
28 foreign_port=>'22',
29 local_host=>'10.0.0.2',
30 local_port=>'12322',
31 proto=>'tcp4',
32 state=>'ESTABLISHED',
33 };
34
35 my $conn=Net::Connection->new( $connection_args );
36
37 my %args=(
38 cidrs=>[
39 '127.0.0.0/24',
40 '192.168.0.0/16',
41 '10.0.0.0/8'
42 ],
43 );
44  
45 my $cidr_checker=Net::Connection::Match::CIDR->new( \%args );
46  
47 if ( $cidr_checker->match( $conn ) ){
48 print "It matches.\n";
49 }
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 This intiates the object.
56  
57 It takes a hash reference with one key. One key is required and
58 that is 'cidrs', which is a array of CIDRs to match against.
59  
60 Net::CIDR::cidrvalidate is used to validate the CIDRs.
61  
62 Atleast one CIDR must be present.
63  
64 If the new method fails, it dies.
65  
66 my %args=(
67 cidrs=>[
68 '127.0.0.0/24',
69 '192.168.0.0/16',
70 '10.0.0.0/8'
71 ],
72 );
73  
74 my $cidr_checker=Net::Connection::Match::CIDR->new( \%args );
75  
76 =cut
77  
78 sub new{
79 my %args;
80 if(defined($_[1])){
81 %args= %{$_[1]};
82 };
83  
84 # run some basic checks to make sure we have the minimum stuff required to work
85 if ( ! defined( $args{cidrs} ) ){
86 die ('No cidrs key specified in the argument hash');
87 }
88 if ( ref( \$args{cidrs} ) eq 'ARRAY' ){
89 die ('The cidrs key is not a array');
90 }
91 if ( ! defined $args{cidrs}[0] ){
92 die ('No CIDRs defined in the cidrs array');
93 }
94  
95 my $self = {
96 cidrs=>[],
97 };
98 bless $self;
99  
100 # make sure each cidr is valid before returning it
101 my $cidrs_int=0;
102 while( defined( $args{cidrs}[$cidrs_int] ) ){
103 my $cidr_good=0;
104 eval{
105 if ( Net::CIDR::cidrvalidate( $args{cidrs}[$cidrs_int] ) ){
106 $cidr_good=1;
107 }
108 };
109  
110 # if good add it, otherwise die
111 if ( $cidr_good ){
112 $self->{cidrs}[$cidrs_int]=$args{cidrs}[$cidrs_int];
113 }else{
114 die('"'.$args{cidrs}[$cidrs_int].'" is not a CIDR according to Net::CIDR::cidrvalidate');
115 }
116  
117 $cidrs_int++;
118 }
119  
120 return $self;
121 }
122  
123 =head2 match
124  
125 Checks if a single Net::Connection object matches the stack.
126  
127 One argument is taken and that is a Net::Connection object.
128  
129 The returned value is a boolean.
130  
131 if ( $cidr_checker->match( $conn ) ){
132 print "The connection matches.\n";
133 }
134  
135 =cut
136  
137 sub match{
138 my $self=$_[0];
139 my $object=$_[1];
140  
141 if ( !defined( $object ) ){
142 return 0;
143 }
144  
145 if ( ref( $object ) ne 'Net::Connection' ){
146 return 0;
147 }
148  
149 my $cidrs_int=0;
150 while( defined( $self->{cidrs}[$cidrs_int] ) ){
151 if (
152 (
153 ( $object->foreign_host ne '*' ) &&
154 ( eval{ Net::CIDR::cidrlookup( $object->foreign_host, $self->{cidrs}[$cidrs_int] ) })
155 ) ||
156 (
157 ( $object->local_host ne '*' ) &&
158 ( eval{ Net::CIDR::cidrlookup( $object->local_host, $self->{cidrs}[$cidrs_int] ) })
159 )
160 ){
161 return 1;
162 }
163  
164 $cidrs_int++;
165 }
166  
167 return 0;
168 }
169  
170 =head1 AUTHOR
171  
172 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
173  
174 =head1 BUGS
175  
176 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
177 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
178 automatically be notified of progress on your bug as I make changes.
179  
180  
181  
182  
183 =head1 SUPPORT
184  
185 You can find documentation for this module with the perldoc command.
186  
187 perldoc Net::Connection::Match
188  
189  
190 You can also look for information at:
191  
192 =over 4
193  
194 =item * RT: CPAN's request tracker (report bugs here)
195  
196 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
197  
198 =item * AnnoCPAN: Annotated CPAN documentation
199  
200 L<http://annocpan.org/dist/Net-Connection-Match>
201  
202 =item * CPAN Ratings
203  
204 L<https://cpanratings.perl.org/d/Net-Connection-Match>
205  
206 =item * Search CPAN
207  
208 L<https://metacpan.org/release/Net-Connection-Match>
209  
210 =back
211  
212  
213 =head1 ACKNOWLEDGEMENTS
214  
215  
216 =head1 LICENSE AND COPYRIGHT
217  
218 Copyright 2019 Zane C. Bowers-Hadley.
219  
220 This program is free software; you can redistribute it and/or modify it
221 under the terms of the the Artistic License (2.0). You may obtain a
222 copy of the full license at:
223  
224 L<http://www.perlfoundation.org/artistic_license_2_0>
225  
226 Any use, modification, and distribution of the Standard or Modified
227 Versions is governed by this Artistic License. By using, modifying or
228 distributing the Package, you accept this license. Do not use, modify,
229 or distribute the Package, if you do not accept this license.
230  
231 If your Modified Version has been derived from a Modified Version made
232 by someone other than you, you are nevertheless required to ensure that
233 your Modified Version complies with the requirements of this license.
234  
235 This license does not grant you the right to use any trademark, service
236 mark, tradename, or logo of the Copyright Holder.
237  
238 This license includes the non-exclusive, worldwide, free-of-charge
239 patent license to make, have made, use, offer to sell, sell, import and
240 otherwise transfer the Package with respect to any patent claims
241 licensable by the Copyright Holder that are necessarily infringed by the
242 Package. If you institute patent litigation (including a cross-claim or
243 counterclaim) against any party alleging that the Package constitutes
244 direct or contributory patent infringement, then this Artistic License
245 to you shall terminate on the date that such litigation is filed.
246  
247 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
248 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
249 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
250 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
251 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
252 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
253 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
254 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255  
256  
257 =cut
258  
259 1; # End of Net::Connection::Match