FTPServer in progress

This commit is contained in:
anschrammh 2019-10-13 11:23:59 +02:00
parent 0a924b6848
commit bd23b4a3b2
5 changed files with 69 additions and 31 deletions

View File

@ -5,6 +5,7 @@ _ftpCommand({'\0'}),
_cmdParameters(NULL), _cmdParameters(NULL),
_loggedIn(false), _loggedIn(false),
_username(NULL), _username(NULL),
_dataSocketOpen(false),
_ftpClientState(FTPServer<FTPClient>::FTPClientState::WAITING_FOR_COMMANDS), _ftpClientState(FTPServer<FTPClient>::FTPClientState::WAITING_FOR_COMMANDS),
_binaryFlag(FTPServer<FTPClient>::BinaryFlag::OFF) _binaryFlag(FTPServer<FTPClient>::BinaryFlag::OFF)
{ {

View File

@ -23,6 +23,7 @@ class FTPClient : public TCPClient
Dictionary<DictionaryHelper::StringEntity> *_cmdParameters; Dictionary<DictionaryHelper::StringEntity> *_cmdParameters;
boolean _loggedIn; boolean _loggedIn;
char *_username; char *_username;
boolean _dataSocketOpen;
FTPServer<FTPClient>::FTPClientState _ftpClientState; FTPServer<FTPClient>::FTPClientState _ftpClientState;
FTPServer<FTPClient>::BinaryFlag _binaryFlag; FTPServer<FTPClient>::BinaryFlag _binaryFlag;

View File

@ -14,7 +14,8 @@ class FTPServer : public TCPServer<T>
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), 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),
_login(NULL), _login(NULL),
_password(NULL), _password(NULL),
_dataServer(1024) _dataPort(1024),
_dataServer(_dataPort)
{ {
if(login != NULL) if(login != NULL)
{ {
@ -49,6 +50,27 @@ class FTPServer : public TCPServer<T>
virtual void processClientData(T *client) virtual void processClientData(T *client)
{ {
if(client->_dataSocketOpen)
{
#ifdef DEBUG_FTPS
Serial.println("Listening for new data client");
#endif
WiFiClient dataClient = _dataServer.available();
if(dataClient)
{
if(dataClient.connected())
{
client->_dataSocketOpen = false;
client->setDataClient(dataClient);
#ifdef DEBUG_FTPS
Serial.println("Data client accepted successfully");
#endif
}
else
dataClient.stop();
}
}
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
if(client->_newDataAvailable) if(client->_newDataAvailable)
@ -56,12 +78,15 @@ class FTPServer : public TCPServer<T>
Serial.print("Client --> ");Serial.print(client->_id);Serial.print(" : ");Serial.print((char *)client->_data);Serial.println("#"); Serial.print("Client --> ");Serial.print(client->_id);Serial.print(" : ");Serial.print((char *)client->_data);Serial.println("#");
} }
#endif #endif
switch(client->_ftpClientState) if(client->_dataSize > 0)
{ {
case WAITING_FOR_COMMANDS: switch(client->_ftpClientState)
processCommands(client); {
break; case WAITING_FOR_COMMANDS:
processCommands(client);
break;
}
} }
} }
@ -154,15 +179,18 @@ class FTPServer : public TCPServer<T>
} }
else if(strcmp(client->_ftpCommand,"PASV") == 0) else if(strcmp(client->_ftpCommand,"PASV") == 0)
{ {
_dataServer.begin(); _dataServer.begin(_dataPort);
client->_client.printf("227 Entering Passive Mode (%d,%d,%d,%d,%d,%d).\r\n", 192,168,0,29,1024/256, 1024%256); 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;
} }
ese if()
else else
client->_client.println("502 Command not implemented."); client->_client.println("502 Command not implemented.");
} }
char *_login; char *_login;
char *_password; char *_password;
unsigned int _dataPort;
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream) WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)
}; };

View File

@ -6,7 +6,7 @@
#include "TCPClient.h" #include "TCPClient.h"
#define MAX_CLIENT -1 #define MAX_CLIENT -1
#define DEBUG #define DEBUG_TCPS
template <typename T> template <typename T>
@ -78,14 +78,19 @@ class TCPServer
wc = _wifiServer.available(); wc = _wifiServer.available();
} }
if(wc && wc.connected()) if(wc)
{ {
T *clientPointer = createNewClient(wc); if(wc.connected())
_clientList.addFirst(clientPointer); {
#ifdef DEBUG T *clientPointer = createNewClient(wc);
Serial.print("TCPServer : New client accepted : ");Serial.println(clientPointer->_id); _clientList.addFirst(clientPointer);
#endif #ifdef DEBUG_TCPS
greetClient(clientPointer); Serial.print("TCPServer : New client accepted : ");Serial.println(clientPointer->_id);
#endif
greetClient(clientPointer);
}
else
wc.stop();
} }
_currentClient = _clientList.removeLastRef();//We pick a client in the list to process it's request _currentClient = _clientList.removeLastRef();//We pick a client in the list to process it's request
@ -132,7 +137,7 @@ class TCPServer
} }
else if(!(_currentClient->_client).connected()) else if(!(_currentClient->_client).connected())
{ {
#ifdef DEBUG #ifdef DEBUG_TCPS
Serial.print("TCPServer : Client disconnected and can be discarded : ");Serial.println(_currentClient->_id); Serial.print("TCPServer : Client disconnected and can be discarded : ");Serial.println(_currentClient->_id);
#endif #endif
_currentClient->_clientState = TCPClient::DISCARDED; _currentClient->_clientState = TCPClient::DISCARDED;
@ -140,14 +145,14 @@ class TCPServer
if(_currentClient->_dataSize > 0) if(_currentClient->_dataSize > 0)
{ {
processClientData(_currentClient);//We process the actual data
_currentClient->_newDataAvailable = false; _currentClient->_newDataAvailable = false;
} }
processClientData(_currentClient);//We process the actual data
if(_currentClient->_clientState == TCPClient::ClientState::DISCARDED) if(_currentClient->_clientState == TCPClient::ClientState::DISCARDED)
{ {
_currentClient->closeConnection(); _currentClient->closeConnection();
#ifdef DEBUG #ifdef DEBUG_TCPS
Serial.print("TCPServer : Client was discarded : ");Serial.println(_currentClient->_id); Serial.print("TCPServer : Client was discarded : ");Serial.println(_currentClient->_id);
#endif #endif
delete _currentClient; delete _currentClient;

View File

@ -63,19 +63,22 @@ class WEBServer : public TCPServer<T>
virtual void processClientData(T *client) virtual void processClientData(T *client)
{ {
switch(client->_WEBClientState) if(client->_dataSize > 0)
{ {
case ACCEPTED: switch(client->_WEBClientState)
queryParser(client); {
break; case ACCEPTED:
case QUERY_PARSED: queryParser(client);
sendDataToClient(client); break;
break; case QUERY_PARSED:
case RESPONSE_SENT: sendDataToClient(client);
break; break;
case DONE: case RESPONSE_SENT:
break; break;
} case DONE:
break;
}
}
} }
void queryParser(T *client) void queryParser(T *client)