--- bin/tftpd.pl 2009/07/27 22:25:06 13 +++ bin/tftpd.pl 2009/07/28 21:03:29 24 @@ -5,16 +5,17 @@ die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; -# change ROOTDIR to your TFTP root directory -my $rootdir = 'conf/' . readlink('conf/tftp.dir'); -$rootdir =~ s{[^/]+/\.\./([^/]+)}{$1}; - -unless(-d $rootdir) -{ - print "$rootdir isn't directory!\nUsage: $0 path/to/rootdir\n\n"; - exit 1; +our $tftp_dir; +sub tftp_dir { + require 'config.pl'; + warn "# config: ", readlink 'config.pl', " tftp_dir: $tftp_dir\n"; + return $tftp_dir; } +tftp_dir; + +die "no $tftp_dir\n" unless -e $tftp_dir; + # callback sub used to print transfer status sub callback { @@ -35,7 +36,7 @@ # create the listener my $listener = Net::TFTPd->new( - 'RootDir' => $rootdir, + 'RootDir' => $tftp_dir, 'Writable' => 0, 'Timeout' => 3600, 'CallBack' => \&callback, @@ -45,12 +46,20 @@ BlkSize => 1456, # IBM GE seems to be picky Debug => 99, ) or die Net::TFTPd->error; -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'}; while(1) { # wait for any request (RRQ or WRQ) if(my $request = $listener->waitRQ()) { + + $tftp_dir = tftp_dir; + + if ( $request->{RootDir} ne $tftp_dir ) { + $request->{RootDir} = $tftp_dir; + warn "new root: $tftp_dir\n"; + } + # received request printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'};