-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftpd.c
1285 lines (1056 loc) · 34.2 KB
/
ftpd.c
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
/*
* Copyright (c) 2002 Florian Schulze.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the authors nor the names of the contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ftpd.c - This file is part of the FTP daemon for lwIP
*
*/
/*
* 2021-12-28: Modified by Terje Io for grblHAL networking
* 2022-08-25: Modified by Terje Io for grblHAL VFS
*/
/*
* Based on this version: https://github.com/toelke/lwip-ftpd
*/
#ifdef ARDUINO
#include "../driver.h"
#include "../grbl/vfs.h"
#else
#include "driver.h"
#include "grbl/vfs.h"
#endif
#if FTP_ENABLE
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "ftpd.h"
#include "sfifo.h"
#include "../sdcard/sdcard.h"
#ifndef FTPD_POLL_INTERVAL
#define FTPD_POLL_INTERVAL 4
#endif
#ifdef LWIP_DEBUGF
#undef LWIP_DEBUGF
#endif
#define FTPD_DEBUG 0
#if FTPD_DEBUG
#define LWIP_DEBUGF(debug, message) do { dbg message; } while(0)
void dbg (char *msg, ...)
{
char buffer[200];
va_list arg;
va_start(arg, msg);
vsprintf(buffer, msg, arg);
va_end(arg);
hal.stream.write(buffer);
}
#else
#define LWIP_DEBUGF(debug, message)
#endif
#define TELNET_IAC 255
#ifndef FTP_TXPOLL
#define FTP_TXPOLL 0
#endif
#ifndef EINVAL
#define EINVAL 1
#define ENOMEM 2
#define ENODEV 3
#endif
//PROGMEM static const char *msg110 = "110 MARK %s = %s.";
/*
110 Restart marker reply.
In this case, the text is exact and not left to the
particular implementation; it must read:
MARK yyyy = mmmm
Where yyyy is User-process data stream marker, and mmmm
server's equivalent marker (note the spaces between markers
and "=").
*/
//PROGMEM static const char *msg120 = "120 Service ready in nnn minutes.";
//PROGMEM static const char *msg125 = "125 Data connection already open; transfer starting.";
PROGMEM static const char *msg150 = "150 File status okay; about to open data connection.";
PROGMEM static const char *msg150recv = "150 Opening BINARY mode data connection for %s (%i bytes).";
PROGMEM static const char *msg150stor = "150 Opening BINARY mode data connection for %s.";
PROGMEM static const char *msg200 = "200 Command okay.";
//PROGMEM static const char *msg202 = "202 Command not implemented, superfluous at this site.";
//PROGMEM static const char *msg211 = "211 System status, or system help reply.";
//PROGMEM static const char *msg212 = "212 Directory status.";
//PROGMEM static const char *msg213 = "213 File status.";
//PROGMEM static const char *msg214 = "214 %s.";
/*
214 Help message.
On how to use the server or the meaning of a particular
non-standard command. This reply is useful only to the
human user.
*/
PROGMEM static const char *msg214SYST = "214 %s system type.";
/*
215 NAME system type.
Where NAME is an official system name from the list in the
Assigned Numbers document.
*/
PROGMEM static const char *msg220 = "220 lwIP FTP Server ready.";
/*
220 Service ready for new user.
*/
PROGMEM static const char *msg221 = "221 Goodbye.";
/*
221 Service closing control connection.
Logged out if appropriate.
*/
//PROGMEM static const char *msg225 = "225 Data connection open; no transfer in progress.";
PROGMEM static const char *msg226 = "226 Closing data connection.";
/*
Requested file action successful (for example, file
transfer or file abort).
*/
PROGMEM static const char *msg227 = "227 Entering Passive Mode (%i,%i,%i,%i,%i,%i).";
/*
227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
*/
PROGMEM static const char *msg230 = "230 User logged in, proceed.";
PROGMEM static const char *msg250 = "250 Requested file action okay, completed.";
PROGMEM static const char *msg257PWD = "257 \"%s\" is current directory.";
PROGMEM static const char *msg257 = "257 \"%s\" created.";
/*
257 "PATHNAME" created.
*/
PROGMEM static const char *msg331 = "331 User name okay, need password.";
//PROGMEM static const char *msg332 = "332 Need account for login.";
PROGMEM static const char *msg350 = "350 Requested file action pending further information.";
//PROGMEM static const char *msg421 = "421 Service not available, closing control connection.";
/*
This may be a reply to any command if the service knows it
must shut down.
*/
//PROGMEM static const char *msg425 = "425 Can't open data connection.";
//PROGMEM static const char *msg426 = "426 Connection closed; transfer aborted.";
PROGMEM static const char *msg450 = "450 Requested file action not taken.";
/*
File unavailable (e.g., file busy).
*/
PROGMEM static const char *msg451 = "451 Requested action aborted: local error in processing.";
PROGMEM static const char *msg452 = "452 Requested action not taken.";
/*
Insufficient storage space in system.
*/
//PROGMEM static const char *msg500 = "500 Syntax error, command unrecognized.";
/*
This may include errors such as command line too long.
*/
PROGMEM static const char *msg501 = "501 Syntax error in parameters or arguments.";
PROGMEM static const char *msg502 = "502 Command not implemented.";
PROGMEM static const char *msg503 = "503 Bad sequence of commands.";
//PROGMEM static const char *msg504 = "504 Command not implemented for that parameter.";
//PROGMEM static const char *msg530 = "530 Not logged in.";
//PROGMEM static const char *msg532 = "532 Need account for storing files.";
PROGMEM static const char *msg550 = "550 Requested action not taken.";
/*
File unavailable (e.g., file not found, no access).
*/
//PROGMEM static const char *msg551 = "551 Requested action aborted: page type unknown.";
//PROGMEM static const char *msg552 = "552 Requested file action aborted.";
/*
Exceeded storage allocation (for current directory or
dataset).
*/
//PROGMEM static const char *msg553 = "553 Requested action not taken.";
/*
File name not allowed.
*/
enum ftpd_state_e {
FTPD_USER,
FTPD_PASS,
FTPD_IDLE,
FTPD_NLST,
FTPD_LIST,
FTPD_RETR,
FTPD_RNFR,
FTPD_STOR,
FTPD_QUIT
};
PROGMEM static const char *month_table[12] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
typedef struct {
int connected;
int eof;
vfs_dir_t *vfs_dir;
vfs_dirent_t *vfs_dirent;
vfs_file_t *vfs_file;
sfifo_t fifo;
struct tcp_pcb *msgpcb;
struct ftpd_msgstate *msgfs;
} ftpd_datastate_t;
typedef struct {
char *text;
int len;
} ftpd_cmd_t;
typedef struct ftpd_msgstate {
enum ftpd_state_e state;
sfifo_t fifo;
vfs_t *vfs;
struct ip4_addr dataip;
u16_t dataport;
struct tcp_pcb *datalistenpcb;
struct tcp_pcb *datapcb;
ftpd_datastate_t *datafs;
int passive;
char *renamefrom;
ftpd_cmd_t cmd;
} ftpd_msgstate_t;
#if FTP_TXPOLL
static struct {
ftpd_datastate_t *fsd;
struct tcp_pcb *pcb;
} poll;
#endif
static void send_msg (struct tcp_pcb *pcb, ftpd_msgstate_t *fsm, const char *msg, ...);
static void ftpd_dataerr (void *arg, err_t err)
{
ftpd_datastate_t *fsd = arg;
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_dataerr: %s (%i)\n", lwip_strerr(err), err));
if (fsd != NULL) {
fsd->msgfs->datafs = NULL;
fsd->msgfs->state = FTPD_IDLE;
free(fsd);
}
}
static void ftpd_dataclose (struct tcp_pcb *pcb, ftpd_datastate_t *fsd)
{
if(fsd->vfs_file)
vfs_close(fsd->vfs_file);
if(fsd->vfs_dir)
vfs_closedir(fsd->vfs_dir);
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
if (fsd->msgfs) {
if (fsd->msgfs->datalistenpcb) {
tcp_arg(fsd->msgfs->datalistenpcb, NULL);
tcp_accept(fsd->msgfs->datalistenpcb, NULL);
tcp_close(fsd->msgfs->datalistenpcb);
fsd->msgfs->datalistenpcb = NULL;
}
fsd->msgfs->datafs = NULL;
fsd->msgfs->state = FTPD_IDLE;
}
sfifo_close(&fsd->fifo);
free(fsd);
tcp_arg(pcb, NULL);
tcp_close(pcb);
}
static void send_data (struct tcp_pcb *pcb, ftpd_datastate_t *fsd)
{
u16_t len;
if ((len = sfifo_used(&fsd->fifo)) > 0) {
int i = fsd->fifo.readpos;
/* We cannot send more data than space available in the send buffer. */
len = tcp_sndbuf(pcb) < len ? tcp_sndbuf(pcb) : len;
LWIP_DEBUGF(FTPD_DEBUG, ("send_data: %d %d %d\n", len, i, *(fsd->fifo.buffer + i)));
if ((i + len) > fsd->fifo.size) {
if (tcp_write(pcb, fsd->fifo.buffer + i, (u16_t)(fsd->fifo.size - i), 1) != ERR_OK) {
LWIP_DEBUGF(FTPD_DEBUG, ("send_data: error writing!\n"));
return;
}
len -= fsd->fifo.size - i;
fsd->fifo.readpos = 0;
i = 0;
}
if (tcp_write(pcb, fsd->fifo.buffer + i, len, 1) != ERR_OK) {
LWIP_DEBUGF(FTPD_DEBUG, ("send_data: error writing!\n"));
return;
}
fsd->fifo.readpos += len;
if(fsd->eof && sfifo_used(&fsd->fifo) == 0)
tcp_output(pcb);
}
}
static void send_file (ftpd_datastate_t *fsd, struct tcp_pcb *pcb)
{
if (!fsd->connected)
return;
int len = 0;
if (fsd->vfs_file) {
static char buffer[512];
LWIP_DEBUGF(FTPD_DEBUG, ("send_file: %d\n", sfifo_space(&fsd->fifo)));
while((len = sfifo_space(&fsd->fifo)) > 256) {
len = vfs_read(buffer, 1, len > sizeof(buffer) ? sizeof(buffer) : len, fsd->vfs_file);
if (vfs_errno) {
fsd->vfs_file = NULL; /* FS error */
break;
}
if (len > 0)
sfifo_write(&fsd->fifo, buffer, len);
if ((fsd->eof = vfs_eof(fsd->vfs_file))) {
vfs_close(fsd->vfs_file);
fsd->vfs_file = NULL;
break;
}
}
if(len >= 0)
send_data(pcb, fsd);
}
if (!fsd->vfs_file) {
if (len >= 0 && sfifo_used(&fsd->fifo) > 0) {
send_data(pcb, fsd);
return;
}
ftpd_msgstate_t *fsm = fsd->msgfs;
struct tcp_pcb *msgpcb = fsd->msgpcb;
ftpd_dataclose(pcb, fsd);
send_msg(msgpcb, fsm, len < 0 ? msg451 : msg226);
}
}
static void send_next_directory (ftpd_datastate_t *fsd, struct tcp_pcb *pcb, int shortlist)
{
int len;
char buffer[512];
while (1) {
if (fsd->vfs_dirent == NULL)
fsd->vfs_dirent = vfs_readdir(fsd->vfs_dir);
if (fsd->vfs_dirent) {
if (shortlist) {
len = sprintf(buffer, "%s\r\n", fsd->vfs_dirent->name);
if (sfifo_space(&fsd->fifo) < len) {
send_data(pcb, fsd);
return;
}
sfifo_write(&fsd->fifo, buffer, len);
fsd->vfs_dirent = NULL;
} else {
vfs_stat_t st;
time_t current_time = (time_t)-1;
int current_year;
struct tm *s_time;
#ifndef __IMXRT1062__
time(¤t_time);
#endif
s_time = gmtime(¤t_time);
current_year = s_time->tm_year;
strcat(strcpy(buffer, "/"), fsd->vfs_dirent->name);
// vfs_stat(fsd->vfs_dirent->name, &st);
vfs_stat(fsd->vfs_dirent->name, &st);
#ifdef ESP_PLATFORM
s_time = gmtime(&st.st_mtim);
#else
s_time = gmtime(&st.st_mtime);
#endif
if (s_time->tm_year == current_year)
len = sprintf(buffer, "-rw-rw-rw- 1 user ftp %11" UINT32SFMT " %s %02i %02i:%02i %s\r\n", (uint32_t)st.st_size, month_table[s_time->tm_mon], s_time->tm_mday, s_time->tm_hour, s_time->tm_min, fsd->vfs_dirent->name);
else
len = sprintf(buffer, "-rw-rw-rw- 1 user ftp %11" UINT32SFMT " %s %02i %5i %s\r\n", (uint32_t)st.st_size, month_table[s_time->tm_mon], s_time->tm_mday, s_time->tm_year + 1900, fsd->vfs_dirent->name);
if (st.st_mode.directory)
buffer[0] = 'd';
if (sfifo_space(&fsd->fifo) < len) {
send_data(pcb, fsd);
return;
}
sfifo_write(&fsd->fifo, buffer, len);
fsd->vfs_dirent = NULL;
}
} else {
if (sfifo_used(&fsd->fifo) > 0) {
send_data(pcb, fsd);
return;
}
ftpd_msgstate_t *fsm = fsd->msgfs;
struct tcp_pcb *msgpcb = fsd->msgpcb;
ftpd_dataclose(pcb, fsd);
fsm->datapcb = NULL;
send_msg(msgpcb, fsm, msg226);
return;
}
}
}
static err_t ftpd_datasent (void *arg, struct tcp_pcb *pcb, u16_t len)
{
ftpd_datastate_t *fsd = arg;
switch (fsd->msgfs->state) {
case FTPD_LIST:
send_next_directory(fsd, pcb, 0);
break;
case FTPD_NLST:
send_next_directory(fsd, pcb, 1);
break;
case FTPD_RETR:
#if FTP_TXPOLL
poll.fsd = fsd;
poll.pcb = pcb;
#else
send_file(fsd, pcb);
#endif
break;
default:
break;
}
return ERR_OK;
}
static err_t ftpd_datarecv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
ftpd_datastate_t *fsd = arg;
if (err == ERR_OK && p != NULL) {
u16_t tot_len = 0;
struct pbuf *q = p;
do {
int len;
len = vfs_write(q->payload, 1, q->len, fsd->vfs_file);
tot_len += len;
if (len != q->len)
break;
} while((q = q->next));
/* Inform TCP that we have taken the data. */
tcp_recved(pcb, tot_len);
pbuf_free(p);
}
if (err == ERR_OK && p == NULL) {
ftpd_msgstate_t *fsm = fsd->msgfs;
struct tcp_pcb *msgpcb = fsd->msgpcb;
ftpd_dataclose(pcb, fsd);
fsm->datapcb = NULL;
send_msg(msgpcb, fsm, msg226);
}
return ERR_OK;
}
static err_t ftpd_dataconnected (void *arg, struct tcp_pcb *pcb, err_t err)
{
ftpd_datastate_t *fsd = arg;
fsd->msgfs->datapcb = pcb;
fsd->connected = 1;
/* Tell TCP that we wish to be informed of incoming data by a call
to the ftpd_datarecv() function. */
tcp_recv(pcb, ftpd_datarecv);
/* Tell TCP that we wish be to informed of data that has been
successfully sent by a call to the ftpd_sent() function. */
tcp_sent(pcb, ftpd_datasent);
tcp_err(pcb, ftpd_dataerr);
switch (fsd->msgfs->state) {
case FTPD_LIST:
send_next_directory(fsd, pcb, 0);
break;
case FTPD_NLST:
send_next_directory(fsd, pcb, 1);
break;
case FTPD_RETR:
#if FTP_TXPOLL
poll.fsd = fsd;
poll.pcb = pcb;
#else
send_file(fsd, pcb);
#endif
break;
default:
break;
}
return ERR_OK;
}
static int open_dataconnection (struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
if (fsm->passive)
return 0;
/* Allocate memory for the structure that holds the state of the connection. */
fsm->datafs = malloc(sizeof(ftpd_datastate_t));
if (fsm->datafs == NULL) {
LWIP_DEBUGF(FTPD_DEBUG, ("open_dataconnection: Out of memory\n"));
send_msg(pcb, fsm, msg451);
return 1;
}
memset(fsm->datafs, 0, sizeof(ftpd_datastate_t));
fsm->datafs->msgfs = fsm;
fsm->datafs->msgpcb = pcb;
if (sfifo_init(&fsm->datafs->fifo, 2000) != 0) {
free(fsm->datafs);
send_msg(pcb, fsm, msg451);
return 1;
}
fsm->datapcb = tcp_new();
if (fsm->datapcb == NULL) {
sfifo_close(&fsm->datafs->fifo);
free(fsm->datafs);
send_msg(pcb, fsm, msg451);
return 1;
}
/* Tell TCP that this is the structure we wish to be passed for our callbacks. */
tcp_arg(fsm->datapcb, fsm->datafs);
ip_addr_t dataip;
IP_SET_TYPE_VAL(dataip, IPADDR_TYPE_V4);
ip4_addr_copy(*ip_2_ip4(&dataip), fsm->dataip);
tcp_connect(fsm->datapcb, &dataip, fsm->dataport, ftpd_dataconnected);
LWIP_DEBUGF(FTPD_DEBUG, ("open con\n"));
return 0;
}
static void cmd_user (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, msg331);
fsm->state = FTPD_PASS;
/*
send_msg(pcb, fs, msgLoginFailed);
fs->state = FTPD_QUIT;
*/
}
static void cmd_pass (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, msg230);
fsm->state = FTPD_IDLE;
/*
send_msg(pcb, fs, msgLoginFailed);
fs->state = FTPD_QUIT;
*/
}
static void cmd_port (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
unsigned pHi, pLo;
unsigned ip[4];
if (sscanf(arg, "%u,%u,%u,%u,%u,%u", &(ip[0]), &(ip[1]), &(ip[2]), &(ip[3]), &pHi, &pLo) != 6)
send_msg(pcb, fsm, msg501);
else {
IP4_ADDR(&fsm->dataip, (u8_t) ip[0], (u8_t) ip[1], (u8_t) ip[2], (u8_t) ip[3]);
fsm->dataport = ((u16_t) pHi << 8) | (u16_t) pLo;
send_msg(pcb, fsm, msg200);
}
}
static void cmd_quit (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, msg221);
fsm->state = FTPD_QUIT;
}
static void cmd_cwd (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, vfs_chdir(vfs_fixpath(arg)) ? msg550 : msg250);
}
static void cmd_cdup (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, vfs_chdir("..") ? msg550 : msg250);
}
static void cmd_pwd (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
char *path;
if ((path = vfs_getcwd(NULL, 0))) {
send_msg(pcb, fsm, msg257PWD, path);
free(path);
} else
send_msg(pcb, fsm, msg550);
}
static void cmd_list_common (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm, int shortlist)
{
vfs_dir_t *vfs_dir;
char *cwd;
if (!(cwd = vfs_getcwd(NULL, 0))) {
send_msg(pcb, fsm, msg451);
return;
}
vfs_dir = vfs_opendir(cwd);
free(cwd);
if (!vfs_dir) {
send_msg(pcb, fsm, msg451);
return;
}
if (open_dataconnection(pcb, fsm) != 0) {
vfs_closedir(vfs_dir);
return;
}
fsm->datafs->vfs_dir = vfs_dir;
fsm->datafs->vfs_dirent = NULL;
fsm->state = shortlist ? FTPD_NLST : FTPD_LIST;
send_msg(pcb, fsm, msg150);
}
static void cmd_nlst (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
cmd_list_common(arg, pcb, fsm, 1);
}
static void cmd_list (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
cmd_list_common(arg, pcb, fsm, 0);
}
static void cmd_retr (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
vfs_file_t *vfs_file;
vfs_stat_t st;
vfs_stat(arg, &st);
if (st.st_mode.directory) {
send_msg(pcb, fsm, msg550);
return;
}
if (!(vfs_file = vfs_open(arg, "rb"))) {
send_msg(pcb, fsm, msg550);
return;
}
send_msg(pcb, fsm, msg150recv, arg, st.st_size);
if (open_dataconnection(pcb, fsm) != 0) {
vfs_close(vfs_file);
return;
}
fsm->datafs->vfs_file = vfs_file;
fsm->state = FTPD_RETR;
}
static void cmd_stor (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
vfs_file_t *vfs_file;
if (!(vfs_file = vfs_open(arg, "wb"))) {
send_msg(pcb, fsm, msg550);
return;
}
send_msg(pcb, fsm, msg150stor, arg);
if (open_dataconnection(pcb, fsm) != 0) {
vfs_close(vfs_file);
return;
}
fsm->datafs->vfs_file = vfs_file;
fsm->state = FTPD_STOR;
}
static void cmd_noop (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, msg200);
}
static void cmd_syst (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
send_msg(pcb, fsm, msg214SYST, "UNIX");
}
static void cmd_pasv (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
static u16_t port = 4096;
static u16_t start_port = 4096;
struct tcp_pcb *temppcb;
/* Allocate memory for the structure that holds the state of the connection. */
if (!(fsm->datafs = malloc(sizeof(ftpd_datastate_t)))) {
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_pasv: Out of memory\n"));
send_msg(pcb, fsm, msg451);
return;
}
memset(fsm->datafs, 0, sizeof(ftpd_datastate_t));
if (sfifo_init(&fsm->datafs->fifo, 3000) != 0) {
free(fsm->datafs);
send_msg(pcb, fsm, msg451);
return;
}
if (!(fsm->datalistenpcb = tcp_new())) {
free(fsm->datafs);
sfifo_close(&fsm->datafs->fifo);
send_msg(pcb, fsm, msg451);
return;
}
start_port = port;
while (1) {
err_t err;
if(++port > 0x7fff)
port = 4096;
fsm->dataport = port;
if ((err = tcp_bind(fsm->datalistenpcb, (ip_addr_t*)&pcb->local_ip, fsm->dataport)) == ERR_OK)
break;
if (start_port == port)
err = ERR_CLSD;
if (err == ERR_USE)
continue;
else {
ftpd_dataclose(fsm->datalistenpcb, fsm->datafs);
fsm->datalistenpcb = NULL;
fsm->datafs = NULL;
return;
}
}
if (!(temppcb = tcp_listen(fsm->datalistenpcb))) {
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_pasv: tcp_listen failed\n"));
ftpd_dataclose(fsm->datalistenpcb, fsm->datafs);
fsm->datalistenpcb = NULL;
fsm->datafs = NULL;
return;
}
fsm->datalistenpcb = temppcb;
fsm->passive = 1;
fsm->datafs->connected = 0;
fsm->datafs->msgfs = fsm;
fsm->datafs->msgpcb = pcb;
/* Tell TCP that this is the structure we wish to be passed for our callbacks. */
tcp_arg(fsm->datalistenpcb, fsm->datafs);
tcp_accept(fsm->datalistenpcb, ftpd_dataconnected);
send_msg(pcb, fsm, msg227, ip4_addr1(ip_2_ip4(&pcb->local_ip)), ip4_addr2(ip_2_ip4(&pcb->local_ip)), ip4_addr3(ip_2_ip4(&pcb->local_ip)), ip4_addr4(ip_2_ip4(&pcb->local_ip)), (fsm->dataport >> 8) & 0xff, (fsm->dataport) & 0xff);
}
static void cmd_abrt (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
if (fsm->datafs != NULL) {
tcp_arg(fsm->datapcb, NULL);
tcp_sent(fsm->datapcb, NULL);
tcp_recv(fsm->datapcb, NULL);
tcp_abort(pcb);
sfifo_close(&fsm->datafs->fifo);
free(fsm->datafs);
fsm->datafs = NULL;
}
fsm->state = FTPD_IDLE;
}
static void cmd_type (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
LWIP_DEBUGF(FTPD_DEBUG, ("Got TYPE -%s-\n", arg));
// if(strcmp(arg, "I") != 0) {
// send_msg(pcb, fsm, msg502);
// return;
// }
send_msg(pcb, fsm, msg200);
}
static void cmd_mode (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
LWIP_DEBUGF(FTPD_DEBUG, ("Got MODE -%s-\n", arg));
send_msg(pcb, fsm, msg502);
}
static void cmd_rnfr (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
if (arg == NULL || *arg == '\0') {
send_msg(pcb, fsm, msg501);
return;
}
if (fsm->renamefrom)
free(fsm->renamefrom);
fsm->renamefrom = malloc(strlen(arg) + 1);
if (fsm->renamefrom == NULL) {
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_rnfr: Out of memory\n"));
send_msg(pcb, fsm, msg451);
return;
}
strcpy(fsm->renamefrom, arg);
fsm->state = FTPD_RNFR;
send_msg(pcb, fsm, msg350);
}
static void cmd_rnto (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
if (fsm->state != FTPD_RNFR) {
send_msg(pcb, fsm, msg503);
return;
}
fsm->state = FTPD_IDLE;
if (arg == NULL) {
send_msg(pcb, fsm, msg501);
return;
}
if (*arg == '\0') {
send_msg(pcb, fsm, msg501);
return;
}
send_msg(pcb, fsm, vfs_rename(fsm->renamefrom, arg) ? msg450 : msg250);
}
static void cmd_mkd (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
if (arg == NULL || *arg == '\0') {
send_msg(pcb, fsm, msg501);
return;
}
send_msg(pcb, fsm, vfs_mkdir(arg /*, VFS_IRWXU | VFS_IRWXG | VFS_IRWXO*/) ? msg550 : msg257, arg);
}
static void cmd_rmd (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
vfs_stat_t st;
if (arg == NULL || *arg == '\0') {
send_msg(pcb, fsm, msg501);
return;
}
if (vfs_stat(vfs_fixpath(arg), &st) != 0) {
send_msg(pcb, fsm, msg550);
return;
}
if (!st.st_mode.directory) {
send_msg(pcb, fsm, msg550);
return;
}
send_msg(pcb, fsm, vfs_rmdir(arg) ? msg550 : msg250);
}
static void cmd_dele (char *arg, struct tcp_pcb *pcb, ftpd_msgstate_t *fsm)
{
vfs_stat_t st;
if (arg == NULL || *arg == '\0') {
send_msg(pcb, fsm, msg501);
return;
}
if (vfs_stat(arg, &st) != 0) {
send_msg(pcb, fsm, msg550);
return;
}
if (st.st_mode.directory) {
send_msg(pcb, fsm, msg550);
return;
}
send_msg(pcb, fsm, vfs_unlink(arg) ? msg550 : msg250);
}
typedef struct {
char const *const cmd;
void (*func) (char *arg, struct tcp_pcb * pcb, ftpd_msgstate_t * fsm);
bool check_busy;
} ftpd_command_t;
static const ftpd_command_t ftpd_commands[] = {
{"USER", cmd_user, 0},
{"PASS", cmd_pass, 0},
{"PORT", cmd_port, 0},
{"QUIT", cmd_quit, 0},
{"CWD", cmd_cwd, 1},
{"CDUP", cmd_cdup, 1},
{"PWD", cmd_pwd, 0},
{"XPWD", cmd_pwd, 0},
{"NLST", cmd_nlst, 1},
{"LIST", cmd_list, 1},
{"RETR", cmd_retr, 1},
{"STOR", cmd_stor, 1},
{"NOOP", cmd_noop, 0},
{"SYST", cmd_syst, 0},
{"ABOR", cmd_abrt, 0},
{"TYPE", cmd_type, 0},
{"MODE", cmd_mode, 0},
{"RNFR", cmd_rnfr, 1},
{"RNTO", cmd_rnto, 1},
{"MKD", cmd_mkd, 1},
{"XMKD", cmd_mkd, 1},