lib/Net/TFTPd.pm

filter: Statementstmt 15.8% 71 / 447 Branchbran 0.5% 1 / 198 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 42.8% 18 / 42 Podpod 41.6% 10 / 24 totaltotal 13.0% 100 / 769 CC 113 SCAR 50.2
CC
113
Cov
11%
new
420.0
processRQ
342.0
negotiateOPTS
272.0
CRAP 151.1
↑ Summary script/tftpd-simple.pl »
1 package Net::TFTPd;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # modified by M.Vincent for IPv6 support
9 1 ▶use Socket qw(AF_INET SO_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 my $AF_INET6 = eval { Socket::AF_INET6() };
11 1 my $HAVE_IO_Socket_IP = 0;
12 1 ▶eval "use IO::Socket::IP -register";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶if (!$@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 1 0 1
14 {
15 1 $HAVE_IO_Socket_IP = 1;
16 }
17 else
18 {
19 0 eval "use IO::Socket::INET";
20 }
21  
22 1 require Exporter;
23  
24 # modified for supporting small block sizes, O.Z. 15.08.2007
25 1 ▶use constant TFTP_MIN_BLKSIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant TFTP_DEFAULT_BLKSIZE => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant TFTP_MAX_BLKSIZE => 65464;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant TFTP_MIN_TIMEOUT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant TFTP_MAX_TIMEOUT => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant TFTP_DEFAULT_PORT => 69;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use constant TFTP_OPCODE_RRQ => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant TFTP_OPCODE_WRQ => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant TFTP_OPCODE_DATA => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant TFTP_OPCODE_ACK => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant TFTP_OPCODE_ERROR => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant TFTP_OPCODE_OACK => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # Type Op # Format without header
40 #
41 # 2 bytes string 1 byte string 1 byte
42 # -------------------------------------------------
43 # RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
44 # WRQ -------------------------------------------------
45 # 2 bytes 2 bytes n bytes
46 # -----------------------------------
47 # DATA | 03 | Block # | Data |
48 # -----------------------------------
49 # 2 bytes 2 bytes
50 # ----------------------
51 # ACK | 04 | Block # |
52 # ----------------------
53 # 2 bytes 2 bytes string 1 byte
54 # ------------------------------------------
55 # ERROR | 05 | ErrorCode | ErrMsg | 0 |
56 # ------------------------------------------
57  
58 1 our %OPCODES = (
59 1 => 'RRQ',
60 2 => 'WRQ',
61 3 => 'DATA',
62 4 => 'ACK',
63 5 => 'ERROR',
64 6 => 'OACK',
65 'RRQ' => TFTP_OPCODE_RRQ,
66 'WRQ' => TFTP_OPCODE_WRQ,
67 'DATA' => TFTP_OPCODE_DATA,
68 'ACK' => TFTP_OPCODE_ACK,
69 'ERROR' => TFTP_OPCODE_ERROR,
70 'OACK' => TFTP_OPCODE_OACK
71 );
72  
73 1 my %ERRORS = (
74 0 => 'Not defined, see error message (if any)',
75 1 => 'File not found',
76 2 => 'Access violation',
77 3 => 'Disk full or allocation exceeded',
78 4 => 'Illegal TFTP operation',
79 5 => 'Unknown transfer ID',
80 6 => 'File already exists',
81 7 => 'No such user',
82 8 => 'Option negotiation'
83 );
84  
85 1 our @ISA = qw(Exporter);
86  
87 # Items to export into callers namespace by default. Note: do not export
88 # names by default without a very good reason. Use EXPORT_OK instead.
89 # Do not simply export all your public functions/methods/constants.
90  
91 # This allows declaration use Net::TFTPd ':all';
92 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
93 # will save memory.
94 1 our %EXPORT_TAGS = (
95 'all' => [ qw( %OPCODES ) ]
96 );
97  
98 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
99  
100 1 our @EXPORT = qw( );
101  
102 1 our $VERSION = '0.10';
103  
104 1 our $LASTERROR;
105  
106 1 my $debug;
107  
108 #
109 # Usage: $tftpdOBJ = Net::TFTPd->new( ['RootDir' => 'path/to/files' | 'FileName' => 'path/to/file'] );
110 # return the tftpdOBJ object if success or undef if error
111 #
112 sub new
113 0 ▶{
Subroutine new: not called CC=20 SCAR=60.4
Pod documented
114 # create the future TFTPd object
115 0 my $self = shift;
116 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
117  
118 # read parameters
119 0 my %cfg = @_;
120  
121 # setting defaults
122 0 ▶ $cfg{'FileName'} or $cfg{'RootDir'} or croak "Usage: \$tftpdOBJ = Net::TFTPd->new(['RootDir' => 'path/to/files' | 'FileName' => 'path/to/file'] [, [ LocalPort => portnum ] [, ...]] );";
Branch: 1 branch
BranchTrueFalseTotal
$cfg{"FileName"} or $cfg{"RootDir"} or croak("Usage: \$tftpdOBJ = Net::TFTPd->new(['RootDir' => 'path/to/files' | 'FileName' => 'path/to/file'] [, [ LocalPort => portnum ] [, ...]] );") 0 0 0
Condition0%0/3
$cfg{"FileName"} or $cfg{"RootDir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cfg{"FileName"} or $cfg{"RootDir"}
$cfg{"FileName"} $cfg{"RootDir"}
Truth table0%0/3
A or B
A= $cfg{"FileName"}B= $cfg{"RootDir"}
ABresult
1X1
011
000
123  
124 0 ▶ if ($cfg{'RootDir'} and not -d($cfg{'RootDir'}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"RootDir"} and not -d $cfg{"RootDir"}) 0 0 0
Condition0%0/3
$cfg{"RootDir"} and not -d $cfg{"RootDir"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cfg{"RootDir"} and not -d $cfg{"RootDir"}
$cfg{"RootDir"} not -d $cfg{"RootDir"}
Truth table0%0/3
A and not(B)
A= $cfg{"RootDir"}B= not -d $cfg{"RootDir"}
ABresult
0X0
100
111
125 {
126 0 $LASTERROR = sprintf 'RootDir \'%s\' not found or is not a valid directory name\n', $cfg{'RootDir'};
127 0 return (undef);
128 }
129  
130 0 ▶ if ($cfg{'FileName'} and not -e($cfg{'FileName'}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"FileName"} and not -e $cfg{"FileName"}) 0 0 0
Condition0%0/3
$cfg{"FileName"} and not -e $cfg{"FileName"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cfg{"FileName"} and not -e $cfg{"FileName"}
$cfg{"FileName"} not -e $cfg{"FileName"}
Truth table0%0/3
A and not(B)
A= $cfg{"FileName"}B= not -e $cfg{"FileName"}
ABresult
0X0
100
111
131 {
132 0 $LASTERROR = sprintf 'FileName \'%s\' not found or is not a valid filename\n', $cfg{'FileName'};
133 0 return (undef);
134 }
135  
136 my %params = (
137 'Proto' => 'udp',
138 0 ▶ 'LocalPort' => $cfg{'LocalPort'} || TFTP_DEFAULT_PORT
Condition0%0/2
$cfg{'LocalPort'} || 69
l!l
00
MC/DC0%0/1
$cfg{'LocalPort'} || 69
$cfg{'LocalPort'}
Truth table0%0/2
A
A= $cfg{'LocalPort'}
Aresult
11
00
139 );
140  
141 # modified by M.Vincent for IPv6 support
142 0 ▶ if (defined($cfg{'Family'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg{'Family'}) { } 0 0 0
143 {
144 0 ▶ if ($cfg{'Family'} =~ /^(?:(?:(:?ip)?v?(?:4|6))|${\AF_INET}|$AF_INET6)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{'Family'} =~ /^(?:(?:(:?ip)?v?(?:4|6))|${\2;}|$AF_INET6)$/) { } 0 0 0
145 {
146 0 ▶ if ($cfg{'Family'} =~ /^(?:(?:(:?ip)?v?4)|${\AF_INET})$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{'Family'} =~ /^(?:(?:(:?ip)?v?4)|${\2;})$/) { } 0 0 0
147 {
148 0 $params{'Family'} = AF_INET;
149 }
150 else
151 {
152 0 ▶ if (!$HAVE_IO_Socket_IP)
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAVE_IO_Socket_IP) 0 0 0
153 {
154 0 $LASTERROR = "IO::Socket::IP required for IPv6";
155 0 return (undef);
156 }
157 0 $params{'Family'} = $AF_INET6;
158 0 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
159 0 $params{'V6Only'} = 1;
160 }
161 }
162 }
163 else
164 {
165 0 $LASTERROR = "Invalid family - $cfg{'Family'}";
166 0 return (undef);
167 }
168 }
169 else
170 {
171 0 $params{'Family'} = AF_INET;
172 }
173  
174 0 ▶ if (defined($cfg{'V6Only'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg{"V6Only"}) 0 0 0
175 {
176 0 ▶ if (!$HAVE_IO_Socket_IP)
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAVE_IO_Socket_IP) 0 0 0
177 {
178 0 $LASTERROR = "IO::Socket::IP required for V6Only";
179 0 return (undef);
180 }
181 0 $params{'V6Only'} = $cfg{'V6Only'};
182 }
183  
184 # bind only to specified address
185 0 ▶ if ($cfg{'LocalAddr'})
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"LocalAddr"}) 0 0 0
186 {
187 0 $params{'LocalAddr'} = $cfg{'LocalAddr'};
188 }
189  
190 0 ▶ if ($HAVE_IO_Socket_IP)
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_IO_Socket_IP) { } 0 0 0
191 {
192 0 ▶ if (my $udpserver = IO::Socket::IP->new(%params))
Branch: 1 branch
BranchTrueFalseTotal
if (my $udpserver = 'IO::Socket::IP'->new(%params)) { } 0 0 0
193 {
194 0 return bless {
195 'LocalPort' => TFTP_DEFAULT_PORT,
196 'Timeout' => 10,
197 'ACKtimeout' => 4,
198 'ACKretries' => 4,
199 'Readable' => 1,
200 'Writable' => 0,
201 'CallBack' => undef,
202 'BlkSize' => TFTP_DEFAULT_BLKSIZE,
203 'Debug' => 0,
204 %cfg, # merge user parameters
205 '_UDPSERVER_' => $udpserver
206 }, $class;
207 }
208 else
209 {
210 0 $LASTERROR = "Error opening socket for listener: $@\n";
211 0 return (undef);
212 }
213 }
214 else
215 {
216 0 ▶ if (my $udpserver = IO::Socket::INET->new(%params))
Branch: 1 branch
BranchTrueFalseTotal
if (my $udpserver = 'IO::Socket::INET'->new(%params)) { } 0 0 0
217 {
218 0 return bless {
219 'LocalPort' => TFTP_DEFAULT_PORT,
220 'Timeout' => 10,
221 'ACKtimeout' => 4,
222 'ACKretries' => 4,
223 'Readable' => 1,
224 'Writable' => 0,
225 'CallBack' => undef,
226 'BlkSize' => TFTP_DEFAULT_BLKSIZE,
227 'Debug' => 0,
228 %cfg, # merge user parameters
229 '_UDPSERVER_' => $udpserver
230 }, $class;
231 }
232 else
233 {
234 0 $LASTERROR = "Error opening socket for listener: $@\n";
235 0 return (undef);
236 }
237 }
238 }
239  
240 #
241 # Usage: $tftpdOBJ->waitRQ($timeout);
242 # return requestOBJ if success, 0 if $timeout elapsed, undef if error
243 #
244 sub waitRQ
245 0 ▶{
Subroutine waitRQ: not called CC=8 SCAR=42.8
Pod documented
246 # the tftpd object
247 # my $tftpd = shift;
248  
249 0 my $self = shift;
250 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
251 # return bless {}, $class;
252  
253 # clone the object
254 0 my $request;
255 0 foreach my $key (keys(%{$self}))
256 {
257 # everything but '_xxx_'
258 0 ▶ $key =~ /^\_.+\_$/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^\_.+\_$/ and next 0 0 0
259 0 $request->{$key} = $self->{$key};
260 }
261  
262 # use $timeout or default from $tftpdOBJ
263 0 ▶ my $Timeout = shift || $request->{'Timeout'};
Condition0%0/3
shift() || $request->{'Timeout'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $request->{'Timeout'}
shift() $request->{'Timeout'}
Truth table0%0/3
A || B
A= shift()B= $request->{'Timeout'}
ABresult
1X1
011
000
264  
265 0 my $udpserver = $self->{'_UDPSERVER_'};
266  
267 0 my ($datagram, $opcode, $datain);
268  
269 # vars for IO select
270 0 my ($rin, $rout, $ein, $eout) = ('', '', '', '');
271 0 vec($rin, fileno($udpserver), 1) = 1;
272  
273 # check if a message is waiting
274 0 ▶ if (select($rout=$rin, undef, $eout=$ein, $Timeout))
Branch: 1 branch
BranchTrueFalseTotal
if (select $rout = $rin, undef, $eout = $ein, $Timeout) { } 0 0 0
275 {
276 # read the message
277 0 ▶ if ($udpserver->recv($datagram, TFTP_MAX_BLKSIZE + 4))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->recv($datagram, 65468)) { } 0 0 0
278 {
279 # decode the message
280 0 ($opcode, $datain) = unpack("na*", $datagram);
281  
282 0 $request->{'_REQUEST_'}{'OPCODE'} = $opcode;
283  
284 # get peer port and address
285 0 $request->{'_REQUEST_'}{'PeerPort'} = $udpserver->peerport;
286 0 $request->{'_REQUEST_'}{'PeerAddr'} = $udpserver->peerhost;
287  
288 # get filename and transfer mode
289 0 my @datain = split("\0", $datain);
290  
291 0 $request->{'_REQUEST_'}{'FileName'} = shift(@datain);
292 0 $request->{'_REQUEST_'}{'Mode'} = uc(shift(@datain));
293 0 $request->{'_REQUEST_'}{'BlkSize'} = TFTP_DEFAULT_BLKSIZE;
294 0 $request->{'_REQUEST_'}{'LASTACK'} = 0;
295 0 $request->{'_REQUEST_'}{'PREVACK'} = -1;
296 # counter for transferred bytes
297 0 $request->{'_REQUEST_'}{'TotalBytes'} = 0;
298  
299 0 ▶ if (scalar(@datain) >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @datain >= 2) 0 0 0
300 {
301 0 $request->{'_REQUEST_'}{'RFC2347'} = { @datain };
302 }
303  
304 0 return bless $request, $class;
305 }
306 else
307 {
308 0 $! = $udpserver->sockopt(SO_ERROR);
309 0 $LASTERROR = sprintf "Socket RECV error: %s\n", $!;
310 0 return (undef);
311 }
312 }
313 else
314 {
315 0 $LASTERROR = "Timed out waiting for RRQ/WRQ";
316 0 return (0);
317 }
318 }
319  
320 #
321 # Usage: $requestOBJ->processRQ();
322 # return 1 if success, undef if error
323 #
324 sub processRQ
325 0 ▶{
Subroutine processRQ: not called CC=18 SCAR=58.3
Pod documented
326 # the request object
327 0 my $self = shift;
328  
329 0 ▶ if (defined($self->newSOCK()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->newSOCK) { } 0 0 0
330 {
331 # modified for supporting NETASCII transfers on 25/05/2009
332 0 ▶ if (($self->{'_REQUEST_'}{'Mode'} ne 'OCTET') && ($self->{'_REQUEST_'}{'Mode'} ne 'NETASCII'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"Mode"} ne "OCTET" and $self->{"_REQUEST_"}{"Mode"} ne "NETASCII") 0 0 0
Condition0%0/3
$self->{"_REQUEST_"}{"Mode"} ne "OCTET" and $self->{"_REQUEST_"}{"Mode"} ne "NETASCII"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_REQUEST_"}{"Mode"} ne "OCTET" and $self->{"_REQUEST_"}{"Mode"} ne "NETASCII"
$self->{"_REQUEST_"}{"Mode"} ne "OCTET" $self->{"_REQUEST_"}{"Mode"} ne "NETASCII"
Truth table0%0/3
A and B
A= $self->{"_REQUEST_"}{"Mode"} ne "OCTET"B= $self->{"_REQUEST_"}{"Mode"} ne "NETASCII"
ABresult
0X0
100
111
333 {
334 #request is not OCTET
335 0 $LASTERROR = sprintf "%s transfer mode is not supported\n", $self->{'_REQUEST_'}{'Mode'};
336 0 $self->sendERR(0, $LASTERROR);
337 0 return (undef);
338 }
339  
340 # new socket opened successfully
341 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_RRQ)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'OPCODE'} eq 1) { } 0 0 0
elsif ($self->{'_REQUEST_'}{'OPCODE'} eq 2) { } 0 0 0
342 {
343 #################
344 # opcode is RRQ #
345 #################
346 0 ▶ if ($self->{'Readable'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Readable'}) { } 0 0 0
347 {
348 # read is permitted
349 0 ▶ if ($self->{'_REQUEST_'}{'FileName'} =~ /\.\.[\\\/]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"FileName"} =~ m[\.\.[\\/]]) 0 0 0
350 {
351 # requested file contains '..\' or '../'
352 0 $LASTERROR = sprintf 'Access to \'%s\' is not permitted to %s', $self->{'_REQUEST_'}{'FileName'}, $self->{'_REQUEST_'}{'PeerAddr'};
353 0 $self->sendERR(2);
354 0 return (undef);
355 }
356  
357 0 ▶ if (defined($self->checkFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->checkFILE) { } 0 0 0
358 {
359 # file is present
360 0 ▶ if (defined($self->negotiateOPTS()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->negotiateOPTS) { } 0 0 0
361 {
362 # RFC 2347 options negotiated
363 0 ▶ if (defined($self->openFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->openFILE) { } 0 0 0
364 {
365 # file opened for read, start the transfer
366 0 ▶ if (defined($self->sendFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->sendFILE) { } 0 0 0
367 {
368 # file sent successfully
369 0 return (1);
370 }
371 else
372 {
373 # error sending file
374 0 return (undef);
375 }
376 }
377 else
378 {
379 # error opening file
380 0 return (undef);
381 }
382 }
383 else
384 {
385 # error negotiating options
386 0 $LASTERROR = "TFTP error 8: Option negotiation\n";
387 0 $self->sendERR(8);
388 0 return (undef);
389 }
390 }
391 else
392 {
393 # file not found
394 0 $LASTERROR = sprintf 'File \'%s\' not found', $self->{'_REQUEST_'}{'FileName'};
395 0 $self->sendERR(1);
396 0 return (undef);
397 }
398 }
399 else
400 {
401 # if server is not readable
402 0 $LASTERROR = "TFTP Error: Access violation";
403 0 $self->sendERR(2);
404 0 return (undef);
405 }
406 }
407 elsif ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_WRQ)
408 {
409 #################
410 # opcode is WRQ #
411 #################
412 0 ▶ if ($self->{'Writable'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Writable'}) { } 0 0 0
413 {
414 # write is permitted
415 0 ▶ if ($self->{'_REQUEST_'}{'FileName'} =~ /\.\.[\\\/]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"FileName"} =~ m[\.\.[\\/]]) 0 0 0
416 {
417 # requested file contains '..\' or '../'
418 0 $LASTERROR = sprintf 'Access to \'%s\' is not permitted to %s', $self->{'_REQUEST_'}{'FileName'}, $self->{'_REQUEST_'}{'PeerAddr'};
419 0 $self->sendERR(2);
420 0 return (undef);
421 }
422  
423 0 ▶ if (!defined($self->checkFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->checkFILE) { } 0 0 0
424 {
425 # RFC 2347 options negotiated
426 0 ▶ if (defined($self->openFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->openFILE) { } 0 0 0
427 {
428 # file is not present
429 0 ▶ if (defined($self->negotiateOPTS()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->negotiateOPTS) { } 0 0 0
430 {
431 # file opened for write, start the transfer
432 0 ▶ if (defined($self->recvFILE()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->recvFILE) { } 0 0 0
433 {
434 # file received successfully
435 0 return (1);
436 }
437 else
438 {
439 # error receiving file
440 0 return (undef);
441 }
442 }
443 else
444 {
445 # error negotiating options
446 0 $LASTERROR = "TFTP error 8: Option negotiation\n";
447 0 $self->sendERR(8);
448 0 return (undef);
449 }
450 }
451 else
452 {
453 # error opening file
454 0 $self->sendERR(3);
455 0 return (undef);
456 }
457 }
458 else
459 {
460 # file not found
461 0 $LASTERROR = sprintf 'File \'%s\' already exists', $self->{'_REQUEST_'}{'FileName'};
462 0 $self->sendERR(6);
463 0 return (undef);
464 }
465 }
466 else
467 {
468 # if server is not writable
469 0 $LASTERROR = "TFTP Error: Access violation";
470 0 $self->sendERR(2);
471 0 return (undef);
472 }
473 }
474 else
475 {
476 #################
477 # other opcodes #
478 #################
479 0 $LASTERROR = sprintf "Opcode %d not supported as request", $self->{'_REQUEST_'}{'OPCODE'};
480 0 $self->sendERR(4);
481 0 return (undef);
482 }
483 }
484 else
485 {
486 0 return (undef);
487 }
488 }
489  
490 #
491 # Usage: $requestOBJ->getTotalBytes();
492 # returns the number of bytes transferred by the request
493 #
494 sub getTotalBytes
495 0 ▶{
Subroutine getTotalBytes: not called CC=1 SCAR=6.9
Pod documented
496 # the request object
497 0 my $self = shift;
498
499 0 return $self->{'_REQUEST_'}{'TotalBytes'};
500 }
501  
502 #
503 # Usage: $requestOBJ->getFileName();
504 # returns the requested file name
505 #
506 sub getFileName
507 0 ▶{
Subroutine getFileName: not called CC=1 SCAR=6.9
Pod documented
508 # the request object
509 0 my $self = shift;
510
511 0 return $self->{'_REQUEST_'}{'FileName'};
512 }
513  
514 #
515 # Usage: $requestOBJ->getMode();
516 # returns the transfer mode for the request
517 #
518 sub getMode
519 0 ▶{
Subroutine getMode: not called CC=1 SCAR=6.9
Pod documented
520 # the request object
521 0 my $self = shift;
522
523 0 return $self->{'_REQUEST_'}{'Mode'};
524 }
525  
526 #
527 # Usage: $requestOBJ->getPeerAddr();
528 # returns the address of the requesting client
529 #
530 sub getPeerAddr
531 0 ▶{
Subroutine getPeerAddr: not called CC=1 SCAR=6.9
Pod documented
532 # the request object
533 0 my $self = shift;
534
535 0 return $self->{'_REQUEST_'}{'PeerAddr'};
536 }
537  
538 #
539 # Usage: $requestOBJ->getPeerPort();
540 # returns the port of the requesting client
541 #
542 sub getPeerPort
543 0 ▶{
Subroutine getPeerPort: not called CC=1 SCAR=6.9
Pod documented
544 # the request object
545 0 my $self = shift;
546
547 0 return $self->{'_REQUEST_'}{'PeerPort'};
548 }
549  
550 #
551 # Usage: $requestOBJ->getBlkSize();
552 # returns the block size used for the transfer
553 #
554 sub getBlkSize
555 0 ▶{
Subroutine getBlkSize: not called CC=1 SCAR=6.9
Pod documented
556 # the request object
557 0 my $self = shift;
558
559 0 return $self->{'_REQUEST_'}{'BlkSize'};
560 }
561  
562 #
563 # Usage: $requestOBJ->newSOCK();
564 # return 1 if success or undef if error
565 #
566 sub newSOCK
567 0 ▶{
Subroutine newSOCK: not called CC=5 SCAR=34.0
Pod undocumented
568 # the request object
569 0 my $self = shift;
570  
571 # set parameters for the new socket
572 my %params = (
573 'Proto' => 'udp',
574 'PeerPort' => $self->{'_REQUEST_'}{'PeerPort'},
575 0 'PeerAddr' => $self->{'_REQUEST_'}{'PeerAddr'}
576 );
577  
578 # bind only to specified address
579 0 ▶ if ($self->{'Address'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Address"}) 0 0 0
580 {
581 0 $params{'LocalAddr'} = $self->{'Address'};
582 }
583  
584 # open socket
585 0 ▶ if ($HAVE_IO_Socket_IP)
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_IO_Socket_IP) { } 0 0 0
586 {
587 0 ▶ if (my $udpserver = IO::Socket::IP->new(%params))
Branch: 1 branch
BranchTrueFalseTotal
if (my $udpserver = 'IO::Socket::IP'->new(%params)) { } 0 0 0
588 {
589 0 $self->{'_UDPSERVER_'} = $udpserver;
590 0 return (1);
591 }
592 else
593 {
594 0 $LASTERROR = "Error opening socket for reply: $@\n";
595 0 return (undef);
596 }
597 }
598 else
599 {
600 0 ▶ if (my $udpserver = IO::Socket::INET->new(%params))
Branch: 1 branch
BranchTrueFalseTotal
if (my $udpserver = 'IO::Socket::INET'->new(%params)) { } 0 0 0
601 {
602 0 $self->{'_UDPSERVER_'} = $udpserver;
603 0 return (1);
604 }
605 else
606 {
607 0 $LASTERROR = "Error opening socket for reply: $@\n";
608 0 return (undef);
609 }
610 }
611 }
612  
613  
614 #
615 # Usage: $requestOBJ->negotiateOPTS();
616 # return 1 if success or undef if error
617 #
618 sub negotiateOPTS
619 0 ▶{
Subroutine negotiateOPTS: not called CC=16 SCAR=56.1
Pod undocumented
620 # the request object
621 0 my $self = shift;
622  
623 0 ▶ if ($self->{'_REQUEST_'}{'RFC2347'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'RFC2347'}) { } 0 0 0
624 {
625 # parse RFC 2347 options if present
626 0 foreach my $option (keys(%{ $self->{'_REQUEST_'}{'RFC2347'} }))
627 {
628 0 ▶ if (uc($option) eq 'BLKSIZE')
Branch: 3 branches
BranchTrueFalseTotal
if (uc $option eq 'BLKSIZE') { } 0 0 0
elsif (uc $option eq 'TSIZE') { } 0 0 0
elsif (uc $option eq 'TIMEOUT') { } 0 0 0
629 {
630 # Negotiate the blocksize
631 0 ▶ if ($self->{'_REQUEST_'}{'RFC2347'}{$option} > TFTP_MAX_BLKSIZE or $self->{'_REQUEST_'}{'RFC2347'}{$option} < TFTP_MIN_BLKSIZE)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'RFC2347'}{$option} > 65464 or $self->{'_REQUEST_'}{'RFC2347'}{$option} < 8) { } 0 0 0
Condition0%0/3
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 65464 or $self->{"_REQUEST_"}{"RFC2347"}{$option} < 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 65464 or $self->{"_REQUEST_"}{"RFC2347"}{$option} < 8
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 65464 $self->{"_REQUEST_"}{"RFC2347"}{$option} < 8
Truth table0%0/3
A or B
A= $self->{"_REQUEST_"}{"RFC2347"}{$option} > 65464B= $self->{"_REQUEST_"}{"RFC2347"}{$option} < 8
ABresult
1X1
011
000
632 {
633 0 $self->{'_REQUEST_'}{'RFC2347'}{$option} = $self->{'BlkSize'};
634 }
635 else
636 {
637 0 $self->{'_RESPONSE_'}{'RFC2347'}{$option} = $self->{'_REQUEST_'}{'RFC2347'}{$option};
638 0 $self->{'BlkSize'} = $self->{'_RESPONSE_'}{'RFC2347'}{$option};
639 }
640 }
641 elsif (uc($option) eq 'TSIZE')
642 {
643 # Negotiate the transfer size
644 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_RRQ)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'OPCODE'} eq 1) { } 0 0 0
645 {
646 0 $self->{'_RESPONSE_'}{'RFC2347'}{$option} = $self->{'FileSize'};
647 }
648 else
649 {
650 0 $self->{'FileSize'} = $self->{'_REQUEST_'}{'RFC2347'}{$option};
651 }
652 }
653 elsif (uc($option) eq 'TIMEOUT')
654 {
655 # Negotiate the transfer timeout
656 0 ▶ if ($self->{'_REQUEST_'}{'RFC2347'}{$option} > TFTP_MAX_TIMEOUT or $self->{'_REQUEST_'}{'RFC2347'}{$option} < TFTP_MIN_TIMEOUT)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'RFC2347'}{$option} > 60 or $self->{'_REQUEST_'}{'RFC2347'}{$option} < 1) { } 0 0 0
Condition0%0/3
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 60 or $self->{"_REQUEST_"}{"RFC2347"}{$option} < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 60 or $self->{"_REQUEST_"}{"RFC2347"}{$option} < 1
$self->{"_REQUEST_"}{"RFC2347"}{$option} > 60 $self->{"_REQUEST_"}{"RFC2347"}{$option} < 1
Truth table0%0/3
A or B
A= $self->{"_REQUEST_"}{"RFC2347"}{$option} > 60B= $self->{"_REQUEST_"}{"RFC2347"}{$option} < 1
ABresult
1X1
011
000
657 {
658 0 $self->{'_RESPONSE_'}{'RFC2347'}{$option} = $self->{'ACKtimeout'};
659 }
660 else
661 {
662 0 $self->{'_RESPONSE_'}{'RFC2347'}{$option} = $self->{'_REQUEST_'}{'RFC2347'}{$option};
663 0 $self->{'ACKtimeout'} = $self->{'_REQUEST_'}{'RFC2347'}{$option};
664 }
665 }
666 else
667 {
668 # Negotiate other options...
669 }
670 }
671  
672 # post processing
673 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_WRQ)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"OPCODE"} eq 2) 0 0 0
674 {
675 0 ▶ if ($self->{'FileSize'} and $self->{'BlkSize'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"FileSize"} and $self->{"BlkSize"}) 0 0 0
Condition0%0/3
$self->{"FileSize"} and $self->{"BlkSize"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"FileSize"} and $self->{"BlkSize"}
$self->{"FileSize"} $self->{"BlkSize"}
Truth table0%0/3
A and B
A= $self->{"FileSize"}B= $self->{"BlkSize"}
ABresult
0X0
100
111
676 {
677 0 $self->{'_REQUEST_'}{'LASTACK'} = int($self->{'FileSize'} / $self->{'BlkSize'}) + 1;
678 }
679 }
680  
681 # send OACK for RFC 2347 options
682 0 return ($self->sendOACK());
683 }
684 else
685 {
686 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_WRQ)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'OPCODE'} eq 2) { } 0 0 0
687 {
688 # opcode is WRQ: send ACK for datablock 0
689 0 ▶ if ($self->{'_UDPSERVER_'}->send(pack("nn", TFTP_OPCODE_ACK, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_UDPSERVER_'}->send("\000\cD\000\000")) { } 0 0 0
690 {
691 0 return (1);
692 }
693 else
694 {
695 0 $! = $self->{'_UDPSERVER_'}->sockopt(SO_ERROR);
696 0 $LASTERROR = sprintf "Socket SEND error: %s\n", $!;
697 0 return (undef);
698 }
699 }
700 else
701 {
702 0 return (1);
703 }
704 }
705 }
706  
707  
708 #
709 # Usage: $requestOBJ->readFILE(\$data);
710 # return number of bytes read from file if success or undef if error
711 #
712 sub readFILE
713 0 ▶{
Subroutine readFILE: not called CC=3 SCAR=24.8
Pod undocumented
714 0 my $self = shift;
715 0 my $datablk = shift;
716  
717 0 ▶ if ($self->{'_REQUEST_'}{'PREVACK'} < $self->{'_REQUEST_'}{'LASTACK'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'PREVACK'} < $self->{'_REQUEST_'}{'LASTACK'}) { } 0 0 0
718 {
719 # if requested block is next block, read next block and return bytes read
720 0 my $fh = $self->{'_REQUEST_'}{'_FH_'};
721 # modified for supporting NETASCII transfers on 25/05/2009
722 # my $bytes = read ($fh, $$datablk, $self->{'BlkSize'});
723 0 my $bytes = sysread($fh, $$datablk, $self->{'BlkSize'});
724 0 ▶ if (defined($bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes) { } 0 0 0
725 {
726 0 return ($bytes);
727 }
728 else
729 {
730 0 $LASTERROR = sprintf "Error $! reading file '%s'", $self->{'_REQUEST_'}{'FileName'};
731 0 return (undef);
732 }
733 }
734 else
735 {
736 # if requested block is last block, return length of last block
737 0 return (length($$datablk));
738 }
739 }
740  
741  
742 #
743 # Usage: $requestOBJ->writeFILE(\$data);
744 # return number of bytes written to file if success or undef if error
745 #
746 sub writeFILE
747 0 ▶{
Subroutine writeFILE: not called CC=3 SCAR=24.8
Pod undocumented
748 0 my $self = shift;
749 0 my $datablk = shift;
750  
751 0 ▶ if ($self->{'_REQUEST_'}{'PREVBLK'} > $self->{'_REQUEST_'}{'LASTBLK'})
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'PREVBLK'} > $self->{'_REQUEST_'}{'LASTBLK'}) { } 0 0 0
elsif ($self->{'_REQUEST_'}{'LASTBLK'} eq $self->{'_REQUEST_'}{'PREVBLK'} + 1) { } 0 0 0
752 {
753 # if last block is < than previous block, return length of last block
754 0 return (length($$datablk));
755 }
756 elsif ($self->{'_REQUEST_'}{'LASTBLK'} eq ($self->{'_REQUEST_'}{'PREVBLK'} + 1))
757 {
758 # if block is next block, write next block and return bytes written
759 0 my $fh = $self->{'_REQUEST_'}{'_FH_'};
760 0 my $bytes = syswrite($fh, $$datablk);
761 0 return ($bytes);
762 }
763 else
764 {
765 0 $LASTERROR = sprintf "TFTP Error DATA block %d is out of sequence, expected block was %d", $self->{'_REQUEST_'}{'LASTBLK'}, $self->{'_REQUEST_'}{'PREVBLK'} + 1;
766 0 $self->sendERR(5);
767 0 return (undef);
768 }
769 }
770  
771  
772 #
773 # Usage: $requestOBJ->sendFILE();
774 # return 1 if success or undef if error
775 #
776 sub sendFILE
777 0 ▶{
Subroutine sendFILE: not called CC=5 SCAR=34.0
Pod undocumented
778 0 my $self = shift;
779  
780 0 while (1)
781 {
782 0 ▶ if ($self->{'_REQUEST_'}{'LASTACK'} < $self->{'_REQUEST_'}{'LASTBLK'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'LASTACK'} < $self->{'_REQUEST_'}{'LASTBLK'}) { } 0 0 0
783 {
784 0 my $datablk = 0;
785 0 ▶ if (defined($self->readFILE(\$datablk)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->readFILE(\$datablk)) { } 0 0 0
786 {
787 # read from file successful
788 # increment the transferred bytes counter
789 0 $self->{'_REQUEST_'}{'TotalBytes'} += length($datablk);
790 0 ▶ if ($self->sendDATA(\$datablk))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sendDATA(\$datablk)) { } 0 0 0
791 {
792 # send to socket successful
793 0 ▶ if ($self->{'CallBack'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CallBack"}) 0 0 0
794 {
795 0 &{$self->{'CallBack'}}($self);
796 }
797 }
798 else
799 {
800 # error sending to socket
801 0 return (undef);
802 }
803 }
804 else
805 {
806 # error reading from file
807 0 return (undef);
808 }
809 }
810 else
811 {
812 # transfer completed
813 0 return (1);
814 }
815 }
816 }
817  
818  
819 #
820 # Usage: $requestOBJ->recvFILE();
821 # return 1 if success or undef if error
822 #
823 sub recvFILE
824 0 ▶{
Subroutine recvFILE: not called CC=5 SCAR=34.0
Pod undocumented
825 0 my $self = shift;
826  
827 0 $self->{'_REQUEST_'}{'LASTBLK'} = 0;
828 0 $self->{'_REQUEST_'}{'PREVBLK'} = 0;
829  
830 0 while (1)
831 {
832 0 my $datablk = 0;
833 0 ▶ if ($self->recvDATA(\$datablk))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->recvDATA(\$datablk)) { } 0 0 0
834 {
835 # DATA received
836 0 ▶ if (defined($self->writeFILE(\$datablk)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->writeFILE(\$datablk)) { } 0 0 0
837 {
838 # DATA written to file
839 0 my $udpserver = $self->{'_UDPSERVER_'};
840  
841 0 ▶ if (defined($udpserver->send(pack("nn", TFTP_OPCODE_ACK, $self->{'_REQUEST_'}{'LASTBLK'}))))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $udpserver->send(pack('nn', 4, $self->{'_REQUEST_'}{'LASTBLK'}))) { } 0 0 0
842 {
843 # sent ACK
844 # increment the transferred bytes counter
845 0 $self->{'_REQUEST_'}{'TotalBytes'} += length($datablk);
846 0 ▶ if (length($datablk) < $self->{'BlkSize'})
Branch: 1 branch
BranchTrueFalseTotal
if (length $datablk < $self->{'BlkSize'}) { } 0 0 0
847 {
848 0 return (1);
849 }
850 else
851 {
852 0 next;
853 }
854 }
855 else
856 {
857 0 $! = $udpserver->sockopt(SO_ERROR);
858 0 $LASTERROR = sprintf "Socket SEND error: %s\n", $!;
859 0 return (undef);
860 }
861 }
862 else
863 {
864 # error writing data
865 0 return (undef);
866 }
867 }
868 else
869 {
870 # timeout waiting for data
871 0 return (undef);
872 }
873 }
874 }
875  
876 #
877 # Usage: $requestOBJ->recvDATA(\$data);
878 # return 1 if success or undef if error
879 #
880 sub recvDATA
881 0 ▶{
Subroutine recvDATA: not called CC=6 SCAR=37.4
Pod undocumented
882 0 my $self = shift;
883 0 my $datablk = shift;
884  
885 0 my ($datagram, $opcode, $datain);
886  
887 0 my $udpserver = $self->{'_UDPSERVER_'};
888  
889 # vars for IO select
890 0 my ($rin, $rout, $ein, $eout) = ('', '', '', '');
891 0 vec($rin, fileno($udpserver), 1) = 1;
892  
893 # wait for data
894 0 ▶ if (select($rout=$rin, undef, $eout=$ein, $self->{'ACKtimeout'}))
Branch: 1 branch
BranchTrueFalseTotal
if (select $rout = $rin, undef, $eout = $ein, $self->{'ACKtimeout'}) { } 0 0 0
895 {
896 # read the message
897 0 ▶ if ($udpserver->recv($datagram, $self->{'BlkSize'} + 4))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->recv($datagram, $self->{'BlkSize'} + 4)) { } 0 0 0
898 {
899 # decode the message
900 0 ($opcode, $datain) = unpack("na*", $datagram);
901 0 ▶ if ($opcode eq TFTP_OPCODE_DATA)
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode eq 3) { } 0 0 0
elsif ($opcode eq 5) { } 0 0 0
902 {
903 # message is DATA
904 0 $self->{'_REQUEST_'}{'PREVBLK'} = $self->{'_REQUEST_'}{'LASTBLK'};
905 0 ($self->{'_REQUEST_'}{'LASTBLK'}, $$datablk) = unpack("na*", $datain);
906  
907 0 ▶ if($self->{'CallBack'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CallBack"}) 0 0 0
908 {
909 0 &{$self->{'CallBack'}}($self);
910 }
911  
912 0 return (1);
913 }
914 elsif ($opcode eq TFTP_OPCODE_ERROR)
915 {
916 # message is ERR
917 0 $LASTERROR = sprintf "TFTP error message: %s", $datain;
918 0 return (undef);
919 }
920 else
921 {
922 # other messages...
923 0 $LASTERROR = sprintf "Opcode %d not supported waiting for DATA\n", $opcode;
924 0 return (undef);
925 }
926 }
927 else
928 {
929 0 $! = $udpserver->sockopt(SO_ERROR);
930 0 $LASTERROR = sprintf "Socket RECV error: %s\n", $!;
931 0 return (undef);
932 }
933 }
934 else
935 {
936 0 $LASTERROR = sprintf "Timeout occurred on DATA packet %d\n", $self->{'_REQUEST_'}{'LASTBLK'} + 1;
937 0 return (undef);
938 }
939 }
940  
941  
942 #
943 # Usage: $requestOBJ->sendDATA(\$data);
944 # return 1 if success or undef if error
945 #
946 sub sendDATA
947 0 ▶{
Subroutine sendDATA: not called CC=9 SCAR=45.0
Pod undocumented
948 0 my $self = shift;
949 0 my $datablk = shift;
950  
951 0 my $udpserver = $self->{'_UDPSERVER_'};
952 0 my $retry = 0;
953  
954 0 my ($datagram, $opcode, $datain);
955  
956 0 ▶ while ($retry < $self->{'ACKretries'})
Branch: 1 branch
BranchTrueFalseTotal
if ($retry < $self->{"ACKretries"}) 0 0 0
957 {
958 0 ▶ if ($udpserver->send(pack("nna*", TFTP_OPCODE_DATA, $self->{'_REQUEST_'}{'LASTACK'} + 1, $$datablk)))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->send(pack('nna*', 3, $self->{'_REQUEST_'}{'LASTACK'} + 1, $$datablk))) { } 0 0 0
959 {
960 # vars for IO select
961 0 my ($rin, $rout, $ein, $eout) = ('', '', '', '');
962 0 vec($rin, fileno($udpserver), 1) = 1;
963  
964 # wait for acknowledge
965 0 ▶ if (select($rout=$rin, undef, $eout=$ein, $self->{'ACKtimeout'}))
Branch: 1 branch
BranchTrueFalseTotal
if (select $rout = $rin, undef, $eout = $ein, $self->{'ACKtimeout'}) { } 0 0 0
966 {
967 # read the message
968 0 ▶ if ($udpserver->recv($datagram, TFTP_MAX_BLKSIZE + 4))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->recv($datagram, 65468)) { } 0 0 0
969 {
970 # decode the message
971 0 ($opcode, $datain) = unpack("na*", $datagram);
972 0 ▶ if ($opcode eq TFTP_OPCODE_ACK)
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode eq 4) { } 0 0 0
elsif ($opcode eq 5) { } 0 0 0
973 {
974 # message is ACK
975 # modified for supporting more blocks count than 65535, O.Z. 15.08.2007
976 0 $self->{'_REQUEST_'}{'PREVACK'} = $self->{'_REQUEST_'}{'LASTACK'};
977 0 ▶ if (int(($self->{'_REQUEST_'}{'LASTACK'}+1) % 65536) == unpack("n", $datain)){
Branch: 1 branch
BranchTrueFalseTotal
if (int(($self->{"_REQUEST_"}{"LASTACK"} + 1) % 65536) == unpack("n", $datain)) 0 0 0
978 0 $self->{'_REQUEST_'}{'LASTACK'}++;
979 };
980 0 return (1);
981 }
982 elsif ($opcode eq TFTP_OPCODE_ERROR)
983 {
984 # message is ERR
985 0 $LASTERROR = sprintf "TFTP error message: %s", $datain;
986 0 return (undef);
987 }
988 else
989 {
990 # other messages...
991 0 $LASTERROR = sprintf "Opcode %d not supported as a reply to DATA\n", $opcode;
992 0 return (undef);
993 }
994 }
995 else
996 {
997 0 $! = $udpserver->sockopt(SO_ERROR);
998 0 $LASTERROR = sprintf "Socket RECV error: %s\n", $!;
999 0 return (undef);
1000 }
1001 }
1002 else
1003 {
1004 0 $LASTERROR = sprintf "Retry %d - timeout occurred on ACK packet %d\n", $retry, $self->{'_REQUEST_'}{'LASTACK'} + 1;
1005 0 ▶ $debug and carp($LASTERROR);
Branch: 1 branch
BranchTrueFalseTotal
$debug and carp($Net::TFTPd::LASTERROR) 0 0 0
1006 0 $retry++;
1007 }
1008 }
1009 else
1010 {
1011 0 $! = $udpserver->sockopt(SO_ERROR);
1012 0 $LASTERROR = sprintf "Socket SEND error: %s\n", $!;
1013 0 return (undef);
1014 }
1015 }
1016 }
1017  
1018 #
1019 # Usage: $requestOBJ->openFILE()
1020 # returns 1 if file is opened, undef if error
1021 #
1022 sub openFILE
1023 0 ▶{
Subroutine openFILE: not called CC=7 SCAR=40.3
Pod undocumented
1024 # the request object
1025 0 my $self = shift;
1026  
1027 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_RRQ)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'OPCODE'} eq 1) { } 0 0 0
elsif ($self->{'_REQUEST_'}{'OPCODE'} eq 2) { } 0 0 0
1028 {
1029 ########################################
1030 # opcode is RRQ, open file for reading #
1031 ########################################
1032 0 ▶ if (open(RFH, "<".$self->{'_REQUEST_'}{'FileName'}))
Branch: 1 branch
BranchTrueFalseTotal
if (open RFH, '<' . $self->{'_REQUEST_'}{'FileName'}) { } 0 0 0
1033 {
1034 # if OCTET mode, set FileHandle to binary mode...
1035 0 ▶ if ($self->{'_REQUEST_'}{'Mode'} eq 'OCTET')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"Mode"} eq "OCTET") 0 0 0
1036 {
1037 0 binmode(RFH);
1038 }
1039  
1040 0 my $size = -s($self->{'_REQUEST_'}{'FileName'});
1041 0 $self->{'_REQUEST_'}{'LASTBLK'} = 1 + int($size / $self->{'BlkSize'});
1042  
1043 # save the filehandle reference...
1044 0 $self->{'_REQUEST_'}{'_FH_'} = *RFH;
1045  
1046 0 return (1);
1047 }
1048 else
1049 {
1050 0 $LASTERROR = sprintf "Error opening file \'%s\' for reading\n", $self->{'_REQUEST_'}{'FileName'};
1051 0 return (undef);
1052 }
1053 }
1054 elsif ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_WRQ)
1055 {
1056 ########################################
1057 # opcode is WRQ, open file for writing #
1058 ########################################
1059 0 ▶ if (open(WFH, ">".$self->{'_REQUEST_'}{'FileName'}))
Branch: 1 branch
BranchTrueFalseTotal
if (open WFH, '>' . $self->{'_REQUEST_'}{'FileName'}) { } 0 0 0
1060 {
1061 # if OCTET mode, set FileHandle to binary mode...
1062 0 ▶ if ($self->{'_REQUEST_'}{'Mode'} eq 'OCTET')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_REQUEST_"}{"Mode"} eq "OCTET") 0 0 0
1063 {
1064 0 binmode(WFH);
1065 }
1066  
1067 # save the filehandle reference...
1068 0 $self->{'_REQUEST_'}{'_FH_'} = *WFH;
1069  
1070 0 return (1);
1071 }
1072 else
1073 {
1074 0 $LASTERROR = sprintf "Error opening file \'%s\' for writing\n", $self->{'_REQUEST_'}{'FileName'};
1075 0 return (undef);
1076 }
1077 }
1078 else
1079 {
1080 ############################
1081 # other opcodes are errors #
1082 ############################
1083 0 $LASTERROR = sprintf "OPCODE %d is not supported\n", $self->{'_REQUEST_'}{'OPCODE'};
1084 0 return (undef);
1085 }
1086 }
1087  
1088 #
1089 # Usage: $requestOBJ->closeFILE()
1090 # returns 1 if file is success, undef if error
1091 #
1092 sub closeFILE
1093 0 ▶{
Subroutine closeFILE: not called CC=3 SCAR=24.8
Pod undocumented
1094 0 my $self = shift;
1095  
1096 0 ▶ if ($self->{'_REQUEST_'}{'_FH_'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'_FH_'}) { } 0 0 0
1097 {
1098 0 ▶ if (close($self->{'_REQUEST_'}{'_FH_'}))
Branch: 1 branch
BranchTrueFalseTotal
if (close $self->{'_REQUEST_'}{'_FH_'}) { } 0 0 0
1099 {
1100 0 return (1);
1101 }
1102 else
1103 {
1104 0 $LASTERROR = "Error closing filehandle\n";
1105 0 return (undef);
1106 }
1107 }
1108 else
1109 {
1110 0 return (1);
1111 }
1112 }
1113  
1114 #
1115 # Usage: $requestOBJ->checkFILE()
1116 # returns 1 if file is found, undef if file is not found
1117 #
1118 sub checkFILE
1119 0 ▶{
Subroutine checkFILE: not called CC=6 SCAR=37.4
Pod undocumented
1120 # the request object
1121 0 my $self = shift;
1122  
1123 # requested file
1124 0 my $reqfile = $self->{'_REQUEST_'}{'FileName'};
1125  
1126 0 ▶ if ($self->{'FileName'})
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'FileName'}) { } 0 0 0
elsif ($self->{'RootDir'}) { } 0 0 0
1127 {
1128 # filename is fixed
1129 0 $self->{'_REQUEST_'}{'FileName'} = $self->{'FileName'};
1130  
1131 0 ▶ if (($self->{'FileName'} =~ /$reqfile/) and -e($self->{'FileName'}))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"FileName"} =~ /$reqfile/ and -e $self->{"FileName"}) 0 0 0
Condition0%0/3
$self->{"FileName"} =~ /$reqfile/ and -e $self->{"FileName"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"FileName"} =~ /$reqfile/ and -e $self->{"FileName"}
$self->{"FileName"} =~ /$reqfile/ -e $self->{"FileName"}
Truth table0%0/3
A and B
A= $self->{"FileName"} =~ /$reqfile/B= -e $self->{"FileName"}
ABresult
0X0
100
111
1132 {
1133 # fixed name contains requested file and file exists
1134 0 $self->{'FileSize'} = -s($self->{'FileName'});
1135 0 return (1);
1136 }
1137 }
1138 elsif ($self->{'RootDir'})
1139 {
1140 # rootdir is fixed
1141 0 $reqfile = $self->{'RootDir'}.'/'.$reqfile;
1142 0 $self->{'_REQUEST_'}{'FileName'} = $reqfile;
1143  
1144 0 ▶ if (-e($reqfile))
Branch: 1 branch
BranchTrueFalseTotal
if (-e $reqfile) 0 0 0
1145 {
1146 # file exists in rootdir
1147 0 $self->{'FileSize'} = -s($reqfile);
1148 0 return (1);
1149 }
1150 }
1151  
1152 0 return (undef);
1153 }
1154  
1155 #
1156 # Usage: $requestOBJ->sendOACK();
1157 # return 1 for success and undef for error (see $Net::TFTPd::LASTERROR for cause)
1158 #
1159 sub sendOACK
1160 0 ▶{
Subroutine sendOACK: not called CC=11 SCAR=48.8
Pod undocumented
1161 # the request object
1162 0 my $self = shift;
1163 0 my $udpserver = $self->{'_UDPSERVER_'};
1164 0 my $retry = 0;
1165  
1166 0 my ($datagram, $opcode, $datain);
1167  
1168 0 ▶ while ($retry < $self->{'ACKretries'})
Branch: 1 branch
BranchTrueFalseTotal
if ($retry < $self->{"ACKretries"}) 0 0 0
1169 {
1170 # send oack
1171 0 my $data = join("\0", %{ $self->{'_RESPONSE_'}{'RFC2347'} })."\0";
1172 0 ▶ if ($udpserver->send(pack("na*", TFTP_OPCODE_OACK, $data)))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->send(pack('na*', 6, $data))) { } 0 0 0
1173 {
1174 # opcode is RRQ
1175 0 ▶ if ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_RRQ)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_REQUEST_'}{'OPCODE'} eq 1) { } 0 0 0
elsif ($self->{'_REQUEST_'}{'OPCODE'} eq 2) { } 0 0 0
1176 {
1177 # vars for IO select
1178 0 my ($rin, $rout, $ein, $eout) = ('', '', '', '');
1179 0 vec($rin, fileno($udpserver), 1) = 1;
1180  
1181 # wait for acknowledge
1182 0 ▶ if (select($rout=$rin, undef, $eout=$ein, $self->{'ACKtimeout'}))
Branch: 1 branch
BranchTrueFalseTotal
if (select $rout = $rin, undef, $eout = $ein, $self->{'ACKtimeout'}) { } 0 0 0
1183 {
1184 # read the message
1185 0 ▶ if ($udpserver->recv($datagram, TFTP_MAX_BLKSIZE + 4))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->recv($datagram, 65468)) { } 0 0 0
1186 {
1187 # decode the message
1188 0 ($opcode, $datain) = unpack("na*", $datagram);
1189 0 ▶ if ($opcode == TFTP_OPCODE_ACK)
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode == 4) { } 0 0 0
elsif ($opcode == 5) { } 0 0 0
1190 {
1191 # message is ACK
1192 0 my $lastack = unpack("n", $datain);
1193 0 ▶ if ($lastack)
Branch: 1 branch
BranchTrueFalseTotal
if ($lastack) 0 0 0
1194 {
1195 # ack is not for block 0... ERROR
1196 0 $LASTERROR = sprintf "Received ACK for block %d instead of 0", $lastack;
1197 0 return (undef);
1198 }
1199 0 return 1;
1200 }
1201 elsif ($opcode == TFTP_OPCODE_ERROR)
1202 {
1203 # message is ERR
1204 0 $LASTERROR = sprintf "TFTP error message: %s", $datain;
1205 0 return (undef);
1206 }
1207 else
1208 {
1209 # other messages...
1210 0 $LASTERROR = sprintf "Opcode %d not supported as a reply to OACK\n", $opcode;
1211 0 return (undef);
1212 }
1213 }
1214 else
1215 {
1216 0 $! = $udpserver->sockopt(SO_ERROR);
1217 0 $LASTERROR = sprintf "Socket RECV error: %s\n", $!;
1218 0 return (undef);
1219 }
1220 }
1221 else
1222 {
1223 0 $LASTERROR = sprintf "Retry %d - timeout occurred waiting reply for OACK packet\n", $retry;
1224 0 ▶ $debug and carp($LASTERROR);
Branch: 1 branch
BranchTrueFalseTotal
$debug and carp($Net::TFTPd::LASTERROR) 0 0 0
1225 0 $retry++;
1226 }
1227 }
1228 elsif ($self->{'_REQUEST_'}{'OPCODE'} eq TFTP_OPCODE_WRQ)
1229 {
1230 # opcode is WRQ
1231 0 return (1);
1232 }
1233 }
1234 else
1235 {
1236 0 $! = $udpserver->sockopt(SO_ERROR);
1237 0 $LASTERROR = sprintf "Socket SEND error: %s\n", $!;
1238 0 return (undef);
1239 }
1240 }
1241 }
1242  
1243 #
1244 # Usage: $requestOBJ->sendERR($code, $message);
1245 # returns 1 if success, undef if error
1246 #
1247 sub sendERR
1248 0 ▶{
Subroutine sendERR: not called CC=3 SCAR=24.8
Pod undocumented
1249 0 my $self = shift;
1250 0 my ($errcode, $errmsg) = @_;
1251 # modified for supporting NETASCII transfers on 25/05/2009
1252 #$errmsg or $errmsg = '';
1253 0 ▶ $errmsg or $errmsg = $ERRORS{$errcode};
Branch: 1 branch
BranchTrueFalseTotal
$errmsg or $errmsg = $ERRORS{$errcode} 0 0 0
1254  
1255 0 my $udpserver = $self->{'_UDPSERVER_'};
1256  
1257 0 ▶ if ($udpserver->send(pack("nnZ*", 5, $errcode, $errmsg)))
Branch: 1 branch
BranchTrueFalseTotal
if ($udpserver->send(pack('nnZ*', 5, $errcode, $errmsg))) { } 0 0 0
1258 {
1259 0 return (1);
1260 }
1261 else
1262 {
1263 0 $! = $udpserver->sockopt(SO_ERROR);
1264 0 $LASTERROR = sprintf "Socket SEND error: %s\n", $!;
1265 0 return (undef);
1266 }
1267 }
1268  
1269 sub server
1270 0 ▶{
Subroutine server: not called CC=1 SCAR=6.9
Pod documented
1271 0 my $self = shift;
1272 0 return $self->{'_UDPSERVER_'};
1273 }
1274  
1275 sub error
1276 0 ▶{
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
1277 0 return ($LASTERROR);
1278 }
1279  
1280 # Preloaded methods go here.
1281  
1282 1 1;
1283 __END__
↑ Summary script/tftpd-simple.pl »