Net::TFTPd(3pm)
๐ Net::TFTPd โ the manual, but make it cute ๐
okay bestie, so this is a page from the User Contributed Perl Documentation, and it's all about Net::TFTPd, a Perl extension that gives you a whole Trivial File Transfer Protocol server. yes really. a whole server. in Perl. she's serving (literally) โจ
๐ธ NAME ๐ธ
Net::TFTPd โ Perl extension for Trivial File Transfer Protocol Server ๐
๐ฆ SYNOPSIS ๐ฆ
the vibe is: you make a listener, you wait for a request, you process that request, you brag about the bytes. periodt.
use strict;
use Net::TFTPd;
my $tftpdOBJ = Net::TFTPd->new('RootDir' => 'path/to/files')
or die "Error creating TFTPd listener: %s", Net::TFTPd->error;
my $tftpRQ = $tftpdOBJ->waitRQ(10)
or die "Error waiting for TFTP request: %s", Net::TFTPd->error;
$tftpRQ->processRQ()
or die "Error processing TFTP request: %s", Net::TFTPd->error;
printf "%u bytes has been transferred", $tftpRQ->getTotalBytes() || 0;
no cap, that's the whole lifecycle right there ๐ซถ
๐ DESCRIPTION ๐
Net::TFTPd is a class that implements a simple Trivial File Transfer Protocol server in Perl, exactly as described in RFC1350. she's a whole class. she does the thing.
and not only that โ Net::TFTPd also supports the TFTP Option Extension (as described in RFC2347), with these options:
like?? she read the RFCs so you don't have to. iconic behavior ๐
โจ EXPORT โจ
None by default. nope, not a single thing. you gotta ask. consent is key ๐
๐ %OPCODES ๐
the %OPCODES tag exports the %OPCODES hash:
%OPCODES = (
1 => 'RRQ',
2 => 'WRQ',
3 => 'DATA',
4 => 'ACK',
5 => 'ERROR',
6 => 'OACK',
'RRQ' => 1,
'WRQ' => 2,
'DATA' => 3,
'ACK' => 4,
'ERROR' => 5,
'OACK' => 6
);
it goes both ways. number โ name, name โ number. bilingual queen ๐ง
๐ Listener constructor ๐
new() ๐
$listener = new Net::TFTPd( ['RootDir' => 'path/to/files' | 'FileName' => 'path/to/file'] [, OPTIONS ] );
or, if you prefer the arrow-y way:
$listener = Net::TFTPd->new( ['RootDir' => 'path/to/files' | 'FileName' => 'path/to/file'] [, OPTIONS ] );
so what's happening: you create a new Net::TFTPd object. 'path/to/files' is the default path to the file repository, OR 'path/to/file' is the single file that's allowed for download (pick one, bestie), and OPTIONS are the default server options. โจ
valid options are:
LocalAddrโ Interface to bind to (for multi-homed server) โ default:anyLocalPortโ Port to bind server to โ default:69Timeoutโ Timeout in seconds to wait for a request โ default:10ACKtimeoutโ Timeout in seconds to wait for an ACK packet โ default:4ACKretriesโ Maximum number of retries waiting for ACK โ default:4Readableโ Clients are allowed to read files โ default:1Writableโ Clients are allowed to write files โ default:0BlkSizeโ Minimum blocksize to negotiate for transfers โ default:512CallBackโ Reference to code executed for each transferred block โ default:-Debugโ Activates debug mode (verbose) โ default:0Familyโ Address family IPv4/IPv6 โ default:IPv4- Valid values for IPv4:
4,v4,ip4,ipv4,AF_INET(constant) - Valid values for IPv6:
6,v6,ip6,ipv6,AF_INET6(constant)
- Valid values for IPv4:
V6Onlyโ Enable / disable v6only (see IO::Socket::IP) โ default:1
NOTE: IPv6 requiresIO::Socket::IP. Failback isIO::Socket::INETand only IPv4 support. so if you're trying to be all v6 and fancy withoutIO::Socket::IP, she will simply not. ๐ญ
๐ CallBack ๐
the CallBack code gets called by the processRQ method for each transferred block. every. single. one. she's keeping you posted fr fr.
the code receives (into the @_ array) a reference to the internal $request object.
example:
sub callback
{
my $req = shift;
printf "block: %u\/%u\n", $req->{'_REQUEST_'}{'LASTACK'}, $req->{'_REQUEST_'}{'LASTBLK'};
}
my $tftpdOBJ = Net::TFTPd->new('RootDir' => 'c:/temp', 'Timeout' => 60, 'CallBack' => \&callback) or die Net::TFTPd->error;
look at her, peeking at '_REQUEST_' internals like it's nothing. nosy in the best way ๐
๐ซ Listener methods ๐ซ
waitRQ() โณ
$request = $listener->waitRQ([Timeout]);
waits for a client request (RRQ or WRQ) and returns a $request object, or undef if it timed out. she's just sitting there, patiently, like a bestie waiting for you to text back.
if Timeout is missing, the timeout defined for the $listener object is used instead. respect the defaults. ๐
when the method returns, the program should fork() and process the request by invoking processRQ(), while the parent process should re-start waiting for another request. yes, that's the whole pattern โ parent keeps listening, child does the work. main character energy for the parent, side quest for the child ๐
๐ Request methods ๐
processRQ() ๐ฅ
$ret = $request->processRQ();
processes a request and returns 1 if success, undef if error. that's it, that's the method.
getFileName() ๐
$ret = $request->getFileName();
returns the requested file name.
getMode() ๐๏ธ
$ret = $request->getMode();
returns the transfer mode for the request.
getBlkSize() ๐ฆ
$ret = $request->getBlkSize();
returns the block size used for the transfer.
server() ๐
$ret = $request->server();
return the IO::Socket::* object for the created server. all IO::Socket::* accessors can then be called. go wild, she's giving you the whole socket ๐
getPeerAddr() ๐
$ret = $request->getPeerAddr();
returns the address of the requesting client.
getPeerPort() ๐
$ret = $request->getPeerMode();
returns the port of the requesting client.
(ngl that's exactly what the page says โ the method is getPeerPort() and the call inside is $request->getPeerMode(). i'm not touching it. i report, you decide ๐)
getTotalBytes() ๐งฎ
$ret = $request->getTotalBytes();
returns the number of bytes transferred for the request. the receipts!! ๐
๐ CREDITS ๐
thanks to Michael Vincent (<<VINSWORLD>>) for the NETASCII support, transferred bytes and IPv6 patches. king behavior, we love to see it ๐ซถโจ
๐ AUTHOR ๐
Luigino Masarati, <lmasarati@hotmail.com> ๐
๐ SEE ALSO ๐
Net::TFTP.
and that's the whole page bestie!! this one goes out as perl v5.36.0, dated 2022-10-13, from the lovely folks in the User Contributed Perl Documentation corner of the world ๐ go transfer some files, you got this ๐๐ฆ
๐ฆ where this page comes from
- ubuntu noble: libnet-tftpd-perl 0.10-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnet-tftpd-perl 0.10-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnet-tftpd-perl 0.10-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms