5 |
|
|
6 |
use Net::TFTPd 0.03 qw(%OPCODES); |
use Net::TFTPd 0.03 qw(%OPCODES); |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
|
use Module::Refresh; |
9 |
|
|
10 |
use server; |
use server; |
11 |
|
|
15 |
|
|
16 |
sub path { |
sub path { |
17 |
my $glob = shift; |
my $glob = shift; |
18 |
my $path = glob("$dir/$glob"); |
my $path = (glob("$dir/$glob"))[0]; |
19 |
die "can't find anything for $dir/$glob" unless $path; |
die "can't find anything for $dir/$glob" unless $path; |
20 |
warn $path; |
warn 'path ', $path if $debug; |
21 |
$path =~ s{^$dir}{}; |
$path =~ s{^$dir}{}; |
22 |
return $path; |
return $path; |
23 |
} |
} |
24 |
|
|
25 |
|
STDERR->autoflush(1); |
26 |
|
|
27 |
sub transfer_status { |
sub transfer_status { |
28 |
my $req = shift; |
my $req = shift; |
29 |
if( $req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'RRQ'} ) { |
if( $req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'RRQ'} ) { |
30 |
printf "RRQ %u\/%u\r", $req->{'_REQUEST_'}{'LASTACK'}, $req->{'_REQUEST_'}{'LASTBLK'}; |
printf STDERR "RRQ %s %u\/%u\r", map { $req->{_REQUEST_}->{$_} } ( 'FileName', 'LASTACK', 'LASTBLK' ); |
31 |
} elsif ( $req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'WRQ'} ) { |
} elsif ( $req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'WRQ'} ) { |
32 |
die "WRQ disabled"; |
die "WRQ disabled"; |
33 |
printf "WRQ: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'}; |
printf STDERR "WRQ: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'}; |
34 |
} else { |
} else { |
35 |
warn "IGNORED: ", dump( $req ); |
warn "IGNORED: ", dump( $req ); |
36 |
} |
} |
39 |
sub tftp_request { |
sub tftp_request { |
40 |
my $request = shift; |
my $request = shift; |
41 |
|
|
42 |
|
warn 'request: ', dump( $request ) if $debug; |
43 |
|
|
44 |
if ( $request->{RootDir} ne $dir ) { |
if ( $request->{RootDir} ne $dir ) { |
45 |
$request->{RootDir} = $dir; |
$request->{RootDir} = $dir; |
46 |
warn "new root: $dir"; |
warn "new root: $dir"; |
47 |
} |
} |
48 |
|
|
49 |
|
my $file = $request->{'_REQUEST_'}{'FileName'}; |
50 |
# received request |
# received request |
51 |
printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'}; |
print $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, " $file\n"; |
52 |
|
|
53 |
# process the request |
# process the request |
54 |
if( $request->processRQ() ) { |
if( $request->processRQ() ) { |
55 |
print "OK, transfer completed successfully\n"; |
print "\nOK completed $file ", -s "$dir/$file", "\n"; |
56 |
} else { |
} else { |
57 |
warn Net::TFTPd->error; |
print "ERROR ", Net::TFTPd->error, "\n"; |
58 |
$request->processRQ(); |
$request->processRQ(); |
59 |
} |
} |
60 |
|
|
81 |
|
|
82 |
warn 'listener: ',dump( $listener ) if $debug; |
warn 'listener: ',dump( $listener ) if $debug; |
83 |
|
|
84 |
|
printf "TFTP listen %s:%d timeout: %d dir: $dir\n", |
85 |
|
$listener->{LocalAddr}, |
86 |
|
$listener->{LocalPort}, |
87 |
|
$listener->{Timeout}; |
88 |
|
|
89 |
while(1) { |
while(1) { |
90 |
|
|
91 |
printf "TFTP listen %s:%d timeout: %d dir: $dir\n", |
Module::Refresh->refresh; |
|
$listener->{LocalAddr}, |
|
|
$listener->{LocalPort}, |
|
|
$listener->{Timeout}; |
|
92 |
|
|
93 |
# wait for any request (RRQ or WRQ) |
# wait for any request (RRQ or WRQ) |
94 |
if(my $request = $listener->waitRQ()) { |
if(my $request = $listener->waitRQ()) { |