4 use Net::TFTPd 0.03 qw(%OPCODES);
6 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
11 warn "# config: ", readlink 'config.pl', " tftp_dir: $tftp_dir\n";
17 die "no $tftp_dir\n" unless -e $tftp_dir;
19 # callback sub used to print transfer status
23 if($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'RRQ'})
26 printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTACK'}, $req->{'_REQUEST_'}{'LASTBLK'};
28 elsif($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'WRQ'})
31 printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'};
33 warn "IGNORED: ", dump( $req );
38 my $listener = Net::TFTPd->new(
39 'RootDir' => $tftp_dir,
42 'CallBack' => \&callback,
43 # LocalAddr => '10.0.0.100',
46 BlkSize => 1456, # IBM GE seems to be picky
48 ) or die Net::TFTPd->error;
49 printf "TFTP on %s:%d timeout: %d dir: $tftp_dir\n", $listener->{'LocalAddr'} ? $listener->{'LocalAddr'} : "'any address'", $listener->{'LocalPort'}, $listener->{'Timeout'};
53 # wait for any request (RRQ or WRQ)
54 if(my $request = $listener->waitRQ()) {
58 if ( $request->{RootDir} ne $tftp_dir ) {
59 $request->{RootDir} = $tftp_dir;
60 warn "new root: $tftp_dir\n";
64 printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'};
67 if($request->processRQ()) {
68 print "OK, transfer completed successfully\n";
70 warn Net::TFTPd->error;
71 $request->processRQ();
74 # request not received (timed out waiting for request etc.)
75 warn Net::TFTPd->error;