File Coverage

blib/lib/Net/Connection.pm
Criterion Covered Total %
statement 62 113 54.8
branch 37 74 50.0
condition 33 54 61.1
subroutine 7 24 29.1
pod 20 20 100.0
total 159 285 55.7


line stmt bran cond sub pod time code
1             package Net::Connection;
2              
3 2     2   109578 use 5.006;
  2         16  
4 2     2   10 use strict;
  2         2  
  2         41  
5 2     2   9 use warnings;
  2         3  
  2         54  
6 2     2   906 use Net::DNS;
  2         154967  
  2         3093  
7              
8             =head1 NAME
9              
10             Net::Connection - Represents a network connection as a object.
11              
12             =head1 VERSION
13              
14             Version 0.2.0
15              
16             =cut
17              
18             our $VERSION = '0.2.0';
19              
20              
21             =head1 SYNOPSIS
22              
23             use Net::Connection;
24              
25             #create a hash ref with the desired values
26             my $args={
27             'foreign_host' => '1.2.3.4',
28             'local_host' => '4.3.2.1',
29             'foreign_port' => '22',
30             'local_port' => '11132',
31             'sendq' => '1',
32             'recvq' => '0',
33             'pid' => '34342',
34             'uid' => '1000',
35             'state' => 'ESTABLISHED',
36             'proto' => 'tcp4'
37             };
38            
39             # create the new object using the hash ref
40             my $conn=Net::Connection->new( $args );
41            
42             # the same thing, but this time resolve the UID to a username
43             $args->{'uid_resolve'}='1';
44             $conn=Net::Connection->new( $args );
45            
46             # now with PTR lookup
47             $args->{'ptrs'}='1';
48             $conn=Net::Connection->new( $args );
49            
50             # prints a bit of the connection information...
51             print "L Host:".$conn->local_host."\n".
52             "L Port:".$conn->local_host."\n".
53             "F Host:".$conn->foreign_host."\n".
54             "F Port:".$conn->foreign_host."\n";
55              
56             =head1 Methods
57              
58             =head2 new
59              
60             This initiates a new connection object.
61              
62             One argument is taken is taken and it is a hash reference.
63             The minimum number of arguements is as below.
64              
65             local_host
66             local_port
67             foreign_host
68             foreign_port
69             proto
70             state
71              
72             =head3 keys
73              
74             =head4 foreign_host
75              
76             The local host of the connection.
77              
78             This can either be a IP or hostname. Max utility is achieved via a
79             IP though as that allows PTR lookup to be done.
80              
81             If appears to be a hostname, it is copied to local_ptr and even
82             if asked to resolve PTRs it won't attempt to.
83              
84             =head4 foreign_port
85              
86             This is the foreign port of the connection.
87              
88             For best utility, using numeric here is best.
89              
90             If ports is true it will attempt to resolve it,
91             including reverse resolving if it is a port name instead.
92              
93             If ports is false or not set and this value is
94             non-numeric, it will be copied to foreign_port_name.
95              
96             =head4 foreign_port_name
97              
98             This is the name of foreign port, if one exists in the
99             service records.
100              
101             =head4 foreign_ptr
102              
103             This is the PTR address for foreign_host.
104              
105             If ptrs is not true and foreign_host appears to be
106             a hostname, then it is set to the same as foreign_host.
107              
108             =head4 local_port
109              
110             This is the local port of the connection.
111              
112             For best utility, using numeric here is best.
113              
114             If ports is true it will attempt to resolve it,
115             including reverse resolving if it is a port name instead.
116              
117             If ports is false or not set and this value is
118             non-numeric, it will be copied to local_port_name.
119              
120             =head4 local_port_name
121              
122             This is the name of local port, if one exists in the
123             service records.
124              
125             =head4 local_ptr
126              
127             This is the PTR address for local_host.
128              
129             If ptrs is not true and local_host appears to be
130             a hostname, then it is set to the same as local_host.
131              
132             =head2 pctcpu
133              
134             Percent of CPU usage by the PID for this connection.
135              
136             =head2 pctmem
137              
138             Percent of memory usage by the PID for this connection.
139              
140             =head4 pid
141              
142             This is the pid for a connection.
143              
144             If defined, it needs to be numeric.
145              
146             =head4 pid_start
147              
148             The start time in seconds of the PID for the connection.
149              
150             =head4 ports
151              
152             If true, it will attempt to resolve the port names.
153              
154             =head4 proto
155              
156             This is the protocol type.
157              
158             This needs to be defined, but unfortunately no real checking is done
159             as of currently as various OSes uses varrying capitalizations and slightly
160             different forms of TCP, TCP4, tcp4, tcpv4, and the like.
161              
162             =head4 proc
163              
164             Either the command line or fname if that is blank for the PID.
165              
166             =head4 ptrs
167              
168             If is true, then attempt to look up the PTRs for the hosts.
169              
170             =head4 recvq
171              
172             This is the recieve queue size.
173              
174             If set, it must be numeric.
175              
176             =head4 sendq
177              
178             This is the send queue size.
179              
180             If set, it must be numeric.
181              
182             =head4 state
183              
184             This is the current state of the connection.
185              
186             This needs to be defined, but unfortunately no real checking is
187             done as of currently as there are minor naming differences between
188             OSes as well as some including states that are not found in others.
189              
190             =head4 uid
191              
192             The UID is the of the user the has the connection open.
193              
194             This must be numeric.
195              
196             If uid_resolve is set to true then the UID will be resolved
197             and stored in username.
198              
199             If this is not defined, uid_resolve is true, and username is defined
200             then it will attempt to resolve the UID from the username.
201              
202             =head4 uid_resolve
203              
204             If set to true and uid is given, then a attempt will be made to
205             resolve the UID to a username.
206              
207             =head4 username
208              
209             This is the username for a connection.
210              
211             If uid_resolve is true and uid is defined, then this
212             will attempt to be automatically contemplated.
213              
214             If uid_resolve is true and uid is defined, then this
215             will attempt to be automatically contemplated.
216              
217             =head4 wchan
218              
219             The current wait channel for the PID of the connection in question.
220              
221             =cut
222              
223             sub new{
224 16     16 1 4030 my %args;
225 16 50       35 if(defined($_[1])){
226 16         18 %args= %{$_[1]};
  16         85  
227             };
228              
229             # make sure we got the required bits
230 16 100 100     114 if (
      100        
      100        
      100        
      100        
231             (!defined( $args{'foreign_host'}) ) ||
232             (!defined( $args{'local_host'}) ) ||
233             (!defined( $args{'foreign_port'}) ) ||
234             (!defined( $args{'local_port'}) ) ||
235             (!defined( $args{'state'}) ) ||
236             (!defined( $args{'proto'}) )
237             ){
238 7         51 die "One or more of the required arguments is not defined";
239             }
240              
241             # PID must be numeric if given
242 9 100 100     52 if (
243             defined( $args{'pid'} ) &&
244             ( $args{'pid'} !~ /^[0-9]+$/ )
245             ){
246 1         8 die '$args{"pid"} is not numeric';
247             }
248              
249             # UID must be numeric if given
250 8 100 100     24 if (
251             defined( $args{'uid'} ) &&
252             ( $args{'uid'} !~ /^[0-9]+$/ )
253             ){
254 1         8 die '$args{"uid"} is not numeric';
255             }
256              
257             # set the sendq/recvq and make sure they are numeric if given
258 7 100 66     31 if (
259             defined( $args{'sendq'} ) &&
260             ( $args{'sendq'} !~ /^[0-9]+$/ )
261             ){
262 1         8 die '$args{"sendq"} is not numeric';
263             }
264 6 100 66     24 if (
265             defined( $args{'recvq'} ) &&
266             ( $args{'recvq'} !~ /^[0-9]+$/ )
267             ){
268 1         8 die '$args{"recvq"} is not numeric';
269             }
270              
271             my $self={
272             'foreign_host' => $args{'foreign_host'},
273             'local_host' => $args{'local_host'},
274             'foreign_port' => $args{'foreign_port'},
275             'foreign_port_name' => $args{'foreign_port_name'},
276             'local_port' => $args{'local_port'},
277             'local_port_name' => $args{'local_port_name'},
278             'sendq' => undef,
279             'recvq' => undef,
280             'pid' => undef,
281             'uid' => undef,
282             'username' => undef,
283             'state' => $args{'state'},
284 5         51 'proto' => $args{'proto'},
285             'local_ptr' => undef,
286             'foreign_ptr' => undef,
287             'pctcpu' => undef,
288             'pctmem' => undef,
289             'proc' => undef,
290             'wchan' => undef,
291             };
292 5         9 bless $self;
293              
294             # Set these if defined
295 5 50       10 if (defined( $args{'sendq'} )){
296 5         19 $self->{'sendq'}=$args{'sendq'};
297             }
298 5 50       11 if (defined( $args{'recvq'} )){
299 5         9 $self->{'recvq'}=$args{'recvq'};
300             }
301 5 50       8 if (defined( $args{'local_ptr'} )){
302 0         0 $self->{'local_ptr'}=$args{'local_ptr'};
303             }
304 5 50       11 if (defined( $args{'foreign_ptr'} )){
305 0         0 $self->{'foreign_ptr'}=$args{'foreign_ptr'};
306             }
307 5 100       7 if (defined( $args{'uid'} )){
308 2         3 $self->{'uid'}=$args{'uid'};
309             }
310 5 100       11 if (defined( $args{'pid'} )){
311 4         5 $self->{'pid'}=$args{'pid'};
312             }
313 5 100       9 if (defined( $args{'username'} )){
314 1         2 $self->{'username'}=$args{'username'};
315             }
316 5 50       10 if (defined( $args{'proc'} )){
317 0         0 $self->{'proc'}=$args{'proc'};
318             }
319 5 50       6 if (defined( $args{'wchan'} )){
320 0         0 $self->{'wchan'}=$args{'wchan'};
321             }
322 5 50       24 if (defined( $args{'pctmem'} )){
323 0         0 $self->{'pctmem'}=$args{'pctmem'};
324             }
325 5 50       10 if (defined( $args{'pctcpu'} )){
326 0         0 $self->{'pctcpu'}=$args{'pctcpu'};
327             }
328              
329             # resolve port names if asked to
330 5 50       10 if ( $args{ports} ){
331             # If the port is non-numeric, set the name and attempt to resolve it.
332 0 0       0 if ( $self->{'local_port'} =~ /[A-Za-z]/ ){
    0          
333 0         0 $self->{'local_port_name'}=$self->{'local_port'};
334 0         0 my $service=getservbyname($self->{'local_port_name'}, undef);
335 0 0       0 if (defined( $service )){
336 0         0 $self->{'local_port'}=$service;
337             }
338             }elsif( $self->{'local_port'} =~ /^[0-9]+$/ ){
339 0         0 $self->{'local_port_name'}=getservbyport( $self->{'local_port'}, 'tcp' );
340             }
341 0 0       0 if ( $self->{'foreign_port'} =~ /[A-Za-z]/ ){
    0          
342 0         0 $self->{'foreign_port_name'}=$self->{'foreign_port'};
343 0         0 my $service=getservbyname($self->{'foreign_port_name'}, undef);
344 0 0       0 if (defined( $service )){
345 0         0 $self->{'foreign_port'}=$service;
346             }
347             }elsif( $self->{'foreign_port'} =~ /^[0-9]+$/ ){
348 0         0 $self->{'foreign_port_name'}=getservbyport( $self->{'foreign_port'}, 'tcp' );
349             }
350             }else{
351             # If the port is non-numeric, set it as the port name
352 5 50       12 if ( $self->{'local_port'} =~ /[A-Za-z]/ ){
353 0         0 $self->{'local_port_name'}=$self->{'local_port'};
354             }
355 5 50       16 if ( $self->{'foreign_port'} =~ /[A-Za-z]/ ){
356 0         0 $self->{'foreign_port_name'}=$self->{'foreign_port'};
357             }
358             }
359              
360 5         30 my $dns=Net::DNS::Resolver->new;
361              
362             # resolve PTRs if asked to
363 5 50 33     707 if (
364             defined( $args{ptrs} ) &&
365             $args{ptrs}
366             ){
367             # process foreign_host
368 0 0 0     0 if (
369             ( $self->{'foreign_host'} =~ /[A-Za-z]/ ) &&
370             ( $self->{'foreign_host'} !~ /\:/ )
371             ){
372             # appears to be a hostname already
373 0         0 $self->{'foreign_ptr'}=$self->{'foreign_host'};
374             }else{
375             # attempt to resolve it
376 0         0 eval{
377 0         0 my $answer=$dns->search( $self->{'foreign_host'} );
378 0 0 0     0 if ( defined( $answer->{answer}[0] ) &&
379             ( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
380             ){
381 0         0 $self->{'foreign_ptr'}=lc($answer->{answer}[0]->ptrdname);
382             }
383             }
384             }
385             # process local_host
386 0 0 0     0 if (
387             ( $self->{'local_host'} =~ /[A-Za-z]/ ) &&
388             ( $self->{'local_host'} !~ /\:/ )
389             ){
390             # appears to be a hostname already
391 0         0 $self->{'local_ptr'}=$self->{'local_host'};
392             }else{
393             # attempt to resolve it
394 0         0 eval{
395 0         0 my $answer=$dns->search( $self->{'local_host'} );
396 0 0 0     0 if ( defined( $answer->{answer}[0] ) &&
397             ( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
398             ){
399 0         0 $self->{'local_ptr'}=lc($answer->{answer}[0]->ptrdname);
400             }
401             }
402             }
403             }else{
404             # We are not doing auto PTR resolving...
405             # just set them if it appears to be a hostname
406 5 50 33     15 if (
407             ( $self->{'foreign_host'} =~ /[A-Za-z]/ ) &&
408             ( $self->{'foreign_host'} !~ /\:/ )
409             ){
410 0         0 $self->{'foreign_ptr'}=$self->{'foreign_host'};
411             }
412 5 50 33     11 if (
413             ( $self->{'local_host'} =~ /[A-Za-z]/ ) &&
414             ( $self->{'local_host'} !~ /\:/ )
415             ){
416 0         0 $self->{'local_ptr'}=$self->{'local_host'};
417             }
418             }
419              
420             # resolve the UID/username if asked
421 5 100 100     25 if (
    100 66        
422             $args{'uid_resolve'} &&
423             defined( $self->{'uid'} )
424             ){
425 1         2 eval{
426 1         635 my @pwline=getpwuid( $self->{'uid'} );
427 1 50       7 if ( defined( $pwline[0] ) ){
428 1         5 $self->{'username'}=$pwline[0];
429             }
430             }
431             }elsif (
432             $args{'uid_resolve'} &&
433             ( ! defined( $self->{'uid'} ) )
434             ){
435 1         2 eval{
436 1         86 my @pwline=getpwnam( $self->{'username'} );
437 1 50       5 if ( defined( $pwline[2] ) ){
438 1         4 $self->{'uid'}=$pwline[2];
439             }
440             }
441             }
442              
443 5         45 return $self;
444             }
445              
446             =head2 foreign_host
447              
448             Returns the foreign host.
449              
450             my $f_host=$conn->foreign_host;
451              
452             =cut
453              
454             sub foreign_host{
455 0     0 1 0 return $_[0]->{'foreign_host'};
456             }
457              
458             =head2 foreign_port
459              
460             This returns the foreign port.
461              
462             my $f_port=$conn->foreign_port;
463              
464             =cut
465              
466             sub foreign_port{
467 0     0 1 0 return $_[0]->{'foreign_port'};
468             }
469              
470             =head2 foreign_port_name
471              
472             This returns the foreign port name.
473              
474             This may potentially return undef if one is
475             not set/unknown.
476              
477             my $f_port=$conn->foreign_port;
478              
479             =cut
480              
481             sub foreign_port_name{
482 0     0 1 0 return $_[0]->{'foreign_port_name'};
483             }
484              
485             =head2 foreign_ptr
486              
487             This returns the PTR for the foreign host.
488              
489             If one was not supplied or if it could not be found
490             if resolving was enabled then undef will be returned.
491              
492             my $f_ptr=$conn->foreign_ptr;
493              
494             =cut
495              
496             sub foreign_ptr{
497 0     0 1 0 return $_[0]->{'foreign_ptr'};
498             }
499              
500             =head2 local_host
501              
502             Returns the local host.
503              
504             my $l_host=$conn->local_host;
505              
506             =cut
507              
508             sub local_host{
509 0     0 1 0 return $_[0]->{'local_host'};
510             }
511              
512             =head2 local_port
513              
514             This returns the local port.
515              
516             my $l_port=$conn->local_port;
517              
518             =cut
519              
520             sub local_port{
521 0     0 1 0 return $_[0]->{'local_port'};
522             }
523              
524             =head2 local_port_name
525              
526             This returns the local port name.
527              
528             This may potentially return undef if one is
529             not set/unknown.
530              
531             my $l_port=$conn->local_port;
532              
533             =cut
534              
535             sub local_port_name{
536 0     0 1 0 return $_[0]->{'local_port_name'};
537             }
538              
539             =head2 local_ptr
540              
541             This returns the PTR for the local host.
542              
543             If one was not supplied or if it could not be found
544             if resolving was enabled then undef will be returned.
545              
546             my $l_ptr=$conn->local_ptr;
547              
548             =cut
549              
550             sub local_ptr{
551 0     0 1 0 return $_[0]->{'local_ptr'};
552             }
553              
554             =head2 pctcpu
555              
556             Returns the percent of memory in use by the process
557             that has connection.
558              
559             This may not be if it was not set. Please see new
560             for more information.
561              
562             my $pctcpu=$conn->pctcpu;
563              
564             =cut
565              
566             sub pctcpu{
567 0     0 1 0 return $_[0]->{'pctcpu'};
568             }
569              
570             =head2 pctmem
571              
572             Returns the percent of memory in use by the process
573             that has connection.
574              
575             This may not be if it was not set. Please see new
576             for more information.
577              
578             my $pctmem=$conn->pctmem;
579              
580             =cut
581              
582             sub pctmem{
583 0     0 1 0 return $_[0]->{'pctmem'};
584             }
585              
586             =head2 pid
587              
588             This returns the pid of a connection.
589              
590             This may return undef.
591              
592             my $pid=$conn->pid;
593              
594             =cut
595              
596             sub pid{
597 0     0 1 0 return $_[0]->{'pid'};
598             }
599              
600             =head2 proc
601              
602             Returns the command line or fname for the process
603             that has the connection.
604              
605             This may not be if it was not set. Please see new
606             for more information.
607              
608             my $proc=$conn->proc;
609              
610             =cut
611              
612             sub proc{
613 0     0 1 0 return $_[0]->{'proc'};
614             }
615              
616             =head2 proto
617              
618             Returns the protocol in use by the connection.
619              
620             Please note this value with vary slightly between OSes.
621              
622             my $proto=$conn->proto;
623              
624             =cut
625              
626             sub proto{
627 0     0 1 0 return $_[0]->{'proto'};
628             }
629              
630             =head2 recvq
631              
632             Returns the size of the recieve queue the connection.
633              
634             This may return undef.
635              
636             my $recvq=$conn->recvq;
637              
638             =cut
639              
640             sub recvq{
641 0     0 1 0 return $_[0]->{'recvq'};
642             }
643              
644              
645             =head2 sendq
646              
647             Returns the size of the send queue the connection.
648              
649             This may return undef.
650              
651             my $sendq=$conn->sendq;
652              
653             =cut
654              
655             sub sendq{
656 0     0 1 0 return $_[0]->{'sendq'};
657             }
658              
659             =head2 state
660              
661             Returns the state the connection is currently in.
662              
663             Please note this value with vary slightly between OSes.
664              
665             my $state=$conn->state;
666              
667             =cut
668              
669             sub state{
670 0     0 1 0 return $_[0]->{'state'};
671             }
672              
673             =head2 uid
674              
675             Returns the UID that has the connection.
676              
677             This may not be if it was not set. Please see new
678             for more information.
679              
680             my $uid=$conn->uid;
681              
682             =cut
683              
684             sub uid{
685 2     2 1 10 return $_[0]->{'uid'};
686             }
687              
688             =head2 username
689              
690             Returns the username that has the connection.
691              
692             This may not be if it was not set. Please see new
693             for more information.
694              
695             my $username=$conn->username;
696              
697             =cut
698              
699             sub username{
700 2     2 1 12 return $_[0]->{'username'};
701             }
702              
703             =head2 wchan
704              
705             Returns the wchan for the process that has the connection.
706              
707             This may not be if it was not set. Please see new
708             for more information.
709              
710             my $wchan=$conn->wchan;
711              
712             =cut
713              
714             sub wchan{
715 0     0 1   return $_[0]->{'wchan'};
716             }
717              
718             =head1 AUTHOR
719              
720             Zane C. Bowers-Hadley, C<< >>
721              
722             =head1 BUGS
723              
724             Please report any bugs or feature requests to C, or through
725             the web interface at L. I will be notified, and then you'll
726             automatically be notified of progress on your bug as I make changes.
727              
728              
729              
730              
731             =head1 SUPPORT
732              
733             You can find documentation for this module with the perldoc command.
734              
735             perldoc Net::Connection
736              
737              
738             You can also look for information at:
739              
740             =over 4
741              
742             =item * RT: CPAN's request tracker (report bugs here)
743              
744             L
745              
746             =item * AnnoCPAN: Annotated CPAN documentation
747              
748             L
749              
750             =item * CPAN Ratings
751              
752             L
753              
754             =item * Search CPAN
755              
756             L
757              
758             =item * Repository
759              
760             L
761              
762             =back
763              
764              
765             =head1 ACKNOWLEDGEMENTS
766              
767              
768             =head1 LICENSE AND COPYRIGHT
769              
770             This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
771              
772             This is free software, licensed under:
773              
774             The Artistic License 2.0 (GPL Compatible)
775              
776              
777             =cut
778              
779             1; # End of Net::Connection