Arduino TinyFTP
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tinyftp::FTPServer< TServer, TStorage > Class Template Reference

Simple, extensible FTP server for Arduino using a templated server and storage backend. More...

#include <FTPServer.h>

Classes

struct  CommandEntry
 
struct  Session
 State of a client connection. More...
 
struct  User
 

Public Types

using ClientType = decltype(std::declval< TServer & >().accept())
 Client type returned by the server.
 

Public Member Functions

 FTPServer (uint16_t port=FTP_PORT, TStorage *storage=nullptr, uint16_t dataPort=FTP_DATA_PORT)
 Constructor: initializes server and storage backend.
 
 FTPServer (const FTPServer &)=delete
 
FTPServer & operator= (const FTPServer &)=delete
 
void addUser (const char *user, const char *password)
 
void setPassiveIP (IPAddress ip)
 
void setDataTimeout (uint32_t timeoutMs)
 Defines the timeout in ms for opening and idling on data connections.
 
void begin ()
 Starts the FTP server.
 
void doLoop ()
 
int connectionCount ()
 Number of connected FTP clients.
 
void addCommand (const char *name, std::function< bool(Client &, const String &)> handler, bool needsAuth=true)
 

Protected Types

enum class  DataMode { None , Passive , Active }
 
enum class  Transfer { None , Retr , Stor , List }
 
enum class  OpenMode { Read , Write , Append }
 
using FileType = decltype(openIn(std::declval< TStorage * >(), "", OpenMode::Read, 0))
 File type of the storage.
 
using Handler = bool(FTPServer::*)(Session &, const String &)
 

Protected Member Functions

bool hasUsers ()
 
void reply (Session &s, const String &msg)
 
bool isValidLogin (const String &name, const String &pwd)
 
TServer & dataServer (int index)
 Provides the passive data server of the connection.
 
void acceptClient ()
 Accepts a new client connection.
 
void startSession (Session &s, ClientType &client)
 Resets the per connection state.
 
void closeSession (Session &s)
 
void step (Session &s)
 Processes one step of a connection.
 
void readLine (Session &s)
 Collects the characters of the next command line.
 
bool processCommand (Session &s, const String &cmd)
 Dispatches a command line: returns false to disconnect.
 
String resolvePath (Session &s, const String &arg)
 
FileType openFile (const char *path, OpenMode mode)
 
bool isDirectory (const String &path)
 Checks if the path is an existing directory.
 
void startDataServer (Session &s)
 
bool beginTransfer (Session &s, Transfer transfer, const char *msg)
 
void dataConnected (Session &s)
 
void stepTransfer (Session &s)
 Processes one step of the running transfer.
 
void stepRetr (Session &s)
 
void stepStor (Session &s)
 
void stepList (Session &s)
 Sends one entry of the directory listing.
 
void endTransfer (Session &s, const char *msg)
 Ends the transfer and reports the result.
 
void closeTransfer (Session &s)
 Closes the data connection and the file.
 
template<typename F >
void printListEntry (Client &data, F &entry, bool details)
 
bool checkStorage (Session &s)
 
bool list (Session &s, const String &cmd, bool details)
 
bool store (Session &s, const String &cmd, OpenMode mode)
 Uploads a file (STOR or APPE)
 
bool onUSER (Session &s, const String &cmd)
 
bool onPASS (Session &s, const String &cmd)
 
bool onLIST (Session &s, const String &cmd)
 
bool onNLST (Session &s, const String &cmd)
 
bool onCWD (Session &s, const String &cmd)
 
bool onCDUP (Session &s, const String &)
 
bool onPWD (Session &s, const String &)
 
bool onSYST (Session &s, const String &)
 
bool onTYPE (Session &s, const String &cmd)
 
bool onBIN (Session &s, const String &)
 
bool onASC (Session &s, const String &)
 
bool onNOOP (Session &s, const String &)
 
bool onPASV (Session &s, const String &)
 
bool onEPSV (Session &s, const String &)
 
bool onPORT (Session &s, const String &cmd)
 
bool onHELP (Session &s, const String &)
 
bool onRETR (Session &s, const String &cmd)
 
bool onSTOR (Session &s, const String &cmd)
 
bool onAPPE (Session &s, const String &cmd)
 
bool onABOR (Session &s, const String &)
 
bool onDELE (Session &s, const String &cmd)
 
bool onMKD (Session &s, const String &cmd)
 
bool onRMD (Session &s, const String &cmd)
 
bool onRNFR (Session &s, const String &cmd)
 
bool onRNTO (Session &s, const String &cmd)
 
bool onSIZE (Session &s, const String &cmd)
 
bool onFEAT (Session &s, const String &)
 
bool onOPTS (Session &s, const String &cmd)
 
bool onQUIT (Session &s, const String &)
 
bool onUnknown (Session &s, const String &)
 

Static Protected Member Functions

template<typename S >
static auto openIn (S *fs, const char *path, OpenMode mode, int) -> decltype(fs->open(path, "r"))
 Opens a file with a storage which supports mode strings (e.g. fs::FS)
 
template<typename S >
static auto openIn (S *fs, const char *path, OpenMode mode, long) -> decltype(fs->open(path, FILE_READ))
 Opens a file with the SD API modes (FILE_READ, FILE_WRITE...)
 
static bool isAbort (String line)
 
static String argument (const String &cmd)
 Provides the parameter of a command line.
 
static String baseName (const char *name)
 Provides the file name without the directory.
 
template<typename C >
static auto localIPOf (C &client, int) -> decltype(IPAddress(client.localIP()))
 Local IP of the control connection if the client type supports it.
 
template<typename C >
static IPAddress localIPOf (C &, long)
 
template<typename S >
static auto renameIn (S *fs, const char *from, const char *to, int) -> decltype(fs->rename(from, to), bool())
 Renames a file if the storage supports it.
 
template<typename S >
static bool renameIn (S *, const char *, const char *, long)
 
template<typename S >
static auto canRename (S *fs, int) -> decltype(fs->rename("", ""), bool())
 
template<typename S >
static bool canRename (S *, long)
 

Protected Attributes

std::vector< CommandEntry > commands
 
TServer server
 
uint16_t dataPort
 
TStorage * storage
 
Session sessions [FTP_SERVER_MAX_SESSIONS]
 
std::vector< User > users
 
IPAddress passiveIP
 
bool hasPassiveIP = false
 
uint32_t dataTimeout = FTP_DATA_TIMEOUT
 

Detailed Description

template<typename TServer, typename TStorage>
class tinyftp::FTPServer< TServer, TStorage >

Simple, extensible FTP server for Arduino using a templated server and storage backend.

Template Parameters
TServerServer type (e.g., WiFiServer)
TStorageStorage type (e.g., SDClass, fs::FS, CallbackFSClass). The header of the storage (e.g. SD.h) must be included before FTPServer.h

This class implements a basic FTP server supporting commands like USER, PASS, LIST, PWD, CWD, RETR, STOR, etc. File data is transferred over a separate data connection which is opened with PASV, EPSV (passive) or PORT (active). The server uses a pluggable storage backend for file and directory operations.

Up to FTP_SERVER_MAX_SESSIONS clients can be connected at the same time: doLoop() does not block, it processes one step (a command or a part of a transfer) of each connection. Each connection uses its own passive data port: dataPort + connection index.

Member Function Documentation

◆ addCommand()

void tinyftp::FTPServer< TServer, TStorage >::addCommand ( const char *  name,
std::function< bool(Client &, const String &)>  handler,
bool  needsAuth = true 
)
inline

Adds a custom command handler or replaces the handler of an existing command (in which case the login requirement is kept)

◆ addUser()

void tinyftp::FTPServer< TServer, TStorage >::addUser ( const char *  user,
const char *  password 
)
inline

Adds a user which can log in (or updates the password of an existing user). Without any users the server is open to everybody.

◆ beginTransfer()

bool tinyftp::FTPServer< TServer, TStorage >::beginTransfer ( Session &  s,
Transfer  transfer,
const char *  msg 
)
inlineprotected

Starts a transfer which was requested by PASV, EPSV or PORT: the data is transferred by stepTransfer()

◆ doLoop()

Main loop: accepts new FTP client connections and processes one step of each connection. Call it repeatedly from your loop(): it does not block.

◆ reply()

void tinyftp::FTPServer< TServer, TStorage >::reply ( Session &  s,
const String &  msg 
)
inlineprotected

Sends a reply on the control connection (via Client: some client types hide the String overloads)

◆ resolvePath()

String tinyftp::FTPServer< TServer, TStorage >::resolvePath ( Session &  s,
const String &  arg 
)
inlineprotected

Converts a path relative to the current directory into a normalized absolute path (resolving . and ..)

◆ setPassiveIP()

void tinyftp::FTPServer< TServer, TStorage >::setPassiveIP ( IPAddress  ip)
inline

Defines the IP address which is reported by PASV. If not set, the local IP of the control connection is used (when the client type supports it)

◆ startDataServer()

void tinyftp::FTPServer< TServer, TStorage >::startDataServer ( Session &  s)
inlineprotected

Starts the passive data server of the connection and drops data connections which were opened for an earlier PASV but never used


The documentation for this class was generated from the following file: