4 use Net::TFTPd 0.03 qw(%OPCODES);
6 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
8 # change ROOTDIR to your TFTP root directory
9 my $rootdir = 'conf/' . readlink('conf/tftp.dir');
10 $rootdir =~ s{[^/]+/\.\./([^/]+)}{$1};
14 print "$rootdir isn't directory!\nUsage: $0 path/to/rootdir\n\n";
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' => $rootdir,
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 %s:%d [timeout %d s] $rootdir\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()) {
55 printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->{'_REQUEST_'}{'FileName'};
58 if($request->processRQ()) {
59 print "OK, transfer completed successfully\n";
61 warn Net::TFTPd->error;
62 $request->processRQ();
65 # request not received (timed out waiting for request etc.)
66 warn Net::TFTPd->error;