Match/RegexPTR.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 134 CC 17 SCAR 44.3
CC
17
Cov
0%
match
132.0
new
56.0
BEGIN
2.0
CRAP 84.2
1 package Net::Connection::Match::RegexPTR;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 use Net::DNS;
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 our $VERSION = '0.0.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Net::Connection::Match::RegexPTR;
24 use Net::Connection;
25
26 # The *_ptr feilds do not need populated.
27 # If left undef, they will be resulved using Net::DNS::Resolver
28 my $connection_args={
29 foreign_host=>'10.0.0.1',
30 foreign_port=>'22',
31 foreign_ptr=>'foo.foo',
32 local_host=>'10.0.0.2',
33 local_port=>'12322',
34 local_ptr=>'foo.bar',
35 proto=>'tcp4',
36 state=>'ESTABLISHED',
37 };
38
39 my $conn=Net::Connection->new( $connection_args );
40
41 # All three don't need specified, but
42 # Atleast one of them must be and must not be a empty array.
43 my %args=(
44 ptrs=>[
45 'foo',
46 ],
47 lptrs=>[
48 'bar',
49 ],
50 fptrs=>[
51 'f[oa]o',
52 ],
53 );
54
55 my $checker=Net::Connection::Match::RegexPTR->new( \%args );
56
57 if ( $checker->match( $conn ) ){
58 print "It matches.\n";
59 }
60  
61 =head1 METHODS
62  
63 =head2 new
64  
65 This intiates the object.
66  
67 my %args=(
68 ptrs=>[
69 'foo.bar',
70 ],
71 lptrs=>[
72 'a.foo.bar',
73 ],
74 fptrs=>[
75 'b.foo.bar',
76 ],
77 );
78
79 my $checker=Net::Connection::Match::RegexPTR->new( \%args );
80  
81  
82 =head3 args
83  
84 Atleast one of the following need used.
85  
86 =head4 ptrs
87  
88 This is a array of PTRs to match in for either foreign
89 or local side.
90  
91 =head4 fptrs
92  
93 This is a array of PTRs to match in for the foreign side.
94  
95 =head4 lptrs
96  
97 This is a array of PTRs to match in for the local side.
98  
99 =cut
100  
101 sub new{
102 my %args;
103 if(defined($_[1])){
104 %args= %{$_[1]};
105 };
106  
107 # run some basic checks to make sure we have the minimum stuff required to work
108 if (
109 ( ! defined( $args{ptrs} ) ) &&
110 ( ! defined( $args{fptrs} ) ) &&
111 ( ! defined( $args{lptrs} ) )
112 ){
113 die ('No [fl]ptrs key specified in the argument hash');
114 }
115 if (
116 (
117 defined( $args{ptrs} ) &&
118 ( ! defined( $args{ptrs}[0] ) )
119 ) &&
120 (
121 defined( $args{lptrs} ) &&
122 ( ! defined( $args{lptrs}[0] ) )
123 ) &&
124 (
125 defined( $args{fptrs} ) &&
126 ( ! defined( $args{fptrs}[0] ) )
127 )
128 ){
129 die ('No ports defined in the in any of the [fl]ptrs array');
130 }
131  
132 my $self = {
133 ptrs=>[],
134 lptrs=>[],
135 fptrs=>[],
136 resolver=>Net::DNS::Resolver->new,
137 };
138 bless $self;
139  
140 if ( defined( $args{ptrs}[0] ) ){
141 $self->{ptrs}=$args{ptrs};
142 }
143 if ( defined( $args{lptrs}[0] ) ){
144 $self->{lptrs}=$args{lptrs};
145 }
146 if ( defined( $args{fptrs}[0] ) ){
147 $self->{fptrs}=$args{fptrs};
148 }
149  
150 return $self;
151 }
152  
153 =head2 match
154  
155 Checks if a single Net::Connection object matches the stack.
156  
157 One argument is taken and that is a Net::Connection object.
158  
159 The returned value is a boolean.
160  
161 If the *_ptr feilds for the object are undef, L<Net::DNS::Resolver>
162 will be used for resolving the address.
163  
164 if ( $checker->match( $conn ) ){
165 print "The connection matches.\n";
166 }
167  
168 =cut
169  
170 sub match{
171 my $self=$_[0];
172 my $object=$_[1];
173  
174 if ( !defined( $object ) ){
175 return 0;
176 }
177  
178 if ( ref( $object ) ne 'Net::Connection' ){
179 return 0;
180 }
181  
182 my $l_ptr=$object->local_ptr;
183 my $f_ptr=$object->foreign_ptr;
184  
185 if ( defined( $l_ptr ) ){
186 # If we have one, convert it to lower case for easier processing.
187 $l_ptr=lc( $l_ptr )
188 }else{
189 # We don't have it. Uppercase default will prevent it from being matched.
190 $l_ptr='NOTFOUND';
191 # See if we can look it up.
192 my $answer=$self->{resolver}->search( $object->local_host );
193 if ( defined( $answer->{answer}[0] ) &&
194 ( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
195 ){
196 $l_ptr=lc($answer->{answer}[0]->ptrdname);
197 }
198 }
199  
200 if ( defined( $f_ptr ) ){
201 # If we have one, convert it to lower case for easier processing.
202 $f_ptr=lc( $f_ptr )
203 }else{
204 # We don't have it. Uppercase default will prevent it from being matched.
205 $f_ptr='NOTFOUND';
206 # See if we can look it up.
207 my $answer=$self->{resolver}->search( $object->foreign_host );
208 if ( defined( $answer->{answer}[0] ) &&
209 ( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
210 ){
211 $f_ptr=lc($answer->{answer}[0]->ptrdname);
212 }
213 }
214  
215 foreach my $regex ( @{ $self->{ptrs} } ){
216 if ( $l_ptr =~ /$regex/ ){
217 return 1;
218 }
219 if ( $f_ptr =~ /$regex/ ){
220 return 1;
221 }
222 }
223 foreach my $regex ( @{ $self->{lptrs} } ){
224 if ( $l_ptr =~ /$regex/ ){
225 return 1;
226 }
227 }
228 foreach my $regex ( @{ $self->{fptrs} } ){
229 if ( $f_ptr =~ /$regex/ ){
230 return 1;
231 }
232 }
233  
234 return 0;
235 }
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 1; # End of Net::Connection::Match