TFTPd.pm

filter: Statementstmt 83.2% 104 / 125 Branchbran 76.6% 46 / 60 Conditioncond 78.9% 15 / 19 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 86.9% 20 / 23 Podpod 100% 1 / 1 totaltotal 80.7% 193 / 239 CC 38 SCAR 22.2
CC
38
Cov
81%
_new_request
18.6
start
12.0
_incoming
10.8
CRAP 9.2
« TFTPd/Connection.pm ↑ Summary
1 package Mojo::TFTPd;
2 6 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Mojo::TFTPd::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use constant DEBUG => !!$ENV{MOJO_TFTPD_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use constant OPCODE_RRQ => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use constant OPCODE_WRQ => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use constant OPCODE_DATA => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use constant OPCODE_ACK => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use constant OPCODE_ERROR => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use constant OPCODE_OACK => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use constant MIN_BLOCK_SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use constant MAX_BLOCK_SIZE => 65464; # From RFC 2348
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 our $VERSION = '0.05';
19  
20 6 has connection_class => 'Mojo::TFTPd::Connection';
21 6 has inactive_timeout => 15;
22 5 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
23 6 has listen => 'tftp://*:69';
24 6 has max_connections => 1000;
25 6 has retransmit => 0;
26 6 has retransmit_timeout => 2;
27 6 has retries => 1;
28  
29 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod documented
30 0 my $self = shift;
31 0 ▶ return $self if $self->{connections};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connections"} 0 0 0
32  
33 # split $self->listen into host and port
34 0 my ($host, $port) = $self->_parse_listen;
35 0 warn "[Mojo::TFTPd] Listen to $host:$port\n" if DEBUG;
36  
37 0 my $socket = IO::Socket::INET->new(LocalAddr => $host, LocalPort => $port, Proto => 'udp');
38 0 ▶ return $self->emit(error => "Can't create listen socket: $!") unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
39  
40 0 my $reactor = $self->ioloop->reactor;
41 0 weaken $self;
42 0 $socket->blocking(0);
43 0 ▶ $reactor->io($socket, sub { $self->_incoming });
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 $reactor->watch($socket, 1, 0); # watch read events
45 0 $self->{connections} = {};
46 0 $self->{socket} = $socket;
47  
48 0 return $self;
49 }
50  
51 8 ▶sub _delete_connection {
Subroutine _delete_connection: called CC=2 SCAR=7.0
52 8 my ($self, $connection) = @_;
53 8 delete $self->{connections}{$connection->peername};
54 8 ▶ $self->ioloop->remove($connection->{timer}) if $connection->{timer};
Branch: 1 branch
BranchTrueFalseTotal
if $connection->{"timer"} 8 0 8
55 8 $self->emit(finish => $connection, $connection->error);
56 }
57  
58 131125 ▶sub _incoming {
Subroutine _incoming: called CC=10 SCAR=23.8
59 131125 my $self = shift;
60 131125 my $socket = $self->{socket};
61  
62 # Add 4 Bytes of Opcode + Block#
63 131125 ▶ return $self->emit(error => "Read: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $read = $socket->recv(my $datagram, 65468)) 1 131124 131125
64 unless defined(my $read = $socket->recv(my $datagram, MAX_BLOCK_SIZE + 4));
65  
66 131124 my $opcode = unpack 'n', substr $datagram, 0, 2, '';
67 131124 ▶ if ($opcode eq OPCODE_RRQ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode eq 1) { } 15 131109 131124
elsif ($opcode eq 2) { } 7 131102 131109
68 15 return $self->_new_request(rrq => $datagram);
69 }
70 elsif ($opcode eq OPCODE_WRQ) {
71 7 return $self->_new_request(wrq => $datagram);
72 }
73  
74 131102 my $connection = $self->{connections}{$socket->peername};
75 131102 ▶ return $self->emit(error => "@{[$socket->peerhost]} has no connection") unless $connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $connection 3 131099 131102
76  
77 131099 ▶ if ($opcode == OPCODE_ACK) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opcode == 4) { } 65553 65546 131099
elsif ($opcode == 3) { } 65546 0 65546
elsif ($opcode == 5) { } 0 0 0
78 65553 ▶ $connection->receive_ack($datagram)
Branch: 1 branch
BranchTrueFalseTotal
$connection->receive_ack($datagram) ? : 65548 5 65553
79 ? $self->_reset_timer($connection)
80 : $self->_delete_connection($connection);
81 }
82 elsif ($opcode == OPCODE_DATA) {
83 65546 ▶ $connection->receive_data($datagram)
Branch: 1 branch
BranchTrueFalseTotal
$connection->receive_data($datagram) ? : 65543 3 65546
84 ? $self->_reset_timer($connection)
85 : $self->_delete_connection($connection);
86 }
87 elsif ($opcode == OPCODE_ERROR) {
88 0 $connection->receive_error($datagram);
89 0 $self->_delete_connection($connection);
90 }
91 else {
92 0 $connection->error('Unknown opcode');
93 0 $self->_delete_connection($connection);
94 }
95 }
96  
97 22 ▶sub _new_request {
Subroutine _new_request: called CC=18 SCAR=29.2
98 22 my ($self, $type, $datagram) = @_;
99 22 my ($file, $mode, @rfc) = split "\0", $datagram;
100 22 my $socket = $self->{socket};
101 22 warn "[Mojo::TFTPd] <<< @{[$socket->peerhost]} new request $type $file $mode @rfc\n" if DEBUG;
102  
103 22 ▶ return $self->emit(error => "Cannot handle $type requests") unless $self->has_subscribers($type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_subscribers($type) 4 18 22
104 return $self->emit(error => "Max connections ($self->{max_connections}) reached")
105 18 ▶ if $self->max_connections <= keys %{$self->{connections}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_connections <= keys %{$$self{"connections"};} 1 17 18
106  
107 17 my %rfc = @rfc;
108 17 ▶ my $connection = $self->connection_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->retransmit ? : 4 13 17
109 type => $type,
110 file => $file,
111 mode => $mode,
112 peerhost => $socket->peerhost,
113 peername => $socket->peername,
114 retries => $self->retries,
115 timeout => $self->retransmit ? $self->retransmit_timeout : $self->inactive_timeout,
116 retransmit => $self->retransmit,
117 rfc => \%rfc,
118 socket => $socket,
119 );
120  
121 17 ▶ if ($rfc{blksize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rfc{"blksize"}) 5 12 17
122 5 ▶ $rfc{blksize} = MIN_BLOCK_SIZE if $rfc{blksize} < MIN_BLOCK_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $rfc{"blksize"} < 8 0 5 5
123 5 ▶ $rfc{blksize} = MAX_BLOCK_SIZE if $rfc{blksize} > MAX_BLOCK_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $rfc{"blksize"} > 65464 0 5 5
124 5 $connection->blocksize($rfc{blksize});
125 }
126 17 ▶ if ($rfc{timeout} and $rfc{timeout} >= 0 and $rfc{timeout} <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rfc{"timeout"} and $rfc{"timeout"} >= 0 and $rfc{"timeout"} <= 255) 1 16 17
Condition66%4/6
$rfc{"timeout"} and $rfc{"timeout"} >= 0 and $rfc{"timeout"} <= 255
!ll&&!rl&&r
1601
$rfc{"timeout"} and $rfc{"timeout"} >= 0
!ll&&!rl&&r
1601
MC/DC33%1/3
$rfc{"timeout"} and $rfc{"timeout"} >= 0 and $rfc{"timeout"} <= 255
$rfc{"timeout"} $rfc{"timeout"} >= 0 $rfc{"timeout"} <= 255
Truth table50%2/4
A and B and C
A= $rfc{"timeout"}B= $rfc{"timeout"} >= 0C= $rfc{"timeout"} <= 255
ABCresult
0XX0
10X0
1100
1111
127 1 $connection->timeout($rfc{timeout});
128 }
129 17 ▶ if ($type eq 'wrq' and $rfc{tsize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "wrq" and $rfc{"tsize"}) 1 16 17
Condition100%3/3
$type eq "wrq" and $rfc{"tsize"}
!ll&&!rl&&r
1331
MC/DC100%2/2
$type eq "wrq" and $rfc{"tsize"}
$type eq "wrq" $rfc{"tsize"}
Truth table100%3/3
A and B
A= $type eq "wrq"B= $rfc{"tsize"}
ABresult
0X0
100
111
130 1 $connection->filesize($rfc{tsize});
131 }
132  
133 17 $self->emit($type => $connection);
134  
135 17 ▶ if (!$connection->filehandle) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $connection->filehandle) { } 2 15 17
elsif (%rfc and $connection->send_oack) { } 8 7 15
elsif ($type eq 'rrq' ? $connection->send_data : $connection->send_ack) { } 7 0 7
136 2 ▶ $connection->send_error(file_not_found => $connection->error || 'No filehandle');
Condition50%1/2
$connection->error || 'No filehandle'
l!l
02
MC/DC0%0/1
$connection->error || 'No filehandle'
$connection->error
Truth table50%1/2
A
A= $connection->error
Aresult
11
00
137 2 $self->_reset_timer($connection);
138 }
139 ▶ elsif (%rfc and $connection->send_oack) {
Condition66%2/3
%rfc and $connection->send_oack
!ll&&!rl&&r
708
MC/DC50%1/2
%rfc and $connection->send_oack
%rfc $connection->send_oack
Truth table66%2/3
A and B
A= %rfcB= $connection->send_oack
ABresult
0X0
100
111
140 8 $self->{connections}{$connection->peername} = $connection;
141 8 $self->_reset_timer($connection);
142 }
143 ▶ elsif ($type eq 'rrq' ? $connection->send_data : $connection->send_ack) {
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'rrq' ? : 5 2 7
144 7 $self->{connections}{$connection->peername} = $connection;
145 7 $self->_reset_timer($connection);
146 }
147 else {
148 0 $self->emit(finish => $connection, $connection->error);
149 }
150 }
151  
152 7 ▶sub _parse_listen {
Subroutine _parse_listen: called CC=5 SCAR=16.1
153 7 my $self = shift;
154  
155 7 my ($scheme, $host, $port) = $self->listen =~ m!
156 (?: ([^:/]+) :// )? # part before ://
157 ([^:]*) # everyting until a :
158 (?: : (\d+) )? # any digits after the :
159 !xms;
160  
161 7 ▶ $port = getservbyname($scheme, '') if $scheme && !defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if $scheme and not defined $port 3 4 7
Condition100%3/3
$scheme and not defined $port
!ll&&!rl&&r
313
MC/DC100%2/2
$scheme and not defined $port
$scheme not defined $port
Truth table100%3/3
A and not(B)
A= $schemeB= not defined $port
ABresult
0X0
100
111
162 7 ▶ $port //= 69;
Condition100%2/2
$port //= 69
l!l
25
MC/DC100%1/1
$port //= 69
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
163 7 ▶ $host = '0.0.0.0' if $host eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "*" 2 5 7
164  
165 7 return ($host, $port);
166 }
167  
168 131108 ▶sub _reset_timer {
Subroutine _reset_timer: called CC=2 SCAR=7.9
169 131108 my ($self, $connection) = @_;
170  
171 131108 ▶ $self->ioloop->remove($connection->{timer}) if $connection->{timer};
Branch: 1 branch
BranchTrueFalseTotal
if $connection->{"timer"} 131091 17 131108
172 $connection->{timer} = $self->ioloop->recurring(
173 $connection->timeout,
174 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
175 0 ▶ $connection->send_retransmit or $self->_delete_connection($connection);
Branch: 1 branch
BranchTrueFalseTotal
$connection->send_retransmit or $self->_delete_connection($connection) 0 0 0
176 }
177 131108 );
178 }
179  
180 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
181 2 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 2 2
182 2 my $self = shift;
183 2 ▶ $self->ioloop->reactor->remove($self->{socket}) if $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 1 1 2
184 }
185  
186 6 1;
187  
188 =encoding utf8
189  
190 =head1 NAME
191  
192 Mojo::TFTPd - Trivial File Transfer Protocol daemon
193  
194 =head1 VERSION
195  
196 0.04
197  
198 =head1 SYNOPSIS
199  
200 use Mojo::TFTPd;
201 my $tftpd = Mojo::TFTPd->new;
202  
203 $tftpd->on(error => sub ($tftpd, $error) { warn "TFTPd: $error\n" });
204  
205 $tftpd->on(rrq => sub ($tftpd, $connection) {
206 open my $FH, '<', $connection->file;
207 $connection->filehandle($FH);
208 $connection->filesize(-s $connection->file);
209 });
210  
211 $tftpd->on(wrq => sub ($tftpd, $connection) {
212 open my $FH, '>', '/dev/null';
213 $connection->filehandle($FH);
214 });
215  
216 $tftpd->on(finish => sub ($tftpd, $connection, $error) {
217 warn "Connection: $error\n" if $error;
218 });
219  
220 $tftpd->start;
221 $tftpd->ioloop->start unless $tftpd->ioloop->is_running;
222  
223 =head1 DESCRIPTION
224  
225 This module implements a server for the
226 L<Trivial File Transfer Protocol|http://en.wikipedia.org/wiki/Trivial_File_Transfer_Protocol>.
227  
228 From Wikipedia:
229  
230 Trivial File Transfer Protocol (TFTP) is a file transfer protocol notable
231 for its simplicity. It is generally used for automated transfer of
232 configuration or boot files between machines in a local environment.
233  
234 The connection which is referred to in this document is an instance of
235 L<Mojo::TFTPd::Connection>.
236  
237 =head1 EVENTS
238  
239 =head2 error
240  
241 $tftpd->on(error => sub ($tftpd, $str) { ... });
242  
243 This event is emitted when something goes wrong: Fail to L</listen> to socket,
244 read from socket or other internal errors.
245  
246 =head2 finish
247  
248 $tftpd->on(finish => sub ($tftpd, $connection, $error) { ... });
249  
250 This event is emitted when the L<Mojo::TFTPd::Connection> finish, either
251 successfully or due to an error. C<$error> will be an empty string on success.
252  
253 =head2 rrq
254  
255 $tftpd->on(rrq => sub ($tftpd, $connection) { ... });
256  
257 This event is emitted when a new read request arrives from a client. The
258 callback should set L<Mojo::TFTPd::Connection/filehandle> or the connection
259 will be dropped.
260 L<Mojo::TFTPd::Connection/filehandle> can also be a L<Mojo::Asset> reference.
261  
262 =head2 wrq
263  
264 $tftpd->on(wrq => sub ($tftpd, $connection) { ... });
265  
266 This event is emitted when a new write request arrives from a client. The
267 callback should set L<Mojo::TFTPd::Connection/filehandle> or the connection
268 will be dropped.
269 L<Mojo::TFTPd::Connection/filehandle> can also be a L<Mojo::Asset> reference.
270  
271 =head1 ATTRIBUTES
272  
273 =head2 connection_class
274  
275 $str = $tftpd->connection_class;
276 $tftpd = $tftpd->connection_class($str);
277  
278 Used to set a custom connection class. Defaults to L<Mojo::TFTPd::Connection>.
279  
280 =head2 inactive_timeout
281  
282 $num = $tftpd->inactive_timeout;
283 $tftpd = $tftpd->inactive_timeout(15);
284  
285 How long a L<connection|Mojo::TFTPd::Connection> can stay idle before
286 being dropped. Default is 15 seconds.
287  
288 =head2 ioloop
289  
290 $loop = $tftpd->ioloop;
291 $tftpd = $tftpd->ioloop(Mojo::IOLoop->new);
292  
293 Holds an instance of L<Mojo::IOLoop>.
294  
295 =head2 listen
296  
297 $str = $tftpd->listen;
298 $tftpd = $tftpd->listen('127.0.0.1:69');
299 $tftpd = $tftpd->listen('tftp://*:69');
300  
301 The bind address for this server.
302  
303 =head2 max_connections
304  
305 $int = $tftpd->max_connections;
306 $tftpd = $tftpd->max_connections(1000);
307  
308 How many concurrent connections this server can handle. Default to 1000.
309  
310 =head2 retransmit
311  
312 $int = $tftpd->retransmit;
313 $tftpd = $tftpd->retransmit(1);
314  
315 How many times the server should try to retransmit the last packet on timeout before
316 dropping the L<connection|Mojo::TFTPd::Connection>. Default is 0 (disable retransmits)
317  
318 =head2 retransmit_timeout
319  
320 $num = $tftpd->retransmit_timeout;
321 $tftpd = $tftpd->retransmit_timeout(2);
322  
323 How long a L<connection|Mojo::TFTPd::Connection> can stay idle before last packet
324 being retransmitted. Default is 2 seconds.
325  
326 =head2 retries
327  
328 $int = $tftpd->retries;
329 $tftpd = $tftpd->retries(1);
330  
331 How many times the server should try to send ACK or DATA to the client before
332 dropping the L<connection|Mojo::TFTPd::Connection>.
333  
334 =head1 METHODS
335  
336 =head2 start
337  
338 $tftpd = $tftpd->start;
339  
340 Starts listening to the address and port set in L</Listen>. The L</error>
341 event will be emitted if the server fail to start.
342  
343 =head1 AUTHOR
344  
345 Svetoslav Naydenov - C<harryl@cpan.org>
346  
347 Jan Henning Thorsen - C<jhthorsen@cpan.org>
348  
349 =cut
« TFTPd/Connection.pm ↑ Summary