blib/lib/Net/Ident.pm

filter: Statementstmt 88.4% 215 / 243 Branchbran 62.9% 112 / 178 Conditioncond 67.7% 21 / 31 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 76.6% 23 / 30 Podpod 90.0% 9 / 10 totaltotal 75.9% 388 / 511 CC 100 SCAR 28.4
CC
100
Cov
77%
username
34.2
ready
29.4
newFromInAddr
17.7
CRAP 17.2
↑ Summary
1 package Net::Ident;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 require Exporter;
12  
13 13 our @ISA = qw(Exporter);
14 13 our @EXPORT_OK = qw(ident_lookup lookup lookupFromInAddr);
15 13 our @EXPORT_FAIL;
16 13 our %EXPORT_TAGS;
17  
18 # EXPORT_HOOKS is a sortof Exporter extension. Whenever one of the keys
19 # of this hash is imported as a "tag", the corresponding function is called
20 13 our %EXPORT_HOOKS = (
21 'fh' => \&_add_fh_method,
22 'apache' => \&_add_apache_method,
23 'debug' => \&_set_debug,
24 );
25  
26 # provide import magic
27 sub _export_hooks () {
28 13 ▶ my ( $tag, $hook );
Subroutine _export_hooks: called CC=2 SCAR=6.9
29 13 ▶ while ( ( $tag, $hook ) = each %EXPORT_HOOKS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Net::Ident::EXPORT_HOOKS) 39 13 52
30 39 my $hookname = "_export_hook_$tag"; # pseudo-function name
31 39 $EXPORT_TAGS{$tag} = [$hookname];
32 39 push @EXPORT_OK, $hookname;
33 39 push @EXPORT_FAIL, $hookname;
34 }
35 }
36  
37 # put the export hooks in the standard Exporter structures
38 13 _export_hooks();
39  
40 # for compatibility mode, uncomment the next line @@ s/^#\s*// @@
41 # our @EXPORT = qw(_export_hook_fh);
42  
43 13 our $VERSION = "1.29";
44  
45 13 our $DEBUG = 0;
46 13 *STDDBG = *STDERR;
47  
48 sub _set_debug {
49 6 ▶ $DEBUG++;
Subroutine _set_debug: called CC=1 SCAR=0.0
50 6 print STDDBG "Debugging turned to level $DEBUG\n";
51 }
52  
53 # protocol number for tcp.
54 13 ▶my $tcpproto = ( getprotobyname('tcp') )[2] || 6;
Condition50%1/2
(getprotobyname 'tcp')[2] || 6
l!l
013
MC/DC0%0/1
(getprotobyname 'tcp')[2] || 6
(getprotobyname 'tcp')[2]
Truth table50%1/2
A
A= (getprotobyname 'tcp')[2]
Aresult
11
00
55  
56 # get identd port (default to 113).
57 13 ▶my $identport = ( getservbyname( 'ident', 'tcp' ) )[2] || 113;
Condition50%1/2
(getservbyname 'ident', 'tcp')[2] || 113
l!l
013
MC/DC0%0/1
(getservbyname 'ident', 'tcp')[2] || 113
(getservbyname 'ident', 'tcp')[2]
Truth table50%1/2
A
A= (getservbyname 'ident', 'tcp')[2]
Aresult
11
00
58  
59 # turn a filehandle passed as a string, or glob, into a ref
60 # private subroutine
61 sub _passfh ($) {
62 7 ▶ my ($fh) = @_;
Subroutine _passfh: called CC=9 SCAR=22.2
63  
64 # test if $fh is a reference. if it's not, we need to process...
65 7 ▶ if ( !ref $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 2 5 7
66 2 ▶ print STDDBG "passed fh: $fh is not a reference\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 2 2
67  
68 # check for fully qualified name
69 2 ▶ if ( $fh !~ /'|::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh =~ /'|::/) 1 1 2
70 1 ▶ print STDDBG "$fh is not fully qualified\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 1 1
71  
72 # get our current package
73 1 my $mypkg = (caller)[0];
74 1 ▶ print STDDBG "We are package $mypkg\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 1 1
75  
76 # search for calling package
77 1 my $depth = 1;
78 1 my $otherpkg;
79 1 ▶ $depth++ while ( ( $otherpkg = caller($depth) ) eq $mypkg );
Branch: 1 branch
BranchTrueFalseTotal
($otherpkg = caller $depth) eq $mypkg and do { ++$depth } 1 1 2
80 1 ▶ print STDDBG "We are called from package $otherpkg\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 1 1
81 1 $fh = "${otherpkg}::$fh";
82 1 ▶ print STDDBG "passed fh now fully qualified: $fh\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 1 1
83 }
84  
85 # turn $fh into a reference to a $fh. we need to disable strict refs
86 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 $fh = \*{$fh};
88 }
89 7 $fh;
90 }
91  
92 # create a Net::Ident object, and perform a non-blocking connect()
93 # to the remote identd port.
94 # class method, constructor
95 sub new {
96 9 ▶ my ( $class, $fh, $timeout ) = @_;
Subroutine new: called CC=9 SCAR=22.1
Pod documented
97 9 my ( $localaddr, $remoteaddr );
98  
99 9 ▶ print STDDBG "Net::Ident::new fh=$fh, timeout=" . ( defined $timeout ? $timeout : "<undef>" ) . "\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 1 8 9
defined $timeout ? : 1 0 1
100 if $DEBUG > 1;
101  
102 # "try"
103 9 eval {
104 9 ▶ defined $fh or die "= fh undef\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or die "= fh undef\n" 2 7 9
105 7 $fh = _passfh($fh);
106  
107 # get information about this (the local) end of the connection. We
108 # assume that $fh is a connected socket of type SOCK_STREAM. If
109 # it isn't, you'll find out soon enough because one of these functions
110 # will return undef real fast.
111 7 ▶ $localaddr = getsockname($fh) or die "= getsockname failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$localaddr = getsockname $fh or die "= getsockname failed: $!\n" 5 2 7
112  
113 # get information about remote end of connection
114 2 ▶ $remoteaddr = getpeername($fh) or die "= getpeername failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$remoteaddr = getpeername $fh or die "= getpeername failed: $!\n" 1 1 2
115 };
116 9 ▶ if ( $@ =~ /^= (.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^= (.*)/) { } 8 1 9
elsif ($@) { } 0 1 1
117  
118 # here's the catch of the throw
119 # return false, try to preserve errno
120 8 local ($!);
121  
122 # we make a "fake" $self
123 8 my $self = {
124 'state' => 'error',
125 'error' => "Net::Ident::new: $1\n",
126 };
127 8 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 1 7 8
128  
129 # return our blessed $self
130 8 return bless $self, $class;
131 }
132 elsif ($@) {
133  
134 # something else went wrong. barf up completely.
135 0 confess($@);
136 }
137  
138 # continue with the NewFromInAddr constructor
139 1 $class->newFromInAddr( $localaddr, $remoteaddr, $timeout );
140 }
141  
142 sub newFromInAddr {
143 3 ▶ my ( $class, $localaddr, $remoteaddr, $timeout ) = @_;
Subroutine newFromInAddr: called CC=15 SCAR=28.7
Pod documented
144 3 my $self = {};
145  
146 0 ▶ print STDDBG "Net::Ident::newFromInAddr localaddr=", sub { inet_ntoa( $_[1] ) . ":$_[0]" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 ▶ ->( sockaddr_in($localaddr) ), ", remoteaddr=", sub { inet_ntoa( $_[1] ) . ":$_[0]" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 3 ▶ ->( sockaddr_in($remoteaddr) ), ", timeout=", defined $timeout ? $timeout : "<undef>", "\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 3 3
defined $timeout ? : 0 0 0
149 if $DEBUG > 1;
150  
151 3 eval {
152 # unpack addresses and store in
153 3 my ( $localip, $remoteip );
154 3 ( $self->{localport}, $localip ) = sockaddr_in($localaddr);
155 3 ( $self->{remoteport}, $remoteip ) = sockaddr_in($remoteaddr);
156  
157 # create a local binding port. We cannot bind to INADDR_ANY, it has
158 # to be bind (bound?) to the same IP address as the connection we're
159 # interested in on machines with multiple IP addresses
160 3 my $localbind = sockaddr_in( 0, $localip );
161  
162 # store max time
163 3 ▶ $self->{maxtime} = defined($timeout) ? time + $timeout : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 2 1 3
164  
165 # create a remote connect point
166 3 my $identbind = sockaddr_in( $identport, $remoteip );
167  
168 # create a new FileHandle
169 3 $self->{fh} = FileHandle->new;
170  
171 # create a stream socket.
172 3 ▶ socket( $self->{fh}, PF_INET, SOCK_STREAM, $tcpproto )
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, 2, 1, $tcpproto or die "= socket failed: $!\n" 0 3 3
173 or die "= socket failed: $!\n";
174  
175 # bind it to the same IP number as the local end of THESOCK
176 3 ▶ bind( $self->{fh}, $localbind ) or die "= bind failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
bind $self->{"fh"}, $localbind or die "= bind failed: $!\n" 1 2 3
177  
178 # make it a non-blocking socket
179 2 ▶ if ( $^O ne 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 2 0 2
180 2 ▶ $self->{fh}->blocking(0) or die "= set non-blocking failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->blocking(0) or die "= set non-blocking failed: $!\n" 0 2 2
181 }
182  
183 # connect it to the remote identd port, this can return EINPROGRESS.
184 # for some reason, reading $! twice doesn't work as it should
185 connect( $self->{fh}, $identbind )
186 or $!{EINPROGRESS}
187 2 ▶ or die "= connect failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
connect $self->{"fh"}, $identbind or $!{"EINPROGRESS"} or die "= connect failed: $!\n" 1 1 2
Condition66%2/3
connect $self->{"fh"}, $identbind or $!{"EINPROGRESS"}
l!l&&r!l&&!r
011
MC/DC50%1/2
connect $self->{"fh"}, $identbind or $!{"EINPROGRESS"}
connect $self->{"fh"}, $identbind $!{"EINPROGRESS"}
Truth table66%2/3
A or B
A= connect $self->{"fh"}, $identbindB= $!{"EINPROGRESS"}
ABresult
1X1
011
000
188 1 ▶ $self->{fh}->blocking(0) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 1 1
189 };
190 3 ▶ if ( $@ =~ /^= (.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^= (.*)/) { } 2 1 3
elsif ($@) { } 0 1 1
191  
192 # here's the catch of the throw
193 # return false, try to preserve errno
194 2 local ($!);
195 2 $self->{error} = "Net::Ident::new: $1\n";
196 2 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 2 2
197  
198 # this deletes the FileHandle, which gets closed,
199 # so that might change errno
200 2 delete $self->{fh};
201  
202 # do NOT return, so the constructor always succeeds
203 }
204 elsif ($@) {
205  
206 # something else went wrong. barf up completely.
207 0 confess($@);
208 }
209  
210 # clear errno in case it contains EINPROGRESS
211 3 $! = 0;
212  
213 # mark the state of the connection, consistent with new()
214 3 ▶ $self->{state} = $self->{error} ? 'error' : 'connect';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 2 1 3
215  
216 # return a blessed reference
217 3 bless $self, $class;
218 }
219  
220 # send the query to the remote daemon.
221 # object method
222 sub query {
223 44 ▶ my ($self) = @_;
Subroutine query: called CC=14 SCAR=27.4
Pod documented
224 44 my ( $wmask, $timeout, $fileno, $err, $query );
225  
226 44 ▶ print STDDBG "Net::Ident::query\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 44 44
227  
228 # bomb out if no fh
229 44 ▶ return undef unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 12 32 44
230  
231 # "try"
232 32 eval {
233 32 ▶ $self->{state} eq 'connect' or die "= calling in the wrong order\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"state"} eq "connect" or die "= calling in the wrong order\n" 5 27 32
234 27 $fileno = fileno $self->{fh};
235  
236 # calculate the time left, abort if necessary. Note that $timeout
237 # is simply left undef if $self->{maxtime} is not defined
238 27 ▶ if ( defined( $self->{maxtime} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0) 0 27 27
Condition66%2/3
defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0
!ll&&!rl&&r
16110
MC/DC0%0/2
defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0
defined $self->{"maxtime"} ($timeout = $self->{"maxtime"} - time) < 0
Truth table66%2/3
A and B
A= defined $self->{"maxtime"}B= ($timeout = $self->{"maxtime"} - time) < 0
ABresult
0X0
100
111
239 && ( $timeout = $self->{maxtime} - time ) < 0 ) {
240 0 die "= Connection timed out\n";
241 }
242  
243 # wait until the socket becomes writable.
244 27 $wmask = '';
245 27 vec( $wmask, $fileno, 1 ) = 1;
246 27 ▶ scalar select( undef, $wmask, undef, $timeout )
Branch: 1 branch
BranchTrueFalseTotal
scalar select(undef, $wmask, undef, $timeout) or die "= Connection timed out\n" 0 27 27
247 or die "= Connection timed out\n";
248  
249 # fh must be writable now
250 27 ▶ vec( $wmask, $fileno, 1 ) or die "= connection timed out or error: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
vec $wmask, $fileno, 1 or die "= connection timed out or error: $!\n" 0 27 27
251  
252 # check for errors via getsockopt(SO_ERROR)
253 27 $err = getsockopt( $self->{fh}, SOL_SOCKET, SO_ERROR );
254 27 ▶ if ( !defined($err) || ( $! = unpack( 'L', $err ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $err or $! = unpack("L", $err)) 1 26 27
Condition66%2/3
not defined $err or $! = unpack("L", $err)
l!l&&r!l&&!r
0126
MC/DC50%1/2
not defined $err or $! = unpack("L", $err)
not defined $err $! = unpack("L", $err)
Truth table66%2/3
not(A) or B
A= not defined $errB= $! = unpack("L", $err)
ABresult
1X1
011
000
255 1 die "= connect: $!\n";
256 }
257  
258 # create the query, based on the remote port and the local port
259 26 $query = "$self->{remoteport},$self->{localport}\r\n";
260  
261 # write the query. Ignore the chance that such a short
262 # write will be fragmented.
263 26 ▶ syswrite( $self->{fh}, $query, length $query ) == length $query
Branch: 1 branch
BranchTrueFalseTotal
syswrite($self->{"fh"}, $query, length $query) == length $query or die "= fragmented write on socket: $!\n" 0 26 26
264 or die "= fragmented write on socket: $!\n";
265 };
266 32 ▶ if ( $@ =~ /^= (.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^= (.*)/) { } 6 26 32
elsif ($@) { } 0 26 26
267  
268 # here's the catch of the throw
269 # return false, try to preserve errno
270 6 local ($!);
271 6 $self->{error} = "Net::Ident::query: $1\n";
272 6 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 6 6
273  
274 # this deletes the FileHandle, which gets closed,
275 # so that might change errno
276 6 delete $self->{fh};
277 6 return undef;
278 }
279 elsif ($@) {
280  
281 # something else went wrong. barf up completely.
282 0 confess($@);
283 }
284  
285 # initialise empty answer to prevent uninitialised value warning
286 26 $self->{answer} = '';
287  
288 # mark the state of the connection
289 26 $self->{state} = 'query';
290  
291 # return the same object on success
292 26 $self;
293 }
294  
295 # read data, if any, and check if it's enough.
296 # object method
297 sub ready {
298 44 ▶ my ( $self, $blocking ) = @_;
Subroutine ready: called CC=23 SCAR=33.8
Pod documented
299 44 my ( $timeout, $rmask, $answer, $ret, $fileno );
300  
301 44 ▶ print STDDBG "Net::Ident::ready blocking=" . ( $blocking ? "true\n" : "false\n" ) if $DEBUG > 1;
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 44 44
$blocking ? : 0 0 0
302  
303 # exit immediately if ready returned 1 before.
304 44 ▶ if ( $self->{state} eq 'ready' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "ready") 7 37 44
305 7 return 1;
306 }
307  
308 # perform the query if not already done.
309 37 ▶ if ( $self->{state} ne 'query' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} ne "query") 7 30 37
310 7 ▶ $self->query or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->query or return undef 5 2 7
311 }
312  
313 # bomb out if no fh
314 32 ▶ return undef unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 0 32 32
315  
316 # "try"
317 32 $ret = eval {
318 32 $fileno = fileno $self->{fh};
319  
320 # while $blocking, but at least once...
321 32 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$blocking and do { do { if (defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0) { die "= Timeout\n"; } ; $timeout = 0 unless $blocking; $rmask = ""; vec($rmask, $fileno, 1) = 1; if (select $rmask, undef, undef, $timeout) { if (vec $rmask, $fileno, 1) { $answer = ""; my $nread = sysread($self->{"fh"}, $answer, 1000); unless (defined $nread) { die "= remote end closed connection\n" if $!{"ESPIPE"} or $!{"ECONNRESET"}; die "= read returned error: $!\n"; } ; $nread or die "= remote end closed connection\n"; $self->{"answer"} .= $answer; length $self->{"answer"} <= 1000 or die "= remote daemon babbling too much\n"; if ($self->{"answer"} =~ /[\n\r]/) { $self->{"answer"} =~ s/[\n\r].*//s; print STDDBG qq[Net::Ident::ready received: $self->{"answer"}\n] if $Net::Ident::DEBUG; close $self->{"fh"}; $self->{"state"} = "ready"; return 1; } ; } ; } } } 3 6 9
322 # calculate the time left, abort if necessary.
323 35 ▶ if ( defined( $self->{maxtime} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0) 0 35 35
Condition66%2/3
defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0
!ll&&!rl&&r
15200
MC/DC0%0/2
defined $self->{"maxtime"} and ($timeout = $self->{"maxtime"} - time) < 0
defined $self->{"maxtime"} ($timeout = $self->{"maxtime"} - time) < 0
Truth table66%2/3
A and B
A= defined $self->{"maxtime"}B= ($timeout = $self->{"maxtime"} - time) < 0
ABresult
0X0
100
111
324 && ( $timeout = $self->{maxtime} - time ) < 0 ) {
325 0 die "= Timeout\n";
326 }
327  
328 # zero timeout for non-blocking
329 35 ▶ $timeout = 0 unless $blocking;
Branch: 1 branch
BranchTrueFalseTotal
unless $blocking 17 18 35
330  
331 # wait for something
332 35 $rmask = '';
333 35 vec( $rmask, $fileno, 1 ) = 1;
334 35 ▶ if ( select( $rmask, undef, undef, $timeout ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (select $rmask, undef, undef, $timeout) 32 3 35
335  
336 # check for incoming data
337 32 ▶ if ( vec( $rmask, $fileno, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $rmask, $fileno, 1) 32 0 32
338  
339 # try to read as much data as possible.
340 32 $answer = '';
341 32 my $nread = sysread( $self->{fh}, $answer, 1000 );
342 32 ▶ if ( !defined $nread ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nread) 0 32 32
343 # On Solaris, sysread on a socketpair may
344 # return ESPIPE instead of 0 for EOF.
345 # On Windows, close() without shutdown() sends
346 # TCP RST, causing ECONNRESET on the peer.
347 die "= remote end closed connection\n"
348 0 ▶ if $!{ESPIPE} || $!{ECONNRESET};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ESPIPE"} or $!{"ECONNRESET"} 0 0 0
Condition0%0/3
$!{"ESPIPE"} or $!{"ECONNRESET"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"ESPIPE"} or $!{"ECONNRESET"}
$!{"ESPIPE"} $!{"ECONNRESET"}
Truth table0%0/3
A or B
A= $!{"ESPIPE"}B= $!{"ECONNRESET"}
ABresult
1X1
011
000
349 0 die "= read returned error: $!\n";
350 }
351 $nread
352 32 ▶ or die "= remote end closed connection\n";
Branch: 1 branch
BranchTrueFalseTotal
$nread or die "= remote end closed connection\n" 4 28 32
353  
354 # append incoming data to total received
355 28 $self->{answer} .= $answer;
356  
357 # check for max length
358 28 ▶ length( $self->{answer} ) <= 1000
Branch: 1 branch
BranchTrueFalseTotal
length $self->{"answer"} <= 1000 or die "= remote daemon babbling too much\n" 1 27 28
359 or die "= remote daemon babbling too much\n";
360  
361 # if data contains a CR or LF, we are ready receiving.
362 # strip everything after and including the CR or LF and
363 # return success
364 27 ▶ if ( $self->{answer} =~ /[\n\r]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"answer"} =~ /[\n\r]/) 21 6 27
365 21 $self->{answer} =~ s/[\n\r].*//s;
366 21 ▶ print STDDBG "Net::Ident::ready received: $self->{answer}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 21 21
367 if $DEBUG;
368  
369 # close the socket to the remote identd
370 21 close( $self->{fh} );
371 21 $self->{state} = 'ready';
372 21 return 1;
373 }
374 }
375 }
376 } while $blocking;
377  
378 # we don't block, but we didn't receive everything yet... return false.
379 6 0;
380 };
381 32 ▶ if ( $@ =~ /^= (.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^= (.*)/) { } 5 27 32
elsif ($@) { } 0 27 27
382  
383 # here's the catch of the throw
384 # return undef, try to preserve errno
385 5 local ($!);
386 5 $self->{error} = "Net::Ident::ready: $1\n";
387 5 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 5 5
388  
389 # this deletes the FileHandle, which gets closed,
390 # so that might change errno
391 5 delete $self->{fh};
392 5 return undef;
393 }
394 elsif ($@) {
395  
396 # something else went wrong. barf up completely.
397 0 confess($@);
398 }
399  
400 # return the return value from the eval{}
401 27 $ret;
402 }
403  
404 # return the username from the rfc931 query return.
405 # object method
406 sub username {
407 31 ▶ my ($self) = @_;
Subroutine username: called CC=21 SCAR=35.3
Pod documented
408 my (
409 31 $remoteport, $localport, $port1, $port2, $replytype, $reply, $opsys,
410 $userid, $error
411 );
412  
413 31 ▶ print STDDBG "Net::Ident::username\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 31 31
414  
415 # wait for data, if necessary.
416 31 ▶ return wantarray ? ( undef, undef, $self->{error} ) : undef
Branch: 2 branches
BranchTrueFalseTotal
unless $self->ready(1) 4 27 31
wantarray ? : 3 1 4
417 unless $self->ready(1);
418  
419 # parse the received string, split it into parts.
420 27 ( $port1, $port2, $replytype, $reply ) = ( $self->{answer} =~ /^\s*(\d+)\s*,\s*(\d+)\s*:\s*(ERROR|USERID)\s*:\s*(.*)$/ );
421  
422 # make sure the answer parsed properly, and that the ports are the same.
423 27 ▶ if ( !defined($reply)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $reply or $self->{"remoteport"} != $port1 or $self->{"localport"} != $port2) 2 23 25
Condition100%6/6
not defined $reply or $self->{"remoteport"} != $port1 or $self->{"localport"} != $port2
l!l&&r!l&&!r
3123
not defined $reply or $self->{"remoteport"} != $port1
l!l&&r!l&&!r
2124
MC/DC100%3/3
not defined $reply or $self->{"remoteport"} != $port1 or $self->{"localport"} != $port2
not defined $reply $self->{"remoteport"} != $port1 $self->{"localport"} != $port2
Truth table100%4/4
not(A) or B or C
A= not defined $replyB= $self->{"remoteport"} != $port1C= $self->{"localport"} != $port2
ABCresult
1XX1
01X1
0011
0000
424 || ( $self->{remoteport} != $port1 )
425 || ( $self->{localport} != $port2 ) ) {
426 4 $self->{error} = "Net::Ident::username couldn't parse reply or port mismatch\n";
427 4 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 4 4
428 4 ▶ return wantarray ? ( undef, undef, $self->{error} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
429 }
430  
431 # check for error return type
432 23 ▶ if ( $replytype eq "ERROR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replytype eq 'ERROR') { } 10 13 23
433 10 ▶ print STDDBG "Net::Ident::username: lookup returned ERROR\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 10 10
434 10 $userid = undef;
435 10 $opsys = "ERROR";
436 10 ( $error = $reply ) =~ s/\s+$//;
437 }
438 else {
439 # a normal reply, parse the opsys and userid. Note that the opsys may
440 # contain \ escaped colons, which is why the hairy regexp is necessary.
441 13 ▶ unless ( ( $opsys, $userid ) = ( $reply =~ /\s*((?:[^\\:]+|\\.)*):(.*)$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $reply =~ /\s*((?:[^\\:]+|\\.)*):(.*)$/) 0 13 13
442  
443 # didn't parse properly, abort.
444 0 $self->{error} = "Net::Ident::username: couldn't parse userid\n";
445 0 ▶ print STDDBG $self->{error} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 0 0
446 0 ▶ return wantarray ? ( undef, undef, $self->{error} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
447 }
448  
449 # remove trailing whitespace, except backwhacked whitespaces from opsys
450 13 $opsys =~ s/([^\\])\s+$/$1/;
451  
452 # un-backwhack opsys.
453 13 $opsys =~ s/\\(.)/$1/g;
454  
455 # in all cases is leading whitespace removed from the username, even
456 # though rfc1413 mentions that it shouldn't be done, current
457 # implementation practice dictates otherwise. What insane OS would
458 # use leading whitespace in usernames anyway...
459 13 $userid =~ s/^\s+//;
460  
461 # Test if opsys is "special": if it contains a charset definition,
462 # or if it is "OTHER". This means that it is rfc1413-like, instead
463 # of rfc931-like. (Why can't they make these RFCs non-conflicting??? ;)
464 # Note that while rfc1413 (the one that superseded rfc931) indicates
465 # that _any_ characters following the final colon are part of the
466 # username, current implementation practice inserts a space there,
467 # even "modern" identd daemons.
468 # Also, rfc931 specifically mentions escaping characters, while
469 # rfc1413 does not mention it (it isn't really necessary). Anyway,
470 # I'm going to remove trailing whitespace from userids, and I'm
471 # going to un-backwhack them, unless the opsys is "special".
472 13 ▶ unless ( $opsys =~ /,/ || $opsys eq 'OTHER' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opsys =~ /,/ or $opsys eq "OTHER") 11 2 13
Condition100%3/3
$opsys =~ /,/ or $opsys eq "OTHER"
l!l&&r!l&&!r
1111
MC/DC100%2/2
$opsys =~ /,/ or $opsys eq "OTHER"
$opsys =~ /,/ $opsys eq "OTHER"
Truth table100%3/3
A or B
A= $opsys =~ /,/B= $opsys eq "OTHER"
ABresult
1X1
011
000
473  
474 # remove trailing whitespace, except backwhacked whitespaces.
475 11 $userid =~ s/([^\\])\s+$/$1/;
476  
477 # un-backwhack
478 11 $userid =~ s/\\(.)/$1/g;
479 }
480 13 $error = undef;
481 }
482  
483 # return the requested information, depending on whether in array context.
484 23 ▶ if ( $DEBUG > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Ident::DEBUG > 1) 0 23 23
485 0 print STDDBG "Net::Ident::username returns:\n";
486 0 ▶ print STDDBG "userid = ", defined $userid ? $userid : "<undef>", "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $userid ? : 0 0 0
487 0 ▶ print STDDBG "opsys = ", defined $opsys ? $opsys : "<undef>", "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $opsys ? : 0 0 0
488 0 ▶ print STDDBG "error = ", defined $error ? $error : "<undef>", "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
489 }
490 23 ▶ wantarray ? ( $userid, $opsys, $error ) : $userid;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 3 23
491 }
492  
493 # do the entire rfc931 lookup in one blow.
494 # exportable subroutine, not a method
495 sub lookup ($;$) {
496 0 ▶ my ( $fh, $timeout ) = @_;
Subroutine lookup: not called CC=3 SCAR=24.8
Pod documented
497  
498 0 ▶ print STDDBG "Net::Ident::lookup fh=$fh, timeout=", defined $timeout ? $timeout : "<undef>", "\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 0 0
defined $timeout ? : 0 0 0
499 if $DEBUG > 1;
500  
501 0 Net::Ident->new( $fh, $timeout )->username;
502 }
503  
504 # do the entire rfc931 lookup from two in_addr structs
505 sub lookupFromInAddr ($$;$) {
506 0 ▶ my ( $localaddr, $remoteaddr, $timeout ) = @_;
Subroutine lookupFromInAddr: not called CC=3 SCAR=24.8
Pod documented
507  
508 0 ▶ print STDDBG "Net::Ident::lookupFromInAddr localaddr=", sub { inet_ntoa( $_[1] ) . ":$_[0]" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
509 0 ▶ ->( sockaddr_in($localaddr) ), ", remoteaddr=", sub { inet_ntoa( $_[1] ) . ":$_[0]" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
510 0 ▶ ->( sockaddr_in($remoteaddr) ), ", timeout=", defined $timeout ? $timeout : "<undef>", "\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 0 0
defined $timeout ? : 0 0 0
511 if $DEBUG > 1;
512  
513 0 Net::Ident->newFromInAddr( $localaddr, $remoteaddr, $timeout )->username;
514 }
515  
516 # alias Net::Ident::ident_lookup to Net::Ident::lookup
517 sub ident_lookup ($;$);
518 13 *ident_lookup = \&lookup;
519  
520 # prevent "used only once" warning
521 13 ident_lookup(0) if 0;
522  
523 # get the FileHandle ref from the object, to be used in an external select().
524 # object method
525 sub getfh ($) {
526 9 ▶ my ($self) = @_;
Subroutine getfh: called CC=1 SCAR=0.0
Pod documented
527  
528 9 $self->{fh};
529 }
530  
531 # get the last error message.
532 # object method
533 sub geterror ($) {
534 20 ▶ my ($self) = @_;
Subroutine geterror: called CC=1 SCAR=0.0
Pod documented
535  
536 20 $self->{error};
537 }
538  
539 # this is called whenever a function in @EXPORT_FAIL is imported.
540 # simply calls the installed export hooks from %EXPORT_HOOKS, or
541 # passes along the export_fail up the inheritance chain
542 sub export_fail {
543 7 ▶ my $pkg = shift;
Subroutine export_fail: called CC=5 SCAR=16.1
Pod undocumented
544 7 my $fail;
545 my @other;
546 7 foreach $fail (@_) {
547 9 ▶ if ( $fail =~ /^_export_hook_(.*)$/ && $EXPORT_HOOKS{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fail =~ /^_export_hook_(.*)$/ and $Net::Ident::EXPORT_HOOKS{$1}) { } 7 2 9
Condition66%2/3
$fail =~ /^_export_hook_(.*)$/ and $Net::Ident::EXPORT_HOOKS{$1}
!ll&&!rl&&r
207
MC/DC50%1/2
$fail =~ /^_export_hook_(.*)$/ and $Net::Ident::EXPORT_HOOKS{$1}
$fail =~ /^_export_hook_(.*)$/ $Net::Ident::EXPORT_HOOKS{$1}
Truth table66%2/3
A and B
A= $fail =~ /^_export_hook_(.*)$/B= $Net::Ident::EXPORT_HOOKS{$1}
ABresult
0X0
100
111
548 7 &{ $EXPORT_HOOKS{$1} };
549 }
550 else {
551 2 push @other, $fail;
552 }
553 }
554 7 ▶ if (@other) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other) 2 5 7
555 2 @other = $pkg->SUPER::export_fail(@other);
556 }
557 7 @other;
558 }
559  
560 # add lookup method for FileHandle objects. Note that this relies on the
561 # use FileHandle;
562 sub _add_fh_method {
563  
564 # determine package to add method to
565 5 ▶ my $pkg = grep( /^IO::/, @FileHandle::ISA ) ? "IO::Handle" : "FileHandle";
Subroutine _add_fh_method: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
grep(/^IO::/, @FileHandle::ISA) ? : 5 0 5
566  
567 # insert method in package. Arguments are already OK for std lookup
568 # turn off strict refs for this glob-mangling trick
569 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
570 5 *{"${pkg}::ident_lookup"} = \&lookup;
571  
572 5 ▶ print STDDBG "Added ${pkg}::ident_lookup method\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 5 5
573 }
574  
575 sub _add_apache_method {
576  
577 # add method to Apache::Connection class
578 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
579 2 *{"Apache::Connection::ident_lookup"} = sub {
580 0 ▶ my ( $self, $timeout ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
581  
582 0 ▶ print STDDBG "Apache::Connection::ident_lookup self=$self, ", "timeout=", defined $timeout ? $timeout : "<undef>", "\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Net::Ident::DEBUG > 1 0 0 0
defined $timeout ? : 0 0 0
583 if $DEBUG > 1;
584 0 lookupFromInAddr( $self->local_addr, $self->remote_addr, $timeout );
585 2 ▶ };
Subroutine _add_apache_method: called CC=2 SCAR=7.2
586  
587 2 ▶ print STDDBG "Added Apache::Connection::ident_lookup method\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Ident::DEBUG 0 2 2
588 }
589  
590 13 1;
591  
592 __END__
↑ Summary