Match/RegexPTR.pm

filter: Statementstmt 78.8% 56 / 71 Branchbran 66.6% 24 / 36 Conditioncond 47.6% 10 / 21 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 69.6% 101 / 145 CC 31 SCAR 32.2
CC
31
Cov
70%
match
36.4
new
17.4
BEGIN
1.0
CRAP 25.1
« Match/Ports.pm ↑ Summary Match/PTR.pm »
1 package Net::Connection::Match::RegexPTR;
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::RegexPTR - Runs a PTR check against a Net::Connection object using regular expressions.
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::RegexPTR;
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',
45 ],
46 lptrs=>[
47 'bar',
48 ],
49 fptrs=>[
50 'f[oa]o',
51 ],
52 );
53
54 my $checker=Net::Connection::Match::RegexPTR->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::RegexPTR->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=14 SCAR=28.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 lptrs => [],
127 fptrs => [],
128 resolver => Net::DNS::Resolver->new,
129 };
130 3 bless $self;
131  
132 3 ▶ if ( defined( $args{ptrs}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ptrs"}[0]) 1 2 3
133 1 $self->{ptrs} = $args{ptrs};
134 }
135 3 ▶ if ( defined( $args{lptrs}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"lptrs"}[0]) 1 2 3
136 1 $self->{lptrs} = $args{lptrs};
137 }
138 3 ▶ if ( defined( $args{fptrs}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"fptrs"}[0]) 1 2 3
139 1 $self->{fptrs} = $args{fptrs};
140 }
141  
142 3 return $self;
143 } ## end sub new
144  
145 =head2 match
146  
147 Checks if a single Net::Connection object matches the stack.
148  
149 One argument is taken and that is a Net::Connection object.
150  
151 The returned value is a boolean.
152  
153 If the *_ptr feilds for the object are undef, L<Net::DNS::Resolver>
154 will be used for resolving the address.
155  
156 if ( $checker->match( $conn ) ){
157 print "The connection matches.\n";
158 }
159  
160 =cut
161  
162 8 ▶sub match {
Subroutine match: called CC=18 SCAR=36.0
Pod documented
163 8 my $self = $_[0];
164 8 my $object = $_[1];
165  
166 8 ▶ if ( !defined($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 7 8
167 1 return 0;
168 }
169  
170 7 ▶ if ( ref($object) ne 'Net::Connection' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Net::Connection") 1 6 7
171 1 return 0;
172 }
173  
174 6 my $l_ptr = $object->local_ptr;
175 6 my $f_ptr = $object->foreign_ptr;
176  
177 6 ▶ if ( defined($l_ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l_ptr) { } 6 0 6
178 # If we have one, convert it to lower case for easier processing.
179 6 $l_ptr = lc($l_ptr);
180 } else {
181 # We don't have it. Uppercase default will prevent it from being matched.
182 0 $l_ptr = 'NOTFOUND';
183 # See if we can look it up.
184 0 my $answer = $self->{resolver}->search( $object->local_host );
185 0 ▶ if ( defined($answer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer) 0 0 0
186 # search the whole answer section as the first RR may be a CNAME
187 0 foreach my $rr ( $answer->answer ) {
188 0 ▶ if ( $rr->type eq 'PTR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 0 0 0
189 0 $l_ptr = lc( $rr->ptrdname );
190 0 last;
191 }
192 }
193 }
194 } ## end else [ if ( defined($l_ptr) ) ]
195  
196 6 ▶ if ( defined($f_ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f_ptr) { } 6 0 6
197 # If we have one, convert it to lower case for easier processing.
198 6 $f_ptr = lc($f_ptr);
199 } else {
200 # We don't have it. Uppercase default will prevent it from being matched.
201 0 $f_ptr = 'NOTFOUND';
202 # See if we can look it up.
203 0 my $answer = $self->{resolver}->search( $object->foreign_host );
204 0 ▶ if ( defined($answer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer) 0 0 0
205 # search the whole answer section as the first RR may be a CNAME
206 0 foreach my $rr ( $answer->answer ) {
207 0 ▶ if ( $rr->type eq 'PTR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "PTR") 0 0 0
208 0 $f_ptr = lc( $rr->ptrdname );
209 0 last;
210 }
211 }
212 }
213 } ## end else [ if ( defined($f_ptr) ) ]
214  
215 6 foreach my $regex ( @{ $self->{ptrs} } ) {
216 2 ▶ if ( $l_ptr =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l_ptr =~ /$regex/) 1 1 2
217 1 return 1;
218 }
219 1 ▶ if ( $f_ptr =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_ptr =~ /$regex/) 1 0 1
220 1 return 1;
221 }
222 }
223 4 foreach my $regex ( @{ $self->{lptrs} } ) {
224 2 ▶ if ( $l_ptr =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l_ptr =~ /$regex/) 1 1 2
225 1 return 1;
226 }
227 }
228 3 foreach my $regex ( @{ $self->{fptrs} } ) {
229 2 ▶ if ( $f_ptr =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_ptr =~ /$regex/) 1 1 2
230 1 return 1;
231 }
232 }
233  
234 2 return 0;
235 } ## end sub match
236  
237 =head1 AUTHOR
238  
239 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
240  
241 =head1 BUGS
242  
243 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
244 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
245 automatically be notified of progress on your bug as I make changes.
246  
247  
248  
249  
250 =head1 SUPPORT
251  
252 You can find documentation for this module with the perldoc command.
253  
254 perldoc Net::Connection::Match
255  
256  
257 You can also look for information at:
258  
259 =over 4
260  
261 =item * RT: CPAN's request tracker (report bugs here)
262  
263 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
264  
265 =item * AnnoCPAN: Annotated CPAN documentation
266  
267 L<http://annocpan.org/dist/Net-Connection-Match>
268  
269 =item * CPAN Ratings
270  
271 L<https://cpanratings.perl.org/d/Net-Connection-Match>
272  
273 =item * Search CPAN
274  
275 L<https://metacpan.org/release/Net-Connection-Match>
276  
277 =back
278  
279  
280 =head1 ACKNOWLEDGEMENTS
281  
282  
283 =head1 LICENSE AND COPYRIGHT
284  
285 Copyright 2019 Zane C. Bowers-Hadley.
286  
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of the the Artistic License (2.0). You may obtain a
289 copy of the full license at:
290  
291 L<http://www.perlfoundation.org/artistic_license_2_0>
292  
293 Any use, modification, and distribution of the Standard or Modified
294 Versions is governed by this Artistic License. By using, modifying or
295 distributing the Package, you accept this license. Do not use, modify,
296 or distribute the Package, if you do not accept this license.
297  
298 If your Modified Version has been derived from a Modified Version made
299 by someone other than you, you are nevertheless required to ensure that
300 your Modified Version complies with the requirements of this license.
301  
302 This license does not grant you the right to use any trademark, service
303 mark, tradename, or logo of the Copyright Holder.
304  
305 This license includes the non-exclusive, worldwide, free-of-charge
306 patent license to make, have made, use, offer to sell, sell, import and
307 otherwise transfer the Package with respect to any patent claims
308 licensable by the Copyright Holder that are necessarily infringed by the
309 Package. If you institute patent litigation (including a cross-claim or
310 counterclaim) against any party alleging that the Package constitutes
311 direct or contributory patent infringement, then this Artistic License
312 to you shall terminate on the date that such litigation is filed.
313  
314 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
315 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
316 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
317 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
318 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
319 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
320 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
321 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322  
323  
324 =cut
325  
326 2 1; # End of Net::Connection::Match
« Match/Ports.pm ↑ Summary Match/PTR.pm »