.TH tftp 3erl "inets 5.10.3" "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 in this release is the "netascii" transfer mode\&. .LP The \fBstart/1\fR\& function starts a daemon process which listens for UDP packets on a port\&. When it receives a request for read or write it spawns a temporary server process which handles the actual transfer of the file\&. .LP On the client side the \fBread_file/3\fR\& and \fBwrite_file/3\fR\& functions spawns a temporary client process which establishes contact with a TFTP daemon and performs the actual transfer of the file\&. .LP \fItftp\fR\& uses a callback module to handle the actual 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 more information about these\&. The user can also implement own callback modules, see \fBCALLBACK FUNCTIONS\fR\& below\&. A callback module provided by the user is registered using the \fIcallback\fR\& option, see \fBDATA TYPES\fR\& below\&. .SH "TFTP SERVER SERVICE START/STOP " .LP A TFTP server can be configured to start statically when starting the Inets application\&. Alternatively it can be started dynamically (when Inets already is started) by calling the Inets 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 below in the \fBCOMMON DATA 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 Inets service framework\&. .SH "COMMON DATA TYPES" .LP .nf ServiceConfig = Options Options = [option()] option() -- see below .fi .LP Most of the options are common for both the client and the server side, but some of them differs a little\&. Here are the available options: .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\&. The 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\&. It defaults to the standardized number 69\&. On the server side it may sometimes make sense to set it to 0, which means that the daemon just will pick a free port (which one is returned by the \fIinfo/1\fR\& function)\&. .RE .RS 2 .LP If a socket has somehow already has been connected, the {udp, [{fd, integer()}]} option can be used to pass the open file descriptor to gen_udp\&. This can be automated a bit by using a command line argument stating the prebound file descriptor number\&. For example, if the Port is 69 and the file descriptor 22 has been opened by setuid_socket_wrap\&. Then the command line argument "-tftpd_69 22" will trigger the prebound file descriptor 22 to be used instead of opening port 69\&. The UDP option {udp, [{fd, 22}]} automatically be added\&. See init:get_argument/ about command line arguments and gen_udp:open/2 about UDP options\&. .RE .TP 2 .B \fI{port_policy, Policy}\fR\&: \fIPolicy = random | Port | {range, MinPort, MaxPort}\fR\& .br \fIPort = MinPort = MaxPort = int()\fR\& .RS 2 .LP Policy for the selection of the temporary port which is used by the server/client during the file transfer\&. It defaults to \fIrandom\fR\& which is the standardized policy\&. With this policy a randomized free port used\&. A single port or a range of ports can be useful if the protocol should pass 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 usage of the \fItsize\fR\& option\&. With this set to true, the \fIwrite_file/3\fR\& client will determine the filesize and send it to the server as the standardized \fItsize\fR\& option\&. A \fIread_file/3\fR\& client will just acquire 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 will be aborted if the limit is exceeded\&. It defaults to \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 will reject the setup of new connections if the limit is exceeded\&. It defaults to \fIinfinity\fR\&\&. .RE .TP 2 .B \fI{TftpKey, TftpVal}\fR\&: \fITftpKey = string()\fR\& .br \fITftpVal = string()\fR\& .RS 2 .LP The 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 Control which features that should be rejected\&. This is mostly useful for the server as it may restrict usage 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 will be matched to the regular expressions of the registered callbacks\&. The first matching callback will be used the 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, \fBCALLBACK FUNCTIONS\fR\&\&. .RE .TP 2 .B \fI{logger, Module}\fR\&: \fIModule = module()()\fR\& .RS 2 .LP Callback module for customized logging of error, warning and info messages\&. >The callback module must implement the \fItftp_logger\fR\& behavior, \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 will try to resend a message up to \fI5\fR\& times when the timeout expires\&. .RE .RE .SH EXPORTS .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 which listens for udp packets on a port\&. When it receives a request for read or write it spawns a temporary server process which handles the actual transfer of the (virtual) file\&. .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 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 .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 info 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 info 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 info about a TFTP daemon, server or client process\&. .RE .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 config 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 config 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 config for a TFTP daemon, server or client process .RE .LP .B start() -> ok | {error, Reason} .br .RS .LP Types: .RS 3 Reason = term() .br .RE .RE .RS .LP Starts the Inets application\&. .RE .SH "CALLBACK FUNCTIONS" .LP A \fItftp\fR\& callback module should be implemented as a \fItftp\fR\& behavior and export the functions listed below\&. .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 the \fIread/1\fR\& or \fIwrite/2\fR\& functions are invoked repeatedly, once per transferred block\&. At each function call the state returned from the previous call is obtained\&. When the last block has been encountered the \fIread/1\fR\& or \fIwrite/2\fR\& functions is expected to close the (virtual) file and return its last state\&. The \fIabort/3\fR\& function is only used in error situations\&. \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 return the subset of them that it accepts\&. Then the options is sent to the server which will perform the same TFTP option negotiation procedure\&. The options that are accepted by the server are forwarded to the \fIopen/5\fR\& function on the client side\&. On the client side the \fIopen/5\fR\& function must accept all option as is or reject the transfer\&. Then the callback interaction follows the same pattern as described above 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 (which performs the file access in the TFTP server) takes too long time (more than the double TFTP timeout), the server will abort the connection and send an error reply to the client\&. This implies that the server will release 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 will simply ignore the new request if the request is equal with 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 will consumer lesser resources\&. .SH EXPORTS .LP .B 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 may be added, but the ones that are present in \fISuggestedOptions\fR\& may be omitted or replaced with new values in \fIAcceptedOptions\fR\&\&. .LP Will be 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 those options that it accepts\&. These will be forwarded to \fIopen/4\fR\& as \fISuggestedOptions\fR\&\&. .RE .LP .B 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 may be added, but the ones that are present in \fISuggestedOptions\fR\& may be omitted or replaced with new values in \fIAcceptedOptions\fR\&\&. .RE .LP .B 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 Read 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 etc\&. In both cases there will be no more calls to any of the callback functions\&. .RE .LP .B 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 Write 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 etc\&. In both cases there will be no more calls to any of the callback functions\&. .RE .LP .B 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 etc\&. The function will not be invoked if any of the other callback functions returns an error, as it is expected that they already have cleaned up the necessary resources\&. It will however be invoked if the functions fails (crashes)\&. .RE .SH "LOGGER FUNCTIONS" .LP A \fItftp_logger\fR\& callback module should be implemented as a \fItftp_logger\fR\& behavior and export the functions listed below\&. .SH EXPORTS .LP .B 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 Log an error message\&. See \fIerror_logger:error_msg/2 for details\&.\fR\& .RE .LP .B 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 Log a warning message\&. See \fIerror_logger:warning_msg/2 for details\&.\fR\& .RE .LP .B 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 Log an info message\&. See \fIerror_logger:info_msg/2 for details\&.\fR\& .RE