blib/lib/POE/Component/Server/Ident.pm

filter: Statementstmt 85.0% 125 / 147 Branchbran 58.3% 28 / 48 Conditioncond 37.5% 9 / 24 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 84.0% 21 / 25 Podpod 100% 7 / 7 totaltotal 72.6% 191 / 263 CC 39 SCAR 18.5
CC
39
Cov
74%
ident_server_error
20.0
_valid_ports
17.6
spawn
6.0
CRAP 6.3
↑ Summary
1 package POE::Component::Server::Ident;
2 6 $POE::Component::Server::Ident::VERSION = '1.18';
3 #ABSTRACT: A POE component that provides non-blocking ident services to your sessions.
4  
5 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use POE qw( Wheel::SocketFactory Wheel::ReadWrite Driver::SysRW
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 Filter::Line );
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶sub spawn {
Subroutine spawn: called CC=6 SCAR=18.0
Pod documented
14 5 my $package = shift;
15 5 my %opts = @_;
16 5 $opts{lc $_} = delete $opts{$_} for keys %opts;
17  
18 5 ▶ $opts{bindport} = 113 unless defined $opts{bindport};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"bindport"} 0 5 5
19 5 ▶ $opts{multiple} = 0 unless $opts{multiple};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"multiple"} 4 1 5
20 5 ▶ $opts{timeout} = 60 unless $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"timeout"} 5 0 5
21 5 ▶ $opts{random} = 0 unless $opts{random};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"random"} 4 1 5
22  
23 5 my $self = bless \%opts, $package;
24  
25 $self->{session_id} = POE::Session->create (
26 object_states => [
27 $self => { _start => '_server_start',
28 'shutdown' => '_server_close',
29 5 map { ( $_ => '_' . $_ ) } qw(accept_new_client accept_failed),
30 },
31 $self => [ qw(register unregister) ],
32 ],
33 )->ID();
34 5 return $self;
35 }
36  
37 0 ▶sub session_id {
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
38 0 return $_[0]->{session_id};
39 }
40  
41 5 ▶sub getsockname {
Subroutine getsockname: called CC=1 SCAR=0.0
Pod documented
42 5 my $self = shift;
43 5 return $self->{listener}->getsockname();
44 }
45  
46 5 ▶sub _server_start {
Subroutine _server_start: called CC=4 SCAR=14.5
47 5 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
48 5 $self->{session_id} = $session->ID();
49  
50 5 ▶ $kernel->alias_set( $self->{alias} ) if $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alias"} 5 0 5
51 5 ▶ $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 0 5 5
52  
53 $self->{listener} = POE::Wheel::SocketFactory->new (
54 BindPort => $self->{bindport},
55 5 ▶ ( $self->{bindaddr} ? (BindAddr => $self->{bindaddr}) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bindaddr'} ? : 5 0 5
56 Reuse => 'on',
57 SuccessEvent => 'accept_new_client',
58 FailureEvent => 'accept_failed',
59 );
60 5 undef;
61 }
62  
63 5 ▶sub _server_close {
Subroutine _server_close: called CC=5 SCAR=16.1
64 5 my ($kernel,$self) = @_[KERNEL,OBJECT];
65 5 $kernel->alias_remove( $_ ) for $kernel->alias_list();
66 5 ▶ $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 0 5 5
67 5 $kernel->post( $_, 'client_timeout' ) for %{ $self->{clients} };
68 5 delete $self->{listener};
69 5 $kernel->refcount_decrement( $_, __PACKAGE__ ) for keys %{ $self->{sessions} };
70 5 undef;
71 }
72  
73 5 ▶sub _accept_new_client {
Subroutine _accept_new_client: called CC=1 SCAR=0.0
74 5 my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0 .. ARG2];
75 5 $peeraddr = inet_ntoa($peeraddr);
76  
77 POE::Session->create (
78 object_states => [
79 $self => { _start => '_client_start',
80 _stop => '_client_stop',
81 5 map { ( $_ => '_' . $_ ) } qw(client_input client_error client_done client_timeout client_default),
82 },
83 $self => [ qw(ident_server_reply ident_server_error) ],
84 ],
85 args => [ $socket, $peeraddr, $peerport ],
86 );
87 5 undef;
88 }
89  
90 0 ▶sub _accept_failed {
Subroutine _accept_failed: not called CC=1 SCAR=6.9
91 0 my ($kernel,$self,$function,$error) = @_[KERNEL,OBJECT,ARG0,ARG2];
92 0 my $package = ref $self;
93  
94 0 $kernel->call ( $self->{session_id}, 'shutdown' );
95  
96 0 warn "$package: call to $function() failed: $error";
97 0 undef;
98 }
99  
100 5 ▶sub register {
Subroutine register: called CC=3 SCAR=11.6
Pod documented
101 5 my ($kernel,$self,$sender,$session) = @_[KERNEL,OBJECT,SENDER,SESSION];
102 5 $sender = $sender->ID();
103 5 $session = $session->ID();
104  
105 5 $self->{sessions}->{ $sender }++;
106 $kernel->refcount_increment( $sender => __PACKAGE__ )
107 5 ▶ if $self->{sessions}->{ $sender } == 1 and $sender ne $session;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sessions"}{$sender} == 1 and $sender ne $session 5 0 5
Condition33%1/3
$self->{"sessions"}{$sender} == 1 and $sender ne $session
!ll&&!rl&&r
005
MC/DC0%0/2
$self->{"sessions"}{$sender} == 1 and $sender ne $session
$self->{"sessions"}{$sender} == 1 $sender ne $session
Truth table33%1/3
A and B
A= $self->{"sessions"}{$sender} == 1B= $sender ne $session
ABresult
0X0
100
111
108 5 undef;
109 }
110  
111  
112 4 ▶sub unregister {
Subroutine unregister: called CC=3 SCAR=12.0
Pod documented
113 4 my ($kernel,$self,$sender,$session) = @_[KERNEL,OBJECT,SENDER,SESSION];
114 4 my $thing = delete $self->{sessions}->{ $sender };
115 4 ▶ $kernel->refcount_decrement( $sender => __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
if $thing and $sender ne $session 0 4 4
Condition33%1/3
$thing and $sender ne $session
!ll&&!rl&&r
400
MC/DC0%0/2
$thing and $sender ne $session
$thing $sender ne $session
Truth table33%1/3
A and B
A= $thingB= $sender ne $session
ABresult
0X0
100
111
116 if $thing and $sender ne $session;
117 4 return;
118 }
119  
120 5 ▶sub _client_start {
Subroutine _client_start: called CC=2 SCAR=6.9
121 5 my ($kernel,$session,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,SESSION,OBJECT,ARG0,ARG1,ARG2];
122 5 my $session_id = $session->ID();
123  
124 5 $self->{clients}->{ $session_id }->{PeerAddr} = $peeraddr;
125 5 $self->{clients}->{ $session_id }->{PeerPort} = $peerport;
126  
127 $self->{clients}->{ $session_id }->{readwrite} =
128 POE::Wheel::ReadWrite->new(
129 Handle => $socket,
130 Filter => POE::Filter::Line->new( Literal => "\x0D\x0A" ),
131 InputEvent => 'client_input',
132 ErrorEvent => 'client_error',
133 5 ▶ ( $self->{'multiple'} ? () : ( FlushedEvent => 'client_timeout' ) ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'multiple'} ? : 1 4 5
134 );
135  
136 # Set a delay to close the connection if we are idle for 60 seconds.
137 5 $kernel->delay ( 'client_timeout' => $self->{'timeout'} );
138 5 undef;
139 }
140  
141 5 ▶sub _client_stop {
Subroutine _client_stop: called CC=1 SCAR=0.0
142 5 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
143  
144 5 $kernel->delay ( 'client_timeout' => undef );
145 5 delete $self->{clients}->{ $session->ID };
146 5 undef;
147 }
148  
149 5 ▶sub _client_input {
Subroutine _client_input: called CC=5 SCAR=16.1
150 5 my ($kernel,$self,$session,$input) = @_[KERNEL,OBJECT,SESSION,ARG0];
151 5 my $session_id = $session->ID();
152  
153 # Parse what is passed. We want <number>,<number> or nothing.
154  
155 5 ▶ if ( $input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1,$2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)) { } 4 1 5
Condition66%2/3
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)
!ll&&!rl&&r
104
MC/DC50%1/2
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ _valid_ports($1, $2)
Truth table66%2/3
A and B
A= $input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/B= _valid_ports($1, $2)
ABresult
0X0
100
111
156 4 my $port1 = $1; my $port2 = $2;
157 4 $self->{clients}->{ $session_id }->{'Port1'} = $port1;
158 4 $self->{clients}->{ $session_id }->{'Port2'} = $port2;
159 # Okay got a sort of valid query. Send it to all interested sessions.
160 4 $kernel->call( $_ => 'identd_request' => $self->{clients}->{ $session_id }->{PeerAddr} => $port1 => $port2 ) for keys %{ $self->{sessions} };
161 4 $kernel->delay ( 'client_default' => 10 );
162 } else {
163 # Client sent us rubbish.
164 1 $self->{clients}->{ $session_id }->{readwrite}->put("0 , 0 : ERROR : INVALID-PORT");
165 }
166 5 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 1 4 5
167 5 undef;
168 }
169  
170 0 ▶sub _client_done {
Subroutine _client_done: not called CC=1 SCAR=6.9
171 0 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
172 0 $kernel->delay ( 'client_timeout' => undef );
173 0 delete $self->{clients}->{ $session->ID };
174 0 undef;
175 }
176  
177 1 ▶sub _client_error {
Subroutine _client_error: called CC=1 SCAR=0.0
178 1 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
179 1 $kernel->delay ( 'client_timeout' => undef );
180 1 delete $self->{clients}->{ $session->ID }->{readwrite};
181 1 undef;
182 }
183  
184 4 ▶sub _client_timeout {
Subroutine _client_timeout: called CC=1 SCAR=0.0
185 4 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
186 4 $kernel->delay ( 'client_timeout' => undef );
187 4 $kernel->delay ( 'client_default' => undef );
188 4 delete $self->{clients}->{ $session->ID }->{readwrite};
189 4 undef;
190 }
191  
192 3 ▶sub _client_default {
Subroutine _client_default: called CC=6 SCAR=17.9
193 3 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
194 3 my $session_id = $session->ID();
195  
196 3 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'};
197 SWITCH: {
198 3 ▶ if ( $self->{'default'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default"}) 1 2 3
199 1 $reply .= " : USERID : UNIX : " . $self->{'default'};
200 1 last SWITCH;
201 }
202 2 ▶ if ( $self->{'random'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"random"}) 1 1 2
203 1 srand( $session_id * $$ );
204 1 my @numbers;
205 1 push @numbers, int rand (26) for 1 .. 8;
206 1 my $user_id = join '', map { chr($_+97) } @numbers;
207 1 $reply .= " : USERID : UNIX : $user_id";
208 1 last SWITCH;
209 }
210 1 $reply .= " : ERROR : HIDDEN-USER";
211 }
212 3 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if defined $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"clients"}{$session_id}{"readwrite"} 3 0 3
213 3 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 3 3
214 3 undef;
215 }
216  
217 1 ▶sub ident_server_reply {
Subroutine ident_server_reply: called CC=4 SCAR=14.2
Pod documented
218 1 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
219 1 my $session_id = $session->ID();
220  
221 1 my ($opsys,$userid) = @_[ARG0 .. ARG1];
222  
223 1 ▶ $opsys = "UNIX" unless defined ( $opsys );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opsys 0 1 1
224  
225 1 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'} . " : USERID : " . $opsys . " : " . $userid;
226  
227 1 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"clients"}{$session_id}{"readwrite"} 1 0 1
228 1 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 1 1
229 1 $kernel->delay ( 'client_default' => undef );
230 1 undef;
231 }
232  
233 0 ▶sub ident_server_error {
Subroutine ident_server_error: not called CC=4 SCAR=30.0
Pod documented
234 0 my ($kernel,$self,$session,$error_type) = @_[KERNEL,OBJECT,SESSION,ARG0];
235 0 my $session_id = $session->ID();
236 0 $error_type = uc $error_type;
237  
238 0 ▶ unless ( grep {$_ eq $error_type} qw(INVALID-PORT NO-USER HIDDEN-USER UNKNOWN-ERROR) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $error_type;} "INVALID-PORT", "NO-USER", "HIDDEN-USER", "UNKNOWN-ERROR") 0 0 0
239 0 $error_type = 'UNKNOWN-ERROR';
240 }
241  
242 0 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'} . " : ERROR : " . $error_type;
243  
244 0 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"clients"}{$session_id}{"readwrite"} 0 0 0
245 0 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 0 0
246 0 $kernel->delay ( 'client_default' => undef );
247 0 undef;
248 }
249  
250 4 ▶sub _valid_ports {
Subroutine _valid_ports: called CC=7 SCAR=28.7
251 4 my ($port1,$port2) = @_;
252  
253 4 ▶ return 1 if ( defined ( $port1 ) and defined ( $port2 ) ) and ( $port1 >= 1 and $port1 <= 65535 ) and ( $port2 >= 1 and $port2 <= 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535) 4 0 4
Condition33%5/15
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535)
!ll&&!rl&&r
004
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535)
!ll&&!rl&&r
004
defined $port1 and defined $port2
!ll&&!rl&&r
004
$port1 >= 1 and $port1 <= 65535
!ll&&!rl&&r
004
$port2 >= 1 and $port2 <= 65535
!ll&&!rl&&r
004
MC/DC0%0/6
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535)
defined $port1 defined $port2 $port1 >= 1 $port1 <= 65535 $port2 >= 1 $port2 <= 65535
Truth table14%1/7
A and B and C and D and E and F
A= defined $port1B= defined $port2C= $port1 >= 1D= $port1 <= 65535E= $port2 >= 1F= $port2 <= 65535
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
254 0 return 0;
255 }
256  
257 6 qq{Papers, please};
258  
259 __END__
↑ Summary