Continuing to implement the FTPServer

This commit is contained in:
anschrammh 2019-10-13 16:42:34 +02:00
parent 537298e7d7
commit 1450a8c0e5
4 changed files with 49 additions and 9 deletions

View File

@ -5,9 +5,11 @@ _ftpCommand({'\0'}),
_cmdParameters(NULL),
_loggedIn(false),
_username(NULL),
_dataSocketOpen(false),
_currentDirectory(NULL),
_waitingForDataConnection(false),
_ftpClientState(FTPServer<FTPClient>::FTPClientState::WAITING_FOR_COMMANDS),
_binaryFlag(FTPServer<FTPClient>::BinaryFlag::OFF)
_binaryFlag(FTPServer<FTPClient>::BinaryFlag::OFF),
_dataTransferPending(FTPServer<FTPClient>::FTPClientDataTransfer::NONE)
{
}
@ -16,11 +18,12 @@ FTPClient::~FTPClient()
{
delete _cmdParameters;
free(_username);
free(_currentDirectory);
}
void FTPClient::setDataClient(WiFiClient dataClient)
{
_dataClient = _dataClient;
_dataClient = dataClient;
}
boolean FTPClient::parseCommandAndParameters()

View File

@ -23,10 +23,13 @@ class FTPClient : public TCPClient
Dictionary<DictionaryHelper::StringEntity> *_cmdParameters;
boolean _loggedIn;
char *_username;
boolean _dataSocketOpen;
char *_currentDirectory;
boolean _waitingForDataConnection;
FTPServer<FTPClient>::FTPClientState _ftpClientState;
FTPServer<FTPClient>::BinaryFlag _binaryFlag;
FTPServer<FTPClient>::FTPClientDataTransfer _dataTransferPending;
WiFiClient _dataClient; //data socket
};

View File

@ -9,6 +9,7 @@ class FTPServer : public TCPServer<T>
{
public:
enum FTPClientState {WAITING_FOR_COMMANDS};
enum FTPClientDataTransfer {NONE, LIST_DF, NLST_DF};
enum BinaryFlag {OFF = 0, ON};
FTPServer(unsigned int port = 21, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
@ -30,6 +31,11 @@ class FTPServer : public TCPServer<T>
}
}
void setCustomDataPort(unsigned int port)
{
_dataPort = port;
}
virtual ~FTPServer()
{
free(_login);free(_password);
@ -50,7 +56,7 @@ class FTPServer : public TCPServer<T>
virtual void processClientData(T *client)
{
if(client->_dataSocketOpen)
if(client->_waitingForDataConnection)
{
#ifdef DEBUG_FTPS
Serial.println("Listening for new data client");
@ -61,7 +67,7 @@ class FTPServer : public TCPServer<T>
{
if(dataClient.connected())
{
client->_dataSocketOpen = false;
client->_waitingForDataConnection = false;
client->setDataClient(dataClient);
#ifdef DEBUG_FTPS
Serial.println("Data client accepted successfully");
@ -72,6 +78,30 @@ class FTPServer : public TCPServer<T>
}
}
if(client->_dataTransferPending == LIST_DF)//We list the files of the current directory
{
//We check if the dataConnection is established:
if(client->_dataClient.connected())
{
//client->_client.println("125 Data connection already open.");
client->_dataClient.print("-rw-r--r-- 1 owner group 213 Aug 26 16:31 README.txt\r\n\
-rw-r--r-- 1 owner group 213 Aug 26 16:31 music.mp3\r\n\
drw-r--r-- 1 owner group 213 Aug 26 16:31 directory\r\n\
drw-r--r-- 1 owner group 213 Aug 26 16:31 subdir\r\n\
");
client->_client.println("226 Closing data connection.");
client->_dataClient.stop();
client->_dataTransferPending = NONE;
}
//With timeout
/*else
{
client->_client.println("425 Can't open data connection.");
}*/
}
#ifdef DEBUG_FTPS
if(client->_newDataAvailable)
{
@ -181,9 +211,13 @@ class FTPServer : public TCPServer<T>
{
_dataServer.begin(_dataPort);
client->_client.printf("227 Entering Passive Mode (%u,%u,%u,%u,%d,%d).\r\n", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3], _dataPort/256, _dataPort%256);
client->_dataSocketOpen = true;
client->_waitingForDataConnection = true;
}
else if(strcmp(client->_ftpCommand,"LIST") == 0)
{
//We inform that a data transfer is pending
client->_dataTransferPending = LIST_DF;
}
ese if()
else
client->_client.println("502 Command not implemented.");
}

View File

@ -15,7 +15,7 @@ uint16_t lastClientCount(0);
//TCPServer<TCPClient> server(80, MAX_CLIENT, 5);
//WEBServer<WEBClient> webServer(8080);
FTPServer<FTPClient> ftpServer;
FTPServer<FTPClient> ftpServer;//(21,"test","test");
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;