-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathftp_client_session.hpp
1169 lines (1092 loc) · 43.9 KB
/
ftp_client_session.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <atomic>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <dirent.h>
#include <sys/types.h>
#if defined(ESP_PLATFORM)
#include "esp_random.h"
#else
#include <random>
#endif
#include "logger.hpp"
#include "task.hpp"
#include "tcp_socket.hpp"
/// Function to convert a time_point to a time_t.
/// \details This function converts a time_point to a time_t. This function
/// is needed because the standard library does not provide a function to
/// convert a time_point to a time_t (until c++20 but support seems lacking
/// on esp32). This function is taken from
/// https://stackoverflow.com/a/61067330
/// \tparam TP The type of the time_point.
/// \param tp The time_point to convert.
/// \return The time_t.
template <typename TP> std::time_t to_time_t(TP tp) {
using namespace std::chrono;
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
return system_clock::to_time_t(sctp);
}
namespace espp {
/// Class representing a client that is connected to the FTP server. This
/// class is used by the FtpServer class to handle the client's requests.
class FtpClientSession {
public:
explicit FtpClientSession(int id, std::string_view local_address,
std::unique_ptr<TcpSocket> socket,
const std::filesystem::path &root_path)
: id_(id), local_ip_address_(local_address), current_directory_(root_path),
socket_(std::move(socket)), passive_socket_({.log_level = Logger::Verbosity::WARN}),
logger_(
{.tag = "FtpClientSession " + std::to_string(id), .level = Logger::Verbosity::WARN}) {
logger_.debug("Client session {} created", id_);
send_welcome_message();
using namespace std::placeholders;
task_ = std::make_unique<Task>(Task::Config{
.name = "FtpClientSession",
.callback = std::bind(&FtpClientSession::task_function, this, _1, _2),
.stack_size_bytes = 1024 * 6,
.log_level = Logger::Verbosity::WARN,
});
task_->start();
}
~FtpClientSession() {
logger_.debug("Client session {} destroyed", id_);
task_->stop();
}
/// \brief Get the id of the client session.
/// \return The id of the client session.
int id() const { return id_; }
/// \brief Get the current directory of the client session.
/// \return The current directory of the client session.
std::filesystem::path current_directory() const { return current_directory_; }
/// \brief Check if the client session has a valid control connection.
/// \details This function checks if the client session has a valid control
/// connection. A control connection is valid if the control socket is
/// valid and connected.
/// \return True if the control connection is valid, false otherwise.
bool is_connected() const { return socket_ && socket_->is_connected(); }
/// \brief Check if the client is using a passive data connection.
/// \details This function checks if the client is using a passive data
/// connection. A client is using a passive data connection if the
/// client has sent a PASV command and the session was able to create
/// a passive socket.
/// \return True if the client is using a passive data connection, false
/// otherwise.
bool is_passive_data_connection() const { return is_passive_data_connection_; }
/// \brief Check if the client session is alive.
/// \details This function checks if the client session is alive. A client
/// session is alive if the task is running.
/// \return True if the client session is alive, false otherwise.
bool is_alive() const { return task_ && task_->is_started(); }
protected:
/// \brief Function which handles requests from the client.
/// \details This function is called by the task to handle requests from
/// the client. This function is called in a loop until it returns
/// true, which indicates that the task should stop.
/// \param m The mutex to use for waiting on the condition variable.
/// \param cv The condition variable to use for waiting.
/// \return True if the task should stop, false otherwise.
bool task_function(std::mutex &m, std::condition_variable &cv) {
{
// delay here
using namespace std::chrono_literals;
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, 1ms);
}
if (!socket_) {
logger_.error("Socket is null, stopping the task");
// stop the task
return true;
}
if (!socket_->is_connected()) {
socket_.reset();
logger_.error("Socket is not connected, stopping the task");
// stop the task
return true;
}
static constexpr int max_request_size = 1024;
std::vector<uint8_t> request_data;
if (!socket_->receive(request_data, max_request_size)) {
// didn't receive anything, the client may have not sent anything yet
// or may have disconnected. If it disconnected, the socket will be
// closed and is_connected() will return false, so we'll handle that
// case in the next iteration.
return false;
}
if (request_data.size() == 0) {
logger_.debug("Received empty request, ignoring");
// don't want to stop the task
return false;
}
logger_.info("Received request of size {}", request_data.size());
std::string_view request((const char *)request_data.data(), request_data.size());
if (!handle_request(request)) {
logger_.error("Failed to handle request");
return false;
}
// don't want to stop the task
return false;
}
/// \brief Send a response to the client.
/// \details This function sends a response to the client. This function
/// uses the control socket and not the data socket.
/// \param status_code The status code of the response.
/// \param message The message of the response.
/// \param multiline Whether or not the response is multiline.
/// \return True if the response was sent successfully, false otherwise.
bool send_response(int status_code, std::string_view message, bool multiline = false) {
std::string response =
std::to_string(status_code) + (multiline ? "-" : " ") + std::string{message} + "\r\n";
detail::TcpTransmitConfig config{}; // default config, no wait for response.
if (!socket_->transmit(response, config)) {
logger_.error("Failed to send response");
return false;
}
return true;
}
/// \brief Receive data from the client.
/// \details This function receives data from the client and stores it in
/// the given buffer. This function uses the data socket and not the
/// control socket, and handles both active and passive mode.
/// \param buffer The buffer to store the data in.
/// \param size The size of the buffer.
/// \return The number of bytes received.
std::optional<std::vector<uint8_t>> receive_data() {
if (is_passive_data_connection_) {
if (!passive_socket_.is_valid()) {
logger_.error("Passive socket is invalid");
return {};
}
// accept the connection
data_socket_ = passive_socket_.accept();
if (!data_socket_) {
logger_.error("Failed to accept data connection");
return {};
}
if (!data_socket_->is_valid()) {
logger_.error("Failed to accept data connection");
return {};
}
} else {
// connect to the client
if (!data_socket_->connect({.ip_address = data_ip_address_, .port = data_port_})) {
logger_.error("Failed to connect to client");
return {};
}
}
// receive the data
std::vector<uint8_t> data;
while (true) {
std::vector<uint8_t> buffer(1024);
std::size_t received = data_socket_->receive(buffer.data(), buffer.size());
if (received == 0) {
break;
}
data.insert(data.end(), buffer.begin(), buffer.begin() + received);
}
data_socket_->close();
data_socket_.reset();
return data;
}
/// \brief Send data to the client.
/// \details This function sends data to the client. This function uses the
/// data socket and not the control socket, and handles both active and
/// passive mode.
/// \param data The data to send.
/// \return True if the data was sent successfully, false otherwise.
bool send_data(std::string_view data) {
if (is_passive_data_connection_) {
if (!passive_socket_.is_valid()) {
logger_.error("Passive socket is invalid");
return false;
}
// accept the connection
data_socket_ = passive_socket_.accept();
if (!data_socket_) {
logger_.error("Failed to accept data connection");
return false;
}
if (!data_socket_->is_valid()) {
logger_.error("Failed to accept data connection");
return false;
}
} else {
// connect to the client
if (!data_socket_->connect({.ip_address = data_ip_address_, .port = data_port_})) {
logger_.error("Failed to connect to client");
return false;
}
}
// send the data
detail::TcpTransmitConfig config{};
bool success = data_socket_->transmit(data, config);
// close the data socket
data_socket_->close();
data_socket_.reset();
return success;
}
/// \brief Receive a file from the client.
/// \details This function receives a file from the client. This function
/// uses the data socket and not the control socket, and handles both
/// active and passive mode.
/// \note This function has to be used instaed of receive_data() because
/// receive_data() needs the whole data in memory, which is not possible
/// for large files.
/// \param file_path The path to the file to store the data in.
/// \return True if the file was received successfully, false otherwise.
bool receive_file(std::filesystem::path &file_path) {
if (is_passive_data_connection_) {
if (!passive_socket_.is_valid()) {
logger_.error("Passive socket is invalid");
return false;
}
// accept the connection
data_socket_ = passive_socket_.accept();
if (!data_socket_) {
logger_.error("Failed to accept data connection");
return false;
}
if (!data_socket_->is_valid()) {
logger_.error("Failed to accept data connection");
return false;
}
} else {
// connect to the client
if (!data_socket_->connect({.ip_address = data_ip_address_, .port = data_port_})) {
logger_.error("Failed to connect to client");
return false;
}
}
// open the file
std::ofstream file(file_path, std::ios::binary);
if (!file.is_open()) {
logger_.error("Failed to open file");
return false;
}
auto start = std::chrono::high_resolution_clock::now();
size_t total_size = 0;
// receive the data
while (true) {
std::vector<uint8_t> buffer(1024);
std::size_t received = data_socket_->receive(buffer.data(), buffer.size());
if (received == 0) {
break;
}
total_size += received;
// write it to the filen
logger_.debug("Writing {} bytes", received);
file.write(reinterpret_cast<char *>(buffer.data()), received);
}
auto end = std::chrono::high_resolution_clock::now();
float elapsed = std::chrono::duration<float>(end - start).count();
logger_.info("Received {} bytes in {:.2f} seconds ({:.2f} bytes/s)", total_size, elapsed,
total_size / elapsed);
file.close();
file.flush();
data_socket_->close();
data_socket_.reset();
return true;
}
/// \brief Send a file to the client.
/// \details This function sends a file to the client. This function uses
/// the data socket and not the control socket, and handles both active
/// and passive mode.
/// \note This function has to be used instaed of send_data() because
/// send_data() needs the whole data in memory, which is not possible
/// for large files.
/// \param file_path The path to the file to send.
/// \return True if the file was sent successfully, false otherwise.
bool send_file(std::filesystem::path &file_path) {
if (is_passive_data_connection_) {
if (!passive_socket_.is_valid()) {
logger_.error("Passive socket is invalid");
return false;
}
// accept the connection
data_socket_ = passive_socket_.accept();
if (!data_socket_) {
logger_.error("Failed to accept data connection");
return false;
}
if (!data_socket_->is_valid()) {
logger_.error("Failed to accept data connection");
return false;
}
} else {
// connect to the client
if (!data_socket_->connect({.ip_address = data_ip_address_, .port = data_port_})) {
logger_.error("Failed to connect to client");
return false;
}
}
detail::TcpTransmitConfig config{};
// open the file
std::ifstream file(file_path, std::ios::in | std::ios::binary);
if (!file.is_open()) {
logger_.error("Failed to open file");
return false;
}
// get the file size
file.seekg(0, std::ios::end);
std::size_t file_size = file.tellg();
logger_.debug("File size: {}", file_size);
file.seekg(0, std::ios::beg);
// send the file
auto start = std::chrono::high_resolution_clock::now();
size_t total_size = 0;
static constexpr std::size_t buffer_size = 1024;
std::unique_ptr<char[]> buffer(new char[buffer_size]);
while (true) {
file.read(buffer.get(), buffer_size);
std::size_t bytes_read = file.gcount();
if (bytes_read == 0) {
break;
}
std::string_view data(buffer.get(), bytes_read);
if (!data_socket_->transmit(data, config)) {
logger_.error("Failed to send file");
return false;
}
total_size += bytes_read;
}
file.close();
auto end = std::chrono::high_resolution_clock::now();
float elapsed = std::chrono::duration<float>(end - start).count();
logger_.info("Sent {} bytes in {:.2f} seconds ({:.2f} bytes/s)", total_size, elapsed,
total_size / elapsed);
// close the data socket
data_socket_->close();
data_socket_.reset();
return true;
}
bool parse_ftp_command(std::string_view request, std::string_view &command,
std::string_view &arguments) {
// parses the command from the FTP client's request. The command is the
// first word in the request. The command is case insensitive.
// The command is followed by a space and then the arguments.
// The arguments are separated by spaces.
// The command and arguments are all ASCII strings.
if (request.empty()) {
return false;
}
auto command_end = request.find(' ');
// if there is a space, then there are arguments
if (command_end != std::string_view::npos) {
// the command is the first word
command = request.substr(0, command_end);
// the arguments are the rest of the request
arguments = request.substr(command_end + 1);
return true;
}
// if there is no space, then there are no arguments
// no arguments, strip the \r\n from the end
auto request_end = request.find("\r\n");
if (request_end == std::string_view::npos) {
return false;
}
// the command is the first word
command = request.substr(0, request_end);
// there are no arguments
arguments = "";
return true;
}
/// \brief Handle a request.
/// \param request The request to handle.
/// \return True if the request was handled, false otherwise.
bool handle_request(std::string_view request) {
logger_.info("Handling request: {}", request);
std::string_view command;
std::string_view arguments;
if (!parse_ftp_command(request, command, arguments)) {
logger_.error("Failed to parse FTP command");
return false;
}
if (command == "USER") {
return handle_user(arguments);
}
if (command == "PASS") {
return handle_pass(arguments);
}
if (command == "SYST") {
return handle_syst(arguments);
}
if (command == "FEAT") {
return handle_feat(arguments);
}
if (command == "PWD") {
return handle_pwd(arguments);
}
if (command == "CWD") {
return handle_cwd(arguments);
}
if (command == "CDUP") {
return handle_cdup(arguments);
}
if (command == "TYPE") {
return handle_type(arguments);
}
if (command == "PASV") {
return handle_pasv(arguments);
}
if (command == "PORT") {
return handle_port(arguments);
}
if (command == "LIST") {
return handle_list(arguments);
}
if (command == "SIZE") {
return handle_size(arguments);
}
if (command == "RETR") {
return handle_retr(arguments);
}
if (command == "STOR") {
return handle_stor(arguments);
}
if (command == "DELE") {
return handle_dele(arguments);
}
if (command == "MKD") {
return handle_mkd(arguments);
}
if (command == "RMD") {
return handle_rmd(arguments);
}
if (command == "RNFR") {
return handle_rnfr(arguments);
}
if (command == "RNTO") {
return handle_rnto(arguments);
}
if (command == "NOOP") {
return handle_noop(arguments);
}
if (command == "QUIT") {
return handle_quit(arguments);
}
return handle_unknown(request);
}
/// \brief Send a welcome message to the client.
/// \details This function sends a welcome message to the client.
/// \return True if the welcome message was sent successfully, false
bool send_welcome_message() { return send_response(220, "Welcome to espp FTP server"); }
/// \brief Handle the USER command.
/// The USER command is used to specify the user name (USER is a
/// synonym for the USER NAME command). The argument field is a
/// Telnet string identifying the user.
/// \param arguments The arguments to the USER command.
/// \return True if the command was handled, false otherwise.
bool handle_user(std::string_view arguments) {
logger_.info("Handling user: {}", arguments);
// parse the username
auto username_end = arguments.find("\r\n");
if (username_end == std::string_view::npos) {
logger_.error("Failed to parse username");
return false;
}
username_ = arguments.substr(0, username_end);
logger_.info("Username: {}", username_);
return send_response(331, "User name okay, need password.");
}
/// \brief Handle the PASS command.
/// The PASS command is used to specify the user's password. The
/// argument field is a Telnet string specifying the user's
/// password. This command must be immediately preceded by the
/// user name command, and, for some sites, completes the user's
/// identification for access control.
/// \param arguments The arguments to the PASS command.
/// \return True if the command was handled, false otherwise.
bool handle_pass(std::string_view arguments) {
logger_.info("Handling pass: {}", arguments);
// parse the password
auto password_end = arguments.find("\r\n");
if (password_end == std::string_view::npos) {
logger_.error("Failed to parse password");
return false;
}
password_ = arguments.substr(0, password_end);
/*
// TODO: check the username and password
if (username_ == "anonymous" && password_ == "anonymous") {
return send_response(230, "User logged in, proceed.");
} else {
return send_response(530, "Incorrect credentials.");
}
*/
return send_response(230, "User logged in, proceed.");
}
/// \brief Handle the SYST command.
/// \details The SYST command prints the system type.
/// \param arguments The arguments to the SYST command.
/// \return True if the command was handled, false otherwise.
bool handle_syst(std::string_view arguments) {
logger_.info("Handling syst: {}", arguments);
// TODO: print the system type
return send_response(215, "UNIX Type: L8");
}
/// \brief Handle the FEAT command.
/// \details The FEAT command lists the features supported by the server.
/// \param arguments The arguments to the FEAT command.
/// \return True if the command was handled, false otherwise.
bool handle_feat(std::string_view arguments) {
logger_.info("Handling feat: {}", arguments);
// format the message containing the features
std::string message = "Features:\r\n";
message += " USER\r\n";
message += " PASS\r\n";
message += " SYST\r\n";
message += " FEAT\r\n";
message += " PWD\r\n";
message += " CWD\r\n";
message += " CDUP\r\n";
message += " TYPE\r\n";
message += " PASV\r\n";
message += " PORT\r\n";
message += " LIST\r\n";
message += " SIZE\r\n";
message += " RETR\r\n";
message += " STOR\r\n";
message += " DELE\r\n";
message += " MKD\r\n";
message += " RMD\r\n";
message += " RNFR\r\n";
message += " RNTO\r\n";
message += " NOOP\r\n";
message += " QUIT\r\n";
bool success = send_response(211, message, true);
return success && send_response(211, "End");
}
/// \brief Handle the PWD command.
/// \details The PWD command prints the current directory.
/// \param arguments The arguments to the PWD command.
/// \return True if the command was handled, false otherwise.
bool handle_pwd(std::string_view arguments) {
logger_.info("Handling pwd: {}", arguments);
// make the message containing the current directory
std::string message =
std::string{"\""} + current_directory_.string() + "\" is the current directory";
return send_response(257, message);
}
/// \brief Handle the CWD command.
/// \details The CWD command changes the current directory.
/// \param arguments The arguments to the CWD command.
/// \return True if the command was handled, false otherwise.
bool handle_cwd(std::string_view arguments) {
logger_.info("Handling cwd: {}", arguments);
// parse the directory
auto directory_end = arguments.find("\r\n");
if (directory_end == std::string_view::npos) {
logger_.error("Failed to parse directory");
return false;
}
std::string_view directory = arguments.substr(0, directory_end);
// change the current directory
std::filesystem::path new_directory(directory);
if (new_directory.is_relative()) {
logger_.debug("Relative path");
new_directory = current_directory_ / new_directory;
}
if (!std::filesystem::exists(new_directory)) {
return send_response(550, "Directory does not exist.");
}
if (!std::filesystem::is_directory(new_directory)) {
return send_response(550, "Not a directory.");
}
current_directory_ = new_directory;
logger_.debug("Current directory: {}", current_directory_.string());
return send_response(250, "Directory successfully changed.");
}
/// \brief Handle the CDUP command.
/// \details The CDUP command changes the current directory to the parent
/// directory.
/// \param arguments The arguments to the CDUP command.
/// \return True if the command was handled, false otherwise.
bool handle_cdup(std::string_view arguments) {
logger_.info("Handling cdup: {}", arguments);
// change the current directory
current_directory_ = current_directory_.parent_path();
logger_.debug("Current directory: {}", current_directory_.string());
return send_response(250, "Directory successfully changed.");
}
/// \brief Handle the TYPE command.
/// \details The TYPE command specifies the representation type as described
/// in the Section on Data Representation and Storage. Several types
/// take a second parameter. The first parameter is denoted by a single
/// Telnet character, as is the second Format parameter for ASCII and
/// EBCDIC; the second parameter for local byte is a decimal integer to
/// indicate Bytesize. The parameters are separated by a <SP> (Space,
/// ASCII code 32).
/// \param arguments The arguments to the TYPE command.
/// \return True if the command was handled, false otherwise.
bool handle_type(std::string_view arguments) {
logger_.info("Handling type: {}", arguments);
// parse the type
auto type_end = arguments.find("\r\n");
if (type_end == std::string_view::npos) {
logger_.error("Failed to parse type");
return false;
}
std::string_view type = arguments.substr(0, type_end);
// handle the ASCII (A) type
if (type == "A") {
return send_response(200, "Switching to ASCII mode.");
}
// handle the binary (I) type
if (type == "I") {
return send_response(200, "Switching to Binary mode.");
}
// handle the EBCDIC (E) type
if (type == "E") {
return send_response(200, "Switching to EBCDIC mode.");
}
return send_response(504, "Command not implemented for that parameter.");
}
/// \brief Handle the PASV command.
/// \details The PASV command requests the server to "listen" on a data
/// port (which is not its default data port) and to wait for a
/// connection rather than initiate one upon receipt of a transfer
/// command. The response to this command includes the host and port
/// address this server is listening on.
/// \param arguments The arguments to the PASV command.
/// \return True if the command was handled, false otherwise.
bool handle_pasv(std::string_view arguments) {
logger_.info("Handling pasv: {}", arguments);
#if defined(ESP_PLATFORM)
int port = esp_random() % 10000 + 1024;
#else
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, std::numeric_limits<int>::max());
int port = dis(gen) % 10000 + 1024;
#endif
// randomly select a port number
logger_.debug("Selected port: {}", port);
// ensure that the socket is closed and ready to be used again
passive_socket_.reinit();
// ensure the data connection is closed
data_socket_.reset();
// create the data connection socket and listen on it
if (!passive_socket_.bind(port)) {
logger_.error("Failed to bind data socket");
return send_response(425, "Failed to create data connection.");
}
int max_pending_connections = 1;
if (!passive_socket_.listen(max_pending_connections)) {
logger_.error("Failed to listen on data socket");
return send_response(425, "Failed to create data connection.");
}
// replace the periods with commas
std::string ip_address = local_ip_address_;
std::replace(ip_address.begin(), ip_address.end(), '.', ',');
// format the response message with the address and port
auto message =
fmt::format("Entering Passive Mode ({},{},{})", ip_address, port / 256, port % 256);
logger_.debug("Response message: {}", message);
// set the data connection mode to passive
is_passive_data_connection_ = true;
// send the response
return send_response(227, message);
}
/// \brief Handle the PORT command.
/// \details The PORT command is used to specify the address and port to
/// which the server should connect for the next data transfer.
/// \param arguments The arguments to the PORT command.
/// \return True if the command was handled, false otherwise.
bool handle_port(std::string_view arguments) {
logger_.info("Handling port: {}", arguments);
// check if the arguments are valid
std::array<std::string_view, 6> parts;
auto part = arguments.begin();
for (int i = 0; i < 5; ++i) {
auto comma = std::find(part, arguments.end(), ',');
if (comma == arguments.end()) {
logger_.error("Failed to parse port");
return send_response(501, "Syntax error in parameters or arguments.");
}
parts[i] = std::string_view(&*part, std::distance(part, comma));
part = comma + 1;
}
parts[5] = std::string_view(&*part, std::distance(part, arguments.end()));
// parse the IP address
data_ip_address_.clear();
for (int i = 0; i < 4; ++i) {
data_ip_address_ += parts[i];
if (i < 3) {
data_ip_address_ += '.';
}
}
// parse the port
data_port_ = std::stoi(std::string{parts[4]}) * 256 + std::stoi(std::string{parts[5]});
logger_.info("IP address: {}", data_ip_address_);
logger_.info("Port: {}", data_port_);
// create the data socket and connect to it
data_socket_.reset();
data_socket_ =
std::make_unique<TcpSocket>(TcpSocket::Config{.log_level = Logger::Verbosity::WARN});
is_passive_data_connection_ = false;
return send_response(200, "PORT command successful.");
}
/// @brief Handle the LIST command
/// The LIST command lists the contents of the current directory.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_list(std::string_view arguments) {
logger_.info("Handling list: {}", arguments);
if (!send_response(150, "File status okay; about to open data connection.")) {
logger_.error("Failed to send response");
return false;
}
// get the directory listing
std::string directory_listing = list_directory(current_directory_);
logger_.debug("Directory listing:\n{}", directory_listing);
if (!send_data(directory_listing)) {
logger_.error("Failed to send directory listing");
return send_response(426, "Connection closed; transfer aborted.");
}
return send_response(226, "Closing data connection. Requested file action successful.");
}
/// @brief Handle the SIZE command
/// The SIZE command returns the size of a file.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_size(std::string_view arguments) {
logger_.info("Handling size: {}", arguments);
// get the path of the file to retrieve
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (!std::filesystem::exists(full_path)) {
return send_response(550, "File does not exist.");
}
if (!std::filesystem::is_regular_file(full_path)) {
return send_response(550, "Not a regular file.");
}
// get the size of the file
std::error_code error;
auto size = std::filesystem::file_size(full_path.string().data(), error);
if (error) {
logger_.error("Failed to get file size: {}", error.message());
return send_response(550, "Failed to get file size.");
}
// send the response
return send_response(213, std::to_string(size));
}
/// @brief Handle the RETR command
/// The RETR command retrieves a file from the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_retr(std::string_view arguments) {
logger_.info("Handling retr: {}", arguments);
// get the path of the file to retrieve
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (!std::filesystem::exists(full_path)) {
return send_response(550, "File does not exist.");
}
if (!std::filesystem::is_regular_file(full_path)) {
return send_response(550, "Not a regular file.");
}
if (!send_response(150, "File status okay; about to open data connection.")) {
logger_.error("Failed to send response");
return false;
}
// send the file over the data connection
if (!send_file(full_path)) {
logger_.error("Failed to send file");
return send_response(426, "Connection closed; transfer aborted.");
}
return send_response(226, "Closing data connection. Requested file action successful.");
}
/// @brief Handle the STOR command
/// The STOR command stores a file on the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_stor(std::string_view arguments) {
logger_.info("Handling stor: {}", arguments);
// get the path of the file to store
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
// NOTE: we don't check if the file exists, because we want to overwrite it
if (!send_response(150, "File status okay; about to open data connection.")) {
logger_.error("Failed to send response");
return false;
}
// receive the file over the data connection
if (!receive_file(full_path)) {
logger_.error("Failed to receive file");
return send_response(426, "Connection closed; transfer aborted.");
}
// and send the response
return send_response(226, "Closing data connection. Requested file action successful.");
}
/// @brief Handle the QUIT command
/// The QUIT command closes the connection.
/// @note After sending the response, the control connection is closed and the
/// control connection socket is cleaned up. Any further commands will
/// not be handled and the client will have to reconnect.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_quit(std::string_view arguments) {
logger_.info("Handling quit: {}", arguments);
bool success = send_response(221, "Service closing control connection.");
// close the control connection
socket_->close();
return success;
}
/// @brief Handle the DELE command
/// The DELE command deletes a file on the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_dele(std::string_view arguments) {
logger_.info("Handling dele: {}", arguments);
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (!std::filesystem::exists(full_path)) {
return send_response(550, "File does not exist.");
}
if (!std::filesystem::is_regular_file(full_path)) {
return send_response(550, "Not a regular file.");
}
// NOTE: cannot use std::filesystem::remove because it does not work on
// littlefs, it calls POSIX remove() which is not implemented
// properly. Instead, we use unlink() directly.
if (unlink(full_path.string().data()) != 0) {
logger_.error("Failed to delete file: {}", strerror(errno));
return send_response(550, "Failed to delete file.");
}
return send_response(250, "Requested file action okay, completed.");
}
/// @brief Handle the RMD command
/// The RMD command removes a directory on the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_rmd(std::string_view arguments) {
logger_.info("Handling rmd: {}", arguments);
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (!std::filesystem::exists(full_path)) {
return send_response(550, "File does not exist.");
}
if (!std::filesystem::is_directory(full_path)) {
return send_response(550, "Not a directory.");
}
// NOTE: cannot use std::filesystem::remove because it does not work on
// littlefs, it calls POSIX remove() which is not implemented
// properly. Instead, we use rmdir() directly.
if (rmdir(full_path.string().data()) != 0) {
logger_.error("Failed to delete directory: {}", strerror(errno));
return send_response(550, "Failed to delete directory.");
}
return send_response(250, "Requested file action okay, completed.");
}
/// @brief Handle the MKD command
/// The MKD command creates a directory on the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_mkd(std::string_view arguments) {
logger_.info("Handling mkd: {}", arguments);
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (std::filesystem::exists(full_path)) {
return send_response(550, "File already exists.");
}
std::filesystem::create_directory(full_path);
std::string message = full_path.string() + " created.";
return send_response(257, message);
}
/// @brief Handle the RNFR command
/// The RNFR command renames a file or directory on the server.
/// @param arguments The arguments of the command
/// @return True if the command was handled successfully, false otherwise
bool handle_rnfr(std::string_view arguments) {
logger_.info("Handling rnfr: {}", arguments);
auto path_end = arguments.find("\r\n");
if (path_end == std::string_view::npos) {
logger_.error("Failed to parse path");
return send_response(501, "Syntax error in parameters or arguments.");
}
std::string_view path = arguments.substr(0, path_end);
std::filesystem::path full_path = current_directory_ / std::filesystem::path{path};
if (!std::filesystem::exists(full_path)) {
return send_response(550, "File does not exist.");