File Coverage

blib/lib/Net/Connection/Match/WChan.pm
Criterion Covered Total %
statement 11 53 20.7
branch 0 22 0.0
condition 0 3 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 17 86 19.7


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