Nut/OS  5.0.5
API Reference
FTP

File transfer protocol. More...

Collaboration diagram for FTP:

Data Structures

struct  FTPSESSION
 FTP session information structure. More...

Functions

int NutFtpRespondOk (FTPSESSION *session, int code)
 Send a positive response.
int NutFtpRespondBad (FTPSESSION *session, int code)
 Send a negative response.
int NutFtpSendMode (FTPSESSION *session, int binary)
 Send a response including the specified transfer mode.
char * CreateFullPathName (char *root, char *work, char *path)
 Create an absolute path name.
TCPSOCKETNutFtpDataConnect (FTPSESSION *session)
 Establish an FTP connection for data transfer.
int NutRegisterFtpRoot (const char *path)
 Register the FTP server's root directory.
int NutRegisterFtpUser (const char *user, const char *pass)
 Register an FTP user.
FTPSESSIONNutFtpOpenSession (TCPSOCKET *sock)
 Open an FTP server session.
void NutFtpCloseSession (FTPSESSION *session)
 Close an FTP server session.
int NutFtpProcessCwd (FTPSESSION *session, char *path)
 Process an FTP client's CWD command.
int NutFtpProcessDelete (FTPSESSION *session, char *path)
 Process an FTP client's DELE command.
int NutFtpTransferFile (FTPSESSION *session, char *path, int mode)
 Transfer a file to or from the FTP client.
int NutFtpTransferDirectoryOptions (FTPSESSION *session, char *path, int options)
 Process an FTP client's LIST or NLST command.
int NutFtpTransferDirectory (FTPSESSION *session, char *path)
 Process an FTP client's LIST or NLST command.
int NutFtpProcessMkd (FTPSESSION *session, char *path)
 Process an FTP client's MKD command.
int NutFtpRenamePrepare (FTPSESSION *session, char *path)
int NutFtpRenameAction (FTPSESSION *session, char *path)
int NutFtpProcessPass (FTPSESSION *session, char *pass)
 Process an FTP client's PASS command.
int NutFtpProcessPassiv (FTPSESSION *session)
 Process an FTP client's PASV command.
int NutFtpProcessPort (FTPSESSION *session, char *args)
 Process an FTP client's PORT command.
int NutFtpProcessPwd (FTPSESSION *session)
 Process an FTP client's PWD command.
int NutFtpProcessRmd (FTPSESSION *session, char *path)
 Process an FTP client's RMD command.
int NutFtpProcessSystem (FTPSESSION *session)
 Process an FTP client's SYST command.
int NutFtpProcessType (FTPSESSION *session, char *typecode)
 Process an FTP client's TYPE command.
int NutFtpProcessUser (FTPSESSION *session, char *user)
 Process an FTP client's USER command.
int NutFtpProcessRequest (FTPSESSION *session, char *request)
 Process an FTP request.
int NutFtpServerSession (TCPSOCKET *sock)
 Process an FTP sever session.

FTP Server Configuration

The Nut/OS Configurator may be used to override the default values.

#define FTP_MAX_CMDBUF   128
 UDP port of DHCP server.

FTP Server Configuration

The Nut/OS Configurator may be used to override the default values.

#define FTP_ROOTPATH   "PNUT:"
 Default FTP root path.
#define FTP_DATA_PORT   20
 Default data port.

Detailed Description

File transfer protocol.

Note, that FTP servers don't make much sense without a file system providing write access. Right now, FTP had been tested with the PNUT file system only.

Internet File Transfer Protocol Server.


Define Documentation

#define FTP_MAX_CMDBUF   128

UDP port of DHCP server.

Referenced by NutFtpServerSession().

#define FTP_ROOTPATH   "PNUT:"

Default FTP root path.

Referenced by NutRegisterFtpRoot().

#define FTP_DATA_PORT   20

Default data port.

Referenced by NutFtpOpenSession().


Function Documentation

int NutFtpRespondOk ( FTPSESSION session,
int  code 
)

Send a positive response.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
codeResponse code.
Returns:
-1 if the session ended. Otherwise 0 is returned.

References fflush(), fprintf_P, FTPSESSION::ftp_stream, printf, and prog_char.

Referenced by NutFtpProcessCwd(), NutFtpProcessDelete(), NutFtpProcessMkd(), NutFtpProcessPass(), NutFtpProcessPort(), NutFtpProcessRequest(), NutFtpProcessRmd(), NutFtpProcessType(), NutFtpProcessUser(), NutFtpRenameAction(), NutFtpRenamePrepare(), NutFtpTransferDirectoryOptions(), and NutFtpTransferFile().

Here is the call graph for this function:

int NutFtpRespondBad ( FTPSESSION session,
int  code 
)

Send a negative response.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
codeResponse code.
Returns:
-1 if the session ended. Otherwise 0 is returned.

References fflush(), fprintf_P, FTPSESSION::ftp_stream, printf, and prog_char.

Referenced by NutFtpProcessCwd(), NutFtpProcessDelete(), NutFtpProcessMkd(), NutFtpProcessPass(), NutFtpProcessPort(), NutFtpProcessRequest(), NutFtpProcessRmd(), NutFtpProcessUser(), NutFtpRenameAction(), NutFtpRenamePrepare(), NutFtpServerSession(), NutFtpTransferDirectoryOptions(), and NutFtpTransferFile().

Here is the call graph for this function:

int NutFtpSendMode ( FTPSESSION session,
int  binary 
)

Send a response including the specified transfer mode.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
binary0 indicates ASCII transfer mode.
Returns:
-1 if the session ended. Otherwise 0 is returned.

References fflush(), fputs_P, FTPSESSION::ftp_stream, printf, and prog_char.

Referenced by NutFtpTransferDirectoryOptions(), and NutFtpTransferFile().

Here is the call graph for this function:

char* CreateFullPathName ( char *  root,
char *  work,
char *  path 
)

Create an absolute path name.

Combines an absolute directory path with a relative path name to a full absolute path name. The absolute directory is split into two parts, the root and the current work directory. The resulting path will never be above the specified root.

Parameters:
rootNon-empty absolute root directory path including the device name, but without a trailing slash.
workAbsolute work directory path below root including a leading, but without a trailing slash. This path is ignored if the relative path starts with a slash.
pathRelative path name of a file or directory without any trailing slash.
Returns:
Pointer to the resulting path name. The buffer for this name is allocated from heap memory. The caller is responsible for freeing it. In case of an error, a NULL pointer is returned.

References malloc(), NULL, strcpy(), and strlen().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

TCPSOCKET* NutFtpDataConnect ( FTPSESSION session)

Establish an FTP connection for data transfer.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
Returns:
Socket descriptor of the newly created data connection or 0 if we were unable to establish the connection.

References FTPSESSION::ftp_data_ip, FTPSESSION::ftp_data_port, FTPSESSION::ftp_maxseg, FTPSESSION::ftp_passive, NutTcpAccept(), NutTcpCloseSocket(), NutTcpConnect(), NutTcpCreateSocket(), NutTcpError(), NutTcpSetSockOpt(), printf_P(), prog_char, puts_P(), rc, and TCP_MAXSEG.

Referenced by NutFtpTransferDirectoryOptions(), and NutFtpTransferFile().

Here is the call graph for this function:

int NutRegisterFtpRoot ( const char *  path)

Register the FTP server's root directory.

Only one root directory is supported. Subsequent calls will override previous settings.

Parameters:
pathPath name of the root directory. Must include the device name followed by a colon followed by an absolute directory path. May be set to NULL for the default FTP_ROOTPATH.
Returns:
0 on success, -1 otherwise.

References free(), FTP_ROOTPATH, malloc(), NULL, strchr(), strcpy(), and strlen().

Referenced by main(), and NutFtpServerSession().

Here is the call graph for this function:

int NutRegisterFtpUser ( const char *  user,
const char *  pass 
)

Register an FTP user.

Only one username/password pair is supported. Subsequent calls will override previous settings.

Warning:
Not successfully registering any will make file systems accessible by anyone.
Parameters:
userUser's name.
passUncrypted password for the specified user.
Returns:
0 on success. -1 is returned on failures, in which case no protection may be assumed.

References free(), NULL, and strdup().

Here is the call graph for this function:

FTPSESSION* NutFtpOpenSession ( TCPSOCKET sock)

Open an FTP server session.

Parameters:
sockSocket of an established TCP connection.
Returns:
Pointer to an FTPSESSION structure, which can be used in subsequent API calls. NULL is returned in case of any error.

References _fdopen(), free(), FTPSESSION::ftp_cwd, FTPSESSION::ftp_data_port, FTP_DATA_PORT, FTPSESSION::ftp_maxseg, FTPSESSION::ftp_sock, FTPSESSION::ftp_stream, malloc(), memset(), and tcp_socket::so_mss.

Referenced by NutFtpServerSession().

Here is the call graph for this function:

void NutFtpCloseSession ( FTPSESSION session)

Close an FTP server session.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().

References fclose(), free(), FTPSESSION::ftp_cwd, FTPSESSION::ftp_renamesource, and FTPSESSION::ftp_stream.

Referenced by NutFtpServerSession().

Here is the call graph for this function:

int NutFtpProcessCwd ( FTPSESSION session,
char *  path 
)

Process an FTP client's CWD command.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the directory.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References free(), FTPSESSION::ftp_cwd, NULL, NutFtpRespondBad(), NutFtpRespondOk(), S_ISDIR, stat::st_mode, stat(), strcmp(), strdup(), and strlen().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessDelete ( FTPSESSION session,
char *  path 
)

Process an FTP client's DELE command.

Causes the specified file to be deleted.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the file.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References NutFtpRespondBad(), NutFtpRespondOk(), and unlink().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpTransferFile ( FTPSESSION session,
char *  path,
int  mode 
)

Transfer a file to or from the FTP client.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the file.
modeIf set to zero, the server will accept the data transferred via the data connection and to store the data as a file. If the file exists, then its contents will be replaced by the data being transferred. A new file is created, if the file does not already exist. If this parameter is not equal zero, then the server will transfer a copy of the specified file.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References _close(), _O_BINARY, _O_CREAT, _O_RDONLY, _O_TRUNC, _open(), _read(), _write(), free(), FTPSESSION::ftp_tran_mode, malloc(), NutFtpDataConnect(), NutFtpRespondBad(), NutFtpRespondOk(), NutFtpSendMode(), NutSleep(), NutTcpCloseSocket(), NutTcpReceive(), NutTcpSend(), printf, prog_char, puts_P(), S_ISREG, tcp_socket::so_mss, stat::st_mode, stat(), and unlink().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpTransferDirectoryOptions ( FTPSESSION session,
char *  path,
int  options 
)

Process an FTP client's LIST or NLST command.

Causes a directory listing to be sent to the client.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the directory.
optionsSet to 1 to incude filenames beginning with a "." (unix hidden files)
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References _fdopen(), closedir(), dirent::d_name, fclose(), fprintf_P, fputc(), fputs(), free(), gmtime(), malloc(), NutFtpDataConnect(), NutFtpRespondBad(), NutFtpRespondOk(), NutFtpSendMode(), NutTcpCloseSocket(), opendir(), prog_char, readdir(), S_ISDIR, stat::st_mode, stat::st_mtime, stat::st_size, stat(), strcat(), strcpy(), strlen(), _tm::tm_hour, _tm::tm_mday, _tm::tm_min, and _tm::tm_mon.

Referenced by NutFtpProcessRequest(), and NutFtpTransferDirectory().

Here is the call graph for this function:

int NutFtpTransferDirectory ( FTPSESSION session,
char *  path 
)

Process an FTP client's LIST or NLST command.

Causes a directory listing to be sent to the client.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the directory.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References NutFtpTransferDirectoryOptions().

Here is the call graph for this function:

int NutFtpProcessMkd ( FTPSESSION session,
char *  path 
)

Process an FTP client's MKD command.

Causes the specified directory to be created.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the directory.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References mkdir(), NutFtpRespondBad(), and NutFtpRespondOk().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpRenamePrepare ( FTPSESSION session,
char *  path 
)

References free(), FTPSESSION::ftp_renamesource, NULL, NutFtpRespondBad(), NutFtpRespondOk(), stat(), and strdup().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpRenameAction ( FTPSESSION session,
char *  path 
)

References free(), FTPSESSION::ftp_renamesource, NULL, NutFtpRespondBad(), NutFtpRespondOk(), and rename().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessPass ( FTPSESSION session,
char *  pass 
)

Process an FTP client's PASS command.

Only one login per session is accepted.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
passPassword.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References FTPSESSION::ftp_login, NutFtpRespondBad(), NutFtpRespondOk(), and strcmp().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessPassiv ( FTPSESSION session)

Process an FTP client's PASV command.

This command requests the server to listen on a data port and to wait for a connection rather than initiate one upon receipt of a transfer command.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References fflush(), fprintf_P, FTPSESSION::ftp_passive, FTPSESSION::ftp_sock, FTPSESSION::ftp_stream, prog_char, and tcp_socket::so_local_addr.

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessPort ( FTPSESSION session,
char *  args 
)

Process an FTP client's PORT command.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
argsCommand argument, which is the concatenation of the 32-bit internet host address and a 16-bit TCP port address. This address information is broken into 8-bit fields and the value of each field is transmitted as a decimal number.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References FTPSESSION::ftp_data_ip, FTPSESSION::ftp_data_port, FTPSESSION::ftp_sock, NutFtpRespondBad(), NutFtpRespondOk(), and tcp_socket::so_remote_addr.

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessPwd ( FTPSESSION session)

Process an FTP client's PWD command.

Causes the name of the current working directory to be returned in the reply.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References fprintf_P, FTPSESSION::ftp_cwd, FTPSESSION::ftp_stream, printf, and prog_char.

Referenced by NutFtpProcessRequest().

int NutFtpProcessRmd ( FTPSESSION session,
char *  path 
)

Process an FTP client's RMD command.

Causes the specified directory to be removed.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pathFull absolute path name of the directory.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References NutFtpRespondBad(), NutFtpRespondOk(), and rmdir().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessSystem ( FTPSESSION session)

Process an FTP client's SYST command.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References fputs_P, FTPSESSION::ftp_stream, printf, and prog_char.

Referenced by NutFtpProcessRequest().

int NutFtpProcessType ( FTPSESSION session,
char *  typecode 
)

Process an FTP client's TYPE command.

The type is not fully checked. Any argument starting with the letters 'A' or 'a' will switch the transfer mode to ASCII. Otherwise binary mode is set.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
typecodeCommand arguments.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References FTPSESSION::ftp_tran_mode, and NutFtpRespondOk().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessUser ( FTPSESSION session,
char *  user 
)

Process an FTP client's USER command.

Only one login per session is accepted.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
userUser name.
Returns:
0 if the command has been processed or -1 if the session has been aborted.

References FTPSESSION::ftp_login, NutFtpRespondBad(), NutFtpRespondOk(), and strcmp().

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpProcessRequest ( FTPSESSION session,
char *  request 
)

Process an FTP request.

This routine implements the protocol interpreter of the FTP server.

Parameters:
sessionPointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
requestRequest string.
Returns:
-1 if the session ended. Otherwise 0 is returned.

References CreateFullPathName(), free(), FTPSESSION::ftp_cwd, FTPSESSION::ftp_login, FTPSESSION::ftp_renamesource, NULL, NutFtpProcessCwd(), NutFtpProcessDelete(), NutFtpProcessMkd(), NutFtpProcessPass(), NutFtpProcessPassiv(), NutFtpProcessPort(), NutFtpProcessPwd(), NutFtpProcessRmd(), NutFtpProcessSystem(), NutFtpProcessType(), NutFtpProcessUser(), NutFtpRenameAction(), NutFtpRenamePrepare(), NutFtpRespondBad(), NutFtpRespondOk(), NutFtpTransferDirectoryOptions(), NutFtpTransferFile(), printf, rc, strchr(), strcmp_P, and strlen().

Referenced by NutFtpServerSession().

Here is the call graph for this function:

int NutFtpServerSession ( TCPSOCKET sock)

Process an FTP sever session.

Processes FTP requests on an established connection with an FTP client. This routine completely implements an FTP server except TCP connect and disconnect.

For file transfers, the same maximum segment size and timeouts as set for this socket will be used for the data connection.

Parameters:
sockSocket of an established TCP connection.
Returns:
0 if the session ended successfully or -1 if the session has been aborted.

References fflush(), fgets(), fprintf_P, free(), FTP_MAX_CMDBUF, FTPSESSION::ftp_stream, localtime(), malloc(), NutFtpCloseSession(), NutFtpOpenSession(), NutFtpProcessRequest(), NutFtpRespondBad(), NutRegisterFtpRoot(), NutVersionString(), printf, printf_P(), rc, strlen(), time(), _tm::tm_hour, _tm::tm_mday, _tm::tm_min, _tm::tm_mon, and _tm::tm_sec.

Here is the call graph for this function: