.TH tftp 3erl "inets 6.3.4" "Ericsson AB" "Erlang Module Definition" .SH NAME tftp \- Trivial FTP. .SH DESCRIPTION .LP This is a complete implementation of the following IETF standards: .RS 2 .TP 2 * RFC 1350, The TFTP Protocol (revision 2) .LP .TP 2 * RFC 2347, TFTP Option Extension .LP .TP 2 * RFC 2348, TFTP Blocksize Option .LP .TP 2 * RFC 2349, TFTP Timeout Interval and Transfer Size Options .LP .RE .LP The only feature that not is implemented is the "netascii" transfer mode\&. .LP The \fBstart/1\fR\& function 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 transfer\&. .LP On the client side, function \fBread_file/3\fR\& and \fBwrite_file/3\fR\& spawn a temporary client process establishing contact with a TFTP daemon and perform the file transfer\&. .LP \fItftp\fR\& uses a callback module to handle the file transfer\&. Two such callback modules are provided, \fItftp_binary\fR\& and \fItftp_file\fR\&\&. See \fBread_file/3\fR\& and \fBwrite_file/3\fR\& for details\&. You can also implement your own callback modules, see \fBCALLBACK FUNCTIONS\fR\&\&. A callback module provided by the user is registered using option \fIcallback\fR\&, see \fBDATA TYPES\fR\&\&. .SH "TFTP SERVER SERVICE START/STOP" .LP A TFTP server can be configured to start statically when starting the \fIInets\fR\& application\&. Alternatively, it can be started dynamically (when \fIInets\fR\& is already started) by calling the \fIInets\fR\& application API \fIinets:start(tftpd, ServiceConfig)\fR\& or \fIinets:start(tftpd, ServiceConfig, How)\fR\&, see \fBinets(3erl)\fR\& for details\&. The \fIServiceConfig\fR\& for TFTP is described in the \fBDATA TYPES\fR\& section\&. .LP The TFTP server can be stopped using \fIinets:stop(tftpd, Pid)\fR\&, see \fBinets(3erl)\fR\& for details\&. .LP The TPFT client is of such a temporary nature that it is not handled as a service in the \fIInets\fR\& service framework\&. .SH "DATA TYPES" .LP \fIServiceConfig = Options\fR\& .LP \fIOptions = [option()]\fR\& .LP Most of the options are common for both the client and the server side, but some of them differs a little\&. The available \fIoption()\fR\&s are as follows: .RS 2 .TP 2 .B \fI{debug, Level}\fR\&: \fILevel = none | error | warning | brief | normal | verbose | all\fR\& .RS 2 .LP Controls the level of debug printouts\&. Default is \fInone\fR\&\&. .RE .TP 2 .B \fI{host, Host}\fR\&: \fIHost = hostname()\fR\&, see \fBinet(3erl)\fR\&\&. .RS 2 .LP The name or IP address of the host where the TFTP daemon resides\&. This option is only used by the client\&. .RE .TP 2 .B \fI{port, Port}\fR\&: \fIPort = int()\fR\& .RS 2 .LP The TFTP port where the daemon listens\&. Defaults is the standardized number 69\&. On the server side, it can sometimes make sense to set it to 0, meaning that the daemon just picks a free port (which one is returned by function \fIinfo/1\fR\&)\&. .RE .RS 2 .LP If a socket is connected already, option \fI{udp, [{fd, integer()}]}\fR\& can be used to pass the open file descriptor to \fIgen_udp\fR\&\&. 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 \fIsetuid_socket_wrap\fR\&, the command-line argument "-tftpd_69 22" triggers the prebound file descriptor 22 to be used instead of opening port 69\&. The UDP option \fI{udp, [{fd, 22}]}\fR\& is automatically added\&. See \fIinit:get_argument/\fR\& about command-line arguments and \fIgen_udp:open/2\fR\& about UDP options\&. .RE .TP 2 .B \fI{port_policy, Policy}\fR\&: \fIPolicy = random | Port | {range, MinPort, MaxPort}\fR\& .RS 2 .LP \fIPort = MinPort = MaxPort = int()\fR\& .RE .RS 2 .LP Policy for the selection of the temporary port that is used by the server/client during the file transfer\&. Default is \fIrandom\fR\&, 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\&. .RE .TP 2 .B \fI{udp, Options}\fR\&: \fIOptions = [Opt]\fR\&, see \fBgen_udp:open/2\fR\&\&. .TP 2 .B \fI{use_tsize, Bool}\fR\&: \fIBool = bool()\fR\& .RS 2 .LP Flag for automated use of option \fItsize\fR\&\&. With this set to \fItrue\fR\&, the \fIwrite_file/3\fR\& client determines the filesize and sends it to the server as the standardized \fItsize\fR\& option\&. A \fIread_file/3\fR\& client acquires only a filesize from the server by sending a zero \fItsize\fR\&\&. .RE .TP 2 .B \fI{max_tsize, MaxTsize}\fR\&: \fIMaxTsize = int() | infinity\fR\& .RS 2 .LP Threshold for the maximal filesize in bytes\&. The transfer is aborted if the limit is exceeded\&. Default is \fIinfinity\fR\&\&. .RE .TP 2 .B \fI{max_conn, MaxConn}\fR\&: \fIMaxConn = int() | infinity\fR\& .RS 2 .LP Threshold for the maximal number of active connections\&. The daemon rejects the setup of new connections if the limit is exceeded\&. Default is \fIinfinity\fR\&\&. .RE .TP 2 .B \fI{TftpKey, TftpVal}\fR\&: \fITftpKey = string()\fR\& .br \fITftpVal = string()\fR\& .RS 2 .LP Name and value of a TFTP option\&. .RE .TP 2 .B \fI{reject, Feature}\fR\&: \fIFeature = Mode | TftpKey\fR\& .br \fI Mode = read | write\fR\& .br \fI TftpKey = string()\fR\& .RS 2 .LP 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\&. .RE .TP 2 .B \fI{callback, {RegExp, Module, State}}\fR\&: \fIRegExp = string()\fR\& .br \fIModule = atom()\fR\& .br \fIState = term()\fR\& .RS 2 .LP 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\&. See \fBread_file/3\fR\& and \fBwrite_file/3\fR\&\&. .RE .RS 2 .LP The callback module must implement the \fItftp\fR\& behavior, see \fBCALLBACK FUNCTIONS\fR\&\&. .RE .TP 2 .B \fI{logger, Module}\fR\&: \fIModule = module()()\fR\& .RS 2 .LP Callback module for customized logging of errors, warnings, and info messages\&. The callback module must implement the \fItftp_logger\fR\& behavior, see \fBLOGGER FUNCTIONS\fR\&\&. The default module is \fItftp_logger\fR\&\&. .RE .TP 2 .B \fI{max_retries, MaxRetries}\fR\&: \fIMaxRetries = int()\fR\& .RS 2 .LP 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\&. .RE .RE .SH EXPORTS .LP .B change_config(daemons, Options) -> [{Pid, Result}] .br .RS .LP Types: .RS 3 Options = [option()] .br Pid = pid() .br Result = ok | {error, Reason} .br Reason = term() .br .RE .RE .RS .LP Changes configuration for all TFTP daemon processes\&. .RE .LP .B change_config(servers, Options) -> [{Pid, Result}] .br .RS .LP Types: .RS 3 Options = [option()] .br Pid = pid() .br Result = ok | {error, Reason} .br Reason = term() .br .RE .RE .RS .LP Changes configuration for all TFTP server processes\&. .RE .LP .B change_config(Pid, Options) -> Result .br .RS .LP Types: .RS 3 Pid = pid() .br Options = [option()] .br Result = ok | {error, Reason} .br Reason = term() .br .RE .RE .RS .LP Changes configuration for a TFTP daemon, server, or client process\&. .RE .LP .B info(daemons) -> [{Pid, Options}] .br .RS .LP Types: .RS 3 Pid = [pid()()] .br Options = [option()] .br Reason = term() .br .RE .RE .RS .LP Returns information about all TFTP daemon processes\&. .RE .LP .B info(servers) -> [{Pid, Options}] .br .RS .LP Types: .RS 3 Pid = [pid()()] .br Options = [option()] .br Reason = term() .br .RE .RE .RS .LP Returns information about all TFTP server processes\&. .RE .LP .B info(Pid) -> {ok, Options} | {error, Reason} .br .RS .LP Types: .RS 3 Options = [option()] .br Reason = term() .br .RE .RE .RS .LP Returns information about a TFTP daemon, server, or client process\&. .RE .LP .B read_file(RemoteFilename, LocalFilename, Options) -> {ok, LastCallbackState} | {error, Reason} .br .RS .LP Types: .RS 3 RemoteFilename = string() .br LocalFilename = binary | string() .br Options = [option()] .br LastCallbackState = term() .br Reason = term() .br .RE .RE .RS .LP Reads a (virtual) file \fIRemoteFilename\fR\& from a TFTP server\&. .LP If \fILocalFilename\fR\& is the atom \fIbinary\fR\&, \fItftp_binary\fR\& is used as callback module\&. It concatenates all transferred blocks and returns them as one single binary in \fILastCallbackState\fR\&\&. .LP If \fILocalFilename\fR\& is a string and there are no registered callback modules, \fItftp_file\fR\& is used as callback module\&. It writes each transferred block to the file named \fILocalFilename\fR\& and returns the number of transferred bytes in \fILastCallbackState\fR\&\&. .LP If \fILocalFilename\fR\& is a string and there are registered callback modules, \fILocalFilename\fR\& 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\&. .RE .LP .B start(Options) -> {ok, Pid} | {error, Reason} .br .RS .LP Types: .RS 3 Options = [option()] .br Pid = pid() .br Reason = term() .br .RE .RE .RS .LP 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\&. .RE .LP .B write_file(RemoteFilename, LocalFilename, Options) -> {ok, LastCallbackState} | {error, Reason} .br .RS .LP Types: .RS 3 RemoteFilename = string() .br LocalFilename = binary() | string() .br Options = [option()] .br LastCallbackState = term() .br Reason = term() .br .RE .RE .RS .LP Writes a (virtual) file \fIRemoteFilename\fR\& to a TFTP server\&. .LP If \fILocalFilename\fR\& is a binary, \fItftp_binary\fR\& is used as callback module\&. The binary is transferred block by block and the number of transferred bytes is returned in \fILastCallbackState\fR\&\&. .LP If \fILocalFilename\fR\& is a string and there are no registered callback modules, \fItftp_file\fR\& is used as callback module\&. It reads the file named \fILocalFilename\fR\& block by block and returns the number of transferred bytes in \fILastCallbackState\fR\&\&. .LP If \fILocalFilename\fR\& is a string and there are registered callback modules, \fILocalFilename\fR\& 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\&. .RE .SH "CALLBACK FUNCTIONS" .LP A \fItftp\fR\& callback module is to be implemented as a \fItftp\fR\& behavior and export the functions listed in the following\&. .LP On the server side, the callback interaction starts with a call to \fIopen/5\fR\& with the registered initial callback state\&. \fIopen/5\fR\& is expected to open the (virtual) file\&. Then either function \fIread/1\fR\& or \fIwrite/2\fR\& 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 \fIread/1\fR\& or \fIwrite/2\fR\& is expected to close the (virtual) file and return its last state\&. Function \fIabort/3\fR\& is only used in error situations\&. Function \fIprepare/5\fR\& is not used on the server side\&. .LP On the client side, the callback interaction is the same, but it starts and ends a bit differently\&. It starts with a call to \fIprepare/5\fR\& with the same arguments as \fIopen/5\fR\& takes\&. \fIprepare/5\fR\& 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 \fIopen/5\fR\& on the client side\&. On the client side, function \fIopen/5\fR\& must accept all option 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 \fIread/1\fR\& or \fIwrite/2\fR\&, the returned state is forwarded to the user and returned from \fIread_file\fR\&/3 or \fIwrite_file/3\fR\&\&. .LP 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\&. .LP 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\&. .SH EXPORTS .LP .B Module:abort(Code, Text, State) -> ok .br .RS .LP Types: .RS 3 Code = undef | enoent | eacces | enospc .br | badop | eexist | baduser | badopt .br | int() .br Text = string() .br State = term() .br .RE .RE .RS .LP Invoked when the file transfer is aborted\&. .LP 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\&. The function is not invoked if any of the other callback functions returns an error, as it is expected that they already have cleaned up the necessary resources\&. However, it is invoked if the functions fail (crash)\&. .RE .LP .B Module:open(Peer, Access, Filename, Mode, SuggestedOptions, State) -> {ok, AcceptedOptions, NewState} | {error, {Code, Text}} .br .RS .LP Types: .RS 3 Peer = {PeerType, PeerHost, PeerPort} .br PeerType = inet | inet6 .br PeerHost = ip_address() .br PeerPort = integer() .br Access = read | write .br Filename = string() .br Mode = string() .br SuggestedOptions = AcceptedOptions = [{Key, Value}] .br Key = Value = string() .br State = InitialState | term() .br InitialState = [] | [{root_dir, string()}] .br NewState = term() .br Code = undef | enoent | eacces | enospc .br | badop | eexist | baduser | badopt .br | int() .br Text = string() .br .RE .RE .RS .LP Opens a file for read or write access\&. .LP On the client side, where the \fIopen/5\fR\& call has been preceded by a call to \fIprepare/5\fR\&, all options must be accepted or rejected\&. .LP On the server side, where there is no preceding \fIprepare/5\fR\& call, no new options can be added, but those present in \fISuggestedOptions\fR\& can be omitted or replaced with new values in \fIAcceptedOptions\fR\&\&. .RE .LP .B Module:prepare(Peer, Access, Filename, Mode, SuggestedOptions, InitialState) -> {ok, AcceptedOptions, NewState} | {error, {Code, Text}} .br .RS .LP Types: .RS 3 Peer = {PeerType, PeerHost, PeerPort} .br PeerType = inet | inet6 .br PeerHost = ip_address() .br PeerPort = integer() .br Access = read | write .br Filename = string() .br Mode = string() .br SuggestedOptions = AcceptedOptions = [{Key, Value}] .br Key = Value = string() .br InitialState = [] | [{root_dir, string()}] .br NewState = term() .br Code = undef | enoent | eacces | enospc .br | badop | eexist | baduser | badopt .br | int() .br Text = string() .br .RE .RE .RS .LP Prepares to open a file on the client side\&. .LP No new options can be added, but those present in \fISuggestedOptions\fR\& can be omitted or replaced with new values in \fIAcceptedOptions\fR\&\&. .LP This is followed by a call to \fIopen/4\fR\& before any read/write access is performed\&. \fIAcceptedOptions\fR\& is sent to the server, which replies with the options that it accepts\&. These are then forwarded to \fIopen/4\fR\& as \fISuggestedOptions\fR\&\&. .RE .LP .B Module:read(State) -> {more, Bin, NewState} | {last, Bin, FileSize} | {error, {Code, Text}} .br .RS .LP Types: .RS 3 State = NewState = term() .br Bin = binary() .br FileSize = int() .br Code = undef | enoent | eacces | enospc .br | badop | eexist | baduser | badopt .br | int() .br Text = string() .br .RE .RE .RS .LP Reads a chunk from the file\&. .LP 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\&. .RE .LP .B Module:write(Bin, State) -> {more, NewState} | {last, FileSize} | {error, {Code, Text}} .br .RS .LP Types: .RS 3 Bin = binary() .br State = NewState = term() .br FileSize = int() .br Code = undef | enoent | eacces | enospc .br | badop | eexist | baduser | badopt .br | int() .br Text = string() .br .RE .RE .RS .LP Writes a chunk to the file\&. .LP 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\&. .RE .SH "LOGGER FUNCTIONS" .LP A \fItftp_logger\fR\& callback module is to be implemented as a \fItftp_logger\fR\& behavior and export the following functions: .SH EXPORTS .LP .B Logger:error_msg(Format, Data) -> ok | exit(Reason) .br .RS .LP Types: .RS 3 Format = string() .br Data = [term()] .br Reason = term() .br .RE .RE .RS .LP Logs an error message\&. See \fIerror_logger:error_msg/2\fR\& for details\&. .RE .LP .B Logger:info_msg(Format, Data) -> ok | exit(Reason) .br .RS .LP Types: .RS 3 Format = string() .br Data = [term()] .br Reason = term() .br .RE .RE .RS .LP Logs an info message\&. See \fIerror_logger:info_msg/2\fR\& for details\&. .RE .LP .B Logger:warning_msg(Format, Data) -> ok | exit(Reason) .br .RS .LP Types: .RS 3 Format = string() .br Data = [term()] .br Reason = term() .br .RE .RE .RS .LP Logs a warning message\&. See \fIerror_logger:warning_msg/2\fR\& for details\&. .RE