File Coverage

blib/lib/Parse/Netstat/Search/Sort.pm
Criterion Covered Total %
statement 59 86 68.6
branch 31 60 51.6
condition 3 9 33.3
subroutine 11 11 100.0
pod 6 6 100.0
total 110 172 63.9


line stmt bran cond sub pod time code
1             package Parse::Netstat::Search::Sort;
2              
3 2     2   112809 use 5.006;
  2         14  
4 2     2   9 use strict;
  2         3  
  2         52  
5 2     2   16 use warnings;
  2         4  
  2         55  
6 2     2   8 use base 'Error::Helper';
  2         4  
  2         496  
7 2     2   1686 use Net::IP;
  2         94762  
  2         1792  
8              
9             =head1 NAME
10              
11             Parse::Netstat::Search::Sort - Sorts the returned array from Parse::Netstat::Search.
12              
13             =head1 VERSION
14              
15             Version 0.0.1
16              
17             =cut
18              
19             our $VERSION = '0.0.1';
20              
21              
22             =head1 SYNOPSIS
23              
24             use Parse::Netstat::Search;
25             use Parse::Netstat::Search::Sort;
26             use Parse::Netstat qw(parse_netstat);
27              
28             my $res = parse_netstat(output => join("", `netstat -n`), tcp=>1, udp=>1, unix=>0, flavor=>$^O);
29              
30             my $pn-search=Parse::Netstat::Search->new;
31              
32             my @found=$pn-search->search( $res );
33              
34             my $sorter = Parse::Netstat::Search::Sort->new;
35              
36             @found = $sorter->sort( \@found );
37              
38             The supported sort methods are as below.
39              
40             host_f Host, Foreign (default)
41             host_l Host, Local
42             port_f Port, Foriegn
43             port_l Port, Local
44             state State
45             protocol Protocol
46             q_r Queue, Receive
47             q_s Queue, Send
48             none No sorting is done.
49              
50             These dual sort can take noticably longer than the ones above.
51              
52             host_ff Host, Foreign First
53             host_lf Host, Local First
54             port_ff Port, Foreign First
55             port_lf Port, Local First
56             q_rf Queue, Receive First
57             q_sf Queue, Send First
58              
59             =head1 Methods
60              
61             =head2 new
62              
63             Initiates the object.
64              
65             my $sorter=Parse::Netstat::Search->new;
66              
67             =cut
68              
69             sub new{
70 1     1 1 98 my $self={
71             perror=>undef,
72             error=>undef,
73             errorString=>'',
74             errorExtra=>{
75             1 => 'badSort',
76             2 => 'badArray',
77             },
78             sort=>'host_f',
79             invert=>undef,
80             sort_check=>{
81             host_ff=>1,
82             host_lf=>1,
83             host_f=>1,
84             host_l=>1,
85             port_ff=>1,
86             port_lf=>1,
87             port_f=>1,
88             port_l=>1,
89             state=>1,
90             protocol=>1,
91             q_rf=>1,
92             q_sf=>1,
93             q_r=>1,
94             q_s=>1,
95             none=>1,
96             }
97             };
98 1         2 bless $self;
99              
100 1         2 return $self;
101             }
102              
103             =head2 get_sort
104              
105             This returns the current sort method.
106              
107             my $sort=$pnc->get_sort;
108              
109             =cut
110              
111             sub get_sort{
112 1     1 1 4 my $self=$_[0];
113              
114 1 50       2 if( ! $self->errorblank ){
115 0         0 return undef;
116             }
117              
118 1         9 return $self->{sort};
119             }
120              
121             =head2 set_sort
122              
123             This sets the sort method to be used and if it should
124             be inverted.
125              
126             The first argument is the sort method name and the second is a
127             boolean on if it should be inverted or not.
128              
129             Leaving either undef resets the undef value back to the default.
130              
131             The supported sorting methods are as below.
132              
133             $ sorter->set_sort( $sort_method );
134             if( $sorter->error ){
135             warn( '"'.$sort_method.'"' is not a valid sort method );
136             }
137            
138             # reset to defaults
139             $sorter->set_sort
140            
141             # Set the sort method to host_f and invert.
142             $sorter->set_sort( 'host_f' )
143              
144             =cut
145              
146             sub set_sort{
147 7     7 1 3508 my $self=$_[0];
148 7         17 my $sort=$_[1];
149              
150 7 50       18 if( ! $self->errorblank ){
151 0         0 return undef;
152             }
153              
154 7 50       66 if (!defined( $sort ) ){
155 0         0 $sort='host_f';
156             }
157              
158 7 50       19 if (! defined( $self->{sort_check}{$sort} ) ){
159 0         0 $self->{error}=1;
160 0         0 $self->{errorString}='"'.$sort.'" is not a valid sort type';
161 0         0 $self->warn;
162 0         0 return undef;
163             }
164            
165 7         12 $self->{sort}=$sort;
166              
167 7         13 return 1;
168             }
169              
170             =head2 sort
171              
172             Sorts the provided array from Parse::Netstat::Search.
173              
174             my @sorted=$sorter->sort( \@found );
175              
176             =cut
177              
178             sub sort{
179 8     8 1 260 my $self=$_[0];
180 8         11 my @found;
181 8 50 33     37 if (
182             defined( $_[1] ) &&
183             ( ref($_[1]) eq 'ARRAY' )
184             ){
185 8         11 @found=@{ $_[1] };
  8         17  
186             }else{
187 0         0 $self->{error}=2;
188 0         0 $self->{errorString}='The passed item is either not a array or undefined';
189 0         0 $self->warn;
190 0         0 return undef;
191             }
192              
193 8 50       21 if( ! $self->errorblank ){
194 0         0 return undef;
195             }
196              
197             # handle sorting if needed
198 8 50       65 if ( $self->{sort} ne 'none' ){
199 8 50       58 if( $self->{sort} eq 'host_ff' ){
    50          
    100          
    100          
    50          
    50          
    100          
    100          
    100          
    100          
    50          
    50          
    100          
    50          
200             @found=sort {
201 0         0 &host_sort_helper( $a->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} ) or
202             &host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} )
203 0 0       0 } @found;
204             }elsif( $self->{sort} eq 'host_lf' ){
205             @found=sort {
206 0         0 &host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} ) or
207             &host_sort_helper( $a->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} )
208 0 0       0 } @found;
209             }elsif( $self->{sort} eq 'host_f' ){
210             @found=sort {
211 1         4 &host_sort_helper( $a->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} )
212 5         101 } @found;
213             }elsif( $self->{sort} eq 'host_l' ){
214             @found=sort {
215 1         4 &host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} )
216 5         92 } @found;
217             }elsif( $self->{sort} eq 'port_ff' ){
218             @found=sort {
219 0         0 &port_sort_helper( $a->{foreign_port} ) <=> &port_sort_helper( $b->{foreign_port} ) or
220             &port_sort_helper( $a->{local_port} ) <=> &port_sort_helper( $b->{local_port} )
221 0 0       0 } @found;
222             }elsif( $self->{sort} eq 'port_lf' ){
223             @found=sort {
224 0         0 &port_sort_helper( $a->{local_port} ) <=> &port_sort_helper( $b->{local_port} ) or
225             &port_sort_helper( $a->{foreign_port} ) <=> &port_sort_helper( $b->{foreign_port} )
226 0 0       0 } @found;
227             }elsif( $self->{sort} eq 'port_f' ){
228             @found=sort {
229 1         5 &port_sort_helper( $a->{foreign_port} ) <=> &port_sort_helper( $b->{foreign_port} )
230 5         11 } @found;
231             }elsif( $self->{sort} eq 'port_l' ){
232             @found=sort {
233 1         4 &port_sort_helper( $a->{local_port} ) <=> &port_sort_helper( $b->{local_port} )
234 5         9 } @found;
235             }elsif( $self->{sort} eq 'state' ){
236             @found=sort {
237 1         4 $a->{state} cmp $b->{state}
238 5         11 } @found;
239             }elsif( $self->{sort} eq 'protocol' ){
240             @found=sort {
241 1         4 $a->{proto} cmp $b->{proto}
242 4         9 } @found;
243             }elsif( $self->{sort} eq 'q_rf' ){
244             @found=sort {
245 0         0 $a->{recvq} <=> $b->{recvq} or
246             $a->{sendq} <=> $b->{sendq}
247 0 0       0 } @found;
248             }elsif( $self->{sort} eq 'q_sf' ){
249             @found=sort {
250 0         0 $a->{sendq} <=> $b->{sendq} or
251             $a->{recvq} <=> $b->{recvq}
252 0 0       0 } @found;
253             }elsif( $self->{sort} eq 'q_r' ){
254             @found=sort {
255 1         4 $a->{recvq} <=> $b->{recvq}
256 4         8 } @found;
257             }elsif( $self->{sort} eq 'q_s' ){
258             @found=sort {
259 1         4 $a->{sendq} <=> $b->{sendq}
260 5         10 } @found;
261             }
262             }
263              
264 8         75 return @found;
265             }
266              
267             =head2 host_sort_helper
268              
269             Internal function.
270              
271             Takes a host and converts it to a number.
272              
273             =cut
274              
275             sub host_sort_helper{
276 20 50 33 20 1 75 if (
277             ( !defined($_[0]) ) ||
278             ( $_[0] eq '*' )
279             ){
280 0         0 return 0;
281             }
282 20         29 my $host=eval { Net::IP->new( $_[0] )->intip} ;
  20         50  
283 20 50       81349 if (!defined( $host )){
284 0         0 return 0;
285             }
286 20         66 return $host;
287             }
288              
289             =head2 port_sort_helper
290              
291             Internal function.
292              
293             Makes sure a port number is always returned.
294              
295             =cut
296              
297             sub port_sort_helper{
298 20 50 33 20 1 50 if (
299             ( !defined($_[0]) ) ||
300             ( $_[0] eq '*' )
301             ){
302 0         0 return 0;
303             }
304 20         34 return $_[0];
305             }
306              
307             =head1 ERROR CODES / FLAGS
308              
309             Error handling is provided by L.
310              
311             =head2 1 / badSort
312              
313             Invalid value specified for sort.
314              
315             =head2 2 / badArray
316              
317             The passed item is not a array.
318              
319             =head1 AUTHOR
320              
321             Zame C. Bowers-Hadley, C<< >>
322              
323             =head1 BUGS
324              
325             Please report any bugs or feature requests to C, or through
326             the web interface at L. I will be notified, and then you'll
327             automatically be notified of progress on your bug as I make changes.
328              
329              
330              
331              
332             =head1 SUPPORT
333              
334             You can find documentation for this module with the perldoc command.
335              
336             perldoc Parse::Netstat::Search::Sort
337              
338              
339             You can also look for information at:
340              
341             =over 4
342              
343             =item * RT: CPAN's request tracker (report bugs here)
344              
345             L
346              
347             =item * AnnoCPAN: Annotated CPAN documentation
348              
349             L
350              
351             =item * CPAN Ratings
352              
353             L
354              
355             =item * Search CPAN
356              
357             L
358              
359             =item * Code Rep
360              
361             L
362              
363             =back
364              
365              
366             =head1 ACKNOWLEDGEMENTS
367              
368              
369             =head1 LICENSE AND COPYRIGHT
370              
371             Copyright 2019 Zame C. Bowers-Hadley.
372              
373             This program is free software; you can redistribute it and/or modify it
374             under the terms of the the Artistic License (2.0). You may obtain a
375             copy of the full license at:
376              
377             L
378              
379             Any use, modification, and distribution of the Standard or Modified
380             Versions is governed by this Artistic License. By using, modifying or
381             distributing the Package, you accept this license. Do not use, modify,
382             or distribute the Package, if you do not accept this license.
383              
384             If your Modified Version has been derived from a Modified Version made
385             by someone other than you, you are nevertheless required to ensure that
386             your Modified Version complies with the requirements of this license.
387              
388             This license does not grant you the right to use any trademark, service
389             mark, tradename, or logo of the Copyright Holder.
390              
391             This license includes the non-exclusive, worldwide, free-of-charge
392             patent license to make, have made, use, offer to sell, sell, import and
393             otherwise transfer the Package with respect to any patent claims
394             licensable by the Copyright Holder that are necessarily infringed by the
395             Package. If you institute patent litigation (including a cross-claim or
396             counterclaim) against any party alleging that the Package constitutes
397             direct or contributory patent infringement, then this Artistic License
398             to you shall terminate on the date that such litigation is filed.
399              
400             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
401             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
402             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
403             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
404             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
405             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
406             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
407             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
408              
409              
410             =cut
411              
412             1; # End of Parse::Netstat::Search::Sort