Match/Ports.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 147 CC 20 SCAR 48.1
CC
20
Cov
0%
new
182.0
match
72.0
BEGIN
2.0
CRAP 122.8
1 package Net::Connection::Match::Ports;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6  
7 =head1 NAME
8  
9 Net::Connection::Match::Ports - Runs a basic port check against a Net::Connection object.
10  
11 =head1 VERSION
12  
13 Version 0.0.0
14  
15 =cut
16  
17 our $VERSION = '0.0.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Match::Port;
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 ports=>[
38 'smtp',
39 '22',
40 ],
41 lports=>[
42 '21',
43 ],
44 fports=>[
45 'http',
46 ],
47 );
48
49 my $checker=Net::Connection::Match::Ports->new( \%args );
50
51 if ( $checker->match( $conn ) ){
52 print "It matches.\n";
53 }
54  
55 =head1 METHODS
56  
57 =head2 new
58  
59 This intiates the object.
60  
61 It takes a hash reference with atleast one of of the three
62 possible keys defined. The possible keys are 'ports'(which
63 matches either side), 'lports'(which matches the local side),
64 and 'fports'(which matches the foreign side).
65  
66 The value of each key is a array with either port numbers or
67 names. If names are given, getservbyname will be called. If
68 it errors for any of them, it will die.
69  
70 If the new method fails, it dies.
71  
72 my %args=(
73 ports=>[
74 'smtp',
75 '22',
76 ],
77 lports=>[
78 '21',
79 ],
80 fports=>[
81 'http',
82 ],
83 );
84
85 my $checker=Net::Connection::Match::Ports->new( \%args );
86  
87 =cut
88  
89 sub new{
90 my %args;
91 if(defined($_[1])){
92 %args= %{$_[1]};
93 };
94  
95 # run some basic checks to make sure we have the minimum stuff required to work
96 if (
97 ( ! defined( $args{ports} ) ) &&
98 ( ! defined( $args{fports} ) ) &&
99 ( ! defined( $args{lports} ) )
100 ){
101 die ('No [fl]ports key specified in the argument hash');
102 }
103 if (
104 (
105 defined( $args{ports} ) &&
106 ( ! defined( $args{ports}[0] ) )
107 ) &&
108 (
109 defined( $args{lports} ) &&
110 ( ! defined( $args{lports}[0] ) )
111 ) &&
112 (
113 defined( $args{fports} ) &&
114 ( ! defined( $args{fports}[0] ) )
115 )
116 ){
117 die ('No ports defined in the in any of the [fl]ports array');
118 }
119  
120 my $self = {
121 ports=>{},
122 fports=>{},
123 lports=>{},
124 };
125 bless $self;
126  
127 # Process the ports for matching either
128 my $ports_int=0;
129 if ( defined( $args{ports} ) ){
130 while (defined( $args{ports}[$ports_int] )) {
131 if ( $args{ports}[$ports_int] =~ /^[0-9]+$/ ){
132 $self->{ports}{ $args{ports}[$ports_int] }= $args{ports}[$ports_int];
133 }else{
134 my $port_number=(getservbyname( $args{ports}[$ports_int] , '' ))[2];
135  
136 if( !defined( $port_number ) ){
137 die("Could not resolve port '".$args{ports}[$ports_int]."' to a number");
138 }
139  
140 $self->{ports}{$port_number}=$port_number;
141 }
142  
143 $ports_int++;
144 }
145 }
146  
147 # Process the ports for matching local ports
148 $ports_int=0;
149 if ( defined( $args{lports} ) ){
150 while (defined( $args{lports}[$ports_int] )) {
151 if ( $args{lports}[$ports_int] =~ /^[0-9]+$/ ){
152 $self->{lports}{ $args{lports}[$ports_int] }= $args{lports}[$ports_int];
153 }else{
154 my $port_number=(getservbyname( $args{lports}[$ports_int] , '' ))[2];
155  
156 if( !defined( $port_number ) ){
157 die("Could not resolve port '".$args{lports}[$ports_int]."' to a number");
158 }
159  
160 $self->{lports}{$port_number}=$port_number;
161 }
162  
163 $ports_int++;
164 }
165 }
166  
167 # Process the ports for matching foreign ports
168 $ports_int=0;
169 if ( defined( $args{fports} ) ){
170 while (defined( $args{fports}[$ports_int] )) {
171 if ( $args{fports}[$ports_int] =~ /^[0-9]+$/ ){
172 $self->{fports}{ $args{fports}[$ports_int] }= $args{fports}[$ports_int];
173 }else{
174 my $port_number=(getservbyname( $args{fports}[$ports_int] , '' ))[2];
175  
176 if( !defined( $port_number ) ){
177 die("Could not resolve port '".$args{fports}[$ports_int]."' to a number");
178 }
179  
180 $self->{fports}{$port_number}=$port_number;
181 }
182  
183 $ports_int++;
184 }
185 }
186  
187 return $self;
188 }
189  
190 =head2 match
191  
192 Checks if a single Net::Connection object matches the stack.
193  
194 One argument is taken and that is a Net::Connection object.
195  
196 The returned value is a boolean.
197  
198 if ( $checker->match( $conn ) ){
199 print "The connection matches.\n";
200 }
201  
202 =cut
203  
204 sub match{
205 my $self=$_[0];
206 my $object=$_[1];
207  
208 if ( !defined( $object ) ){
209 return 0;
210 }
211  
212 if ( ref( $object ) ne 'Net::Connection' ){
213 return 0;
214 }
215  
216 my $lport=$object->local_port;
217 my $fport=$object->foreign_port;
218  
219 # If either are non-numeric, resolve them if possible
220 if ( $lport !~ /^[0-9]+$/ ){
221 my $lport_number=(getservbyname( $lport , '' ))[2];
222 if ( defined( $lport_number ) ){
223 $lport=$lport_number;
224 }
225 }
226 if ( $fport !~ /^[0-9]+$/ ){
227 my $fport_number=(getservbyname( $fport , '' ))[2];
228 if ( defined( $fport_number ) ){
229 $fport=$fport_number;
230 }
231 }
232  
233 # check if this is one of the ones we are looking for
234 if (
235 defined( $self->{ports}{ $lport } ) ||
236 defined( $self->{ports}{ $fport } ) ||
237 defined( $self->{lports}{ $lport } ) ||
238 defined( $self->{fports}{ $fport } )
239 ){
240 return 1;
241 }
242  
243 return 0;
244 }
245  
246 =head1 AUTHOR
247  
248 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
249  
250 =head1 BUGS
251  
252 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
253 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
254 automatically be notified of progress on your bug as I make changes.
255  
256  
257  
258  
259 =head1 SUPPORT
260  
261 You can find documentation for this module with the perldoc command.
262  
263 perldoc Net::Connection::Match
264  
265  
266 You can also look for information at:
267  
268 =over 4
269  
270 =item * RT: CPAN's request tracker (report bugs here)
271  
272 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
273  
274 =item * AnnoCPAN: Annotated CPAN documentation
275  
276 L<http://annocpan.org/dist/Net-Connection-Match>
277  
278 =item * CPAN Ratings
279  
280 L<https://cpanratings.perl.org/d/Net-Connection-Match>
281  
282 =item * Search CPAN
283  
284 L<https://metacpan.org/release/Net-Connection-Match>
285  
286 =back
287  
288  
289 =head1 ACKNOWLEDGEMENTS
290  
291  
292 =head1 LICENSE AND COPYRIGHT
293  
294 Copyright 2019 Zane C. Bowers-Hadley.
295  
296 This program is free software; you can redistribute it and/or modify it
297 under the terms of the the Artistic License (2.0). You may obtain a
298 copy of the full license at:
299  
300 L<http://www.perlfoundation.org/artistic_license_2_0>
301  
302 Any use, modification, and distribution of the Standard or Modified
303 Versions is governed by this Artistic License. By using, modifying or
304 distributing the Package, you accept this license. Do not use, modify,
305 or distribute the Package, if you do not accept this license.
306  
307 If your Modified Version has been derived from a Modified Version made
308 by someone other than you, you are nevertheless required to ensure that
309 your Modified Version complies with the requirements of this license.
310  
311 This license does not grant you the right to use any trademark, service
312 mark, tradename, or logo of the Copyright Holder.
313  
314 This license includes the non-exclusive, worldwide, free-of-charge
315 patent license to make, have made, use, offer to sell, sell, import and
316 otherwise transfer the Package with respect to any patent claims
317 licensable by the Copyright Holder that are necessarily infringed by the
318 Package. If you institute patent litigation (including a cross-claim or
319 counterclaim) against any party alleging that the Package constitutes
320 direct or contributory patent infringement, then this Artistic License
321 to you shall terminate on the date that such litigation is filed.
322  
323 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
324 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
325 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
326 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
327 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
328 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
329 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
330 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
331  
332  
333 =cut
334  
335 1; # End of Net::Connection::Match