File Coverage

blib/lib/SVN/Utils/ClientIP.pm
Criterion Covered Total %
statement 34 46 73.9
branch 3 12 25.0
condition n/a
subroutine 7 8 87.5
pod 0 4 0.0
total 44 70 62.8


line stmt bran cond sub pod time code
1             ###########################################
2             package SVN::Utils::ClientIP;
3             ###########################################
4 2     2   69706 use strict;
  2         4  
  2         70  
5 2     2   10 use warnings;
  2         4  
  2         150  
6             require Exporter;
7             our @ISA = qw(Exporter);
8             our @EXPORT_OK = qw(ssh_client_ip);
9             our $VERSION = "0.02";
10              
11 2     2   1486 use Proc::ProcessTable;
  2         24840  
  2         114  
12 2     2   16 use Proc::Info::Environment;
  2         4  
  2         754  
13              
14             ###########################################
15             sub ssh_client_ip {
16             ###########################################
17              
18 0     0 0 0 my $finder = __PACKAGE__->new();
19              
20 0         0 my($ip, $pid, $port) = $finder->ssh_client_ip_find();
21            
22 0 0       0 if( !defined $ip ) {
23 0         0 warn $finder->error();
24             }
25              
26             # only IP
27 0         0 return $ip;
28             }
29              
30             ###########################################
31             sub new {
32             ###########################################
33 1     1 0 8153 my($class, %options) = @_;
34              
35 1         105 my $proc_info = Proc::Info::Environment->new();
36 1         3601 my $proc_table = Proc::ProcessTable->new();
37              
38 1         2214 my %ppid_of = ();
39 1         3 foreach my $proc ( @{$proc_table->table} ) {
  1         3109  
40 11         410 $ppid_of{ $proc->pid() } = $proc->ppid();
41             }
42              
43 1         55 my $self = {
44             proc_info => $proc_info,
45             ppid_of => \%ppid_of,
46             pid => $$,
47             ssh_client_var_name => "SSH_CLIENT",
48             error => undef,
49             %options
50             };
51              
52 1         52 bless $self, $class;
53             }
54              
55             ###########################################
56             sub ssh_client_ip_find {
57             ###########################################
58 1     1 0 7 my($self) = @_;
59              
60 1         8 my $pid = $self->{pid};
61              
62 1         8 while( exists $self->{ppid_of}->{ $pid } ) {
63              
64 1         3 $pid = $self->{ppid_of}->{ $pid };
65              
66 1 50       4 last if $pid == 0;
67              
68 1         7 my $env = $self->{proc_info}->env( $pid );
69              
70 1 50       154 if(! defined $env) {
71 1         13 $self->error( $self->{proc_info}->error() );
72 1         4 return undef;
73             }
74              
75 0 0       0 if( exists $env->{ $self->{ssh_client_var_name} } ) {
76 0         0 my($ip, $pid, $port) = split /\s+/,
77             $env->{ $self->{ssh_client_var_name} };
78              
79 0 0       0 if( wantarray ) {
80 0         0 return ($ip, $pid, $port);
81             }
82              
83 0         0 return $ip;
84             }
85             }
86              
87 0         0 $self->error( "Can't find $self->{ssh_client_var_name} anywhere" );
88 0         0 return undef;
89             }
90              
91             ###########################################
92             sub error {
93             ###########################################
94 1     1 0 8 my($self, $error) = @_;
95              
96 1 50       5 if(defined $error) {
97 1         4 $self->{error} = $error;
98             }
99              
100 1         2 return $self->{error};
101             }
102              
103             1;
104              
105             __END__