File Coverage

blib/lib/Net/Connection/Match/Command.pm
Criterion Covered Total %
statement 11 52 21.1
branch 0 22 0.0
condition 0 3 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 17 85 20.0


line stmt bran cond sub pod time code
1             package Net::Connection::Match::Command;
2              
3 1     1   63867 use 5.006;
  1         11  
4 1     1   6 use strict;
  1         2  
  1         19  
5 1     1   4 use warnings;
  1         2  
  1         35  
6 1     1   482 use Proc::ProcessTable;
  1         4765  
  1         434  
7              
8             =head1 NAME
9              
10             Net::Connection::Match::Command - Check if the process command matches fix regexp for a Net::Connection object.
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::Command;
24             use Net::Connection;
25            
26             my $connection_args={
27             foreign_host=>'10.0.0.1',
28             foreign_port=>'22',
29             local_host=>'10.0.0.2',
30             local_port=>'12322',
31             proto=>'tcp4',
32             state=>'ESTABLISHED',
33             pid=>0,
34             };
35            
36             my $conn=Net::Connection->new( $connection_args );
37            
38             my %args=(
39             commands=>[
40             'kernel',
41             ],
42             );
43            
44             my $checker=Net::Connection::Match::Command->new( \%args );
45            
46             if ( $checker->match( $conn ) ){
47             print "It matches.\n";
48             }
49              
50             =head1 METHODS
51              
52             =head2 new
53              
54             This intiates the object.
55              
56             The key in the hash ref commands is a array
57             that holds regular expressions to match against
58             the command line or fname(in case of blank command
59             line). At least one value must be defined.
60              
61             If the new method fails, it dies.
62              
63             my %args=(
64             commands=>[
65             'kernel',
66             ],
67             );
68            
69             my $checker=Net::Connection::Match::Command->new( \%args );
70              
71             =cut
72              
73             sub new{
74 0     0 1   my %args;
75 0 0         if(defined($_[1])){
76 0           %args= %{$_[1]};
  0            
77             };
78              
79             # run some basic checks to make sure we have the minimum stuff required to work
80 0 0         if ( ! defined( $args{commands} ) ){
81 0           die ('No commands key specified in the argument hash');
82             }
83 0 0         if ( ref( \$args{commands} ) eq 'ARRAY' ){
84 0           die ('The commands key is not a array');
85             }
86 0 0         if ( ! defined $args{commands}[0] ){
87 0           die ('Nothing defined in the commands array');
88             }
89              
90             my $self = {
91             commands=>$args{commands},
92 0           };
93 0           bless $self;
94              
95 0           return $self;
96             }
97              
98             =head2 match
99              
100             Checks if a single Net::Connection object matches the stack.
101              
102             One argument is taken and that is a Net::Connection object.
103              
104             The returned value is a boolean.
105              
106             if ( $checker->match( $conn ) ){
107             print "The connection matches.\n";
108             }
109              
110             =cut
111              
112             sub match{
113 0     0 1   my $self=$_[0];
114 0           my $object=$_[1];
115              
116 0 0         if ( !defined( $object ) ){
117 0           return 0;
118             }
119              
120 0 0         if ( ref( $object ) ne 'Net::Connection' ){
121 0           return 0;
122             }
123              
124 0           my $conn_pid=$object->pid;
125              
126             # don't bother proceeding, the object won't match ever
127             # as it does not have a PID
128 0 0         if ( ! defined( $conn_pid ) ){
129 0           return 0;
130             }
131              
132             # go through each proc and look for a matching pid
133 0           my $proctable=Proc::ProcessTable->new;
134 0           my $procs=$proctable->table;
135 0           my $loop=1;
136 0           my $proc_int=0;
137 0           my $command;
138 0   0       while (
139             $loop &&
140             defined( $procs->[$proc_int] )
141             ){
142              
143 0 0         if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
144 0           $command=$procs->[$proc_int]->cmndline;
145             # '' means it is a kernel process
146 0 0         if ( $command =~ /^$/ ){
147 0           $command='['.$procs->[$proc_int]->fname.']';
148             }
149             # exit the loop as we found it
150 0           $loop=0;
151             }
152              
153 0           $proc_int++;
154             }
155              
156             # likely a dead connection that is handing around...
157             # or disappeared since grabbing the connection list
158             # and starting processing
159 0 0         if ( !defined( $command ) ){
160 0           return 0;
161             }
162              
163             # check each command regex and see if any of them match
164 0           foreach my $regex ( @{ $self->{commands} } ){
  0            
165 0 0         if ( $command =~ /$regex/ ){
166 0           return 1;
167             }
168             }
169              
170 0           return 0;
171             }
172              
173             =head1 AUTHOR
174              
175             Zane C. Bowers-Hadley, C<< >>
176              
177             =head1 BUGS
178              
179             Please report any bugs or feature requests to C, or through
180             the web interface at L. I will be notified, and then you'll
181             automatically be notified of progress on your bug as I make changes.
182              
183              
184              
185              
186             =head1 SUPPORT
187              
188             You can find documentation for this module with the perldoc command.
189              
190             perldoc Net::Connection::Match
191              
192              
193             You can also look for information at:
194              
195             =over 4
196              
197             =item * RT: CPAN's request tracker (report bugs here)
198              
199             L
200              
201             =item * AnnoCPAN: Annotated CPAN documentation
202              
203             L
204              
205             =item * CPAN Ratings
206              
207             L
208              
209             =item * Search CPAN
210              
211             L
212              
213             =back
214              
215              
216             =head1 ACKNOWLEDGEMENTS
217              
218              
219             =head1 LICENSE AND COPYRIGHT
220              
221             Copyright 2019 Zane C. Bowers-Hadley.
222              
223             This program is free software; you can redistribute it and/or modify it
224             under the terms of the the Artistic License (2.0). You may obtain a
225             copy of the full license at:
226              
227             L
228              
229             Any use, modification, and distribution of the Standard or Modified
230             Versions is governed by this Artistic License. By using, modifying or
231             distributing the Package, you accept this license. Do not use, modify,
232             or distribute the Package, if you do not accept this license.
233              
234             If your Modified Version has been derived from a Modified Version made
235             by someone other than you, you are nevertheless required to ensure that
236             your Modified Version complies with the requirements of this license.
237              
238             This license does not grant you the right to use any trademark, service
239             mark, tradename, or logo of the Copyright Holder.
240              
241             This license includes the non-exclusive, worldwide, free-of-charge
242             patent license to make, have made, use, offer to sell, sell, import and
243             otherwise transfer the Package with respect to any patent claims
244             licensable by the Copyright Holder that are necessarily infringed by the
245             Package. If you institute patent litigation (including a cross-claim or
246             counterclaim) against any party alleging that the Package constitutes
247             direct or contributory patent infringement, then this Artistic License
248             to you shall terminate on the date that such litigation is filed.
249              
250             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
251             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
252             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
253             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
254             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
255             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
256             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
257             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258              
259              
260             =cut
261              
262             1; # End of Net::Connection::Match