4 use Net::TFTPd 0.03 qw(%OPCODES);
6 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
9 my $dir = 'conf/' . readlink('conf/tftp.dir');
10 $dir =~ s{[^/]+/\.\./([^/]+)}{$1};
14 my $tftp_dir = tftp_dir;
16 die "no $tftp_dir\n" unless -e $tftp_dir;
18 # callback sub used to print transfer status
22 if($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'RRQ'})
25 printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTACK'}, $req->{'_REQUEST_'}{'LASTBLK'};
27 elsif($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'WRQ'})
30 printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'};
32 warn "IGNORED: ", dump( $req );
37 my $listener = Net::TFTPd->new(
38 'RootDir' => $tftp_dir,
41 'CallBack' => \&callback,
42 # LocalAddr => '10.0.0.100',
45 BlkSize => 1456, # IBM GE seems to be picky
47 ) or die Net::TFTPd->error;
48 printf "TFTP on %s:%d timeout: %d dir: $tftp_dir\n", $listener->{'LocalAddr'} ? $listener->{'LocalAddr'} : "'any address'", $listener->{'LocalPort'}, $listener->{'Timeout'};
52 # wait for any request (RRQ or WRQ)
53 if(my $request = $listener->waitRQ()) {
57 if ( $request->{RootDir} ne $tftp_dir ) {
58 $request->{RootDir} = $tftp_dir;
59 warn "new root: $tftp_dir\n";
63 printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'};
66 if($request->processRQ()) {
67 print "OK, transfer completed successfully\n";
69 warn Net::TFTPd->error;
70 $request->processRQ();
73 # request not received (timed out waiting for request etc.)
74 warn Net::TFTPd->error;