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 |
sub tftp_dir { |
9 |
my $rootdir = 'conf/' . readlink('conf/tftp.dir'); |
my $dir = 'conf/' . readlink('conf/tftp.dir'); |
10 |
$rootdir =~ s{[^/]+/\.\./([^/]+)}{$1}; |
$dir =~ s{[^/]+/\.\./([^/]+)}{$1}; |
11 |
|
return $dir; |
|
unless(-d $rootdir) |
|
|
{ |
|
|
print "$rootdir isn't directory!\nUsage: $0 path/to/rootdir\n\n"; |
|
|
exit 1; |
|
12 |
} |
} |
13 |
|
|
14 |
|
my $tftp_dir = tftp_dir; |
15 |
|
|
16 |
|
die "no $tftp_dir\n" unless -e $tftp_dir; |
17 |
|
|
18 |
# callback sub used to print transfer status |
# callback sub used to print transfer status |
19 |
sub callback |
sub callback |
20 |
{ |
{ |
35 |
|
|
36 |
# create the listener |
# create the listener |
37 |
my $listener = Net::TFTPd->new( |
my $listener = Net::TFTPd->new( |
38 |
'RootDir' => $rootdir, |
'RootDir' => $tftp_dir, |
39 |
'Writable' => 0, |
'Writable' => 0, |
40 |
'Timeout' => 3600, |
'Timeout' => 3600, |
41 |
'CallBack' => \&callback, |
'CallBack' => \&callback, |
45 |
BlkSize => 1456, # IBM GE seems to be picky |
BlkSize => 1456, # IBM GE seems to be picky |
46 |
Debug => 99, |
Debug => 99, |
47 |
) or die Net::TFTPd->error; |
) or die Net::TFTPd->error; |
48 |
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'}; |
49 |
|
|
50 |
while(1) { |
while(1) { |
51 |
|
|
52 |
# wait for any request (RRQ or WRQ) |
# wait for any request (RRQ or WRQ) |
53 |
if(my $request = $listener->waitRQ()) { |
if(my $request = $listener->waitRQ()) { |
54 |
|
|
55 |
|
$tftp_dir = tftp_dir; |
56 |
|
|
57 |
|
if ( $request->{RootDir} ne $tftp_dir ) { |
58 |
|
$request->{RootDir} = $tftp_dir; |
59 |
|
warn "new root: $tftp_dir\n"; |
60 |
|
} |
61 |
|
|
62 |
# received request |
# received request |
63 |
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'}; |
64 |
|
|