Match/CIDR.pm

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