--- bin/tftpd.pl 2009/07/26 00:38:57 1 +++ bin/tftpd.pl 2009/07/28 10:29:33 17 @@ -5,15 +5,16 @@ die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; -# change ROOTDIR to your TFTP root directory -my $rootdir = $ARGV[0] || 'tftp'; - -unless(-d $rootdir) -{ - print "\nUsage: $0 path/to/rootdir\n\n"; - exit 1; +sub tftp_dir { + my $dir = 'conf/' . readlink('conf/tftp.dir'); + $dir =~ s{[^/]+/\.\./([^/]+)}{$1}; + return $dir; } +my $tftp_dir = tftp_dir; + +die "no $tftp_dir\n" unless -e $tftp_dir; + # callback sub used to print transfer status sub callback { @@ -27,12 +28,14 @@ { # WRQ printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'}; + } else { + warn "IGNORED: ", dump( $req ); } } # create the listener my $listener = Net::TFTPd->new( - 'RootDir' => $rootdir, + 'RootDir' => $tftp_dir, 'Writable' => 0, 'Timeout' => 3600, 'CallBack' => \&callback, @@ -42,12 +45,20 @@ BlkSize => 1456, # IBM GE seems to be picky Debug => 99, ) or die Net::TFTPd->error; -printf "TFTP listener is bound to %s:%d\nTFTP listener is waiting %d seconds for a request\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'};