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