Nut/OS  4.10.3
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.

Definition at line 71 of file ftpd.h.

Referenced by NutFtpServerSession().

#define FTP_ROOTPATH   "PNUT:"

Default FTP root path.

Definition at line 138 of file ftpd.c.

Referenced by NutRegisterFtpRoot().

#define FTP_DATA_PORT   20

Default data port.

Definition at line 147 of file ftpd.c.

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.

Definition at line 287 of file ftpd.c.

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.

Definition at line 309 of file ftpd.c.

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.

Definition at line 331 of file ftpd.c.

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.

Definition at line 367 of file ftpd.c.

References malloc(), 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.

Definition at line 465 of file ftpd.c.

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(), 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.

Definition at line 516 of file ftpd.c.

References free(), FTP_ROOTPATH, malloc(), 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.

Definition at line 576 of file ftpd.c.

References free(), 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.

Definition at line 609 of file ftpd.c.

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().

Definition at line 650 of file ftpd.c.

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.

Definition at line 675 of file ftpd.c.

References free(), FTPSESSION::ftp_cwd, 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.

Definition at line 716 of file ftpd.c.

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.

Definition at line 741 of file ftpd.c.

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.

Definition at line 860 of file ftpd.c.

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.

Definition at line 938 of file ftpd.c.

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.

Definition at line 955 of file ftpd.c.

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

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpRenamePrepare ( FTPSESSION session,
char *  path 
)

Definition at line 964 of file ftpd.c.

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

Referenced by NutFtpProcessRequest().

Here is the call graph for this function:

int NutFtpRenameAction ( FTPSESSION session,
char *  path 
)

Definition at line 984 of file ftpd.c.

References free(), FTPSESSION::ftp_renamesource, 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.

Definition at line 1019 of file ftpd.c.

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.

Definition at line 1044 of file ftpd.c.

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.

Definition at line 1073 of file ftpd.c.

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.

Definition at line 1096 of file ftpd.c.

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.

Definition at line 1118 of file ftpd.c.

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.

Definition at line 1135 of file ftpd.c.

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.

Definition at line 1159 of file ftpd.c.

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.

Definition at line 1177 of file ftpd.c.

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.

Definition at line 1208 of file ftpd.c.

References CreateFullPathName(), free(), FTPSESSION::ftp_cwd, FTPSESSION::ftp_login, FTPSESSION::ftp_renamesource, NutFtpProcessCwd(), NutFtpProcessDelete(), NutFtpProcessMkd(), NutFtpProcessPass(), NutFtpProcessPassiv(), NutFtpProcessPort(), NutFtpProcessPwd(), NutFtpProcessRmd(), NutFtpProcessSystem(), NutFtpProcessType(), NutFtpProcessUser(), NutFtpRenameAction(), NutFtpRenamePrepare(), NutFtpRespondBad(), NutFtpRespondOk(), NutFtpTransferDirectoryOptions(), NutFtpTransferFile(), printf, 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.

Definition at line 1363 of file ftpd.c.

References fflush(), fgets(), fprintf_P, free(), FTP_MAX_CMDBUF, FTPSESSION::ftp_stream, localtime(), malloc(), NutFtpCloseSession(), NutFtpOpenSession(), NutFtpProcessRequest(), NutFtpRespondBad(), NutRegisterFtpRoot(), NutVersionString(), printf, printf_P(), 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: