Browse Source

begin work on the ports matcher

tags/0.0.0
Zane C. B-H 1 month ago
parent
commit
781437abf3
1 changed files with 309 additions and 0 deletions
  1. 309
    0
      Net-Connection-Match/lib/Net/Connection/Match/Ports.pm

+ 309
- 0
Net-Connection-Match/lib/Net/Connection/Match/Ports.pm View File

@@ -0,0 +1,309 @@
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
+
217
+	return 0;
218
+}
219
+
220
+=head1 AUTHOR
221
+
222
+Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
223
+
224
+=head1 BUGS
225
+
226
+Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
227
+the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>.  I will be notified, and then you'll
228
+automatically be notified of progress on your bug as I make changes.
229
+
230
+
231
+
232
+
233
+=head1 SUPPORT
234
+
235
+You can find documentation for this module with the perldoc command.
236
+
237
+    perldoc Net::Connection::Match
238
+
239
+
240
+You can also look for information at:
241
+
242
+=over 4
243
+
244
+=item * RT: CPAN's request tracker (report bugs here)
245
+
246
+L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
247
+
248
+=item * AnnoCPAN: Annotated CPAN documentation
249
+
250
+L<http://annocpan.org/dist/Net-Connection-Match>
251
+
252
+=item * CPAN Ratings
253
+
254
+L<https://cpanratings.perl.org/d/Net-Connection-Match>
255
+
256
+=item * Search CPAN
257
+
258
+L<https://metacpan.org/release/Net-Connection-Match>
259
+
260
+=back
261
+
262
+
263
+=head1 ACKNOWLEDGEMENTS
264
+
265
+
266
+=head1 LICENSE AND COPYRIGHT
267
+
268
+Copyright 2019 Zane C. Bowers-Hadley.
269
+
270
+This program is free software; you can redistribute it and/or modify it
271
+under the terms of the the Artistic License (2.0). You may obtain a
272
+copy of the full license at:
273
+
274
+L<http://www.perlfoundation.org/artistic_license_2_0>
275
+
276
+Any use, modification, and distribution of the Standard or Modified
277
+Versions is governed by this Artistic License. By using, modifying or
278
+distributing the Package, you accept this license. Do not use, modify,
279
+or distribute the Package, if you do not accept this license.
280
+
281
+If your Modified Version has been derived from a Modified Version made
282
+by someone other than you, you are nevertheless required to ensure that
283
+your Modified Version complies with the requirements of this license.
284
+
285
+This license does not grant you the right to use any trademark, service
286
+mark, tradename, or logo of the Copyright Holder.
287
+
288
+This license includes the non-exclusive, worldwide, free-of-charge
289
+patent license to make, have made, use, offer to sell, sell, import and
290
+otherwise transfer the Package with respect to any patent claims
291
+licensable by the Copyright Holder that are necessarily infringed by the
292
+Package. If you institute patent litigation (including a cross-claim or
293
+counterclaim) against any party alleging that the Package constitutes
294
+direct or contributory patent infringement, then this Artistic License
295
+to you shall terminate on the date that such litigation is filed.
296
+
297
+Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
298
+AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
299
+THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
300
+PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
301
+YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
302
+CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
303
+CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
304
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305
+
306
+
307
+=cut
308
+
309
+1; # End of Net::Connection::Match

Loading…
Cancel
Save