Match/Ports.pm

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