5 |
|
|
6 |
die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; |
die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; |
7 |
|
|
8 |
# change ROOTDIR to your TFTP root directory |
our $tftp_dir; |
9 |
my $rootdir = 'conf/' . readlink('conf/tftp.dir'); |
sub tftp_dir { |
10 |
$rootdir =~ s{[^/]+/\.\./([^/]+)}{$1}; |
require 'config.pl'; |
11 |
|
warn "# config: ", readlink 'config.pl', " tftp_dir: $tftp_dir\n"; |
12 |
unless(-d $rootdir) |
return $tftp_dir; |
|
{ |
|
|
print "$rootdir isn't directory!\nUsage: $0 path/to/rootdir\n\n"; |
|
|
exit 1; |
|
13 |
} |
} |
14 |
|
|
15 |
|
tftp_dir; |
16 |
|
|
17 |
|
die "no $tftp_dir\n" unless -e $tftp_dir; |
18 |
|
|
19 |
# callback sub used to print transfer status |
# callback sub used to print transfer status |
20 |
sub callback |
sub callback |
21 |
{ |
{ |
36 |
|
|
37 |
# create the listener |
# create the listener |
38 |
my $listener = Net::TFTPd->new( |
my $listener = Net::TFTPd->new( |
39 |
'RootDir' => $rootdir, |
'RootDir' => $tftp_dir, |
40 |
'Writable' => 0, |
'Writable' => 0, |
41 |
'Timeout' => 3600, |
'Timeout' => 3600, |
42 |
'CallBack' => \&callback, |
'CallBack' => \&callback, |
46 |
BlkSize => 1456, # IBM GE seems to be picky |
BlkSize => 1456, # IBM GE seems to be picky |
47 |
Debug => 99, |
Debug => 99, |
48 |
) or die Net::TFTPd->error; |
) or die Net::TFTPd->error; |
49 |
printf "TFTP %s:%d [timeout %d s] $rootdir\n", $listener->{'LocalAddr'} ? $listener->{'LocalAddr'} : "'any address'", $listener->{'LocalPort'}, $listener->{'Timeout'}; |
printf "TFTP on %s:%d timeout: %d dir: $tftp_dir\n", $listener->{'LocalAddr'} ? $listener->{'LocalAddr'} : "'any address'", $listener->{'LocalPort'}, $listener->{'Timeout'}; |
50 |
|
|
51 |
while(1) { |
while(1) { |
52 |
|
|
53 |
# wait for any request (RRQ or WRQ) |
# wait for any request (RRQ or WRQ) |
54 |
if(my $request = $listener->waitRQ()) { |
if(my $request = $listener->waitRQ()) { |
55 |
|
|
56 |
|
$tftp_dir = tftp_dir; |
57 |
|
|
58 |
|
if ( $request->{RootDir} ne $tftp_dir ) { |
59 |
|
$request->{RootDir} = $tftp_dir; |
60 |
|
warn "new root: $tftp_dir\n"; |
61 |
|
} |
62 |
|
|
63 |
# received request |
# received request |
64 |
printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'}; |
printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'}; |
65 |
|
|