tftp(3erl)
okay so bestie, welcome to tftp(3erl) β the Erlang module definition for Trivial FTP. the manual name is folded in right here, cute and all. letβs get into it π
π NAME π
tftp β Trivial FTP. thatβs it, thatβs the name. iconic.
β¨ DESCRIPTION β¨
Interface module for the tftp application. literally the whole description, short and slay.
π DATA TYPES π
ServiceConfig = Options
Options = [option()]
okay so most of the options are common for both the client and the server side, but some of them differ a little. The available option()s are as follows:
{debug, Level}βLevel = none | error | warning | brief | normal | verbose | all. Controls the level of debug printouts. Default isnone.{host, Host}βHost = hostname(), seeinet(3erl). The name or IP address of the host where the TFTP daemon resides. This option is only used by the client.{port, Port}βPort = int(). The TFTP port where the daemon listens. Default is the standardized number69. On the server side, it can sometimes make sense to set it to0, meaning that the daemon just picks a free port (which one is returned by functioninfo/1).
and about that already-connected socket situation: if a socket is connected already, option {udp, [{fd, integer()}]} can be used to pass the open file descriptor to gen_udp. This can be automated by using a command-line argument stating the prebound file descriptor number. For example, if the port is 69 and file descriptor 22 is opened by setuid_socket_wrap, the command-line argument "-tftpd_69 22" triggers the prebound file descriptor 22 to be used instead of opening port 69. The UDP option {udp, [{fd, 22}]} is automatically added. See init:get_argument/ about command-line arguments and gen_udp:open/2 about UDP options.
{port_policy, Policy}βPolicy = random | Port | {range, MinPort, MaxPort}, andPort = MinPort = MaxPort = int(). Policy for the selection of the temporary port that is used by the server/client during the file transfer. Default israndom, which is the standardized policy. With this policy a randomized free port is used. A single port or a range of ports can be useful if the protocol passes through a firewall.{udp, Options}βOptions = [Opt], seegen_udp:open/2.{use_tsize, Bool}βBool = bool(). Flag for automated use of optiontsize. With this set totrue, thewrite_file/3client determines the filesize and sends it to the server as the standardizedtsizeoption. Aread_file/3client acquires only a filesize from the server by sending a zerotsize.{max_tsize, MaxTsize}βMaxTsize = int() | infinity. Threshold for the maximal filesize in bytes. The transfer is aborted if the limit is exceeded. Default isinfinity.{max_conn, MaxConn}βMaxConn = int() | infinity. Threshold for the maximal number of active connections. The daemon rejects the setup of new connections if the limit is exceeded. Default isinfinity.{TftpKey, TftpVal}βTftpKey = string(),TftpVal = string(). Name and value of a TFTP option.{reject, Feature}βFeature = Mode | TftpKey,Mode = read | write,TftpKey = string(). Controls which features to reject. This is mostly useful for the server as it can restrict the use of certain TFTP options or read/write access.{callback, {RegExp, Module, State}}βRegExp = string(),Module = atom(),State = term(). Registration of a callback module. When a file is to be transferred, its local filename is matched to the regular expressions of the registered callbacks. The first matching callback is used during the transfer. Seeread_file/3andwrite_file/3.
The callback module must implement the tftp behavior, see CALLBACK FUNCTIONS.
{logger, Module}βModule = module(). Callback module for customized logging of errors, warnings, and info messages. The callback module must implement thetftp_loggerbehavior, see LOGGER FUNCTIONS. The default module istftp_logger.{max_retries, MaxRetries}βMaxRetries = int(). Threshold for the maximal number of retries. By default the server/client tries to resend a message up to five times when the time-out expires.
π EXPORTS π
okay bestie, here are the exported functions, in the exact order, no skipping. β¨
πΈ change_config(daemons, Options) -> [{Pid, Result}] πΈ
Types:
Options = [option()]
Pid = pid()
Result = ok | {error, Reason}
Reason = term()
Changes configuration for all TFTP daemon processes. all of them, periodt.
πΈ change_config(servers, Options) -> [{Pid, Result}] πΈ
Types:
Options = [option()]
Pid = pid()
Result = ok | {error, Reason}
Reason = term()
Changes configuration for all TFTP server processes. yes bestie, all the server processes.
πΈ change_config(Pid, Options) -> Result πΈ
Types:
Pid = pid()
Options = [option()]
Result = ok | {error, Reason}
Reason = term()
Changes configuration for a TFTP daemon, server, or client process. one process, all the config change energy π
π¦ info(daemons) -> [{Pid, Options}] π¦
Types:
Pid = [pid()]
Options = [option()]
Reason = term()
Returns information about all TFTP daemon processes. literally the info desk for daemons.
π¦ info(servers) -> [{Pid, Options}] π¦
Types:
Pid = [pid()]
Options = [option()]
Reason = term()
Returns information about all TFTP server processes. same vibe, but for servers.
π¦ info(Pid) -> {ok, Options} | {error, Reason} π¦
Types:
Options = [option()]
Reason = term()
Returns information about a TFTP daemon, server, or client process. one process, one info moment.
π¦ read_file(RemoteFilename, LocalFilename, Options) -> {ok, LastCallbackState} | {error, Reason} π¦
Types:
RemoteFilename = string()
LocalFilename = binary | string()
Options = [option()]
LastCallbackState = term()
Reason = term()
Reads a (virtual) file RemoteFilename from a TFTP server.
If LocalFilename is the atom binary, tftp_binary is used as callback module. It concatenates all transferred blocks and returns them as one single binary in LastCallbackState.
If LocalFilename is a string and there are no registered callback modules, tftp_file is used as callback module. It writes each transferred block to the file named LocalFilename and returns the number of transferred bytes in LastCallbackState.
If LocalFilename is a string and there are registered callback modules, LocalFilename is tested against the regexps of these and the callback module corresponding to the first match is used, or an error tuple is returned if no matching regexp is found.
β start(Options) -> {ok, Pid} | {error, Reason} β
Types:
Options = [option()]
Pid = pid()
Reason = term()
Starts a daemon process listening for UDP packets on a port. When it receives a request for read or write, it spawns a temporary server process handling the actual transfer of the (virtual) file. main character daemon behavior, honestly.
π¦ write_file(RemoteFilename, LocalFilename, Options) -> {ok, LastCallbackState} | {error, Reason} π¦
Types:
RemoteFilename = string()
LocalFilename = binary() | string()
Options = [option()]
LastCallbackState = term()
Reason = term()
Writes a (virtual) file RemoteFilename to a TFTP server.
If LocalFilename is a binary, tftp_binary is used as callback module. The binary is transferred block by block and the number of transferred bytes is returned in LastCallbackState.
If LocalFilename is a string and there are no registered callback modules, tftp_file is used as callback module. It reads the file named LocalFilename block by block and returns the number of transferred bytes in LastCallbackState.
If LocalFilename is a string and there are registered callback modules, LocalFilename is tested against the regexps of these and the callback module corresponding to the first match is used, or an error tuple is returned if no matching regexp is found.
π¦ CALLBACK FUNCTIONS π¦
A tftp callback module is to be implemented as a tftp behavior and export the functions listed in the following. iykyk, callback modules are the main characters here π
On the server side, the callback interaction starts with a call to open/5 with the registered initial callback state. open/5 is expected to open the (virtual) file. Then either function read/1 or write/2 is invoked repeatedly, once per transferred block. At each function call, the state returned from the previous call is obtained. When the last block is encountered, function read/1 or write/2 is expected to close the (virtual) file and return its last state. Function abort/3 is only used in error situations. Function prepare/5 is not used on the server side.
On the client side, the callback interaction is the same, but it starts and ends a bit differently. It starts with a call to prepare/5 with the same arguments as open/5 takes. prepare/5 is expected to validate the TFTP options suggested by the user and to return the subset of them that it accepts. Then the options are sent to the server, which performs the same TFTP option negotiation procedure. The options that are accepted by the server are forwarded to function open/5 on the client side. On the client side, function open/5 must accept all options as-is or reject the transfer. Then the callback interaction follows the same pattern as described for the server side. When the last block is encountered in read/1 or write/2, the returned state is forwarded to the user and returned from read_file/3 or write_file/3.
If a callback (performing the file access in the TFTP server) takes too long time (more than the double TFTP time-out), the server aborts the connection and sends an error reply to the client. This implies that the server releases resources attached to the connection faster than before. The server simply assumes that the client has given up. not me being dramatic, thatβs literally what it says π
If the TFTP server receives yet another request from the same client (same host and port) while it already has an active connection to the client, it ignores the new request if the request is equal to the first one (same filename and options). This implies that the (new) client will be served by the already ongoing connection on the server side. By not setting up yet another connection, in parallel with the ongoing one, the server consumes less resources. periodt π
β¨ EXPORTS β¨
Module:abort(Code, Text, State) -> ok
Types:
Code = undef | enoent | eacces | enospc
| badop | eexist | baduser | badopt
| int()
Text = string()
State = term()
Okay so this one gets called when a file transfer gets aborted π β like, the vibe check failed and we're packing up.
The callback function is expected to clean up its used resources after the aborted file transfer, such as closing open file descriptors and so on. Basically: tidy your room before you leave, bestie π§Ή
Not me gatekeeping the details: this function is not invoked if any of the other callback functions returns an error, because it's expected that they already cleaned up the necessary resources. However, it is invoked if the functions fail (crash) π. So the crash path still gets the cleanup callback, periodt.
Module:open(Peer, Access, Filename, Mode, SuggestedOptions, State) -> {ok, AcceptedOptions, NewState} | {error, {Code, Text}}
Types:
Peer = {PeerType, PeerHost, PeerPort}
PeerType = inet | inet6
PeerHost = ip_address()
PeerPort = integer()
Access = read | write
Filename = string()
Mode = string()
SuggestedOptions = AcceptedOptions = [{Key, Value}]
Key = Value = string()
State = InitialState | term()
InitialState = [] | [{root_dir, string()}]
NewState = term()
Code = undef | enoent | eacces | enospc
| badop | eexist | baduser | badopt
| int()
Text = string()
This is the one that opens a file for read or write access π₯
So like, here's the split, and it lowkey matters:
- On the client side, where the
open/5call has been preceded by a call toprepare/5, all options must be accepted or rejected. No cherry-picking, bestie β it's all or nothing π - On the server side, where there is no preceding
prepare/5call, no new options can be added, but those present inSuggestedOptionscan be omitted or replaced with new values inAcceptedOptions. That's the whole game π
Yes, the signature says/6and the prose saysopen/5β I'm just the messenger, and the facts stay exactly as written. iykyk π¦
Module:prepare(Peer, Access, Filename, Mode, SuggestedOptions, InitialState) -> {ok, AcceptedOptions, NewState} | {error, {Code, Text}}
Types:
Peer = {PeerType, PeerHost, PeerPort}
PeerType = inet | inet6
PeerHost = ip_address()
PeerPort = integer()
Access = read | write
Filename = string()
Mode = string()
SuggestedOptions = AcceptedOptions = [{Key, Value}]
Key = Value = string()
InitialState = [] | [{root_dir, string()}]
NewState = term()
Code = undef | enoent | eacces | enospc
| badop | eexist | baduser | badopt
| int()
Text = string()
Prepares to open a file on the client side β¨ β this is the pre-game, the getting-ready-with-me moment before the actual file work starts.
No new options can be added, but those present in SuggestedOptions can be omitted or replaced with new values in AcceptedOptions. Same rule as before, no new stuff invented out of thin air π
Then, the storyline continues: this is followed by a call to open/4 before any read/write access is performed. AcceptedOptions is sent to the server, which replies with the options that it accepts. These are then forwarded to open/4 as SuggestedOptions. It's giving relay race πββοΈπ¨ β options go out, options come back, options get handed off.
Module:read(State) -> {more, Bin, NewState} | {last, Bin, FileSize} | {error, {Code, Text}}
Types:
State = NewState = term()
Bin = binary()
FileSize = int()
Code = undef | enoent | eacces | enospc
| badop | eexist | baduser | badopt
| int()
Text = string()
Reads a chunk from the file π β little bite-sized vibes, one chunk at a time.
The callback function is expected to close the file when the last file chunk is encountered. And when an error is encountered, the callback function is expected to clean up after the aborted file transfer, such as closing open file descriptors, and so on. In both cases there will be no more calls to any of the callback functions. So like β last chunk, you close it; error, you clean it; either way, that's a wrap, no encore ππ
Module:write(Bin, State) -> {more, NewState} | {last, FileSize} | {error, {Code, Text}}
Types:
Bin = binary()
State = NewState = term()
FileSize = int()
Code = undef | enoent | eacces | enospc
| badop | eexist | baduser | badopt
| int()
Text = string()
Writes a chunk to the file π β the read function's main character twin.
Same deal, fr fr: the callback function is expected to close the file when the last file chunk is encountered. When an error is encountered, the callback function is expected to clean up after the aborted file transfer, such as closing open file descriptors, and so on. In both cases there will be no more calls to any of the callback functions. Close it or clean it, then we're done β no coming back for seconds π
π LOGGER FUNCTIONS π
A tftp_logger callback module is to be implemented as a tftp_logger behavior and export the following functions β yes, that's the assignment, bestie π
β¨ EXPORTS β¨
Logger:error_msg(Format, Data) -> ok | exit(Reason)
Types:
Format = string()
Data = [term()]
Reason = term()
Logs an error message π¨. See error_logger:error_msg/2 for details. (Go look, it's right there π)
Logger:info_msg(Format, Data) -> ok | exit(Reason)
Types:
Format = string()
Data = [term()]
Reason = term()
Logs an info message β¨. See error_logger:info_msg/2 for details.
Logger:warning_msg(Format, Data) -> ok | exit(Reason)
Types:
Format = string()
Data = [term()]
Reason = term()
Logs a warning message β οΈ. See error_logger:warning_msg/2 for details. Last one, and she ate π
And that's the whole page, bestie β brought to you by Ericsson AB, running tftp version 1.0.4, and yes it's the tftp(3erl) page you know and love π«Άβ¨
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms