My Project
 All Classes Functions Variables Pages
FTPServer.h
1 /*
2  * FTPServer.h
3  *
4  * Created on: May 6, 2018
5  * Author: kolban
6  */
7 
8 #ifndef NETWORKING_FTPSERVER_FTPSERVER_H_
9 #define NETWORKING_FTPSERVER_FTPSERVER_H_
10 #include <stdint.h>
11 #include <sstream>
12 #include <fstream>
13 #include <string>
14 #include <exception>
15 
16 
17 class FTPCallbacks {
18 public:
19  virtual void onStoreStart(std::string fileName);
20  virtual size_t onStoreData(uint8_t* data, size_t size);
21  virtual void onStoreEnd();
22  virtual void onRetrieveStart(std::string fileName);
23  virtual size_t onRetrieveData(uint8_t *data, size_t size);
24  virtual void onRetrieveEnd();
25  virtual std::string onDir();
26  virtual ~FTPCallbacks();
27 
28 };
29 
34 public:
35  void onStoreStart(std::string fileName) override; // Called for a STOR request.
36  size_t onStoreData(uint8_t* data, size_t size) override; // Called when a chunk of STOR data becomes available.
37  void onStoreEnd() override; // Called at the end of a STOR request.
38  void onRetrieveStart(std::string fileName) override; // Called at the start of a RETR request.
39  size_t onRetrieveData(uint8_t* data, size_t size) override; // Called to retrieve a chunk of RETR data.
40  void onRetrieveEnd() override; // Called when we have retrieved all the data.
41  std::string onDir() override; // Called to retrieve all the directory entries.
42 
43 private:
44  std::ofstream m_storeFile; // File used to store data from the client.
45  std::ifstream m_retrieveFile; // File used to retrieve data for the client.
46  uint32_t m_byteCount; // Count of bytes sent over wire.
47 
48 };
49 
50 
51 class FTPServer {
52 public:
53  FTPServer();
54  virtual ~FTPServer();
55  void setCredentials(std::string userid, std::string password);
56  void start();
57  void setPort(uint16_t port);
58  void setCallbacks(FTPCallbacks* pFTPCallbacks);
59  static std::string getCurrentDirectory();
60  class FileException: public std::exception {
61  };
62 
63  // Response codes.
64  static const int RESPONSE_150_ABOUT_TO_OPEN_DATA_CONNECTION = 150;
65  static const int RESPONSE_200_COMMAND_OK = 200;
66  static const int RESPONSE_202_COMMAND_NOT_IMPLEMENTED = 202;
67  static const int RESPONSE_212_DIRECTORY_STATUS = 212;
68  static const int RESPONSE_213_FILE_STATUS = 213;
69  static const int RESPONSE_214_HELP_MESSAGE = 214;
70  static const int RESPONSE_220_SERVICE_READY = 220;
71  static const int RESPONSE_221_CLOSING_CONTROL_CONNECTION = 221;
72  static const int RESPONSE_230_USER_LOGGED_IN = 230;
73  static const int RESPONSE_226_CLOSING_DATA_CONNECTION = 226;
74  static const int RESPONSE_227_ENTERING_PASSIVE_MODE = 227;
75  static const int RESPONSE_331_PASSWORD_REQUIRED = 331;
76  static const int RESPONSE_332_NEED_ACCOUNT = 332;
77  static const int RESPONSE_500_COMMAND_UNRECOGNIZED = 500;
78  static const int RESPONSE_502_COMMAND_NOT_IMPLEMENTED = 502;
79  static const int RESPONSE_503_BAD_SEQUENCE = 503;
80  static const int RESPONSE_530_NOT_LOGGED_IN = 530;
81  static const int RESPONSE_550_ACTION_NOT_TAKEN = 550;
82  static const int RESPONSE_553_FILE_NAME_NOT_ALLOWED = 553;
83 
84 private:
85  int m_serverSocket; // The socket the FTP server is listening on.
86  int m_clientSocket; // The current client socket.
87  int m_dataSocket; // The data socket.
88  int m_passiveSocket; // The socket on which the server is listening for passive FTP connections.
89  uint16_t m_port; // The port the FTP server will use.
90  uint16_t m_dataPort; // The port for data connections.
91  uint32_t m_dataIp; // The ip address for data connections.
92  bool m_isPassive; // Are we in passive mode? If not, then we are in active mode.
93  bool m_isImage; // Are we in image mode?
94  size_t m_chunkSize; // The maximum chunk size.
95  std::string m_userid; // The required userid.
96  std::string m_password; // The required password.
97  std::string m_suppliedUserid; // The userid supplied from the USER command.
98  bool m_loginRequired; // Do we required a login?
99  bool m_isAuthenticated; // Have we authenticated?
100  std::string m_lastCommand; // The last command that was processed.
101 
102  FTPCallbacks* m_callbacks; // The callbacks for processing.
103 
104  void closeConnection();
105  void closeData();
106  void closePassive();
107  void onAuth(std::istringstream& ss);
108  void onCwd(std::istringstream& ss);
109  void onList(std::istringstream& ss);
110  void onMkd(std::istringstream& ss);
111  void onNoop(std::istringstream& ss);
112  void onPass(std::istringstream& ss);
113  void onPasv(std::istringstream& ss);
114  void onPort(std::istringstream& ss);
115  void onPWD(std::istringstream& ss);
116  void onQuit(std::istringstream& ss);
117  void onRetr(std::istringstream& ss);
118  void onRmd(std::istringstream& ss);
119  void onStor(std::istringstream& ss);
120  void onSyst(std::istringstream& ss);
121  void onType(std::istringstream& ss);
122  void onUser(std::istringstream& ss);
123  void onXmkd(std::istringstream& ss);
124  void onXrmd(std::istringstream& ss);
125 
126  bool openData();
127 
128  void receiveFile(std::string fileName);
129  void sendResponse(int code);
130  void sendResponse(int code, std::string text);
131  void sendData(uint8_t* pData, uint32_t size);
132  std::string listenPassive();
133  int waitForFTPClient();
134  void processCommand();
135 
136 };
137 
138 #endif /* NETWORKING_FTPSERVER_FTPSERVER_H_ */
Definition: FTPServer.h:51
Definition: FTPServer.h:60
Definition: FTPServer.h:33
virtual void onStoreStart(std::string fileName)
-— END OF FTPFileCallbacks
Definition: FTPCallbacks.cpp:106
static std::string getCurrentDirectory()
Definition: FTPServer.cpp:110
void setCallbacks(FTPCallbacks *pFTPCallbacks)
Definition: FTPServer.cpp:728
void onStoreEnd() override
Definition: FTPCallbacks.cpp:37
void onStoreStart(std::string fileName) override
Definition: FTPCallbacks.cpp:12
void onRetrieveEnd() override
Definition: FTPCallbacks.cpp:79
size_t onStoreData(uint8_t *data, size_t size) override
Definition: FTPCallbacks.cpp:25
size_t onRetrieveData(uint8_t *data, size_t size) override
Definition: FTPCallbacks.cpp:66
Definition: FTPServer.h:17
void onRetrieveStart(std::string fileName) override
Definition: FTPCallbacks.cpp:47
std::string onDir() override
Definition: FTPCallbacks.cpp:90
void setPort(uint16_t port)
Definition: FTPServer.cpp:745
void start()
Definition: FTPServer.cpp:753