FTP
[User Protocols]

File transfer protocol. More...

Collaboration diagram for FTP:

Data Structures

struct  FTPSESSION
 UDP port of DHCP server. More...

FTP Server Configuration

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



PNUT __pad0__
 Default FTP root path.
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.
TCPSOCKET * NutFtpDataConnect (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.

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.


Function Documentation

int NutFtpRespondOk ( FTPSESSION session,
int  code 
)

Send a positive response.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
code Response 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().

int NutFtpRespondBad ( FTPSESSION session,
int  code 
)

Send a negative response.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
code Response 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().

int NutFtpSendMode ( FTPSESSION session,
int  binary 
)

Send a response including the specified transfer mode.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
binary 0 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().

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:
root Non-empty absolute root directory path including the device name, but without a trailing slash.
work Absolute 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.
path Relative 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().

TCPSOCKET* NutFtpDataConnect ( FTPSESSION session  ) 

Establish an FTP connection for data transfer.

Parameters:
session Pointer 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().

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:
path Path 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, malloc, strchr(), strcpy(), and strlen().

Referenced by main(), and NutFtpServerSession().

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:
user User's name.
pass Uncrypted 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().

FTPSESSION* NutFtpOpenSession ( TCPSOCKET *  sock  ) 

Open an FTP server session.

Parameters:
sock Socket 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, FTP_DATA_PORT, FTPSESSION::ftp_data_port, FTPSESSION::ftp_maxseg, FTPSESSION::ftp_sock, FTPSESSION::ftp_stream, malloc, and memset().

Referenced by NutFtpServerSession().

void NutFtpCloseSession ( FTPSESSION session  ) 

Close an FTP server session.

Parameters:
session Pointer 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().

int NutFtpProcessCwd ( FTPSESSION session,
char *  path 
)

Process an FTP client's CWD command.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full 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().

int NutFtpProcessDelete ( FTPSESSION session,
char *  path 
)

Process an FTP client's DELE command.

Causes the specified file to be deleted.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full 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().

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

Transfer a file to or from the FTP client.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full absolute path name of the file.
mode If 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, stat::st_mode, stat(), and unlink().

Referenced by NutFtpProcessRequest().

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:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full absolute path name of the directory.
options Set 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(), and strlen().

Referenced by NutFtpProcessRequest(), and NutFtpTransferDirectory().

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:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full 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().

int NutFtpProcessMkd ( FTPSESSION session,
char *  path 
)

Process an FTP client's MKD command.

Causes the specified directory to be created.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full 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().

int NutFtpRenamePrepare ( FTPSESSION session,
char *  path 
)
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().

int NutFtpProcessPass ( FTPSESSION session,
char *  pass 
)

Process an FTP client's PASS command.

Only one login per session is accepted.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
pass Password.
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().

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:
session Pointer 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, and prog_char.

Referenced by NutFtpProcessRequest().

int NutFtpProcessPort ( FTPSESSION session,
char *  args 
)

Process an FTP client's PORT command.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
args Command 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(), and NutFtpRespondOk().

Referenced by NutFtpProcessRequest().

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:
session Pointer 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:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
path Full 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().

int NutFtpProcessSystem ( FTPSESSION session  ) 

Process an FTP client's SYST command.

Parameters:
session Pointer 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:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
typecode Command 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().

int NutFtpProcessUser ( FTPSESSION session,
char *  user 
)

Process an FTP client's USER command.

Only one login per session is accepted.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
user User 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().

int NutFtpProcessRequest ( FTPSESSION session,
char *  request 
)

Process an FTP request.

This routine implements the protocol interpreter of the FTP server.

Parameters:
session Pointer to an FTPSESSION structure, obtained by a previous call to NutFtpOpenSession().
request Request 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().

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:
sock Socket 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, 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.


Variable Documentation

PNUT __pad0__

Default FTP root path.

Definition at line 152 of file ftpd.c.


© 2000-2010 by contributors - visit http://www.ethernut.de/