|
6 | 6 | |
7 | 7 | FTP server reads and executes FTP commands. The transfer of files in active in passive mode is supported but some of the commands may |
8 | 8 |
|
9 | | - October 23, 2023, Bojan Jurca |
| 9 | + December 25, 2023, Bojan Jurca |
10 | 10 |
|
11 | 11 | Nomenclature used here for easier understaning of the code: |
12 | 12 |
|
|
90 | 90 | // control ftpServer critical sections |
91 | 91 | static SemaphoreHandle_t __ftpServerSemaphore__ = xSemaphoreCreateMutex (); |
92 | 92 |
|
| 93 | + // log what is going on within telnetServer |
| 94 | + byte ftpServerConcurentTasks = 0; |
| 95 | + byte ftpServerConcurentTasksHighWatermark = 0; |
| 96 | + |
| 97 | + |
93 | 98 | // cycle through set of port numbers when FTP server is working in pasive mode |
94 | 99 | int __pasiveDataPort__ () { |
95 | 100 | static int lastPasiveDataPort = 1024; // change pasive data port range if needed |
|
132 | 137 | if (pdPASS != taskCreated) { |
133 | 138 | dmesg ("[ftpControlConnection] xTaskCreate error"); |
134 | 139 | } else { |
135 | | - __state__ = RUNNING; |
136 | | - } |
| 140 | + __state__ = RUNNING; |
137 | 141 |
|
| 142 | + xSemaphoreTake (__ftpServerSemaphore__, portMAX_DELAY); |
| 143 | + ftpServerConcurentTasks++; |
| 144 | + if (ftpServerConcurentTasks > ftpServerConcurentTasksHighWatermark) |
| 145 | + ftpServerConcurentTasksHighWatermark = ftpServerConcurentTasks; |
| 146 | + xSemaphoreGive (__ftpServerSemaphore__); |
| 147 | + |
| 148 | + return; // success |
| 149 | + } |
| 150 | + dmesg ("[ftpConnection] xTaskCreate error"); // failure |
138 | 151 | } |
139 | 152 |
|
140 | 153 | ~ftpControlConnection () { |
| 154 | + xSemaphoreTake (__ftpServerSemaphore__, portMAX_DELAY); |
| 155 | + ftpServerConcurentTasks--; |
| 156 | + xSemaphoreGive (__ftpServerSemaphore__); |
| 157 | + |
141 | 158 | closeControlConnection (); |
142 | 159 | closeDataConnection (); |
143 | 160 | } |
|
182 | 199 |
|
183 | 200 | // writing output to FTP control connection with error logging (dmesg) |
184 | 201 | int sendFtp (char *buf) { |
185 | | - int i = sendAll (__controlConnectionSocket__, buf, strlen (buf), FTP_CONTROL_CONNECTION_TIME_OUT); |
| 202 | + int i = sendAll (__controlConnectionSocket__, buf, FTP_CONTROL_CONNECTION_TIME_OUT); |
186 | 203 | if (i <= 0) |
187 | 204 | dmesg ("[ftpControlConnection] send error: ", errno, strerror (errno)); |
188 | 205 | return i; |
|
716 | 733 | STATE_TYPE state () { return __state__; } |
717 | 734 |
|
718 | 735 | ftpServer ( // the following parameters will be handeled by ftpServer instance |
719 | | - const char *serverIP, // FTP server IP address, 0.0.0.0 for all available IP addresses |
720 | | - int serverPort, // FTP server port |
721 | | - bool (*firewallCallback) (char *connectingIP) // a reference to callback function that will be celled when new connection arrives |
| 736 | + const char *serverIP = "0.0.0.0", // FTP server IP address, 0.0.0.0 for all available IP addresses |
| 737 | + int serverPort = 21, // FTP server port |
| 738 | + bool (*firewallCallback) (char *connectingIP) = NULL // a reference to callback function that will be celled when new connection arrives |
722 | 739 | ) { |
723 | 740 | // create a local copy of parameters for later use |
724 | 741 | strncpy (__serverIP__, serverIP, sizeof (__serverIP__)); __serverIP__ [sizeof (__serverIP__) - 1] = 0; |
|
834 | 851 | close (connectingSocket); // normally ftpControlConnection would do this but if it is not created we have to do it here |
835 | 852 | } else { |
836 | 853 | if (fcn->state () != ftpControlConnection::RUNNING) { |
837 | | - sendAll (connectingSocket, ftpServiceUnavailable, strlen (ftpServiceUnavailable), FTP_CONTROL_CONNECTION_TIME_OUT); |
| 854 | + sendAll (connectingSocket, ftpServiceUnavailable, FTP_CONTROL_CONNECTION_TIME_OUT); |
838 | 855 | delete (fcn); // normally ftpControlConnection would do this but if it is not running we have to do it here |
839 | 856 | } |
840 | 857 | } |
|
0 commit comments