| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use Net::TFTPd 0.05 qw(%OPCODES); | ||
| 4 | |||
| 5 | # change ROOTDIR to your TFTP root directory | ||
| 6 | my $rootdir = $ARGV[0]; | ||
| 7 | |||
| 8 | unless(-d $rootdir) | ||
| 9 | { | ||
| 10 | print "\nUsage: simpleTFTPd.pl path/to/rootdir\n\n"; | ||
| 11 | exit 1; | ||
| 12 | } | ||
| 13 | |||
| 14 | # callback sub used to print transfer status | ||
| 15 | sub callback | ||
| 16 | { | ||
| 17 | my $req = shift; | ||
| 18 | if($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'RRQ'}) | ||
| 19 | { | ||
| 20 | # RRQ | ||
| 21 | printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTACK'}, $req->{'_REQUEST_'}{'LASTBLK'}; | ||
| 22 | } | ||
| 23 | elsif($req->{'_REQUEST_'}{'OPCODE'} eq $OPCODES{'WRQ'}) | ||
| 24 | { | ||
| 25 | # WRQ | ||
| 26 | printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTBLK'}, $req->{'_REQUEST_'}{'LASTACK'}; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | # create the listener | ||
| 31 | my $listener = Net::TFTPd->new('RootDir' => $rootdir, 'Writable' => 1, 'Timeout' => 10, 'CallBack' => \&callback) or die Net::TFTPd->error; | ||
| 32 | 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'}; | ||
| 33 | |||
| 34 | # wait for any request (RRQ or WRQ) | ||
| 35 | if(my $request = $listener->waitRQ()) | ||
| 36 | { | ||
| 37 | # received request | ||
| 38 | printf "Received a %s for file '%s'\n", $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, $request->getFileName(); | ||
| 39 | |||
| 40 | # process the request | ||
| 41 | if($request->processRQ()) | ||
| 42 | { | ||
| 43 | printf "OK, transfer completed successfully for file %s, %u bytes transferred\n", $request->getFileName(), $request->getTotalBytes(); | ||
| 44 | } | ||
| 45 | else | ||
| 46 | { | ||
| 47 | die Net::TFTPd->error; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | # request not received (timed out waiting for request etc.) | ||
| 53 | die Net::TFTPd->error; | ||
| 54 | } |