| 1 | package POE::Component::TFTPd; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | POE::Component::TFTPd - A tftp-server, implemented through POE | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0.0302 | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | POE::Session->create( | ||||||||||||||||||||
| 14 | inline_states => { | ||||||||||||||||||||
| 15 | _start => sub { | ||||||||||||||||||||
| 16 | POE::Component::TFTPd->create; | ||||||||||||||||||||
| 17 | $_[KERNEL]->post($alias => 'start'); | ||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||
| 19 | tftpd_init => sub { | ||||||||||||||||||||
| 20 | my($client, $fh) = ($_[ARG0], undef); | ||||||||||||||||||||
| 21 | open($fh, "<", $client->filename) if($client->rrq); | ||||||||||||||||||||
| 22 | open($fh, ">", $client->filename) if($client->wrq); | ||||||||||||||||||||
| 23 | $client->{'fh'} = $fh; | ||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||
| 25 | tftpd_done => sub { | ||||||||||||||||||||
| 26 | my $client = $_[ARG0]; | ||||||||||||||||||||
| 27 | close $client->{'fh'}; | ||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||
| 29 | tftpd_send => sub { | ||||||||||||||||||||
| 30 | my $client = $_[ARG0]; | ||||||||||||||||||||
| 31 | if ($client->{'fh'}) { | ||||||||||||||||||||
| 32 | seek $client->{'fh'}, $client->last_block * $client->block_size, 0; | ||||||||||||||||||||
| 33 | read $client->{'fh'}, my $data, $client->block_size; | ||||||||||||||||||||
| 34 | $_[KERNEL]->post($alias => send_data => $client, $data); | ||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||
| 37 | tftpd_receive => sub { | ||||||||||||||||||||
| 38 | my($client, $data) = @_[ARG0,ARG1]; | ||||||||||||||||||||
| 39 | print { $client->{'fh'} } $data; | ||||||||||||||||||||
| 40 | $_[KERNEL]->post($alias => send_ack => $client); | ||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||
| 42 | tftpd_log => sub { | ||||||||||||||||||||
| 43 | my($level, $client, $msg) = @_[ARG0..ARG2]; | ||||||||||||||||||||
| 44 | warn(sprintf "%s - %s:%i - %s\n", | ||||||||||||||||||||
| 45 | $level, $client->address, $client->port, $msg, | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | 3 | ▶ | use POE::Component::TFTPd::Client; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 3 | ▶ | use POE qw/Wheel::UDP Filter::Stream/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 3 | our $VERSION = eval '0.0302'; | |||||||||||||||||||
| 59 | 3 | our %TFTP_ERROR = ( | |||||||||||||||||||
| 60 | not_defined => [0, "Not defined, see error message"], | ||||||||||||||||||||
| 61 | unknown_opcode => [0, "Unknown opcode: %s"], | ||||||||||||||||||||
| 62 | no_connection => [0, "No connection"], | ||||||||||||||||||||
| 63 | file_not_found => [1, "File not found"], | ||||||||||||||||||||
| 64 | access_violation => [2, "Access violation"], | ||||||||||||||||||||
| 65 | disk_full => [3, "Disk full or allocation exceeded"], | ||||||||||||||||||||
| 66 | illegal_operation => [4, "Illegal TFTP operation"], | ||||||||||||||||||||
| 67 | unknown_transfer_id => [5, "Unknown transfer ID"], | ||||||||||||||||||||
| 68 | file_exists => [6, "File already exists"], | ||||||||||||||||||||
| 69 | no_such_user => [7, "No such user"], | ||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head2 create(%args) | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | Component constructor. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Args: | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Name => default # Comment | ||||||||||||||||||||
| 81 | -------------------------------------------------------------------- | ||||||||||||||||||||
| 82 | alias => TFTPd # Alias for the POE session | ||||||||||||||||||||
| 83 | address => 127.0.0.1 # Address to listen to | ||||||||||||||||||||
| 84 | port => 69 # Port to listen to | ||||||||||||||||||||
| 85 | timeout => 10 # Seconds between block sent and ACK | ||||||||||||||||||||
| 86 | retries => 3 # How many retries before giving up on host | ||||||||||||||||||||
| 87 | max_clients => undef # Maximum concurrent connections | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | ▶ | sub create { | ||||||||||||||||||
|
Subroutine
create: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||
| 92 | 1 | my $class = shift; | |||||||||||||||||||
| 93 | 1 | my %args = @_; | |||||||||||||||||||
| 94 | 1 | my $self = bless \%args, $class; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | ▶ | $self->{'alias'} ||= 'TFTPd'; | ||||||||||||||||||
|
Condition50%1/2
$self->{"alias"} ||= "TFTPd"
MC/DC0%0/1
$self->{"alias"} ||= "TFTPd"
$self->{"alias"}
Truth table50%1/2
A
A= $self->{"alias"}
| |||||||||||||||||||||
| 97 | 1 | ▶ | $self->{'address'} ||= '127.0.0.1'; | ||||||||||||||||||
|
Condition50%1/2
$self->{"address"} ||= "127.0.0.1"
MC/DC0%0/1
$self->{"address"} ||= "127.0.0.1"
$self->{"address"}
Truth table50%1/2
A
A= $self->{"address"}
| |||||||||||||||||||||
| 98 | 1 | ▶ | $self->{'port'} ||= 69; | ||||||||||||||||||
|
Condition50%1/2
$self->{"port"} ||= 69
MC/DC0%0/1
$self->{"port"} ||= 69
$self->{"port"}
Truth table50%1/2
A
A= $self->{"port"}
| |||||||||||||||||||||
| 99 | 1 | ▶ | $self->{'timeout'} ||= 10; | ||||||||||||||||||
|
Condition50%1/2
$self->{"timeout"} ||= 10
MC/DC0%0/1
$self->{"timeout"} ||= 10
$self->{"timeout"}
Truth table50%1/2
A
A= $self->{"timeout"}
| |||||||||||||||||||||
| 100 | 1 | ▶ | $self->{'retries'} ||= 3; | ||||||||||||||||||
|
Condition50%1/2
$self->{"retries"} ||= 3
MC/DC0%0/1
$self->{"retries"} ||= 3
$self->{"retries"}
Truth table50%1/2
A
A= $self->{"retries"}
| |||||||||||||||||||||
| 101 | 1 | $self->{'clients'} = {}; | |||||||||||||||||||
| 102 | $self->{'session'} = POE::Session->create( | ||||||||||||||||||||
| 103 | inline_states => { | ||||||||||||||||||||
| 104 | 1 | ▶ | _start => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 1 | $_[KERNEL]->alias_set($self->alias); | |||||||||||||||||||
| 106 | 1 | $_[KERNEL]->delay(check_connections => 1); | |||||||||||||||||||
| 107 | }, | ||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||
| 109 | 1 | object_states => [ | |||||||||||||||||||
| 110 | $self => [ qw/start stop send_error input check_connections/ ], | ||||||||||||||||||||
| 111 | $self => { | ||||||||||||||||||||
| 112 | init_rrq => 'init_request', | ||||||||||||||||||||
| 113 | init_wrq => 'init_request', | ||||||||||||||||||||
| 114 | get_ack => 'get_data', | ||||||||||||||||||||
| 115 | get_data => 'get_data', | ||||||||||||||||||||
| 116 | send_ack => 'send_data', | ||||||||||||||||||||
| 117 | send_data => 'send_data', | ||||||||||||||||||||
| 118 | }, | ||||||||||||||||||||
| 119 | ], | ||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 1 | return $self; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 clients | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Returns a hash-ref, containing all the clients: | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | $client_id => $client_obj | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | See C<POE::Component::TFTPd::Client> for details | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head2 max_clients | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | Pointer to max number of concurrent clients: | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | print $self->max_clients; | ||||||||||||||||||||
| 138 | $self->max_clients = 4; | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head2 retries | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Pointer to the number of retries: | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | print $self->retries; | ||||||||||||||||||||
| 145 | $self->retries = 4; | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head2 timeout | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Pointer to the timeout in seconds: | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | print $self->timeout; | ||||||||||||||||||||
| 152 | $self->timeout = 4; | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 address | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Returns the address the server listens to. | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 alias | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | The alias for the POE session. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =head2 kernel | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | Method alias for C<$_[KERNEL]>. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head2 port | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Returns the local port | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head2 sender | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Returns the sender session. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 server | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Returns the server: C<POE::Wheel::UDP>. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 session | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Returns this session. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 3 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 185 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 3 | my @lvalue = qw/retries timeout max_clients/; | |||||||||||||||||||
| 188 | 3 | my @get = qw/alias address port clients kernel server sender session/; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 3 | for my $sub (@lvalue) { | |||||||||||||||||||
| 191 | 0 | ▶ | *$sub = sub :lvalue { shift->{$sub} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 3 | for my $sub (@get) { | |||||||||||||||||||
| 195 | 24 | ▶ | *$sub = sub { shift->{$sub} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =head2 cleanup | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 1. Logs that the server is done with the client | ||||||||||||||||||||
| 202 | 2. deletes the client from C<$self-E<gt>clients> | ||||||||||||||||||||
| 203 | 3. Calls C<tftpd_done> event in sender session | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 0 | ▶ | sub cleanup { | ||||||||||||||||||
|
Subroutine
cleanup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 208 | 0 | my $self = shift; | |||||||||||||||||||
| 209 | 0 | my $client = shift; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 0 | $self->log(trace => $client, 'done'); | |||||||||||||||||||
| 212 | 0 | $self->kernel->post($self->sender => tftpd_done => $client); | |||||||||||||||||||
| 213 | 0 | delete $self->clients->{ $client->id }; | |||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =head2 log | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | Calls SENDER with event name 'tftpd_log' and these arguments: | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | $_[ARG0] = $level | ||||||||||||||||||||
| 221 | $_[ARG1] = $client | ||||||||||||||||||||
| 222 | $_[ARG2] = $msg | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | C<$level> is the same as C<Log::Log4perl> use. | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 0 | ▶ | sub log { | ||||||||||||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 229 | 0 | my $self = shift; | |||||||||||||||||||
| 230 | 0 | $self->kernel->post($self->sender => tftpd_log => @_); | |||||||||||||||||||
| 231 | } | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head1 STATES | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head2 start | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Starts the server, by setting up C<POE::Wheel::UDP>. | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 0 | ▶ | sub start { | ||||||||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 242 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 243 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 0 | $self->{'sender'} = $_[SENDER]; | |||||||||||||||||||
| 246 | 0 | $self->{'kernel'} = $_[KERNEL]; | |||||||||||||||||||
| 247 | 0 | $self->{'server'} = POE::Wheel::UDP->new( | |||||||||||||||||||
| 248 | Filter => POE::Filter::Stream->new, | ||||||||||||||||||||
| 249 | LocalAddr => $self->address, | ||||||||||||||||||||
| 250 | LocalPort => $self->port, | ||||||||||||||||||||
| 251 | InputEvent => 'input', | ||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||
| 253 | } | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =head2 stop | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | Stops the TFTPd server, by deleting the UDP wheel. | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 0 | ▶ | sub stop { | ||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 262 | 0 | delete $_[OBJECT]->{'server'}; | |||||||||||||||||||
| 263 | 0 | $_[KERNEL]->alias_remove($_[OBJECT]->alias); | |||||||||||||||||||
| 264 | 0 | $_[KERNEL]->alarm_remove_all; | |||||||||||||||||||
| 265 | } | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | =head2 check_connections | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | Checks for connections that have timed out, and destroys them. This is done | ||||||||||||||||||||
| 270 | periodically, every second. | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =cut | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | 0 | ▶ | sub check_connections { | ||||||||||||||||||
|
Subroutine
check_connections: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 275 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 276 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 277 | 0 | my $clients = $self->clients; | |||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | CLIENT: | ||||||||||||||||||||
| 280 | 0 | for my $client (values %$clients) { | |||||||||||||||||||
| 281 | 0 | ▶ | if($client->retries <= 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 282 | 0 | $self->log(info => $client, 'timeout'); | |||||||||||||||||||
| 283 | 0 | delete $clients->{ $client->id }; | |||||||||||||||||||
| 284 | } | ||||||||||||||||||||
| 285 | 0 | ▶ | if($client->timestamp < time - $self->timeout) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | 0 | $self->log(trace => $client, 'retry'); | |||||||||||||||||||
| 287 | 0 | $client->retries--; | |||||||||||||||||||
| 288 | 0 | $kernel->post($self->sender => tftpd_send => $client); | |||||||||||||||||||
| 289 | } | ||||||||||||||||||||
| 290 | } | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | 0 | $kernel->delay(check_connections => 1); | |||||||||||||||||||
| 293 | } | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | =head2 input | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | Takes some input, figure out the opcode and pass the request on to the next | ||||||||||||||||||||
| 298 | stage. | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | opcode | event | method | ||||||||||||||||||||
| 301 | -------|----------|------------- | ||||||||||||||||||||
| 302 | rrq | init_rrq | init_request | ||||||||||||||||||||
| 303 | wrq | init_wrq | init_request | ||||||||||||||||||||
| 304 | ack | get_ack | get_data | ||||||||||||||||||||
| 305 | data | get_data | get_data | ||||||||||||||||||||
| 306 | |||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | 0 | ▶ | sub input { | ||||||||||||||||||
|
Subroutine
input: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 310 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 311 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 312 | 0 | my $args = $_[ARG0]; | |||||||||||||||||||
| 313 | 0 | my $client_id = join ":", $args->{'addr'}, $args->{'port'}; | |||||||||||||||||||
| 314 | 0 | my($opcode, $data) = unpack "na*", shift @{ $args->{'payload'} }; | |||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | 0 | ▶ | if($opcode eq &TFTP_OPCODE_RRQ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 317 | 0 | $kernel->yield(init_rrq => $args, $opcode, $data); | |||||||||||||||||||
| 318 | 0 | return; | |||||||||||||||||||
| 319 | } | ||||||||||||||||||||
| 320 | elsif($opcode eq &TFTP_OPCODE_WRQ) { | ||||||||||||||||||||
| 321 | 0 | $kernel->yield(init_wrq => $args, $opcode, $data); | |||||||||||||||||||
| 322 | 0 | return; | |||||||||||||||||||
| 323 | } | ||||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | 0 | ▶ | if(my $client = $self->clients->{$client_id}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 326 | 0 | ▶ | if($opcode == &TFTP_OPCODE_ACK) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 327 | 0 | $kernel->yield(get_ack => $client, $data); | |||||||||||||||||||
| 328 | } | ||||||||||||||||||||
| 329 | elsif($opcode == &TFTP_OPCODE_DATA) { | ||||||||||||||||||||
| 330 | 0 | $kernel->yield(get_data => $client, $data); | |||||||||||||||||||
| 331 | } | ||||||||||||||||||||
| 332 | elsif($opcode eq &TFTP_OPCODE_ERROR) { | ||||||||||||||||||||
| 333 | 0 | $self->log(error => $client, $data); | |||||||||||||||||||
| 334 | } | ||||||||||||||||||||
| 335 | else { | ||||||||||||||||||||
| 336 | 0 | $kernel->yield(send_error => $client, 'unknown_opcode', [$opcode]); | |||||||||||||||||||
| 337 | } | ||||||||||||||||||||
| 338 | } | ||||||||||||||||||||
| 339 | else { | ||||||||||||||||||||
| 340 | 0 | $client = POE::Component::TFTPd::Client->new($self, $args); | |||||||||||||||||||
| 341 | 0 | $self->log(error => $client, 'no connection'); | |||||||||||||||||||
| 342 | 0 | $kernel->yield(send_error => $client, 'no_connection'); | |||||||||||||||||||
| 343 | } | ||||||||||||||||||||
| 344 | } | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | =head2 send_data => $client, $data | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | Sends data to the client. Used for both ACK and DATA. It resends data | ||||||||||||||||||||
| 349 | automatically on failure, and decreases C<$client-E<gt>retries>. | ||||||||||||||||||||
| 350 | |||||||||||||||||||||
| 351 | =cut | ||||||||||||||||||||
| 352 | |||||||||||||||||||||
| 353 | 0 | ▶ | sub send_data { | ||||||||||||||||||
|
Subroutine
send_data: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 354 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 355 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 356 | 0 | my $client = $_[ARG0]; | |||||||||||||||||||
| 357 | 0 | my($opname) = $_[STATE] =~ /send_(\w+)/; # data/ack | |||||||||||||||||||
| 358 | 0 | my($opcode, $data, $n, $done); | |||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | 0 | ▶ | if($opname eq 'data') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 361 | 0 | $opcode = &TFTP_OPCODE_DATA; | |||||||||||||||||||
| 362 | 0 | $data = $_[ARG1]; | |||||||||||||||||||
| 363 | 0 | $n = $client->last_block + 1; | |||||||||||||||||||
| 364 | 0 | $client->almost_done = length $data < $client->block_size; | |||||||||||||||||||
| 365 | } | ||||||||||||||||||||
| 366 | elsif($opname eq 'ack') { | ||||||||||||||||||||
| 367 | 0 | $opcode = &TFTP_OPCODE_ACK; | |||||||||||||||||||
| 368 | 0 | $data = q(); | |||||||||||||||||||
| 369 | 0 | $client->retries = $self->retries; | |||||||||||||||||||
| 370 | 0 | $n = $client->last_block; | |||||||||||||||||||
| 371 | 0 | $done = $client->almost_done; | |||||||||||||||||||
| 372 | } | ||||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | 0 | my $bytes = $self->server->put({ | |||||||||||||||||||
| 375 | addr => $client->address, | ||||||||||||||||||||
| 376 | port => $client->port, | ||||||||||||||||||||
| 377 | payload => [pack("nna*", $opcode, $n, $data)], | ||||||||||||||||||||
| 378 | }); | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | 0 | ▶ | if($bytes) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 381 | 0 | $self->log(trace => $client, "sent $opname $n"); | |||||||||||||||||||
| 382 | 0 | $client->timestamp = time; | |||||||||||||||||||
| 383 | 0 | ▶ | $self->cleanup($client) if($done); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 384 | } | ||||||||||||||||||||
| 385 | elsif($client->retries) { | ||||||||||||||||||||
| 386 | 0 | $client->retries--; | |||||||||||||||||||
| 387 | 0 | $self->log(warning => $client, "failed to transmit $opname $n"); | |||||||||||||||||||
| 388 | 0 | $kernel->yield("send_$opname" => $client); | |||||||||||||||||||
| 389 | } | ||||||||||||||||||||
| 390 | } | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | =head2 get_data => $client, $data | ||||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | Handles both ACK and DATA packets. | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | If correct packet-number: | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | 1. Logs the packet number | ||||||||||||||||||||
| 399 | 2. Calls C<tftpd_receive> / C<tftpd_send> in sender session | ||||||||||||||||||||
| 400 | |||||||||||||||||||||
| 401 | On failure: | ||||||||||||||||||||
| 402 | |||||||||||||||||||||
| 403 | 1. Logs failure | ||||||||||||||||||||
| 404 | 2. Resends the last packet | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | 0 | ▶ | sub get_data { | ||||||||||||||||||
|
Subroutine
get_data: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 409 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 410 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 411 | 0 | my $client = $_[ARG0]; | |||||||||||||||||||
| 412 | 0 | my($opname) = $_[STATE] =~ /get_(\w+)/; # data/ack | |||||||||||||||||||
| 413 | 0 | my($n, $data) = unpack("na*", $_[ARG1]); | |||||||||||||||||||
| 414 | 0 | my($this_state, $sender_state, $done); | |||||||||||||||||||
| 415 | |||||||||||||||||||||
| 416 | 0 | ▶ | if($opname eq 'data') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 417 | 0 | $sender_state = 'tftpd_receive'; | |||||||||||||||||||
| 418 | 0 | $this_state = 'send_ack'; | |||||||||||||||||||
| 419 | 0 | $client->almost_done = length $data < $client->block_size; | |||||||||||||||||||
| 420 | } | ||||||||||||||||||||
| 421 | elsif($opname eq 'ack') { | ||||||||||||||||||||
| 422 | 0 | $sender_state = 'tftpd_send'; | |||||||||||||||||||
| 423 | 0 | $this_state = 'send_data'; | |||||||||||||||||||
| 424 | 0 | $done = $client->almost_done; | |||||||||||||||||||
| 425 | } | ||||||||||||||||||||
| 426 | |||||||||||||||||||||
| 427 | 0 | ▶ | if($n == $client->last_block + 1) { # get data | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 428 | 0 | $client->last_block ++; | |||||||||||||||||||
| 429 | 0 | $self->log(trace => $client, "got $opname $n"); | |||||||||||||||||||
| 430 | 0 | $kernel->post($self->sender => $sender_state => $client, $data); | |||||||||||||||||||
| 431 | 0 | $client->resent_block = 0; | |||||||||||||||||||
| 432 | 0 | ▶ | $self->cleanup($client) if($done); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 433 | } | ||||||||||||||||||||
| 434 | else { # wrong block number | ||||||||||||||||||||
| 435 | # Check if we've already sent block and received an ack | ||||||||||||||||||||
| 436 | # this prevents "Sorcerers Apprentice Syndrome" | ||||||||||||||||||||
| 437 | 0 | ▶ | if (!$client->resent_block) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 438 | 0 | $self->log(trace => $client, sprintf( | |||||||||||||||||||
| 439 | "wrong %s %i (%i)", $opname, $n, $client->last_block + 1, | ||||||||||||||||||||
| 440 | )); | ||||||||||||||||||||
| 441 | 0 | $client->resent_block = 1; | |||||||||||||||||||
| 442 | 0 | $kernel->post($self->sender => $sender_state => $client, $data); | |||||||||||||||||||
| 443 | } | ||||||||||||||||||||
| 444 | else { | ||||||||||||||||||||
| 445 | 0 | $self->log(trace => $client, sprintf("Duplicate ack (%i, %i) - not responding", $n, $client->last_block + 1)); | |||||||||||||||||||
| 446 | |||||||||||||||||||||
| 447 | # We dont want to talk to the client for this block again so return | ||||||||||||||||||||
| 448 | #$kernel->yield($this_state => $client); | ||||||||||||||||||||
| 449 | 0 | return; | |||||||||||||||||||
| 450 | } | ||||||||||||||||||||
| 451 | } | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | 0 | return; | |||||||||||||||||||
| 454 | } | ||||||||||||||||||||
| 455 | |||||||||||||||||||||
| 456 | =head2 init_request => $args, $opcode, $data | ||||||||||||||||||||
| 457 | |||||||||||||||||||||
| 458 | 1. Checks if max_clients limit is reached. If not, sets up | ||||||||||||||||||||
| 459 | |||||||||||||||||||||
| 460 | $client->filename = $file; # the filename to read/write | ||||||||||||||||||||
| 461 | $client->mode = uc $mode; # only OCTET is valid | ||||||||||||||||||||
| 462 | $client->rfc = [ ... ]; | ||||||||||||||||||||
| 463 | $client->timestamp = time; | ||||||||||||||||||||
| 464 | |||||||||||||||||||||
| 465 | 2. Calls C<tftpd_init> in sender session. | ||||||||||||||||||||
| 466 | |||||||||||||||||||||
| 467 | 3. Calls C<tftpd_send> in sender session, if read-request from client | ||||||||||||||||||||
| 468 | |||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | 0 | ▶ | sub init_request { | ||||||||||||||||||
|
Subroutine
init_request: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 472 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 473 | 0 | my $kernel = $_[KERNEL]; | |||||||||||||||||||
| 474 | 0 | my $args = $_[ARG0]; | |||||||||||||||||||
| 475 | 0 | my $opcode = $_[ARG1]; | |||||||||||||||||||
| 476 | 0 | my $datagram = $_[ARG2]; | |||||||||||||||||||
| 477 | 0 | my($opname) = $_[STATE] =~ /init_(\w+)/; | |||||||||||||||||||
| 478 | 0 | my $client_id = join ":", $args->{'addr'}, $args->{'port'}; | |||||||||||||||||||
| 479 | 0 | my($client, $file, $mode, @rfc); | |||||||||||||||||||
| 480 | |||||||||||||||||||||
| 481 | 0 | ▶ | if(my $n = $self->max_clients) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 482 | 0 | ▶ | if(int keys %{ $self->clients } > $n) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 483 | 0 | $self->log(error => $client, 'too many connections'); | |||||||||||||||||||
| 484 | 0 | return; | |||||||||||||||||||
| 485 | } | ||||||||||||||||||||
| 486 | } | ||||||||||||||||||||
| 487 | |||||||||||||||||||||
| 488 | 0 | $client = POE::Component::TFTPd::Client->new($self, $args); | |||||||||||||||||||
| 489 | 0 | $self->clients->{$client_id} = $client; | |||||||||||||||||||
| 490 | |||||||||||||||||||||
| 491 | 0 | ($file, $mode, @rfc) = split("\0", $datagram); | |||||||||||||||||||
| 492 | 0 | $client->filename = $file; | |||||||||||||||||||
| 493 | 0 | $client->mode = uc $mode; | |||||||||||||||||||
| 494 | 0 | $client->rfc = \@rfc; | |||||||||||||||||||
| 495 | 0 | $client->timestamp = time; | |||||||||||||||||||
| 496 | |||||||||||||||||||||
| 497 | 0 | ▶ | if($client->mode ne 'OCTET') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 498 | 0 | $self->log(error => $client, 'mode not supported'); | |||||||||||||||||||
| 499 | 0 | $kernel->yield(send_error => $client, 'illegal_operation'); | |||||||||||||||||||
| 500 | 0 | return; | |||||||||||||||||||
| 501 | } | ||||||||||||||||||||
| 502 | |||||||||||||||||||||
| 503 | 0 | $self->log(info => $client, "$opname $file"); | |||||||||||||||||||
| 504 | |||||||||||||||||||||
| 505 | 0 | $kernel->post($self->sender => tftpd_init => $client); | |||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | 0 | ▶ | if($opcode == &TFTP_OPCODE_RRQ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 508 | 0 | $kernel->post($self->sender => tftpd_send => $client); | |||||||||||||||||||
| 509 | 0 | $client->rrq = 1; | |||||||||||||||||||
| 510 | } | ||||||||||||||||||||
| 511 | else { | ||||||||||||||||||||
| 512 | 0 | $kernel->yield(send_ack => $client); | |||||||||||||||||||
| 513 | 0 | $client->wrq = 1; | |||||||||||||||||||
| 514 | } | ||||||||||||||||||||
| 515 | } | ||||||||||||||||||||
| 516 | |||||||||||||||||||||
| 517 | =head2 send_error => $client, $error_key [, $args] | ||||||||||||||||||||
| 518 | |||||||||||||||||||||
| 519 | Sends an error to the client. | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | $error_key referes to C<%TFTP_ERROR> | ||||||||||||||||||||
| 522 | $args is an array ref that can be used to replace %x in the error string | ||||||||||||||||||||
| 523 | |||||||||||||||||||||
| 524 | =cut | ||||||||||||||||||||
| 525 | |||||||||||||||||||||
| 526 | 0 | ▶ | sub send_error { | ||||||||||||||||||
|
Subroutine
send_error: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 527 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||
| 528 | 0 | my $client = $_[ARG0]; | |||||||||||||||||||
| 529 | 0 | my $key = $_[ARG1]; | |||||||||||||||||||
| 530 | 0 | ▶ | my $args = $_[ARG2] || []; | ||||||||||||||||||
|
Condition0%0/2
$_[12] || []
MC/DC0%0/1
$_[12] || []
$_[12]
Truth table0%0/2
A
A= $_[12]
| |||||||||||||||||||||
| 531 | 0 | ▶ | my $error = $TFTP_ERROR{$key} || $TFTP_ERROR{'not_defined'}; | ||||||||||||||||||
|
Condition0%0/3
$TFTP_ERROR{$key} || $TFTP_ERROR{'not_defined'}
MC/DC0%0/2
$TFTP_ERROR{$key} || $TFTP_ERROR{'not_defined'}
$TFTP_ERROR{$key}
$TFTP_ERROR{'not_defined'}
Truth table0%0/3
A || B
A= $TFTP_ERROR{$key}B= $TFTP_ERROR{'not_defined'}
| |||||||||||||||||||||
| 532 | |||||||||||||||||||||
| 533 | 0 | $self->log(error => $client, sprintf($error->[1], @$args)); | |||||||||||||||||||
| 534 | 0 | delete $self->clients->{$client->id}; | |||||||||||||||||||
| 535 | |||||||||||||||||||||
| 536 | 0 | $self->server->put({ | |||||||||||||||||||
| 537 | addr => $client->address, | ||||||||||||||||||||
| 538 | port => $client->port, | ||||||||||||||||||||
| 539 | payload => [pack("nnZ*", &TFTP_OPCODE_ERROR, @$error)], | ||||||||||||||||||||
| 540 | }); | ||||||||||||||||||||
| 541 | |||||||||||||||||||||
| 542 | 0 | return; | |||||||||||||||||||
| 543 | } | ||||||||||||||||||||
| 544 | |||||||||||||||||||||
| 545 | =head1 FUNCTIONS | ||||||||||||||||||||
| 546 | |||||||||||||||||||||
| 547 | =head2 TFTP_MIN_BLKSIZE | ||||||||||||||||||||
| 548 | |||||||||||||||||||||
| 549 | =head2 TFTP_MAX_BLKSIZE | ||||||||||||||||||||
| 550 | |||||||||||||||||||||
| 551 | =head2 TFTP_MIN_TIMEOUT | ||||||||||||||||||||
| 552 | |||||||||||||||||||||
| 553 | =head2 TFTP_MAX_TIMEOUT | ||||||||||||||||||||
| 554 | |||||||||||||||||||||
| 555 | =head2 TFTP_DEFAULT_PORT | ||||||||||||||||||||
| 556 | |||||||||||||||||||||
| 557 | =head2 TFTP_OPCODE_RRQ | ||||||||||||||||||||
| 558 | |||||||||||||||||||||
| 559 | =head2 TFTP_OPCODE_WRQ | ||||||||||||||||||||
| 560 | |||||||||||||||||||||
| 561 | =head2 TFTP_OPCODE_DATA | ||||||||||||||||||||
| 562 | |||||||||||||||||||||
| 563 | =head2 TFTP_OPCODE_ACK | ||||||||||||||||||||
| 564 | |||||||||||||||||||||
| 565 | =head2 TFTP_OPCODE_ERROR | ||||||||||||||||||||
| 566 | |||||||||||||||||||||
| 567 | =head2 TFTP_OPCODE_OACK | ||||||||||||||||||||
| 568 | |||||||||||||||||||||
| 569 | =cut | ||||||||||||||||||||
| 570 | |||||||||||||||||||||
| 571 | 0 | ▶ | sub TFTP_MIN_BLKSIZE { return 512; } | ||||||||||||||||||
|
Subroutine
TFTP_MIN_BLKSIZE: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 572 | 0 | ▶ | sub TFTP_MAX_BLKSIZE { return 1428; } | ||||||||||||||||||
|
Subroutine
TFTP_MAX_BLKSIZE: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 573 | 0 | ▶ | sub TFTP_MIN_TIMEOUT { return 1; } | ||||||||||||||||||
|
Subroutine
TFTP_MIN_TIMEOUT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 574 | 0 | ▶ | sub TFTP_MAX_TIMEOUT { return 60; } | ||||||||||||||||||
|
Subroutine
TFTP_MAX_TIMEOUT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 575 | 0 | ▶ | sub TFTP_DEFAULT_PORT { return 69; } | ||||||||||||||||||
|
Subroutine
TFTP_DEFAULT_PORT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 576 | 0 | ▶ | sub TFTP_OPCODE_RRQ { return 1; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_RRQ: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 577 | 0 | ▶ | sub TFTP_OPCODE_WRQ { return 2; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_WRQ: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 578 | 0 | ▶ | sub TFTP_OPCODE_DATA { return 3; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_DATA: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 579 | 0 | ▶ | sub TFTP_OPCODE_ACK { return 4; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_ACK: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 580 | 0 | ▶ | sub TFTP_OPCODE_ERROR { return 5; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_ERROR: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 581 | 0 | ▶ | sub TFTP_OPCODE_OACK { return 6; } | ||||||||||||||||||
|
Subroutine
TFTP_OPCODE_OACK: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 582 | |||||||||||||||||||||
| 583 | =head1 AUTHOR | ||||||||||||||||||||
| 584 | |||||||||||||||||||||
| 585 | Jan Henning Thorsen, C<< <jhthorsen-at-cpan-org> >> | ||||||||||||||||||||
| 586 | |||||||||||||||||||||
| 587 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 588 | |||||||||||||||||||||
| 589 | Copyright 2007 Jan Henning Thorsen, all rights reserved. | ||||||||||||||||||||
| 590 | |||||||||||||||||||||
| 591 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 592 | under the same terms as Perl itself. | ||||||||||||||||||||
| 593 | |||||||||||||||||||||
| 594 | =cut | ||||||||||||||||||||
| 595 | |||||||||||||||||||||
| 596 | 3 | 1; | |||||||||||||||||||
| 597 | 3 | 1; | |||||||||||||||||||