blib/lib/Net/TFTP.pm

filter: Statementstmt 13.9% 52 / 373 Branchbran 2.6% 7 / 264 Conditioncond 4.2% 2 / 47 MC/DCmcdc 3.7% 1 / 27 Subroutinesub 29.7% 11 / 37 Podpod 100% 14 / 14 totaltotal 11.4% 87 / 762 CC 150 SCAR 49.3
CC
150
Cov
9%
_read
462.0
put
210.0
_write
210.0
CRAP 138.4
↑ Summary
1 # Net::TFTP.pm
2 #
3 # Copyright (c) 1998,2007 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package Net::TFTP;
8  
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 $VERSION = "0.1901";
14  
15 sub RRQ () { 01 } # read request
16 sub WRQ () { 02 } # write request
17 sub DATA () { 03 } # data packet
18 sub ACK () { 04 } # acknowledgement
19 sub ERROR () { 05 } # error code
20 sub OACK () { 06 } # option acknowledgement
21  
22 3 my @NAME = qw(. RRQ WRQ DATA ACK ERR OACK);
23  
24 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 3 my $pkg = shift;
26 3 my $host = shift;
27  
28 3 bless {
29 Debug => 0, # Debug off
30 Timeout => 5, # resend after 5 seconds
31 Retries => 5, # resend max 5 times
32 Port => 69, # tftp port number
33 BlockSize => 0, # use default blocksize (512)
34 IpMode => 'v4', # Operate in IPv6 mode, off by default
35 Mode => 'netascii', # transfer in netascii
36 @_, # user overrides
37 Host => $host, # the hostname
38 }, $pkg;
39 }
40  
41 0 ▶sub timeout {
Subroutine timeout: not called CC=2 SCAR=17.9
Pod documented
42 0 my $self = shift;
43 0 my $v = $self->{'Timeout'};
44 0 ▶ $self->{'Timeout'} = 0 + shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
45 0 $v
46 }
47  
48 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
49 0 my $self = shift;
50 0 my $v = $self->{'Debug'};
51 0 ▶ $self->{'Debug'} = 0 + shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
52 0 $v
53 }
54  
55 0 ▶sub port {
Subroutine port: not called CC=2 SCAR=17.9
Pod documented
56 0 my $self = shift;
57 0 my $v = $self->{'Port'};
58 0 ▶ $self->{'Port'} = 0 + shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
59 0 $v
60 }
61  
62 0 ▶sub retries {
Subroutine retries: not called CC=2 SCAR=17.9
Pod documented
63 0 my $self = shift;
64 0 my $v = $self->{'Retries'};
65 0 ▶ $self->{'Retries'} = 0 + shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
66 0 $v
67 }
68  
69 0 ▶sub block_size {
Subroutine block_size: not called CC=2 SCAR=17.9
Pod documented
70 0 my $self = shift;
71 0 my $v = $self->{'BlockSize'};
72 0 ▶ $self->{'BlockSize'} = 0 + shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
73 0 $v
74 }
75  
76 0 ▶sub host {
Subroutine host: not called CC=2 SCAR=17.9
Pod documented
77 0 my $self = shift;
78 0 my $v = $self->{'Host'};
79 0 ▶ $self->{'Host'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
80 0 $v
81 }
82  
83 0 ▶sub ip_mode {
Subroutine ip_mode: not called CC=2 SCAR=17.9
Pod documented
84 0 my $self = shift;
85 0 my $v = $self->{'IpMode'};
86 0 ▶ $self->{'IpMode'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
87 0 $v
88 }
89  
90 0 ▶sub ascii {
Subroutine ascii: not called CC=1 SCAR=6.9
Pod documented
91 0 $_[0]->mode('netascii');
92 }
93  
94 0 ▶sub binary {
Subroutine binary: not called CC=1 SCAR=6.9
Pod documented
95 0 $_[0]->mode('octet');
96 }
97  
98 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
99 3 *netascii = \&ascii;
100 3 *octet = \&binary;
101 }
102  
103 0 ▶sub mode {
Subroutine mode: not called CC=3 SCAR=24.8
Pod documented
104 0 my $self = shift;
105 0 my $v = $self->{'Mode'};
106 0 ▶ $self->{'Mode'} = lc($_[0]) eq "netascii" ? "netascii" : "octet"
Branch: 2 branches
BranchTrueFalseTotal
if @_ 0 0 0
lc $_[0] eq 'netascii' ? : 0 0 0
107 if @_;
108 0 $v
109 }
110  
111 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113 exists $self->{'error'}
114 0 ▶ ? $self->{'error'}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'error'} ? : 0 0 0
115 : undef;
116 }
117  
118 2 ▶sub get {
Subroutine get: called CC=13 SCAR=39.2
Pod documented
119 2 my($self,$remote) = splice(@_,0,2);
120 2 ▶ my $local = shift if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 2 0 2
121 2 my %arg = ( %$self, @_ );
122  
123 2 delete $self->{'error'};
124  
125 2 my $io = Net::TFTP::IO->new($self,\%arg,RRQ,$remote);
126  
127 2 ▶ return $io
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and defined $io 1 1 2
Condition66%2/3
defined $local and defined $io
!ll&&!rl&&r
011
MC/DC50%1/2
defined $local and defined $io
defined $local defined $io
Truth table66%2/3
A and B
A= defined $localB= defined $io
ABresult
0X0
100
111
128 unless defined($local) && defined($io);
129  
130 1 my $file = $local;
131 1 ▶ unless(ref($local)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $local) 1 0 1
132 1 $local = IO::File->new($file,O_WRONLY|O_TRUNC|O_CREAT);
133 1 ▶ unless ($local) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local) 1 0 1
134 1 $self->{'error'} = "Can not open $file: $!";
135 1 return undef;
136 }
137 }
138  
139 0 ▶ binmode $local if $self->{'Mode'} eq 'octet';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Mode"} eq "octet" 0 0 0
140  
141 0 my($len,$pkt);
142 0 ▶ while($len = sysread($io,$pkt,10240)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = sysread($io, $pkt, 10240)) 0 0 0
143 0 ▶ if($len < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 0) { } 0 0 0
elsif (syswrite($local, $pkt, length $pkt) < 0) { } 0 0 0
144 0 $self->{'error'} = $io->error;
145 0 last;
146 }
147 elsif(syswrite($local,$pkt,length($pkt)) < 0) {
148 0 $self->{'error'} = "$!";
149 0 last;
150 }
151 }
152  
153 0 ▶ close($local)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
154 unless ref($file);
155  
156 0 ▶ $self->{'error'} = $io->error
Branch: 1 branch
BranchTrueFalseTotal
unless close $io 0 0 0
157 unless(close($io));
158  
159 0 ▶ exists $self->{'error'} ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'error'} ? : 0 0 0
160 }
161  
162 0 ▶sub put {
Subroutine put: not called CC=14 SCAR=53.5
Pod documented
163 0 my($self,$remote) = splice(@_,0,2);
164 0 my $local;
165 0 ▶ ($local,$remote) = ($remote,shift) if @_ %2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
166 0 my %arg = (%$self,@_);
167  
168 0 delete $self->{'error'};
169  
170 0 my $file;
171 0 ▶ if (defined $local) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $local) 0 0 0
172 0 $file = $local;
173 0 ▶ unless(ref($local)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $local) 0 0 0
174 0 ▶ unless ($local = IO::File->new($file,O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local = "IO::File"->new($file, 0)) 0 0 0
175 0 $self->{'error'} = "$file: $!";
176 0 return undef;
177 }
178 }
179 }
180  
181 0 my $io = Net::TFTP::IO->new($self,\%arg,WRQ,$remote);
182  
183 0 ▶ return $io
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and defined $io 0 0 0
Condition0%0/3
defined $local and defined $io
!ll&&!rl&&r
000
MC/DC0%0/2
defined $local and defined $io
defined $local defined $io
Truth table0%0/3
A and B
A= defined $localB= defined $io
ABresult
0X0
100
111
184 unless defined($local) && defined($io);
185  
186 0 ▶ binmode $local if $self->{'Mode'} eq 'octet';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Mode"} eq "octet" 0 0 0
187  
188 0 my($len,$pkt);
189 0 ▶ while($len = sysread($local,$pkt,10240)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = sysread($local, $pkt, 10240)) 0 0 0
190 0 ▶ if($len < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 0) { } 0 0 0
elsif (($len = syswrite($io, $pkt, length $pkt)) < 0) { } 0 0 0
191 0 $self->{'error'} = "$!";
192 0 last;
193 }
194 elsif(($len=syswrite($io,$pkt,length($pkt))) < 0) {
195 0 $self->{'error'} = $io->error;
196 0 last;
197 }
198 }
199  
200 0 ▶ close($local)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
201 unless ref($file);
202  
203 0 ▶ $self->{'error'} = $io->error
Branch: 1 branch
BranchTrueFalseTotal
unless close $io 0 0 0
204 unless(close($io));
205  
206 0 ▶ exists $self->{'error'} ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'error'} ? : 0 0 0
207 }
208  
209 package Net::TFTP::IO;
210  
211 3 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
212 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 3 @ISA = qw(IO::Handle);
216  
217 1 ▶sub new {
Subroutine new: called CC=12 SCAR=45.1
218 1 my($pkg,$tftp,$opts,$op,$remote) = @_;
219 1 my $io = $pkg->SUPER::new;
220  
221 1 $opts->{'Mode'} = lc($opts->{'Mode'});
222 1 $opts->{'IpMode'} = lc($opts->{'IpMode'});
223 $opts->{'Mode'} = "netascii"
224 1 ▶ unless $opts->{'Mode'} eq "octet";
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"Mode"} eq "octet" 1 0 1
225 1 $opts->{'ascii'} = lc($opts->{'Mode'}) eq "netascii";
226  
227 1 my $host = $opts->{'Host'};
228 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 1 0 1
229 1 $tftp->{'error'} = "No hostname given";
230 1 return undef;
231 } unless defined($host);
232  
233 ## jjmb - had to make an adjustment here the logic used originally does not work well
234 ## with IPv6.
235 0 my $port = undef;
236 0 ▶ if($opts->{'IpMode'} eq "v6") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'IpMode'} eq 'v6') { } 0 0 0
237 0 require Socket6;
238 0 require IO::Socket::INET6;
239 0 $port = $opts->{'Port'};
240 } else {
241 0 ▶ $port = $host =~ s/:(\d+)$// ? $1 : $opts->{'Port'};
Branch: 1 branch
BranchTrueFalseTotal
$host =~ s/:(\d+)$// ? : 0 0 0
242 }
243 0 my $addr = inet_aton($host);
244  
245 ## jjmb - added some logic here for the time being to prevent some errors from showing
246 0 ▶ if($opts->{'IpMode'} eq "v6") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'IpMode'} eq 'v6') { } 0 0 0
247 # Skipping validation
248 } else {
249 0 ▶ unless($addr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr) 0 0 0
250 0 $tftp->{'error'} = "Bad hostname '$host'";
251 0 return undef;
252 }
253 }
254  
255 ## jjmb - need to construct different objects depending on the IP version used
256 0 my $sock = undef;
257 0 ▶ if($opts->{'IpMode'} eq "v6") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'IpMode'} eq 'v6') { } 0 0 0
258 0 $sock = IO::Socket::INET6->new(PeerAddr => $opts->{'Host'}, Port => $opts->{'Port'}, Proto => 'udp');
259 } else {
260 0 $sock = IO::Socket::INET->new(Proto => 'udp');
261 }
262  
263 0 my $mode = $opts->{'Mode'};
264 0 my $pkt = pack("n a* c a* c", $op, $remote, 0, $mode, 0);
265  
266 0 ▶ if($opts->{'BlockSize'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"BlockSize"} > 0) 0 0 0
267 0 $pkt .= sprintf("blksize\0%d\0",$opts->{'BlockSize'});
268 }
269  
270 0 my $read = $op == Net::TFTP::RRQ;
271  
272 0 my $sel = IO::Select->new($sock);
273  
274 0 @{$opts}{'read','sock','sel','pkt','blksize'}
275 = ($read,$sock,$sel,$pkt,512);
276  
277 0 ▶ if($read) { # read
Branch: 1 branch
BranchTrueFalseTotal
if ($read) { } 0 0 0
278 0 @{$opts}{'ibuf','icr','blk'} = ('',0,1);
279 }
280 else { # write
281 0 @{$opts}{'obuf','blk','ack'} = ('',0,-1);
282 }
283  
284 0 ▶ if($tftp->{'IpMode'} eq "v6") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tftp->{'IpMode'} eq 'v6') { } 0 0 0
285 0 send($sock,$pkt,0,Socket6::sockaddr_in6($port,Socket6::inet_pton(AF_INET6,$host)));
286 } else {
287 0 send($sock,$pkt,0,pack_sockaddr_in($port,inet_aton($host)));
288 }
289 0 ▶ _dumppkt($sock,1,$pkt) if $opts->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Debug"} 0 0 0
290  
291 0 tie *$io, "Net::TFTP::IO",$opts;
292 0 $io;
293 }
294  
295 0 ▶sub error {
Subroutine error: not called CC=4 SCAR=30.0
296 0 my $self = shift;
297 0 ▶ my $tied = UNIVERSAL::isa($self,'GLOB') && tied(*$self) || $self;
Condition0%0/6
UNIVERSAL::isa($self, 'GLOB') && tied *$self || $self
l!l&&r!l&&!r
000
UNIVERSAL::isa($self, "GLOB") and tied *$self
!ll&&!rl&&r
000
MC/DC0%0/3
UNIVERSAL::isa($self, 'GLOB') && tied *$self || $self
UNIVERSAL::isa($self, "GLOB") tied *$self $self
Truth table0%0/5
A and B || C
A= UNIVERSAL::isa($self, "GLOB")B= tied *$selfC= $self
ABCresult
11X1
0X11
1011
0X00
1000
298 0 ▶ exists $tied->{'error'} ? $tied->{'error'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $tied->{'error'} ? : 0 0 0
299 }
300  
301 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
302 0 my $pkg = shift;
303 0 bless shift , $pkg;
304 }
305  
306 0 ▶sub PRINT {
Subroutine PRINT: not called CC=3 SCAR=24.8
307 0 my $self = shift;
308 # Simulate print
309 0 ▶ my $buf = join(defined($,) ? $, : "",@_) . defined($\) ? $\ : "";
Branch: 2 branches
BranchTrueFalseTotal
join(defined $, ? $, : '', @_) . defined($\) ? : 0 0 0
defined $, ? : 0 0 0
310  
311 # and with the proposed ?? syntax that would be
312 # $buf = join($, ?? "", @_) . $\ ?? "";
313  
314 0 $self->WRITE($buf,length($buf));
315 }
316  
317 0 ▶sub WRITE {
Subroutine WRITE: not called CC=6 SCAR=37.4
318 # $self, $buf, $len, $offset
319 0 my $self = shift;
320 0 ▶ my $buf = substr($_[0],$_[2] || 0,$_[1]);
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
321 0 my $offset = 0;
322  
323 0 ▶ $buf =~ s/([\n\r])/$1 eq "\n" ? "\015\012" : "\015\0"/soge
Branch: 1 branch
BranchTrueFalseTotal
$1 eq "\n" ? : 0 0 0
324 0 ▶ if ($self->{'ascii'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ascii"} 0 0 0
325  
326 0 $self->{'obuf'} .= substr($buf,$offset);
327  
328 0 ▶ while(length($self->{'obuf'}) >= $self->{'blksize'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"obuf"} >= $self->{"blksize"}) 0 0 0
329 0 ▶ return -1 if _write($self,1) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if _write($self, 1) < 0 0 0 0
330 }
331  
332 0 $_[1];
333 }
334  
335 0 ▶sub READLINE {
Subroutine READLINE: not called CC=8 SCAR=42.8
336 0 my $self = shift;
337  
338 # return undef (ie eof) unless we have an input buffer
339 return undef
340 0 ▶ if exists $self->{'error'} || !exists $self->{'ibuf'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"error"} or not exists $self->{"ibuf"} 0 0 0
Condition0%0/3
exists $self->{"error"} or not exists $self->{"ibuf"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"error"} or not exists $self->{"ibuf"}
exists $self->{"error"} not exists $self->{"ibuf"}
Truth table0%0/3
A or not(B)
A= exists $self->{"error"}B= not exists $self->{"ibuf"}
ABresult
1X1
011
000
341  
342 0 _read($self,0);
343  
344 0 while(1) {
345 0 my $sep;
346 # if $/ is undef then we slurp the whole file
347 0 ▶ if(defined($sep = $/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($sep = $/)) 0 0 0
348 # if $/ eq "" then we need to do paragraph mode
349 0 ▶ unless(length($sep)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $sep) 0 0 0
350 # when doing paragraph mode remove all leading \n's
351 0 $self->{'ibuf'} =~ s/^\n+//s;
352 0 $sep = "\n\n";
353 }
354 0 my $offset = index($self->{'ibuf'},$sep);
355 0 ▶ if($offset >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 0) 0 0 0
356 0 my $len = $offset+length($sep);
357 # With 5.005 I could use the 4-arg substr
358 0 my $ret = substr($self->{'ibuf'},0,$len);
359 0 substr($self->{'ibuf'},0,$len) = "";
360  
361 0 return $ret;
362 }
363 }
364  
365 0 my $res = _read($self,1);
366  
367 0 ▶ next if $res > 0; # We have some more, but do we have enough ?
Branch: 1 branch
BranchTrueFalseTotal
if $res > 0 0 0 0
368  
369 0 ▶ if ($res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res < 0) 0 0 0
370 # We have encountered an error, so
371 # force subsequent reads to return eof
372 0 delete $self->{'ibuf'};
373  
374 # And return undef (ie eof)
375 0 return undef;
376 }
377  
378 # $res == 0 so there is no more data to read, just return
379 # the buffer contents
380 0 return delete $self->{'ibuf'};
381 }
382  
383 # NOT REACHED
384 0 return;
385 }
386  
387 0 ▶sub READ {
Subroutine READ: not called CC=12 SCAR=50.5
388 # $self, $buf, $len, $offset
389  
390 0 my $self = shift;
391  
392 return undef
393 0 ▶ if exists $self->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"error"} 0 0 0
394  
395 return 0
396 0 ▶ unless exists $self->{'ibuf'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ibuf"} 0 0 0
397  
398 0 my $ret = length($self->{'ibuf'});
399  
400 0 ▶ unless ($self->{'eof'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 0 0
401 # If there is any data waiting, read it and ask for more
402 0 _read($self,0);
403  
404 # read until we have enough
405 0 ▶ while(($ret = length($self->{'ibuf'})) < $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ret = length $self->{"ibuf"}) < $_[1]) 0 0 0
406 0 ▶ last unless _read($self,1) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless _read($self, 1) > 0 0 0 0
407 }
408 }
409  
410 # Did we encounter an error
411 return undef
412 0 ▶ if exists $self->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"error"} 0 0 0
413  
414 # we may have too much
415 0 ▶ $ret = $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] < $ret 0 0 0
416 if $_[1] < $ret;
417  
418 # We are simulating read() so we may have to insert into $_[0]
419 0 ▶ if($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 0 0 0
420 0 ▶ if($_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
421 0 substr($_[0],$_[2]) = substr($self->{'ibuf'},0,$ret);
422 }
423 else {
424 0 $_[0] = substr($self->{'ibuf'},0,$ret);
425 }
426  
427 # remove what we placed into $_[0]
428 0 substr($self->{'ibuf'},0,$ret) = "";
429 }
430  
431 # If we are returning less than what was asked for
432 # then the next call must return eof
433 delete $self->{'ibuf'}
434 0 ▶ if $self->{'eof'} && length($self->{'ibuf'}) == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} and length $self->{"ibuf"} == 0 0 0 0
Condition0%0/3
$self->{"eof"} and length $self->{"ibuf"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"eof"} and length $self->{"ibuf"} == 0
$self->{"eof"} length $self->{"ibuf"} == 0
Truth table0%0/3
A and B
A= $self->{"eof"}B= length $self->{"ibuf"} == 0
ABresult
0X0
100
111
435  
436 0 $ret;
437 }
438  
439 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=13 SCAR=52.0
440 0 my $self = shift;
441  
442 0 ▶ if (exists $self->{'sock'} && !exists $self->{'closing'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"sock"} and not exists $self->{"closing"}) 0 0 0
Condition0%0/3
exists $self->{"sock"} and not exists $self->{"closing"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"sock"} and not exists $self->{"closing"}
exists $self->{"sock"} not exists $self->{"closing"}
Truth table0%0/3
A and not(B)
A= exists $self->{"sock"}B= not exists $self->{"closing"}
ABresult
0X0
100
111
443 0 $self->{'closing'} = 1;
444 0 ▶ if ($self->{'read'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'read'}) { } 0 0 0
445 0 ▶ unless ($self->{'eof'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 0 0
446 0 my $pkt = pack("nna*c",Net::TFTP::ERROR,0,"Premature close",0);
447 0 ▶ _dumppkt($self->{'sock'},1,$pkt) if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
448 send($self->{'sock'},$pkt,0,$self->{'peer'})
449 0 ▶ if $self->{'peer'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"peer"} 0 0 0
450 }
451 }
452 else {
453 # Clear the buffer
454 0 ▶ unless(exists $self->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"error"}) 0 0 0
455 0 ▶ while(length($self->{'obuf'}) >= $self->{'blksize'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"obuf"} >= $self->{"blksize"}) 0 0 0
456 0 ▶ last if _write($self) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if _write($self) < 0 0 0 0
457 }
458  
459 # Send the last block
460 0 $self->{'blksize'} = length($self->{'obuf'});
461 0 ▶ _write($self) unless(exists $self->{'error'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"error"} 0 0 0
462  
463 # buffer is empty so blksize=1 will ensure I do not send
464 # another packet, but just wait for the ACK
465 0 $self->{'blksize'} = 1;
466 0 ▶ _write($self) unless(exists $self->{'error'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"error"} 0 0 0
467 }
468 }
469 0 close(delete $self->{'sock'});
470 }
471  
472 0 ▶ exists $self->{'error'} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'error'} ? : 0 0 0
473 }
474  
475 # _natoha($data,$cr) - Convert netascii -> host text
476 # updates both input args
477 0 ▶sub _natoha {
Subroutine _natoha: not called CC=5 SCAR=31.2
478 3 ▶ use vars qw($buf $cr);
Subroutine BEGIN: called CC=1 SCAR=0.0
479 0 local *buf = \$_[0];
480 0 local *cr = \$_[1];
481 0 my $last = substr($buf,-1);
482 0 ▶ if($cr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::TFTP::IO::cr) 0 0 0
483 0 my $ch = ord(substr($buf,0,1));
484 0 ▶ if($ch == 012) { # CR.LF => \n
Branch: 2 branches
BranchTrueFalseTotal
if ($ch == 10) { } 0 0 0
elsif ($ch == 0) { } 0 0 0
485 0 substr($buf,0,1) = "\n";
486 }
487 elsif($ch == 0) { # CR.NUL => \r
488 0 substr($buf,0,1) = "\r";
489 }
490 else {
491 # Hm, badly formed netascii
492 0 substr($buf,0,0) = "\015";
493 }
494 }
495  
496 0 ▶ if(ord($last) eq 015) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $last eq 13) { } 0 0 0
497 0 substr($buf,-1) = "";
498 0 $cr = 1;
499 }
500 else {
501 0 $cr = 0;
502 }
503  
504 0 $buf =~ s/\015\0/\r/sg;
505 0 $buf =~ s/\015\012/\n/sg;
506  
507 0 1;
508 }
509  
510 0 ▶sub _abort {
Subroutine _abort: not called CC=3 SCAR=24.8
511 0 my $self = shift;
512 0 ▶ $self->{'error'} ||= 'Protocol error';
Condition0%0/2
$self->{"error"} ||= "Protocol error"
l!l
00
MC/DC0%0/1
$self->{"error"} ||= "Protocol error"
$self->{"error"}
Truth table0%0/2
A
A= $self->{"error"}
Aresult
11
00
513 0 $self->{'eof'} = 1;
514 0 my $pkt = pack("nna*c",Net::TFTP::ERROR,0,$self->{'error'},0);
515 send($self->{'sock'},$pkt,0,$self->{'peer'})
516 0 ▶ if exists $self->{'peer'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"peer"} 0 0 0
517 0 CLOSE($self);
518 0 -1;
519 }
520  
521 # _read: The guts of the reading
522 #
523 # returns
524 # >0 size of data read
525 # 0 eof
526 # <0 error
527  
528 0 ▶sub _read {
Subroutine _read: not called CC=21 SCAR=61.4
529 0 my($self,$wait) = @_;
530  
531 0 ▶ return -1 if exists $self->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"error"} 0 0 0
532 0 ▶ return 0 if $self->{'eof'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 0 0
533  
534 0 ▶ my $sock = $self->{'sock'} || return -1;
Condition0%0/2
$self->{'sock'} || (return -1)
l!l
00
MC/DC0%0/1
$self->{'sock'} || (return -1)
$self->{'sock'}
Truth table0%0/2
A
A= $self->{'sock'}
Aresult
11
00
535 0 my $select = $self->{'sel'};
536 0 ▶ my $timeout = $wait ? $self->{'Timeout'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
537 0 my $retry = 0;
538  
539 0 while(1) {
540 0 ▶ if($select->can_read($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($timeout)) 0 0 0
541 0 my $ipkt = ''; # will be filled by _recv
542 0 ▶ my($peer,$code,$blk) = _recv($self,$ipkt)
Branch: 1 branch
BranchTrueFalseTotal
() = _recv($self, $ipkt) or return _abort($self) 0 0 0
543 or return _abort($self);
544  
545 0 ▶ redo unless defined($peer); # do not send ACK to real peer
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
546  
547 0 ▶ if($code == Net::TFTP::DATA) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code == 3) { } 0 0 0
elsif ($code == 6) { } 0 0 0
elsif ($code == 5) { } 0 0 0
548 # If we receive a packet we are not expecting
549 # then ACK the last packet again
550  
551 0 ▶ if($blk == $self->{'blk'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blk == $self->{'blk'}) { } 0 0 0
elsif ($blk < $self->{'blk'}) { } 0 0 0
552 0 $self->{'blk'} = $blk+1;
553 0 my $data = substr($ipkt,4);
554  
555 _natoha($data,$self->{'icr'})
556 0 ▶ if($self->{'ascii'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ascii"} 0 0 0
557  
558 0 $self->{'ibuf'} .= $data;
559  
560 0 my $opkt = $self->{'pkt'} = pack("nn", Net::TFTP::ACK,$blk);
561 0 send($sock,$opkt,0,$peer);
562  
563 _dumppkt($sock,1,$opkt)
564 0 ▶ if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
565  
566 $self->{'eof'} = 1
567 0 ▶ if ( length($ipkt) < ($self->{'blksize'} + 4) );
Branch: 1 branch
BranchTrueFalseTotal
if length $ipkt < $self->{"blksize"} + 4 0 0 0
568  
569 0 return length($data);
570 }
571 elsif($blk < $self->{'blk'}) {
572 0 redo; # already got this data
573 }
574 }
575 elsif($code == Net::TFTP::OACK) {
576 0 my $opkt = $self->{'pkt'} = pack("nn", Net::TFTP::ACK,0);
577 0 send($sock,$opkt,0,$peer);
578  
579 _dumppkt($sock,1,$opkt)
580 0 ▶ if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
581  
582 0 return _read($self,$wait);
583 }
584 elsif($code == Net::TFTP::ERROR) {
585 0 $self->{'error'} = substr($ipkt,4);
586 0 $self->{'eof'} = 1;
587 0 CLOSE($self);
588 0 return -1;
589 }
590  
591 0 return _abort($self);
592 }
593  
594 0 ▶ last unless $wait;
Branch: 1 branch
BranchTrueFalseTotal
unless $wait 0 0 0
595 # Resend last packet, this will re ACK the last data packet
596 0 ▶ if($retry++ >= $self->{'Retries'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry++ >= $self->{"Retries"}) 0 0 0
597 0 $self->{'error'} = "Transfer Timeout";
598 0 return _abort($self);
599 }
600  
601 send($sock,$self->{'pkt'},0,$self->{'peer'})
602 0 ▶ if $self->{'peer'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"peer"} 0 0 0
603  
604 0 ▶ if ($self->{'Debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Debug"}) 0 0 0
605 0 print STDERR "${sock} << ---- retry=${retry}\n";
606 0 _dumppkt($sock,1,$self->{'pkt'});
607 }
608 }
609  
610 # NOT REACHED
611 }
612  
613 0 ▶sub _recv {
Subroutine _recv: not called CC=9 SCAR=45.0
614 0 my $self = shift;
615 0 my $sock = $self->{'sock'};
616 0 my $bsize = $self->{'blksize'}+4;
617 0 ▶ $bsize = 516 if $bsize < 516;
Branch: 1 branch
BranchTrueFalseTotal
if $bsize < 516 0 0 0
618 0 my $peer = recv($sock,$_[0],$bsize,0);
619  
620 # There is something on the socket, but not a udp packet. Prob. an icmp.
621 0 ▶ return unless ($peer);
Branch: 1 branch
BranchTrueFalseTotal
unless $peer 0 0 0
622  
623 0 ▶ _dumppkt($sock,0,$_[0]) if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
624  
625 # The struct in $peer can be bigger than needed for AF_INET
626 # so could contain garbage at the end. unpacking and re-packing
627 # will ensure it is zero filled (Thanks TomC)
628 0 ▶ if($self->{'IpMode'} eq "v6") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IpMode'} eq 'v6') { } 0 0 0
629 0 $peer = Socket6::pack_sockaddr_in6(Socket6::unpack_sockaddr_in6($peer));
630 } else {
631 0 $peer = pack_sockaddr_in(unpack_sockaddr_in($peer));
632 }
633  
634 0 ▶ $self->{'peer'} ||= $peer; # Remember first peer
Condition0%0/3
$self->{"peer"} ||= $peer
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"peer"} ||= $peer
$self->{"peer"} $peer
Truth table0%0/3
A ||= B
A= $self->{"peer"}B= $peer
ABresult
1X1
011
000
635  
636 0 my($code,$blk) = unpack("nn",$_[0]);
637  
638 0 ▶ if($code == Net::TFTP::OACK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 6) 0 0 0
639 0 my %o = split("\0",substr($_[0],2));
640 0 %$self = (%$self,%o);
641 }
642  
643 0 ▶ if ($self->{'peer'} ne $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"peer"} ne $peer) 0 0 0
644 # All packets must be from same peer
645 # packet from someone else, send them an ERR packet
646 0 my $err = pack("nna*c",Net::TFTP::ERROR, 5, "Unknown transfer ID",0);
647 _dumppkt($sock,1,$err)
648 0 ▶ if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
649 0 send($sock,$err,0,$peer);
650  
651 0 $peer = undef;
652 }
653  
654 0 ($peer,$code,$blk);
655 }
656  
657 0 ▶sub _send_data {
Subroutine _send_data: not called CC=6 SCAR=37.4
658 0 my $self = shift;
659  
660 0 ▶ if(length($self->{'obuf'}) >= $self->{'blksize'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $self->{'obuf'} >= $self->{'blksize'}) { } 0 0 0
elsif (length $self->{'obuf'} == 0 and $self->{'blksize'} == 1) { } 0 0 0
elsif ($^W) { } 0 0 0
661 0 my $blk = ++$self->{'blk'};
662 my $opkt = $self->{'pkt'} = pack("nn", Net::TFTP::DATA,$blk)
663 0 . substr($self->{'obuf'},0,$self->{'blksize'});
664 0 substr($self->{'obuf'},0,$self->{'blksize'}) = '';
665  
666 0 my $sock = $self->{'sock'};
667 0 send($sock,$opkt,0,$self->{'peer'});
668  
669 _dumppkt($sock,1,$opkt)
670 0 ▶ if $self->{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
671 }
672 ▶ elsif (length($self->{'obuf'}) == 0 and $self->{'blksize'} == 1) {
Condition0%0/3
length $self->{"obuf"} == 0 and $self->{"blksize"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->{"obuf"} == 0 and $self->{"blksize"} == 1
length $self->{"obuf"} == 0 $self->{"blksize"} == 1
Truth table0%0/3
A and B
A= length $self->{"obuf"} == 0B= $self->{"blksize"} == 1
ABresult
0X0
100
111
673 # ignore
674 }
675 elsif($^W) {
676 0 require Carp;
677 0 Carp::carp("Net::TFTP: Buffer underflow");
678 }
679  
680 0 1;
681 }
682  
683 0 ▶sub _write {
Subroutine _write: not called CC=14 SCAR=53.5
684 0 my($self) = @_;
685  
686 0 ▶ return -1 if exists $self->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"error"} 0 0 0
687  
688 0 ▶ my $sock = $self->{'sock'} || return -1;
Condition0%0/2
$self->{'sock'} || (return -1)
l!l
00
MC/DC0%0/1
$self->{'sock'} || (return -1)
$self->{'sock'}
Truth table0%0/2
A
A= $self->{'sock'}
Aresult
11
00
689 0 my $select = $self->{'sel'};
690 0 my $timeout = $self->{'Timeout'};
691 0 my $retry = 0;
692  
693 return _send_data($self)
694 0 ▶ if $self->{'ack'} == $self->{'blk'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ack"} == $self->{"blk"} 0 0 0
695  
696 0 while(1) {
697 0 ▶ if($select->can_read($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($timeout)) 0 0 0
698 0 my $ipkt=''; # will be filled by _recv
699 0 ▶ my($peer,$code,$blk) = _recv($self,$ipkt)
Branch: 1 branch
BranchTrueFalseTotal
() = _recv($self, $ipkt) or return _abort($self) 0 0 0
700 or return _abort($self);
701  
702 0 ▶ redo unless defined($peer); # do not send ACK to real peer
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
703  
704 0 ▶ if($code == Net::TFTP::OACK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 6) 0 0 0
705 0 $code = Net::TFTP::ACK;
706 0 $blk = 0;
707 }
708  
709 0 ▶ if($code == Net::TFTP::ACK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 4) 0 0 0
710 0 ▶ if ($self->{'blk'} == $blk) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'blk'} == $blk) { } 0 0 0
elsif ($self->{'blk'} > $blk) { } 0 0 0
711 0 $self->{'ack'} = $blk;
712 0 return _send_data($self);
713 }
714 elsif ($self->{'blk'} > $blk) {
715 0 redo; # duplicate ACK
716 }
717 }
718  
719 0 ▶ if($code == Net::TFTP::ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 5) 0 0 0
720 0 $self->{'error'} = substr($ipkt,4);
721 0 CLOSE($self);
722 0 return -1;
723 }
724  
725 0 return _abort($self);
726 }
727  
728 # Resend last packet, this will resend the last DATA packet
729 0 ▶ if($retry++ >= $self->{'Retries'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry++ >= $self->{"Retries"}) 0 0 0
730 0 $self->{'error'} = "Transfer Timeout";
731 0 return _abort($self);
732 }
733 0 send($sock,$self->{'pkt'},0,$self->{'peer'});
734  
735 0 ▶ if ($self->{'Debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Debug"}) 0 0 0
736 0 print STDERR "${sock} << ---- retry=${retry}\n";
737 0 _dumppkt($sock,1,$self->{'pkt'});
738 }
739 }
740 # NOT REACHED
741 }
742  
743 0 ▶sub _dumppkt {
Subroutine _dumppkt: not called CC=12 SCAR=50.5
744 0 my($sock,$send) = @_;
745 0 my($code,$blk) = unpack("nn",$_[2]);
746 0 ▶ $send = $send ? "$sock <<" : "$sock >>";
Branch: 1 branch
BranchTrueFalseTotal
$send ? : 0 0 0
747 0 my $str = sprintf "%s %-4s",$send,$NAME[$code];
748 0 ▶ $str .= sprintf " %s=%d",$code == Net::TFTP::ERROR ? "code" : "blk",$blk
Branch: 2 branches
BranchTrueFalseTotal
if $code == 3 or $code == 4 or $code == 5 0 0 0
$code == 5 ? : 0 0 0
Condition0%0/6
$code == 3 or $code == 4 or $code == 5
l!l&&r!l&&!r
000
$code == 3 or $code == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$code == 3 or $code == 4 or $code == 5
$code == 3 $code == 4 $code == 5
Truth table0%0/4
A or B or C
A= $code == 3B= $code == 4C= $code == 5
ABCresult
1XX1
01X1
0011
0000
749 if $code == Net::TFTP::DATA
750 || $code == Net::TFTP::ACK
751 || $code == Net::TFTP::ERROR;
752  
753 0 printf STDERR "%s length=%d\n",$str,length($_[2]);
754 0 ▶ if($code == Net::TFTP::RRQ || $code == Net::TFTP::WRQ || $code == Net::TFTP::OACK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 1 or $code == 2 or $code == 6) 0 0 0
Condition0%0/6
$code == 1 or $code == 2 or $code == 6
l!l&&r!l&&!r
000
$code == 1 or $code == 2
l!l&&r!l&&!r
000
MC/DC0%0/3
$code == 1 or $code == 2 or $code == 6
$code == 1 $code == 2 $code == 6
Truth table0%0/4
A or B or C
A= $code == 1B= $code == 2C= $code == 6
ABCresult
1XX1
01X1
0011
0000
755 0 my @a = split("\0",substr($_[2],2));
756 0 ▶ printf STDERR "%s filename=%s mode=%s\n",$send,splice(@a,0,2)
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 6 0 0 0
757 unless $code == Net::TFTP::OACK;
758 0 my %a = @a;
759 0 my($k,$v);
760 0 ▶ while(($k,$v) = each %a) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %a) 0 0 0
761 0 printf STDERR "%s %s=%s\n",$send,$k,$v;
762 }
763  
764 }
765 0 ▶ printf STDERR "%s %s\n",$send,substr($_[2],4)
Branch: 1 branch
BranchTrueFalseTotal
if $code == 5 0 0 0
766 if $code == Net::TFTP::ERROR;
767 }
768  
769 3 1;
770  
771 __END__
↑ Summary