Match/PTR.pm

filter: Statementstmt 77.9% 53 / 68 Branchbran 69.4% 25 / 36 Conditioncond 63.3% 19 / 30 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 72.2% 112 / 155 CC 31 SCAR 31.3
CC
31
Cov
72%
match
32.7
new
19.3
BEGIN
1.0
CRAP 22.9
« Match/RegexPTR.pm ↑ Summary Match.pm »
1 package Net::Connection::Match::PTR;
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::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Connection::Match::PTR - Runs a PTR check against a Net::Connection object.
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 2 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Match::PTR;
23 use Net::Connection;
24
25 # The *_ptr feilds do not need populated.
26 # If left undef, they will be resulved using Net::DNS::Resolver
27 my $connection_args={
28 foreign_host=>'10.0.0.1',
29 foreign_port=>'22',
30 foreign_ptr=>'foo.foo',
31 local_host=>'10.0.0.2',
32 local_port=>'12322',
33 local_ptr=>'foo.bar',
34 proto=>'tcp4',
35 state=>'ESTABLISHED',
36 };
37
38 my $conn=Net::Connection->new( $connection_args );
39
40 # All three don't need specified, but
41 # Atleast one of them must be and must not be a empty array.
42 my %args=(
43 ptrs=>[
44 'foo.bar',
45 ],
46 lptrs=>[
47 'a.foo.bar',
48 ],
49 fptrs=>[
50 'b.foo.bar',
51 ],
52 );
53
54 my $checker=Net::Connection::Match::PTR->new( \%args );
55
56 if ( $checker->match( $conn ) ){
57 print "It matches.\n";
58 }
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 This intiates the object.
65  
66 my %args=(
67 ptrs=>[
68 'foo.bar',
69 ],
70 lptrs=>[
71 'a.foo.bar',
72 ],
73 fptrs=>[
74 'b.foo.bar',
75 ],
76 );
77
78 my $checker=Net::Connection::Match::PTR->new( \%args );
79  
80  
81 =head3 args
82  
83 Atleast one of the following need used.
84  
85 =head4 ptrs
86  
87 This is a array of PTRs to match in for either foreign
88 or local side.
89  
90 =head4 fptrs
91  
92 This is a array of PTRs to match in for the foreign side.
93  
94 =head4 lptrs
95  
96 This is a array of PTRs to match in for the local side.
97  
98 =cut
99  
100 4 ▶sub new {
Subroutine new: called CC=17 SCAR=29.6
Pod documented
101 4 my %args;
102 4 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 3 1 4
103 3 %args = %{ $_[1] };
104 }
105  
106 # run some basic checks to make sure we have the minimum stuff required to work
107 4 ▶ if ( ( !defined( $args{ptrs} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"ptrs"} or defined $args{"fptrs"} or defined $args{"lptrs"}) 1 3 4
Condition100%6/6
defined $args{"ptrs"} or defined $args{"fptrs"} or defined $args{"lptrs"}
l!l&&r!l&&!r
211
defined $args{"ptrs"} or defined $args{"fptrs"}
l!l&&r!l&&!r
112
MC/DC100%3/3
defined $args{"ptrs"} or defined $args{"fptrs"} or defined $args{"lptrs"}
defined $args{"ptrs"} defined $args{"fptrs"} defined $args{"lptrs"}
Truth table100%4/4
A or B or C
A= defined $args{"ptrs"}B= defined $args{"fptrs"}C= defined $args{"lptrs"}
ABCresult
1XX1
01X1
0011
0000
108 && ( !defined( $args{fptrs} ) )
109 && ( !defined( $args{lptrs} ) ) )
110 {
111 1 die('No [fl]ptrs key specified in the argument hash');
112 }
113 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ptrs"} and not defined $args{"ptrs"}[0] and (defined $args{"lptrs"} and not defined $args{"lptrs"}[0]) and (defined $args{"fptrs"} and not defined $args{"fptrs"}[0])) 0 3 3
Condition26%4/15
defined $args{"ptrs"} and not defined $args{"ptrs"}[0] and (defined $args{"lptrs"} and not defined $args{"lptrs"}[0]) and (defined $args{"fptrs"} and not defined $args{"fptrs"}[0])
!ll&&!rl&&r
300
defined $args{"ptrs"} and not defined $args{"ptrs"}[0] and (defined $args{"lptrs"} and not defined $args{"lptrs"}[0])
!ll&&!rl&&r
300
defined $args{"ptrs"} and not defined $args{"ptrs"}[0]
!ll&&!rl&&r
210
defined $args{"lptrs"} and not defined $args{"lptrs"}[0]
!ll&&!rl&&r
000
defined $args{"fptrs"} and not defined $args{"fptrs"}[0]
!ll&&!rl&&r
000
MC/DC0%0/6
defined $args{"ptrs"} and not defined $args{"ptrs"}[0] and (defined $args{"lptrs"} and not defined $args{"lptrs"}[0]) and (defined $args{"fptrs"} and not defined $args{"fptrs"}[0])
defined $args{"ptrs"} not defined $args{"ptrs"}[0] defined $args{"lptrs"} not defined $args{"lptrs"}[0] defined $args{"fptrs"} not defined $args{"fptrs"}[0]
Truth table28%2/7
A and not(B) and C and not(D) and E and not(F)
A= defined $args{"ptrs"}B= not defined $args{"ptrs"}[0]C= defined $args{"lptrs"}D= not defined $args{"lptrs"}[0]E= defined $args{"fptrs"}F= not defined $args{"fptrs"}[0]
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
114 ( defined( $args{ptrs} ) && ( !defined( $args{ptrs}[0] ) ) )
115 && ( defined( $args{lptrs} )
116 && ( !defined( $args{lptrs}[0] ) ) )
117 && ( defined( $args{fptrs} )
118 && ( !defined( $args{fptrs}[0] ) ) )
119 )
120 {
121 0 die('No PTRs defined in any of the [fl]ptrs arrays');
122 } ## end if ( ( defined( $args{ptrs} ) && ( !defined...)))
123  
124 3 my $self = {
125 ptrs => {},
126 fptrs => {},
127 lptrs => {},
128 resolver => Net::DNS::Resolver->new,
129 };
130 3 bless $self;
131  
132 ##
133 ## These are all stored as lower case to make matching easier.
134 ##
135  
136 # Process the PTRs for matching either
137 3 my $ptrs_int = 0;
138 3 ▶ if ( defined( $args{ptrs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ptrs"}) 1 2 3
139 1 ▶ while ( defined( $args{ptrs}[$ptrs_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ptrs"}[$ptrs_int]) 1 1 2
140 1 $self->{ptrs}{ lc( $args{ptrs}[$ptrs_int] ) } = lc( $args{ptrs}[$ptrs_int] );
141  
142 1 $ptrs_int++;
143 }
144 }
145  
146 # Process the PTRs for matching the local side
147 3 $ptrs_int = 0;
148 3 ▶ if ( defined( $args{lptrs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"lptrs"}) 1 2 3
149 1 ▶ while ( defined( $args{lptrs}[$ptrs_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"lptrs"}[$ptrs_int]) 1 1 2
150 1 $self->{lptrs}{ lc( $args{lptrs}[$ptrs_int] ) } = lc( $args{lptrs}[$ptrs_int] );
151  
152 1 $ptrs_int++;
153 }
154 }
155  
156 # Process the PTRs for matching the foreign side
157 3 $ptrs_int = 0;
158 3 ▶ if ( defined( $args{fptrs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"fptrs"}) 1 2 3
159 1 ▶ while ( defined( $args{fptrs}[$ptrs_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"fptrs"}[$ptrs_int]) 1 1 2
160 1 $self->{fptrs}{ lc( $args{fptrs}[$ptrs_int] ) } = lc( $args{fptrs}[$ptrs_int] );
161  
162 1 $ptrs_int++;
163 }
164 }
165  
166 3 return $self;
167 } ## end sub new
168  
169 =head2 match
170  
171 Checks if a single Net::Connection object matches the stack.
172  
173 One argument is taken and that is a Net::Connection object.
174  
175 The returned value is a boolean.
176  
177 If the *_ptr feilds for the object are undef, L<Net::DNS::Resolver>
178 will be used for resolving the address.
179  
180 if ( $checker->match( $conn ) ){
181 print "The connection matches.\n";
182 }
183  
184 =cut
185  
186 8 ▶sub match {
Subroutine match: called CC=15 SCAR=34.9
Pod documented
187 8 my $self = $_[0];
188 8 my $object = $_[1];
189  
190 8 ▶ if ( !defined($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 7 8
191 1 return 0;
192 }
193  
194 7 ▶ if ( ref($object) ne 'Net::Connection' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Net::Connection") 1 6 7
195 1 return 0;
196 }
197  
198 6 my $l_ptr = $object->local_ptr;
199 6 my $f_ptr = $object->foreign_ptr;
200  
201 6 ▶ if ( defined($l_ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l_ptr) { } 6 0 6
202 # If we have one, convert it to lower case for easier processing.
203 6 $l_ptr = lc($l_ptr);
204 } else {
205 # We don't have it. Uppercase default will prevent it from being matched.
206 0 $l_ptr = 'NOTFOUND';
207 # See if we can look it up.
208 0 my $answer = $self->{resolver}->search( $object->local_host );
209 0 ▶ if ( defined($answer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer) 0 0 0
210 # search the whole answer section as the first RR may be a CNAME
211 0 foreach my $rr ( $answer->answer ) {
212 0 ▶ if ( $rr->type eq 'PTR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 0 0 0
213 0 $l_ptr = lc( $rr->ptrdname );
214 0 last;
215 }
216 }
217 }
218 } ## end else [ if ( defined($l_ptr) ) ]
219  
220 6 ▶ if ( defined($f_ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f_ptr) { } 6 0 6
221 # If we have one, convert it to lower case for easier processing.
222 6 $f_ptr = lc($f_ptr);
223 } else {
224 # We don't have it. Uppercase default will prevent it from being matched.
225 0 $f_ptr = 'NOTFOUND';
226 # See if we can look it up.
227 0 my $answer = $self->{resolver}->search( $object->foreign_host );
228 0 ▶ if ( defined($answer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer) 0 0 0
229 # search the whole answer section as the first RR may be a CNAME
230 0 foreach my $rr ( $answer->answer ) {
231 0 ▶ if ( $rr->type eq 'PTR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 0 0 0
232 0 $f_ptr = lc( $rr->ptrdname );
233 0 last;
234 }
235 }
236 }
237 } ## end else [ if ( defined($f_ptr) ) ]
238  
239 # If we matched exactly, we found it.
240 6 ▶ if ( defined( $self->{ptrs}{$l_ptr} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ptrs"}{$l_ptr} or defined $self->{"ptrs"}{$f_ptr} or defined $self->{"lptrs"}{$l_ptr} or defined $self->{"fptrs"}{$f_ptr}) 4 2 6
Condition100%9/9
defined $self->{"ptrs"}{$l_ptr} or defined $self->{"ptrs"}{$f_ptr} or defined $self->{"lptrs"}{$l_ptr} or defined $self->{"fptrs"}{$f_ptr}
l!l&&r!l&&!r
312
defined $self->{"ptrs"}{$l_ptr} or defined $self->{"ptrs"}{$f_ptr} or defined $self->{"lptrs"}{$l_ptr}
l!l&&r!l&&!r
213
defined $self->{"ptrs"}{$l_ptr} or defined $self->{"ptrs"}{$f_ptr}
l!l&&r!l&&!r
114
MC/DC100%4/4
defined $self->{"ptrs"}{$l_ptr} or defined $self->{"ptrs"}{$f_ptr} or defined $self->{"lptrs"}{$l_ptr} or defined $self->{"fptrs"}{$f_ptr}
defined $self->{"ptrs"}{$l_ptr} defined $self->{"ptrs"}{$f_ptr} defined $self->{"lptrs"}{$l_ptr} defined $self->{"fptrs"}{$f_ptr}
Truth table100%5/5
A or B or C or D
A= defined $self->{"ptrs"}{$l_ptr}B= defined $self->{"ptrs"}{$f_ptr}C= defined $self->{"lptrs"}{$l_ptr}D= defined $self->{"fptrs"}{$f_ptr}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
241 || defined( $self->{ptrs}{$f_ptr} )
242 || defined( $self->{lptrs}{$l_ptr} )
243 || defined( $self->{fptrs}{$f_ptr} ) )
244 {
245 4 return 1;
246 }
247  
248 2 return 0;
249 } ## end sub match
250  
251 =head1 AUTHOR
252  
253 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
254  
255 =head1 BUGS
256  
257 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
258 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
259 automatically be notified of progress on your bug as I make changes.
260  
261  
262  
263  
264 =head1 SUPPORT
265  
266 You can find documentation for this module with the perldoc command.
267  
268 perldoc Net::Connection::Match
269  
270  
271 You can also look for information at:
272  
273 =over 4
274  
275 =item * RT: CPAN's request tracker (report bugs here)
276  
277 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
278  
279 =item * AnnoCPAN: Annotated CPAN documentation
280  
281 L<http://annocpan.org/dist/Net-Connection-Match>
282  
283 =item * CPAN Ratings
284  
285 L<https://cpanratings.perl.org/d/Net-Connection-Match>
286  
287 =item * Search CPAN
288  
289 L<https://metacpan.org/release/Net-Connection-Match>
290  
291 =back
292  
293  
294 =head1 ACKNOWLEDGEMENTS
295  
296  
297 =head1 LICENSE AND COPYRIGHT
298  
299 Copyright 2019 Zane C. Bowers-Hadley.
300  
301 This program is free software; you can redistribute it and/or modify it
302 under the terms of the the Artistic License (2.0). You may obtain a
303 copy of the full license at:
304  
305 L<http://www.perlfoundation.org/artistic_license_2_0>
306  
307 Any use, modification, and distribution of the Standard or Modified
308 Versions is governed by this Artistic License. By using, modifying or
309 distributing the Package, you accept this license. Do not use, modify,
310 or distribute the Package, if you do not accept this license.
311  
312 If your Modified Version has been derived from a Modified Version made
313 by someone other than you, you are nevertheless required to ensure that
314 your Modified Version complies with the requirements of this license.
315  
316 This license does not grant you the right to use any trademark, service
317 mark, tradename, or logo of the Copyright Holder.
318  
319 This license includes the non-exclusive, worldwide, free-of-charge
320 patent license to make, have made, use, offer to sell, sell, import and
321 otherwise transfer the Package with respect to any patent claims
322 licensable by the Copyright Holder that are necessarily infringed by the
323 Package. If you institute patent litigation (including a cross-claim or
324 counterclaim) against any party alleging that the Package constitutes
325 direct or contributory patent infringement, then this Artistic License
326 to you shall terminate on the date that such litigation is filed.
327  
328 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
329 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
330 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
331 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
332 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
333 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
334 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
335 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
336  
337  
338 =cut
339  
340 2 1; # End of Net::Connection::Match
« Match/RegexPTR.pm ↑ Summary Match.pm »