| 1 | package Mojo::TFTPd::Connection; | ||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Socket(); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use constant DEBUG => !!$ENV{MOJO_TFTPD_DEBUG}; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use constant OPCODE_DATA => 3; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use constant OPCODE_ACK => 4; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use constant OPCODE_ERROR => 5; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use constant OPCODE_OACK => 6; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use constant ROLLOVER => 256 * 256; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 6 | our %ERROR_CODES = ( | |||||||||||||||||||||||||||||||||||
| 15 | not_defined => [0, 'Not defined, see error message'], | ||||||||||||||||||||||||||||||||||||
| 16 | unknown_opcode => [0, 'Unknown opcode: %s'], | ||||||||||||||||||||||||||||||||||||
| 17 | no_connection => [0, 'No connection'], | ||||||||||||||||||||||||||||||||||||
| 18 | file_not_found => [1, 'File not found'], | ||||||||||||||||||||||||||||||||||||
| 19 | access_violation => [2, 'Access violation'], | ||||||||||||||||||||||||||||||||||||
| 20 | disk_full => [3, 'Disk full or allocation exceeded'], | ||||||||||||||||||||||||||||||||||||
| 21 | illegal_operation => [4, 'Illegal TFTP operation'], | ||||||||||||||||||||||||||||||||||||
| 22 | unknown_transfer_id => [5, 'Unknown transfer ID'], | ||||||||||||||||||||||||||||||||||||
| 23 | file_exists => [6, 'File already exists'], | ||||||||||||||||||||||||||||||||||||
| 24 | no_such_user => [7, 'No such user'], | ||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 28 | # do not use MSG_DONTWAIT on platforms that do not support it (Win32) | ||||||||||||||||||||||||||||||||||||
| 29 | 6 | my $msg_dontwait = 0; | |||||||||||||||||||||||||||||||||||
| 30 | 6 | eval { $msg_dontwait = Socket::MSG_DONTWAIT }; | |||||||||||||||||||||||||||||||||||
| 31 | 131110 | ▶ | sub MSG_DONTWAIT() {$msg_dontwait} | ||||||||||||||||||||||||||||||||||
|
Subroutine
MSG_DONTWAIT: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 6 | has blocksize => 512; | |||||||||||||||||||||||||||||||||||
| 35 | 6 | has error => ''; | |||||||||||||||||||||||||||||||||||
| 36 | 6 | has file => '/dev/null'; | |||||||||||||||||||||||||||||||||||
| 37 | 6 | has filehandle => undef; | |||||||||||||||||||||||||||||||||||
| 38 | 6 | has filesize => undef; | |||||||||||||||||||||||||||||||||||
| 39 | 6 | has lastop => undef; | |||||||||||||||||||||||||||||||||||
| 40 | 6 | has mode => ''; | |||||||||||||||||||||||||||||||||||
| 41 | 6 | has peerhost => ''; | |||||||||||||||||||||||||||||||||||
| 42 | 6 | has peername => ''; | |||||||||||||||||||||||||||||||||||
| 43 | 6 | has retransmit => 0; | |||||||||||||||||||||||||||||||||||
| 44 | 6 | has retries => 2; | |||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | has rfc => sub { +{} }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 46 | 6 | has socket => undef; | |||||||||||||||||||||||||||||||||||
| 47 | 6 | has timeout => undef; | |||||||||||||||||||||||||||||||||||
| 48 | 6 | has type => undef; | |||||||||||||||||||||||||||||||||||
| 49 | 6 | has _attempt => 0; | |||||||||||||||||||||||||||||||||||
| 50 | 6 | has _sequence_number => 1; | |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | 65553 | ▶ | sub receive_ack { | ||||||||||||||||||||||||||||||||||
|
Subroutine
receive_ack: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 53 | 65553 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 54 | 65553 | my ($n) = unpack 'n', shift; | |||||||||||||||||||||||||||||||||||
| 55 | 65553 | my $seq = $self->_sequence_number % ROLLOVER; | |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||
| 57 | 65553 | DEBUG && warn "[Mojo::TFTPd] <<< %s ack %s %s\n", $self->peerhost, $n, | |||||||||||||||||||||||||||||||||||
| 58 | ($n && $n != $seq ? "expected $seq" : ''); | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 65553 | ▶ | return $self->send_data if $n == 0 and $self->lastop eq OPCODE_OACK; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$n == 0 and $self->lastop eq 6
MC/DC100%2/2
$n == 0 and $self->lastop eq 6
$n == 0
$self->lastop eq 6
Truth table100%3/3
A and B
A= $n == 0B= $self->lastop eq 6
| |||||||||||||||||||||||||||||||||||||
| 61 | 65551 | ▶ | return 0 if $self->lastop eq OPCODE_ERROR; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 62 | 65551 | ▶ | return 0 if $self->{last_sequence_number} and $n == $self->{last_sequence_number} % ROLLOVER; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"last_sequence_number"} and $n == $self->{"last_sequence_number"} % 65536
MC/DC50%1/2
$self->{"last_sequence_number"} and $n == $self->{"last_sequence_number"} % 65536
$self->{"last_sequence_number"}
$n == $self->{"last_sequence_number"} % 65536
Truth table66%2/3
A and B
A= $self->{"last_sequence_number"}B= $n == $self->{"last_sequence_number"} % 65536
| |||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | 65549 | ▶ | if ($n == $seq) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | 65542 | $self->{_attempt} = 0; | |||||||||||||||||||||||||||||||||||
| 66 | 65542 | $self->{_sequence_number}++; | |||||||||||||||||||||||||||||||||||
| 67 | 65542 | return $self->send_data; | |||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | return 1 if $self->retransmit and $n < $seq; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->retransmit and $n < $seq
MC/DC100%2/2
$self->retransmit and $n < $seq
$self->retransmit
$n < $seq
Truth table100%3/3
A and B
A= $self->retransmitB= $n < $seq
| |||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | return $self->send_data if $self->{retries}--; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | 2 | $self->error('Invalid packet number'); | |||||||||||||||||||||||||||||||||||
| 73 | 2 | return 0; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 65546 | ▶ | sub receive_data { | ||||||||||||||||||||||||||||||||||
|
Subroutine
receive_data: called CC=12 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 77 | 65546 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 78 | 65546 | my ($n, $data) = unpack 'na*', shift; | |||||||||||||||||||||||||||||||||||
| 79 | 65546 | my $seq = $self->_sequence_number % ROLLOVER; | |||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | 65546 | DEBUG && warn "[Mojo::TFTPd] <<< %s data %s (%s) %s\n", $self->peerhost, $n, length $data, | |||||||||||||||||||||||||||||||||||
| 82 | ($n != $seq ? " expected $seq" : ''); | ||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | 65546 | ▶ | unless ($n == $seq) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return 1 if $self->retransmit and $n < $seq; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->retransmit and $n < $seq
MC/DC0%0/2
$self->retransmit and $n < $seq
$self->retransmit
$n < $seq
Truth table0%0/3
A and B
A= $self->retransmitB= $n < $seq
| |||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return $self->send_ack if $self->{retries}--; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->error('Invalid packet number'); | |||||||||||||||||||||||||||||||||||
| 88 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 65546 | my $handle = $self->filehandle; | |||||||||||||||||||||||||||||||||||
| 92 | 65546 | ▶ | if (blessed $handle and $handle->isa('Mojo::Asset')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($handle) and $handle->isa("Mojo::Asset")
MC/DC50%1/2
builtin::blessed($handle) and $handle->isa("Mojo::Asset")
builtin::blessed($handle)
$handle->isa("Mojo::Asset")
Truth table66%2/3
A and B
A= builtin::blessed($handle)B= $handle->isa("Mojo::Asset")
| |||||||||||||||||||||||||||||||||||||
| 93 | 65541 | local $!; | |||||||||||||||||||||||||||||||||||
| 94 | 65541 | eval { $handle->add_chunk($data) }; | |||||||||||||||||||||||||||||||||||
| 95 | 65541 | ▶ | return $self->send_error(illegal_operation => "Unable to add chunk $!") if $!; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||
| 97 | elsif (!$handle->syswrite($data)) { | ||||||||||||||||||||||||||||||||||||
| 98 | 0 | return $self->send_error(illegal_operation => "Write: $!"); | |||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | 65546 | ▶ | unless (length $data == $self->blocksize) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | 2 | $self->{last_sequence_number} = $n; | |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | 65546 | ▶ | return $self->send_error(disk_full => 'tsize exceeded') | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->filesize and $self->filesize < $self->blocksize * ($n - 1) + length($data)
MC/DC100%2/2
$self->filesize and $self->filesize < $self->blocksize * ($n - 1) + length($data)
$self->filesize
$self->filesize < $self->blocksize * ($n - 1) + length($data)
Truth table100%3/3
A and B
A= $self->filesizeB= $self->filesize < $self->blocksize * ($n - 1) + length($data)
| |||||||||||||||||||||||||||||||||||||
| 106 | if $self->filesize and $self->filesize < $self->blocksize * ($n - 1) + length $data; | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | 65545 | $self->{_sequence_number}++; | |||||||||||||||||||||||||||||||||||
| 109 | 65545 | return $self->send_ack; | |||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub receive_error { | ||||||||||||||||||||||||||||||||||
|
Subroutine
receive_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 114 | 0 | my ($code, $msg) = unpack 'nZ*', shift; | |||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | 0 | warn "[Mojo::TFTPd] <<< $self->{peerhost} error $code $msg\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->error("($code) $msg"); | |||||||||||||||||||||||||||||||||||
| 118 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | 65547 | ▶ | sub send_ack { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_ack: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 122 | 65547 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 123 | 65547 | $self->{lastop} = OPCODE_ACK; | |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | 65547 | my $seq = ($self->_sequence_number - 1) % ROLLOVER; | |||||||||||||||||||||||||||||||||||
| 126 | 65547 | DEBUG && warn "[Mojo::TFTPd] <<< %s ack %s %s\n", $self->peerhost, $seq, | |||||||||||||||||||||||||||||||||||
| 127 | ($self->_attempt ? " retransmit $self->{_attempt}" : ''); | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | 65547 | my $sent = $self->socket->send(pack('nn', OPCODE_ACK, $seq), MSG_DONTWAIT, $self->peername); | |||||||||||||||||||||||||||||||||||
| 130 | 65547 | ▶ | return 0 if defined $self->{last_sequence_number}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 131 | 65545 | ▶ | return 1 if $sent or $self->{retries}--; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sent or $self->{"retries"}--
MC/DC0%0/2
$sent or $self->{"retries"}--
$sent
$self->{"retries"}--
Truth table33%1/3
A or B
A= $sentB= $self->{"retries"}--
| |||||||||||||||||||||||||||||||||||||
| 132 | 0 | $self->error("Send: $!"); | |||||||||||||||||||||||||||||||||||
| 133 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | 65552 | ▶ | sub send_data { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_data: called CC=10 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 137 | 65552 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 138 | 65552 | $self->{lastop} = OPCODE_DATA; | |||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | 65552 | my ($handle, $n, $data) = ($self->filehandle, $self->_sequence_number); | |||||||||||||||||||||||||||||||||||
| 141 | 65552 | ▶ | if (blessed $handle and $handle->isa('Mojo::Asset')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
builtin::blessed($handle) and $handle->isa("Mojo::Asset")
MC/DC50%1/2
builtin::blessed($handle) and $handle->isa("Mojo::Asset")
builtin::blessed($handle)
$handle->isa("Mojo::Asset")
Truth table66%2/3
A and B
A= builtin::blessed($handle)B= $handle->isa("Mojo::Asset")
| |||||||||||||||||||||||||||||||||||||
| 142 | 65545 | $data = $handle->get_chunk(($n - 1) * $self->blocksize, $self->blocksize); | |||||||||||||||||||||||||||||||||||
| 143 | 65545 | ▶ | return $self->send_error(file_not_found => 'Unable to read chunk') unless defined $data; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | elsif (not seek $handle, ($n - 1) * $self->blocksize, 0) { | ||||||||||||||||||||||||||||||||||||
| 146 | 0 | return $self->send_error(file_not_found => "Seek: $!"); | |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | elsif (not defined $handle->sysread($data, $self->blocksize)) { | ||||||||||||||||||||||||||||||||||||
| 149 | 0 | return $self->send_error(file_not_found => "Read: $!"); | |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | 65552 | ▶ | if (length $data < $self->blocksize) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 3 | $self->{last_sequence_number} = $n; | |||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | 65552 | my $seq = $n % ROLLOVER; | |||||||||||||||||||||||||||||||||||
| 157 | 65552 | DEBUG && warn sprintf "[Mojo::TFTPd] >>> %s data %s (%s) %s\n", $self->{peerhost}, $seq, | |||||||||||||||||||||||||||||||||||
| 158 | length $data, $self->_attempt ? "retransmit $self->{_attempt}" : ''; | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | 65552 | my $sent | |||||||||||||||||||||||||||||||||||
| 161 | = $self->socket->send(pack('nna*', OPCODE_DATA, $seq, $data), MSG_DONTWAIT, $self->peername); | ||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | 65552 | ▶ | return 0 unless length $data; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 164 | 65551 | ▶ | return 1 if $sent or $self->{retries}--; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sent or $self->{"retries"}--
MC/DC0%0/2
$sent or $self->{"retries"}--
$sent
$self->{"retries"}--
Truth table33%1/3
A or B
A= $sentB= $self->{"retries"}--
| |||||||||||||||||||||||||||||||||||||
| 165 | 0 | $self->error("Send: $!"); | |||||||||||||||||||||||||||||||||||
| 166 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | sub send_error { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_error: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 170 | 3 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 171 | 3 | ▶ | my $err = $ERROR_CODES{$name} || $ERROR_CODES{not_defined}; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Mojo::TFTPd::Connection::ERROR_CODES{$name} || $Mojo::TFTPd::Connection::ERROR_CODES{'not_defined'}
MC/DC0%0/2
$Mojo::TFTPd::Connection::ERROR_CODES{$name} || $Mojo::TFTPd::Connection::ERROR_CODES{'not_defined'}
$Mojo::TFTPd::Connection::ERROR_CODES{$name}
$Mojo::TFTPd::Connection::ERROR_CODES{'not_defined'}
Truth table33%1/3
A || B
A= $Mojo::TFTPd::Connection::ERROR_CODES{$name}B= $Mojo::TFTPd::Connection::ERROR_CODES{'not_defined'}
| |||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | 3 | $self->{lastop} = OPCODE_ERROR; | |||||||||||||||||||||||||||||||||||
| 174 | 3 | warn "[Mojo::TFTPd] >>> $self->{peerhost} error @$err\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||
| 176 | 3 | $self->error($_[2]); | |||||||||||||||||||||||||||||||||||
| 177 | 3 | $self->socket->send(pack('nnZ*', OPCODE_ERROR, @$err), MSG_DONTWAIT, $self->peername); | |||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | 3 | return 0; | |||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||
| 182 | 8 | ▶ | sub send_oack { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_oack: called CC=7 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 183 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 184 | 8 | $self->{lastop} = OPCODE_OACK; | |||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | 8 | my @options; | |||||||||||||||||||||||||||||||||||
| 187 | 8 | ▶ | push @options, 'blksize', $self->blocksize if $self->rfc->{blksize}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 188 | 8 | ▶ | push @options, 'timeout', $self->timeout if $self->rfc->{timeout}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 189 | 8 | ▶ | push @options, 'tsize', $self->filesize if exists $self->rfc->{tsize} and $self->filesize; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->rfc->{"tsize"} and $self->filesize
MC/DC50%1/2
exists $self->rfc->{"tsize"} and $self->filesize
exists $self->rfc->{"tsize"}
$self->filesize
Truth table66%2/3
A and B
A= exists $self->rfc->{"tsize"}B= $self->filesize
| |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 8 | warn "[Mojo::TFTPd] >>> $self->{peerhost} oack @options" | |||||||||||||||||||||||||||||||||||
| 192 | . ($self->_attempt ? " retransmit $self->{_attempt}" : '') . "\n" | ||||||||||||||||||||||||||||||||||||
| 193 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | 8 | my $sent = $self->socket->send(pack('na*', OPCODE_OACK, join "\0", @options, ''), | |||||||||||||||||||||||||||||||||||
| 196 | MSG_DONTWAIT, $self->peername); | ||||||||||||||||||||||||||||||||||||
| 197 | 8 | ▶ | return 1 if $sent or $self->{retries}--; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sent or $self->{"retries"}--
MC/DC0%0/2
$sent or $self->{"retries"}--
$sent
$self->{"retries"}--
Truth table33%1/3
A or B
A= $sentB= $self->{"retries"}--
| |||||||||||||||||||||||||||||||||||||
| 198 | 0 | $self->error("Send: $!"); | |||||||||||||||||||||||||||||||||||
| 199 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub send_retransmit { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_retransmit: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | return 0 unless $self->lastop; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | unless ($self->retransmit) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 207 | 0 | $self->error('Inactive timeout'); | |||||||||||||||||||||||||||||||||||
| 208 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | # Errors are not retransmitted | ||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | return 0 if $self->lastop == OPCODE_ERROR; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | if ($self->_attempt >= $self->retransmit) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 215 | 0 | $self->error('Retransmit timeout'); | |||||||||||||||||||||||||||||||||||
| 216 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||
| 219 | 0 | $self->{_attempt}++; | |||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | return $self->send_oack if $self->lastop eq OPCODE_OACK; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | return $self->send_ack if $self->lastop eq OPCODE_ACK; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | return $self->send_data if $self->lastop eq OPCODE_DATA; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 224 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||
| 227 | 6 | 1; | |||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||
| 229 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||
| 231 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||
| 233 | Mojo::TFTPd::Connection - A connection class for Mojo::TFTPd | ||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||
| 235 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||
| 237 | See L<Mojo::TFTPd> | ||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||
| 239 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | =head2 type | ||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||
| 243 | $str = $connection->type; | ||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||
| 245 | Type of connection rrq or wrq | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | =head2 blocksize | ||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | $int = $connection->blocksize; | ||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||
| 251 | The negotiated blocksize. Default is 512 Byte. | ||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||
| 253 | =head2 error | ||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | $str = $connection->error; | ||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | Useful to check inside L<Mojo::TFTPd/finish> events to see if anything has | ||||||||||||||||||||||||||||||||||||
| 258 | gone wrong. Holds a string describing the error. | ||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||
| 260 | =head2 file | ||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | $str = $connection->file; | ||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||
| 264 | The filename the client requested to read or write. | ||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | =head2 filehandle | ||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||
| 268 | $fh = $connection->filehandle; | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | This must be set inside the L<rrq|Mojo::TFTPd/rrq> or L<wrq|Mojo::TFTPd/wrq> | ||||||||||||||||||||||||||||||||||||
| 271 | event or the connection will be dropped. | ||||||||||||||||||||||||||||||||||||
| 272 | Can be either L<Mojo::Asset> or filehandle. | ||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||
| 274 | =head2 filesize | ||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||
| 276 | $int = $connection->filesize; | ||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||
| 278 | This must be set inside the L<rrq|Mojo::TFTPd/rrq> | ||||||||||||||||||||||||||||||||||||
| 279 | to report "tsize" option if client requested. | ||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||
| 281 | If set inside L<wrq|Mojo::TFTPd/wrq> limits maximum upload size. | ||||||||||||||||||||||||||||||||||||
| 282 | Set automatically on WRQ with "tsize" option. | ||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||
| 284 | Can be used inside L<finish|Mojo::TFTPd/finish> for uploads | ||||||||||||||||||||||||||||||||||||
| 285 | to check if reported "tsize" and received data length match. | ||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||
| 287 | =head2 timeout | ||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | $num = $connection->timeout; | ||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||
| 291 | Retransmit/Inactive timeout. | ||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||
| 293 | =head2 lastop | ||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||
| 295 | $str = $connection->lastop; | ||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | Last operation. | ||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||
| 299 | =head2 mode | ||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||
| 301 | $str = $connection->mode; | ||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||
| 303 | Either "netascii", "octet" or empty string if unknown. | ||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||
| 305 | =head2 peerhost | ||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||
| 307 | $str = $connection->peerhost; | ||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||
| 309 | The IP address of the remote client. | ||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||
| 311 | =head2 peername | ||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||
| 313 | $bin = $connection->peername; | ||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||
| 315 | Packet address of the remote client. | ||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||
| 317 | =head2 retries | ||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||
| 319 | $int = $connection->retries; | ||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | Number of times L</send_data>, L</send_ack> or L</send_oack> can be retried before the | ||||||||||||||||||||||||||||||||||||
| 322 | connection is dropped. | ||||||||||||||||||||||||||||||||||||
| 323 | This value comes from L<Mojo::TFTPd/retries> or set inside L<rrq|Mojo::TFTPd/rrq> or L<wrq|Mojo::TFTPd/wrq> | ||||||||||||||||||||||||||||||||||||
| 324 | events. | ||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||
| 326 | =head2 retransmit | ||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||
| 328 | $int = $connection->retransmit; | ||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||
| 330 | Number of times last operation (L</send_data>, L</send_ack> or L</send_oack>) | ||||||||||||||||||||||||||||||||||||
| 331 | to be retransmitted on timeout before the connection is dropped. | ||||||||||||||||||||||||||||||||||||
| 332 | This value comes from L<Mojo::TFTPd/retransmit> or set inside L<rrq|Mojo::TFTPd/rrq> or L<wrq|Mojo::TFTPd/wrq> | ||||||||||||||||||||||||||||||||||||
| 333 | events. | ||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | Retransmits are disabled if set to 0. | ||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||
| 337 | =head2 socket | ||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||
| 339 | $fh = $connection->socket; | ||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||
| 341 | The UDP handle to send data to. | ||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||
| 343 | =head2 rfc | ||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||
| 345 | $hash_ref = $connection->rfc; | ||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||
| 347 | Contains RFC 2347 options the client has provided. | ||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||
| 349 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||
| 351 | =head2 receive_ack | ||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||
| 353 | $bool = $connection->receive_ack($bytes); | ||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||
| 355 | This method is called when the client sends ACK to the server. | ||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||
| 357 | =head2 receive_data | ||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||
| 359 | $bool = $connection->receive_data($bytes); | ||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||
| 361 | This method is called when the client sends DATA to the server. | ||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||
| 363 | =head2 receive_error | ||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||
| 365 | $bool = $connection->receive_error($bytes); | ||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||
| 367 | This method is called when the client sends ERROR to the server. | ||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||
| 369 | =head2 send_ack | ||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||
| 371 | $bool = $connection->send_ack; | ||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||
| 373 | This method is called when the server sends ACK to the client. | ||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | =head2 send_data | ||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||
| 377 | $bool = $connection->send_data; | ||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | This method is called when the server sends DATA to the client. | ||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||
| 381 | =head2 send_error | ||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||
| 383 | $bool = $connection->send_error($key => $descr); | ||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||
| 385 | Used to report error to the client. | ||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||
| 387 | =head2 send_oack | ||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||
| 389 | $bool = $connection->send_oack; | ||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||
| 391 | Used to send RFC 2347 OACK to client | ||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||
| 393 | Supported options are | ||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||
| 395 | =over | ||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||
| 397 | =item RFC 2348 blksize | ||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||
| 399 | Report L</blocksize>. | ||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||
| 401 | =item RFC 2349 timeout | ||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||
| 403 | Report L</timeout>. | ||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||
| 405 | =item RFC 2349 tsize | ||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||
| 407 | Report L</filesize> if set inside the L<rrq|Mojo::TFTPd/rrq>. | ||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||
| 409 | =back | ||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||
| 411 | =head2 send_retransmit | ||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||
| 413 | $bool = $connection->send_retransmit; | ||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||
| 415 | Used to retransmit last packet to the client. | ||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||
| 417 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||
| 419 | L<Mojo::TFTPd> | ||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||||||||||