-
Notifications
You must be signed in to change notification settings - Fork 39
/
FtpServer.cpp
2370 lines (2135 loc) · 64.2 KB
/
FtpServer.cpp
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
/*
* FtpServer Arduino, esp8266 and esp32 library for Ftp Server
* Derived form Jean-Michel Gallego version
*
* AUTHOR: Renzo Mischianti
*
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
*
*
* Use Ethernet library
*
* Commands implemented:
* USER, PASS, AUTH (AUTH only return 'not implemented' code)
* CDUP, CWD, PWD, QUIT, NOOP
* MODE, PASV, PORT, STRU, TYPE
* ABOR, DELE, LIST, NLST, MLST, MLSD
* APPE, RETR, STOR
* MKD, RMD
* RNTO, RNFR
* MDTM, MFMT
* FEAT, SIZE
* SITE FREE
* SYST
* HELP
*
* Tested with those clients:
* under Windows:
* FTP Rush
* Filezilla
* WinSCP
* NcFTP, ncftpget, ncftpput
* Firefox
* command line ftp.exe
* under Ubuntu:
* gFTP
* Filezilla
* NcFTP, ncftpget, ncftpput
* lftp
* ftp
* Firefox
* under Android:
* AndFTP
* FTP Express
* Firefox
* with a second Arduino and sketch of SurferTim at
* http://playground.arduino.cc/Code/FTP
*
*/
#include <FtpServer.h>
FtpServer::FtpServer( uint16_t _cmdPort, uint16_t _pasvPort )
: ftpServer( _cmdPort ), dataServer( _pasvPort )
{
cmdPort = _cmdPort;
pasvPort = _pasvPort;
millisDelay = 0;
nbMatch = 0;
iCL = 0;
iniVariables();
}
void FtpServer::begin( const char * _user, const char * _pass, const char * _welcomeMessage )
{
if ( strcmp( _user, "anonymous" ) != 0) {
DEBUG_PRINTLN(F("NOT ANONYMOUS"));
DEBUG_PRINTLN(_user);
this->anonymousConnection = false; // needed to reset after end of anonymnous and begin of not anonymous
}
// Tells the ftp server to begin listening for incoming connection
ftpServer.begin();
#if (defined(ESP8266) && (FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_ASYNC || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266 || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_242)) || defined(ARDUINO_ARCH_RP2040) || FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_SEEED_RTL8720DN
ftpServer.setNoDelay( true );
#endif
// localIp = _localIP == FTP_NULLIP() || (uint32_t) _localIP == 0 ? NET_CLASS.localIP() : _localIP ;
localIp = NET_CLASS.localIP(); //_localIP == FTP_NULLIP() || (uint32_t) _localIP == 0 ? NET_CLASS.localIP() : _localIP ;
// strcpy( user, FTP_USER );
// strcpy( pass, FTP_PASS );
if( strlen( _user ) > 0 && strlen( _user ) < FTP_CRED_SIZE ) {
//strcpy( user, _user );
this->user = _user;
}
if( strlen( _pass ) > 0 && strlen( _pass ) < FTP_CRED_SIZE ) {
// strcpy( pass, _pass );
this->pass = _pass;
}
// strcpy(_welcomeMessage, welcomeMessage);
this->welcomeMessage = _welcomeMessage;
dataServer.begin();
#if (defined(ESP8266) && (FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_ASYNC || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266 || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_242)) || defined(ARDUINO_ARCH_RP2040) || FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_SEEED_RTL8720DN
dataServer.setNoDelay( true );
#endif
millisDelay = 0;
cmdStage = FTP_Stop;
iniVariables();
}
void FtpServer::begin( const char * _welcomeMessage ) {
this->anonymousConnection = true;
this->begin( "anonymous", "anonymous", _welcomeMessage);
}
void FtpServer::end()
{
if(client.connected()) {
disconnectClient();
}
#if FTP_SERVER_NETWORK_TYPE == NETWORK_ESP32 // && !defined(ARDUINO_ARCH_RP2040)
ftpServer.end();
dataServer.end();
#endif
DEBUG_PRINTLN(F("Stop server!"));
if (FtpServer::_callback) {
FtpServer::_callback(FTP_DISCONNECT, free(), capacity());
}
cmdStage = FTP_Init;
transferStage = FTP_Close;
dataConn = FTP_NoConn;
}
void FtpServer::setLocalIp(IPAddress localIp)
{
this->localIp = localIp;
}
void FtpServer::credentials( const char * _user, const char * _pass )
{
if( strlen( _user ) > 0 && strlen( _user ) < FTP_CRED_SIZE )
// strcpy( user, _user );
this->user = _user;
if( strlen( _pass ) > 0 && strlen( _pass ) < FTP_CRED_SIZE )
// strcpy( pass, _pass );
this->pass = _pass;
}
void FtpServer::iniVariables()
{
// Default for data port
dataPort = FTP_DATA_PORT_DFLT;
// Default Data connection is Active
dataConn = FTP_NoConn;
// Set the root directory
strcpy( cwdName, "/" );
rnfrCmd = false;
transferStage = FTP_Close;
}
uint8_t FtpServer::handleFTP() {
#ifdef FTP_ADDITIONAL_DEBUG
// int8_t data0 = data.status();
ftpTransfer transferStage0 = transferStage;
ftpCmd cmdStage0 = cmdStage;
ftpDataConn dataConn0 = dataConn;
#endif
if ((int32_t) (millisDelay - millis()) <= 0) {
if (cmdStage == FTP_Stop) {
if (client.connected()) {
DEBUG_PRINTLN(F("Disconnect client!"));
disconnectClient();
}
cmdStage = FTP_Init;
} else if (cmdStage == FTP_Init) { // Ftp server waiting for connection
abortTransfer();
iniVariables();
DEBUG_PRINT(F(" Ftp server waiting for connection on port "));
DEBUG_PRINTLN(cmdPort);
cmdStage = FTP_Client;
} else if (cmdStage == FTP_Client) { // Ftp server idle
#if (FTP_SERVER_NETWORK_TYPE == NETWORK_WiFiNINA)
// if (client && !client.connected()) {
// client.stop();
// DEBUG_PRINTLN(F("CLIENT STOP!!"));
// }
byte status;
client = ftpServer.available(&status);
/*
* CLOSED = 0,
LISTEN = 1,
SYN_SENT = 2,
SYN_RCVD = 3,
ESTABLISHED = 4,
FIN_WAIT_1 = 5,
FIN_WAIT_2 = 6,
CLOSE_WAIT = 7,
CLOSING = 8,
LAST_ACK = 9,
TIME_WAIT = 10
*
*/
// DEBUG_PRINTLN(status);
#elif (defined(ESP8266) && (FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_ASYNC || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266 || FTP_SERVER_NETWORK_TYPE == NETWORK_ESP8266_242))
if( ftpServer.hasClient())
{
client.stop();
client = ftpServer.available();
}
#else
if (client && !client.connected()) {
client.stop();
DEBUG_PRINTLN(F("CLIENT STOP!!"));
}
client = ftpServer.accept();
#endif
if (client.connected()) // A client connected
{
clientConnected();
millisEndConnection = millis() + 1000L * FTP_AUTH_TIME_OUT; // wait client id for 10 s.
cmdStage = FTP_User;
}
} else if (readChar() > 0) // got response
{
processCommand();
if (cmdStage == FTP_Stop)
millisEndConnection = millis() + 1000L * FTP_AUTH_TIME_OUT; // wait authentication for 10 s.
else if (cmdStage < FTP_Cmd)
millisDelay = millis() + 200; // delay of 100 ms
else
millisEndConnection = millis() + 1000L * FTP_TIME_OUT;
} else if (!client.connected()) {
if (FtpServer::_callback) {
FtpServer::_callback(FTP_DISCONNECT, free(), capacity());
}
cmdStage = FTP_Init;
}
if (transferStage == FTP_Retrieve) // Retrieve data
{
if (!doRetrieve()) {
transferStage = FTP_Close;
}
} else if (transferStage == FTP_Store) // Store data
{
if (!doStore()) {
if (FtpServer::_callback) {
FtpServer::_callback(FTP_FREE_SPACE_CHANGE, free(), capacity());
}
transferStage = FTP_Close;
}
} else if (transferStage == FTP_List || transferStage == FTP_Nlst) // LIST or NLST
{
if (!doList()) {
transferStage = FTP_Close;
}
} else if (transferStage == FTP_Mlsd) // MLSD listing
{
if (!doMlsd()) {
transferStage = FTP_Close;
}
} else if (cmdStage > FTP_Client
&& !((int32_t) (millisEndConnection - millis()) > 0)) {
DEBUG_PRINTLN(F("530 Timeout"));
client.println(F("530 Timeout"));
millisDelay = millis() + 200; // delay of 200 ms
cmdStage = FTP_Stop;
}
#ifdef FTP_ADDITIONAL_DEBUG
if (cmdStage != cmdStage0 || transferStage != transferStage0
|| dataConn != dataConn0) {
DEBUG_PRINT(F(" Command Old: "));
DEBUG_PRINT(cmdStage0);
DEBUG_PRINT(F(" Transfer Old: "));
DEBUG_PRINT(transferStage0);
DEBUG_PRINT(F(" Data Old: "));
DEBUG_PRINTLN(dataConn0);
DEBUG_PRINT(F(" Command : "));
DEBUG_PRINT(cmdStage);
DEBUG_PRINT(F(" Transfer : "));
DEBUG_PRINT(transferStage);
DEBUG_PRINT(F(" Data : "));
DEBUG_PRINTLN(dataConn);
}
#endif
}
return cmdStage | (transferStage << 3) | (dataConn << 6);
}
void FtpServer::clientConnected()
{
DEBUG_PRINTLN( F(" Client connected!") );
client.print (F("220--- ")); client.print(welcomeMessage); client.println(F(" ---"));
client.println(F(" -- By Renzo Mischianti --"));
client.print (F("220 -- Version ")); client.print(FTP_SERVER_VERSION); client.println(F(" --"));
iCL = 0;
if (FtpServer::_callback) {
FtpServer::_callback(FTP_CONNECT, free(), capacity());
}
}
void FtpServer::disconnectClient()
{
DEBUG_PRINTLN( F(" Disconnecting client") );
abortTransfer();
client.println(F("221 Goodbye") );
if (FtpServer::_callback) {
FtpServer::_callback(FTP_DISCONNECT, free(), capacity());
}
if( client ) {
}
if( data ) {
data.stop();
}
}
bool FtpServer::processCommand()
{
///////////////////////////////////////
// //
// AUTHENTICATION COMMANDS //
// //
///////////////////////////////////////
// RoSchmi added the next two lines
DEBUG_PRINT("Command is: ");
DEBUG_PRINTLN(command);
//
// USER - User Identity
//
if( CommandIs( "USER" ))
{
DEBUG_PRINT(F("USER: "));
DEBUG_PRINT(parameter);
DEBUG_PRINT(F(" "));
DEBUG_PRINTLN(user)
if (this->anonymousConnection && ! strcmp( parameter, user )) {
DEBUG_PRINTLN( F(" Anonymous authentication Ok. Waiting for commands.") );
client.println(F("230 Ok") );
cmdStage = FTP_Cmd;
} else if( ! strcmp( parameter, user ))
{
client.println(F("331 Ok. Password required") );
strcpy( cwdName, "/" );
cmdStage = FTP_Pass;
}
else
{
DEBUG_PRINTLN(F("530 ") );
client.println(F("530 ") );
cmdStage = FTP_Stop;
}
}
//
// PASS - Password
//
else if( CommandIs( "PASS" ))
{
DEBUG_PRINT(F("PASS: ")) DEBUG_PRINTLN(pass);
DEBUG_PRINT(F("PASS PARAM: ")) DEBUG_PRINTLN(parameter);
DEBUG_PRINT(F("PASS OK: ")) DEBUG_PRINTLN(strcmp( parameter, pass ));
if( cmdStage != FTP_Pass )
{
client.println(F("503 ") );
cmdStage = FTP_Stop;
}
if( ! strcmp( parameter, pass ))
{
DEBUG_PRINTLN( F(" Authentication Ok. Waiting for commands.") );
client.println(F("230 Ok") );
cmdStage = FTP_Cmd;
}
else
{
client.println( F("530 Wrong password!") );
cmdStage = FTP_Stop;
}
}
//
// FEAT - New Features
//
else if( CommandIs( "FEAT" ))
{
client.println(F("211-Extensions supported:"));
client.println(F(" MLST type*;modify*;size*;") );
client.println(F(" MLSD") );
client.println(F(" MDTM") );
client.println(F(" MFMT") );
#ifdef UTF8_SUPPORT
client.println(F(" UTF8") );
#endif
client.println(F(" SIZE") );
client.println(F(" SITE FREE") );
client.println(F("211 End.") );
}
//
// AUTH - Not implemented
//
else if( CommandIs( "AUTH" )) {
client.println(F("502 ") );
}
//
// SYST - System
//
else if( CommandIs( "SYST" ))
{
DEBUG_PRINTLN(F("215 ESP"));
client.println(F("215 ESP"));
// FtpOutCli << F("215 ESP") << endl;
}
//
// Unrecognized commands at stage of authentication
//
else if( cmdStage < FTP_Cmd )
{
client.println(F("530 ") );
cmdStage = FTP_Stop;
}
///////////////////////////////////////
// //
// ACCESS CONTROL COMMANDS //
// //
///////////////////////////////////////
//
// PWD - Print Directory
//
else if( CommandIs( "PWD" ) ||
( CommandIs( "CWD" ) && ParameterIs( "." ))) {
client.print( F("257 \"")); client.print( cwdName ); client.print( F("\"") ); client.println( F(" is your current directory") );
//
// CDUP - Change to Parent Directory
//
} else if( CommandIs( "CDUP" ) ||
( CommandIs( "CWD" ) && ParameterIs( ".." )))
{
bool ok = false;
if( strlen( cwdName ) > 1 ) // do nothing if cwdName is root
{
// if cwdName ends with '/', remove it (must not append)
if( cwdName[ strlen( cwdName ) - 1 ] == '/' ) {
cwdName[ strlen( cwdName ) - 1 ] = 0;
}
// search last '/'
char * pSep = strrchr( cwdName, '/' );
ok = pSep > cwdName;
// if found, ends the string on its position
if( ok )
{
* pSep = 0;
ok = exists( cwdName );
}
}
// if an error appends, move to root
if( ! ok ) {
strcpy( cwdName, "/" );
}
client.print( F("250 Ok. Current directory is ") ); client.println( cwdName );
}
//
// CWD - Change Working Directory
//
else if( CommandIs( "CWD" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makeExistsPath( path ))
{
strcpy( cwdName, path );
client.print( F("250 Directory changed to ") ); client.print(cwdName); client.println();
}
}
//
// QUIT
//
else if( CommandIs( "QUIT" ))
{
client.println(F("221 Goodbye") );
disconnectClient();
cmdStage = FTP_Stop;
}
///////////////////////////////////////
// //
// TRANSFER PARAMETER COMMANDS //
// //
///////////////////////////////////////
//
// MODE - Transfer Mode
//
else if( CommandIs( "MODE" ))
{
if( ParameterIs( "S" )) {
client.println(F("200 S Ok") );
} else {
client.println(F("504 Only S(tream) is supported") );
}
}
//
// PASV - Passive Connection management
//
else if( CommandIs( "PASV" ))
{
data.stop();
dataServer.begin();
if (((((uint32_t) NET_CLASS.localIP()) & ((uint32_t) NET_CLASS.subnetMask())) ==
(((uint32_t) client.remoteIP()) & ((uint32_t) NET_CLASS.subnetMask()))) && (uint32_t)localIp <= 0) {
dataIp = NET_CLASS.localIP();
} else {
dataIp = localIp;
}
DEBUG_PRINT( F(" IP: ") );
DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) ); DEBUG_PRINT( F(".") );
DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINTLN( int( dataIp[3]) );
#if ((FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266_ASYNC) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP32)) // || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_WiFiNINA) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_SEEED_RTL8720DN))
if (dataIp.toString() == F("0.0.0.0")) {
dataIp = NET_CLASS.softAPIP();
}
#endif
DEBUG_PRINT( F(" Soft IP: ") );
DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) ); DEBUG_PRINT( F(".") );
DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINTLN( int( dataIp[3]) );
dataPort = pasvPort;
DEBUG_PRINTLN( F(" Connection management set to passive") );
DEBUG_PRINT( F(" Listening at ") );
DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) ); DEBUG_PRINT( F(".") );
DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[3]) );
DEBUG_PRINT( F(":") ); DEBUG_PRINTLN( dataPort );
client.print( F("227 Entering Passive Mode") ); client.print( F(" (") );
client.print( int( dataIp[0]) ); client.print( F(",") ); client.print( int( dataIp[1]) ); client.print( F(",") );
client.print( int( dataIp[2]) ); client.print( F(",") ); client.print( int( dataIp[3]) ); client.print( F(",") );
client.print( ( dataPort >> 8 ) ); client.print( F(",") ); client.print( ( dataPort & 255 ) ); client.println( F(")") );
dataConn = FTP_Pasive;
}
//
// PORT - Data Port
//
else if( CommandIs( "PORT" ))
{
data.stop();
// get IP of data client
dataIp[ 0 ] = atoi( parameter );
char * p = strchr( parameter, ',' );
for( uint8_t i = 1; i < 4; i ++ )
{
dataIp[ i ] = atoi( ++ p );
p = strchr( p, ',' );
}
// get port of data client
dataPort = 256 * atoi( ++ p );
p = strchr( p, ',' );
dataPort += atoi( ++ p );
if( p == NULL ) {
client.println(F("501 Can't interpret parameters") );
} else
{
DEBUG_PRINT( F(" Data IP set to ") ); DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) );
DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINTLN( int( dataIp[3]) );
DEBUG_PRINT( F(" Data port set to ") ); DEBUG_PRINTLN( dataPort );
client.println(F("200 PORT command successful") );
dataConn = FTP_Active;
}
}
//
// STRU - File Structure
//
else if( CommandIs( "STRU" ))
{
if( ParameterIs( "F" )) {
client.println(F("200 F Ok") );
// else if( ParameterIs( "R" ))
// client.println(F("200 B Ok") );
}else{
client.println(F("504 Only F(ile) is supported") );
}
}
//
// TYPE - Data Type
//
else if( CommandIs( "TYPE" ))
{
if( ParameterIs( "A" )) {
client.println(F("200 TYPE is now ASCII"));
} else if( ParameterIs( "I" )) {
client.println(F("200 TYPE is now 8-bit binary") );
} else {
client.println(F("504 Unknown TYPE") );
}
}
///////////////////////////////////////
// //
// FTP SERVICE COMMANDS //
// //
///////////////////////////////////////
//
// ABOR - Abort
//
else if( CommandIs( "ABOR" ))
{
abortTransfer();
client.println(F("226 Data connection closed"));
}
//
// DELE - Delete a File
//
else if( CommandIs( "DELE" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makeExistsPath( path )) {
if( remove( path )) {
if (FtpServer::_callback) {
FtpServer::_callback(FTP_FREE_SPACE_CHANGE, free(), capacity());
}
client.print( F("250 Deleted ") ); client.println( parameter );
} else {
client.print( F("450 Can't delete ") ); client.println( parameter );
}
}
}
//
// LIST - List
// NLST - Name List
// MLSD - Listing for Machine Processing (see RFC 3659)
//
else if( CommandIs( "LIST" ) || CommandIs( "NLST" ) || CommandIs( "MLSD" ))
{
DEBUG_PRINT("List of file!!");
if( dataConnect()){
if( openDir( & dir ))
{
DEBUG_PRINT("Dir opened!!");
nbMatch = 0;
if( CommandIs( "LIST" ))
transferStage = FTP_List;
else if( CommandIs( "NLST" ))
transferStage = FTP_Nlst;
else
transferStage = FTP_Mlsd;
}
else {
DEBUG_PRINT("List Data stop!!");
data.stop();
}
}
}
//
// MLST - Listing for Machine Processing (see RFC 3659)
//
else if( CommandIs( "MLST" ))
{
char path[ FTP_CWD_SIZE ];
uint16_t dat=0, tim=0;
char dtStr[ 15 ];
bool isdir;
if( haveParameter() && makeExistsPath( path )){
if( ! getFileModTime( path, &dat, &tim )) {
client.print( F("550 Unable to retrieve time for ") ); client.println( parameter );
} else
{
isdir = isDir( path );
client.println( F("250-Begin") );
client.print( F(" Type=") ); client.print( ( isdir ? F("dir") : F("file")) );
client.print( F(";Modify=") ); client.print( makeDateTimeStr( dtStr, dat, tim ) );
if( ! isdir )
{
if( openFile( path, FTP_FILE_READ ))
{
client.print( F(";Size=") ); client.print( long( fileSize( file )) );
file.close();
}
}
client.print( F("; ") ); client.println( path );
client.println( F("250 End.") );
}
}
}
//
// NOOP
//
else if( CommandIs( "NOOP" )) {
client.println(F("200 Zzz...") );
}
//
#ifdef UTF8_SUPPORT
// OPTS
//
else if( CommandIs( "OPTS" )) {
if( ParameterIs( "UTF8 ON" ) || ParameterIs( "utf8 on" )) {
client.println(F("200 OK, UTF8 ON") );
DEBUG_PRINTLN(F("200 OK, UTF8 ON") );
} else {
client.println(F("504 Unknown OPTS") );
DEBUG_PRINTLN(F("504 Unknown OPTS") );
}
}
//
#endif
// HELP
//
else if( CommandIs( "HELP" )) {
client.println(F("200 Commands implemented:") );
client.println(F(" USER, PASS, AUTH (AUTH only return 'not implemented' code)") );
client.println(F(" CDUP, CWD, PWD, QUIT, NOOP") );
client.println(F(" MODE, PASV, PORT, STRU, TYPE") );
client.println(F(" ABOR, DELE, LIST, NLST, MLST, MLSD") );
client.println(F(" APPE, RETR, STOR") );
client.println(F(" MKD, RMD") );
client.println(F(" RNTO, RNFR") );
client.println(F(" MDTM, MFMT") );
client.println(F(" FEAT, SIZE") );
client.println(F(" SITE FREE") );
client.println(F(" HELP") );
}
//
// RETR - Retrieve
//
else if( CommandIs( "RETR" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makeExistsPath( path )) {
if( ! openFile( path, FTP_FILE_READ )) {
client.print( F("450 Can't open ") ); client.print( parameter );
} else if( dataConnect( false ))
{
DEBUG_PRINT( F(" Sending ") ); DEBUG_PRINT( parameter ); DEBUG_PRINT( F(" size ") ); DEBUG_PRINTLN( long( fileSize( file )) );
if (FtpServer::_transferCallback) {
FtpServer::_transferCallback(FTP_DOWNLOAD_START, parameter, long( fileSize( file )));
}
client.print( F("150-Connected to port ") ); client.println( dataPort );
client.print( F("150 ") ); client.print( long( fileSize( file )) ); client.println( F(" bytes to download") );
millisBeginTrans = millis();
bytesTransfered = 0;
transferStage = FTP_Retrieve;
}
}
}
//
// STOR - Store
// APPE - Append
//
else if( CommandIs( "STOR" ) || CommandIs( "APPE" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makePath( path ))
{
bool open;
if( exists( path )) {
DEBUG_PRINTLN(F("APPEND FILE!!"));
open = openFile( path, ( CommandIs( "APPE" ) ? FTP_FILE_WRITE_APPEND : FTP_FILE_WRITE_CREATE ));
} else {
DEBUG_PRINTLN(F("CREATE FILE!!"));
open = openFile( path, FTP_FILE_WRITE_CREATE );
}
data.stop();
data.flush();
DEBUG_PRINT(F("open/create "));
DEBUG_PRINTLN(open);
if( ! open ){
client.print( F("451 Can't open/create ") ); client.println( parameter );
}else if( ! dataConnect()) // && !data.available())
file.close();
else
{
DEBUG_PRINT( F(" Receiving ") ); DEBUG_PRINTLN( parameter );
millisBeginTrans = millis();
bytesTransfered = 0;
transferStage = FTP_Store;
if (FtpServer::_transferCallback) {
FtpServer::_transferCallback(FTP_UPLOAD_START, parameter, bytesTransfered);
}
}
}
}
//
// MKD - Make Directory
//
else if( CommandIs( "MKD" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makePath( path ))
{
if( exists( path )) {
client.print( F("521 \"") ); client.print( parameter ); client.println( F("\" directory already exists") );
} else
{
DEBUG_PRINT( F(" Creating directory ")); DEBUG_PRINTLN( parameter );
#if STORAGE_TYPE != STORAGE_SPIFFS
if( makeDir( path )) {
client.print( F("257 \"") ); client.print( parameter ); client.print( F("\"") ); client.println( F(" created") );
} else {
#endif
client.print( F("550 Can't create \"") ); client.print( parameter ); client.println( F("\"") );
#if STORAGE_TYPE != STORAGE_SPIFFS
}
#endif
}
}
}
//
// RMD - Remove a Directory
//
else if( CommandIs( "RMD" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makeExistsPath( path )) {
if( removeDir( path ))
{
DEBUG_PRINT( F(" Deleting ") ); DEBUG_PRINTLN( path );
client.print( F("250 \"") ); client.print( parameter ); client.println( F("\" deleted") );
}
else {
client.print( F("550 Can't remove \"") ); client.print( parameter ); client.println( F("\". Directory not empty?") );
}
}
}
//
// RNFR - Rename From
//
else if( CommandIs( "RNFR" ))
{
rnfrName[ 0 ] = 0;
if( haveParameter() && makeExistsPath( rnfrName ))
{
DEBUG_PRINT( F(" Ready for renaming ") ); DEBUG_PRINTLN( rnfrName );
client.println(F("350 RNFR accepted - file exists, ready for destination") );
rnfrCmd = true;
}
}
//
// RNTO - Rename To
//
else if( CommandIs( "RNTO" ))
{
char path[ FTP_CWD_SIZE ];
char dirp[ FTP_FIL_SIZE ];
if( strlen( rnfrName ) == 0 || ! rnfrCmd ) {
client.println(F("503 Need RNFR before RNTO") );
} else if( haveParameter() && makePath( path ))
{
if( exists( path )) {
client.print( F("553 ") ); client.print( parameter ); client.println( F(" already exists") );
} else
{
strcpy( dirp, path );
char * psep = strrchr( dirp, '/' );
bool fail = psep == NULL;
if( ! fail )
{
if( psep == dirp )
psep ++;
* psep = 0;
// fail = ! isDir( dirp );
// if( fail ) {
// client.print( F("550 \"") ); client.print( dirp ); client.println( F("\" is not directory") );
// } else
// {
DEBUG_PRINT( F(" Renaming ") ); DEBUG_PRINT( rnfrName ); DEBUG_PRINT( F(" to ") ); DEBUG_PRINTLN( path );
if( rename( rnfrName, path ))
client.println(F("250 File successfully renamed or moved") );
else
fail = true;
// }
}
if( fail )
client.println(F("451 Rename/move failure") );
}
}
rnfrCmd = false;
}
/*
//
// SYST - System
//
else if( CommandIs( "SYST" ))
FtpOutCli << F("215 MSDOS") << endl;
*/
///////////////////////////////////////
// //
// EXTENSIONS COMMANDS (RFC 3659) //
// //
///////////////////////////////////////
//
// MDTM && MFMT - File Modification Time (see RFC 3659)
//
else if( CommandIs( "MDTM" ) || CommandIs( "MFMT" ))
{
if( haveParameter())
{
char path[ FTP_CWD_SIZE ];
char * fname = parameter;
uint16_t year;
uint8_t month, day, hour, minute, second, setTime;
char dt[ 15 ];
bool mdtm = CommandIs( "MDTM" );
setTime = getDateTime( dt, & year, & month, & day, & hour, & minute, & second );
// fname point to file name
fname += setTime;
if( strlen( fname ) <= 0 ) {
client.println(F("501 No file name") );
} else if( makeExistsPath( path, fname )) {
if( setTime ) // set file modification time
{
if( timeStamp( path, year, month, day, hour, minute, second )) {
client.print( F("213 ") ); client.println( dt );
} else {
client.println(F("550 Unable to modify time" ));
}
}
else if( mdtm ) // get file modification time
{
uint16_t dat=0, tim=0;
char dtStr[ 15 ];
if( getFileModTime( path, &dat, &tim )) {
client.print( F("213 ") ); client.println( makeDateTimeStr( dtStr, dat, tim ) );
} else {
client.println("550 Unable to retrieve time" );
}
}
}
}
}
//
// SIZE - Size of the file
//
else if( CommandIs( "SIZE" ))
{
char path[ FTP_CWD_SIZE ];
if( haveParameter() && makeExistsPath( path )) {
if( ! openFile( path, FTP_FILE_READ )) {
client.print( F("450 Can't open ") ); client.println( parameter );
} else
{
client.print( F("213 ") ); client.println( long( fileSize( file )) );
file.close();
}
}
}
//
// SITE - System command
//
else if( CommandIs( "SITE" ))
{
if( ParameterIs( "FREE" ))
{
uint32_t capa = capacity();
if(( capa >> 10 ) < 1000 ) { // less than 1 Giga
client.print( F("200 ") ); client.print( free() ); client.print( F(" kB free of ") );
client.print( capa ); client.println( F(" kB capacity") );
}else {
client.print( F("200 ") ); client.print( ( free() >> 10 ) ); client.print( F(" MB free of ") );
client.print( ( capa >> 10 ) ); client.println( F(" MB capacity") );
}
}
else {
client.print( F("500 Unknown SITE command ") ); client.println( parameter );
}
}
//
// Unrecognized commands ...
//
else