-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_f.cpp
692 lines (567 loc) · 16 KB
/
ftp_f.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
/* Revision: 2.8.7 */
/******************************************************************************
* Copyright 1998-2018 NetBurner, Inc. ALL RIGHTS RESERVED
*
* Permission is hereby granted to purchasers of NetBurner Hardware to use or
* modify this computer program for any use as long as the resultant program
* is only executed on NetBurner provided hardware.
*
* No other rights to use this program or its derivatives in part or in
* whole are granted.
*
* It may be possible to license this or other NetBurner software for use on
* non-NetBurner Hardware. Contact sales@Netburner.com for more information.
*
* NetBurner makes no representation or warranties with respect to the
* performance of this computer program, and specifically disclaims any
* responsibility for any damages, special or consequential, connected with
* the use of this program.
*
* NetBurner
* 5405 Morehouse Dr.
* San Diego, CA 92121
* www.netburner.com
******************************************************************************/
/**
* Handles and implements high level FTP server API calls.
*/
// NB Libs
#include <startnet.h>
#include <tcp.h>
// NB FTP
#include <ftpd.h>
#ifdef USE_MMC
#include <effs_fat/mmc_mcf.h>
#endif
#ifdef USE_CFC
#include <effs_fat/cfc_mcf.h>
#endif
#include "cardtype.h"
#include "FileSystemUtils.h"
#include "ftp_f.h"
#define LOGME iprintf("We made it to line %d of file %s.\r\n", __LINE__, __FILE__);
#define FTP_BUFFER_SIZE (32 * 1024)
static int FileSysRetryLimit = 10;
static int NetworkRetryLimit = 10;
static char FTP_buffer[FTP_BUFFER_SIZE] __attribute__((aligned(16)));
static const char mstr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
/**
* @brief Set the date.
*/
static void settimedate(F_FIND *f)
{
int nret = f_settimedate(f->filename, f_gettime(), f_getdate());
if (nret == F_NO_ERROR)
{
// iprintf( "Time stamping successful\r\n" );
}
else
{
iprintf("Time stamping failed: %d\r\n", nret);
}
}
/**
* @brief This function displays information in MTTTY of subdirectories
* and files in a current directory. The information displayed is: the
* filename, modified time stamp, modified date stamp, and file size.
*/
static void gettimedate(F_FIND *f)
{
unsigned short t, d;
int nret = f_gettimedate(f->filename, &t, &d);
if (nret == F_NO_ERROR)
{
iprintf("%15s |", f->filename);
iprintf("%2.2d:%2.2d:%2.2d |", ((t & 0xF800) >> 11), ((t & 0x07E0) >> 5), 2 * (t & 0x001F));
iprintf("%2.2d/%2.2d/%4d |", ((d & 0x01E0) >> 5), (d & 0x001F), (1980 + ((d & 0xFE00) >> 9)));
iprintf("%9ld Bytes\r\n", f->filesize);
}
else
{
iprintf("Time stamp retrieval failed: %d\r\n", nret);
}
}
/**
* @brief Generate date string.
*/
static void getdatestring(F_FIND *f, char *tmp)
{
// Converts file time and date stamp to a value that can be interpreted by the user.
// unsigned short sec = 2 * ( ( f->ctime ) & 0x1F );
unsigned short minute = ((f->ctime) & 0x7E0) >> 5;
unsigned short hour = ((f->ctime) & 0xF800) >> 11;
unsigned short day = (f->cdate) & 0x1F;
unsigned short month = ((f->cdate) & 0x01E0) >> 5;
unsigned short year = 1980 + (((f->cdate) & 0xFE00) >> 9);
// For FTP file properties: If the current year matches the year stamp of the
// associated file, then the hour and minutes are displayed. Otherwise, the
// year is used in place of hour and minutes.
if ((1980 + (((f_getdate()) & 0xFE00) >> 9)) == year)
{
siprintf(tmp, "%3s %2d %2.2d:%2.2d", mstr[month - 1], day, hour, minute);
}
else
{
siprintf(tmp, "%3s %2d %4d", mstr[month - 1], day, year);
}
}
/**
* @brief Generate directory entry string.
*/
static void getdirstring(F_FIND *f, char *dst)
{
char tmp[256];
if ((f->attr) & F_ATTR_DIR)
{
dst[0] = 'd';
}
else
{
dst[0] = '-';
}
dst[1] = 0;
strcat(dst, "-rw-rw-rw- 1 none");
strcat(dst, " ");
sniprintf(tmp, 256, "%9ld", f->filesize);
strcat(dst, tmp);
strcat(dst, " ");
getdatestring(f, tmp);
strcat(dst, tmp);
strcat(dst, " ");
strcat(dst, f->filename);
}
/**
* @brief Start the FTP session.
*/
void *FTPDSessionStart(const char *user, const char *passwd, const IPADDR4 hi_ip)
{
iprintf(" Starting FTP session\r\n");
#ifdef USE_MMC
f_chdrive(MMC_DRV_NUM);
#endif /* USE_MMC */
#ifdef USE_CFC
f_chdrive(CFC_DRV_NUM);
#endif /* USE_CFC */
f_chdir("\\");
return ((void *)1);
}
/**
* @brief Finish the FTP session.
*/
void FTPDSessionEnd(void *pSession) {}
/**
* @brief Check for a directory.
*/
int FTPD_DirectoryExists(const char *full_directory, void *pSession)
{
if (*full_directory == 0) { return (FTPD_OK); }
f_chdir("\\");
if (f_chdir((char *)full_directory))
{
return (FTPD_FAIL);
}
else
{
return (FTPD_OK);
}
return (FTPD_OK);
}
/**
* @brief Create a directory.
*/
int FTPD_CreateSubDirectory(const char *current_directory, const char *new_dir, void *pSession)
{
int rc;
f_chdir("/");
if (*current_directory)
{
rc = f_chdir((char *)current_directory);
if (rc) { return (FTPD_FAIL); }
}
rc = f_mkdir((char *)new_dir);
if (rc == 0) { return (FTPD_OK); }
return (FTPD_FAIL);
}
/**
* @brief Delete a directory.
*/
int FTPD_DeleteSubDirectory(const char *current_directory, const char *sub_dir, void *pSession)
{
int rc;
f_chdir("/");
if (*current_directory)
{
rc = f_chdir((char *)current_directory);
if (rc) { return (FTPD_FAIL); }
}
rc = f_rmdir((char *)sub_dir);
if (rc == 0) { return (FTPD_OK); }
return (FTPD_FAIL);
}
/**
* @brief List directories.
*/
int FTPD_ListSubDirectories(const char *current_directory, void *pSession, FTPDCallBackReportFunct *pFunc, int socket)
{
F_FIND find;
long rc;
char s[256];
f_chdir("/");
if (*current_directory)
{
if (f_chdir((char *)current_directory)) { return (FTPD_FAIL); }
}
rc = f_findfirst("*.*", &find);
if (!rc)
{
do
{
if (find.attr & F_ATTR_DIR)
{
getdirstring(&find, s);
pFunc(socket, s);
}
} while (!f_findnext(&find));
}
return (FTPD_OK);
}
/**
* @brief Check for a file.
*/
int FTPD_FileExists(const char *full_directory, const char *file_name, void *pSession)
{
F_FILE *t;
if (strcmp(file_name, "_format") == 0 || strcmp(file_name, "_hformat") == 0
#ifdef USE_CFC
|| strcmp(file_name, "_cfc") == 0
#endif /* USE_CFC */
#ifdef USE_HDD
|| strcmp(file_name, "_hdd") == 0
#endif /* USE_HDD */
#ifdef USE_MMC
|| strcmp(file_name, "_mmc") == 0
#endif /* USE_MMC */
#ifdef USE_FATRAM
|| strcmp(file_name, "_fram") == 0
#endif /* USE_FATRAM */
#ifdef USE_NOR
|| strcmp(file_name, "_nor") == 0
#endif /* USE_NOR */
#ifdef USE_STDRAM
|| strcmp(file_name, "_sram") == 0
#endif /* USE_STDRAM */
)
{
return (FTPD_OK);
}
f_chdir("/");
if (*full_directory)
{
if (f_chdir((char *)full_directory)) { return (FTPD_FAIL); }
}
t = f_open((char *)file_name, "r");
if (t)
{
f_close(t);
return (FTPD_OK);
}
return (FTPD_FAIL);
}
/**
* @brief Get a file's size.
*/
int FTPD_GetFileSize(const char *full_directory, const char *file_name)
{
F_STAT stat;
f_chdir("/");
uint32_t len = strlen(file_name);
if ((file_name[len - 1] != '/') && (FTPD_FileExists(full_directory, file_name, nullptr) == FTPD_FAIL))
{
return FTPD_FILE_SIZE_NOSUCH_FILE;
}
if (*full_directory)
{
if (f_chdir((char *)full_directory)) { return FTPD_FILE_SIZE_NOSUCH_FILE; }
}
if (file_name[len - 1] == '/')
{
stat.filesize = 0;
}
else if (f_stat(file_name, &stat))
{
return FTPD_FILE_SIZE_NOSUCH_FILE;
}
return stat.filesize;
}
/**
* @brief Retrieve function for FTP (get).
*/
int FTPD_SendFileToClient(const char *full_directory, const char *file_name, void *pSession, int fd)
{
F_FILE *rfile; // Actual opened file
int TotalBytesWritten = 0, BytesWritten = 0, BytesRead = 0, RetryAttempts = 0, TransferError = 0;
#ifdef USE_NOR
if (strcmp(file_name, "_nor") == 0)
{
// iprintf( "Change to NOR.\r\n" );
f_chdrive(NOR_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_NOR */
#ifdef USE_STDRAM
if (strcmp(file_name, "_sram") == 0)
{
// iprintf( "Change to STDRAM.\r\n" );
f_chdrive(STDRAM_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_STDRAM */
#ifdef USE_MMC
if (strcmp(file_name, "_mmc") == 0)
{
// iprintf( "Change to MMC.\r\n" );
f_chdrive(MMC_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_MMC */
#ifdef USE_CFC
if (strcmp(file_name, "_cfc") == 0)
{
// iprintf( "Change to CFC.\r\n" );
f_chdrive(CFC_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_CFC */
#ifdef USE_HDD
if (strcmp(file_name, "_hdd") == 0)
{
// iprintf( "Change to HDD.\r\n" );
f_chdrive(HDD_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_HDD */
#ifdef USE_FATRAM
if (strcmp(file_name, "_fram") == 0)
{
// iprintf( "Change to FATRAM.\r\n" );
f_chdrive(FATRAM_DRV_NUM);
return (FTPD_FAIL);
}
#endif /* USE_FATRAM */
if (strcmp(file_name, "_format") == 0)
{
// iprintf( "Formatting...\r\n" );
f_format(f_getdrive(), F_FAT32_MEDIA);
return (FTPD_FAIL);
}
f_chdir("/");
if (*full_directory)
{
if (f_chdir((char *)full_directory)) { return (FTPD_FAIL); }
}
/* If file is not open, then try to open it. */
rfile = f_open((char *)file_name, "r");
/* Return with error if not successful. */
if (!rfile) { return (FTPD_FAIL); }
SetSocketTxBuffers(fd, 20);
while (!f_eof(rfile))
{
BytesRead = 0;
RetryAttempts = 0;
while ((RetryAttempts < FileSysRetryLimit) && (BytesRead == 0))
{
BytesRead = f_read(FTP_buffer, 1, FTP_BUFFER_SIZE, rfile); // Read from file
// retry if busy
if ((BytesRead == 0) && (!f_eof(rfile)))
{
int error = f_getlasterror();
if (error) { DisplayEffsErrorCode(error); }
OSTimeDly(TICKS_PER_SECOND / 4);
RetryAttempts++;
}
}
if (RetryAttempts >= FileSysRetryLimit)
{
TransferError = 1;
break;
}
BytesWritten = 0;
TotalBytesWritten = 0;
RetryAttempts = 0;
while ((RetryAttempts < NetworkRetryLimit) && (TotalBytesWritten != BytesRead))
{
BytesWritten = write(fd, FTP_buffer + TotalBytesWritten, BytesRead - TotalBytesWritten);
// retry if busy
if (BytesWritten <= 0)
{
RetryAttempts++;
OSTimeDly(TICKS_PER_SECOND / 4);
}
TotalBytesWritten += BytesWritten;
}
if (RetryAttempts >= NetworkRetryLimit) { TransferError = 2; }
}
f_close(rfile); // Close the file
if (TransferError)
{
if (TransferError == 1)
{
iprintf("There was an error reading %s/%s from file system\r\n", full_directory, file_name);
}
else if (TransferError == 2)
{
iprintf("There was an error writing %s/%s to the network\r\n", full_directory, file_name);
}
return (FTPD_FAIL);
}
else
{
return (FTPD_OK);
}
}
/**
* @brief Check if it is possible to create a file.
*/
int FTPD_AbleToCreateFile(const char *full_directory, const char *file_name, void *pSession)
{
return (FTPD_OK);
}
/**
* @brief Write file to drive.
*/
int FTPD_GetFileFromClient(const char *full_directory, const char *file_name, void *pSession, int fd)
{
F_FILE *wfile = 0; // Actual opened file
F_FIND find;
int BytesWritten = 0, LastBytesWritten = 0, BytesRead = 0, RetryAttempts = 0, TransferError = 0;
f_chdir("/");
if (*full_directory)
{
if (f_chdir((char *)full_directory)) { return (FTPD_FAIL); }
}
/* Only accept file names of size 12 (8+1+3). */
// if ( strlen( ( char * ) file_name ) > 12 ) return( FTPD_FAIL );
wfile = f_open((char *)file_name, "w"); // Open it for write
int rc = f_findfirst(file_name, &find);
if (rc == 0)
{
if (!(find.attr & F_ATTR_DIR)) { settimedate(&find); }
}
else
{
iprintf("f_findfirst failed\r\n");
}
/* Return error if not successful. */
if (!wfile) { return (FTPD_FAIL); }
SetSocketRxBuffers(fd, 20);
while (1)
{
// Read file data from the FTP data socket
RetryAttempts = 0;
BytesRead = 0;
while ((BytesRead == 0) && (RetryAttempts < NetworkRetryLimit))
{
BytesRead = ReadWithTimeout(fd, FTP_buffer, FTP_BUFFER_SIZE, TICKS_PER_SECOND);
RetryAttempts++;
}
if ((RetryAttempts >= NetworkRetryLimit) && (BytesRead == 0)) { TransferError = 2; }
if (BytesRead < 1) { break; }
// Now write the file data to the file system
BytesWritten = 0;
LastBytesWritten = 0;
RetryAttempts = 0;
while ((RetryAttempts < FileSysRetryLimit) && (BytesWritten < BytesRead))
{
BytesWritten += f_write(FTP_buffer + BytesWritten, 1, BytesRead - BytesWritten, wfile);
if (BytesWritten == LastBytesWritten)
{
int error = f_getlasterror();
if (error) DisplayEffsErrorCode(error);
OSTimeDly(TICKS_PER_SECOND / 4);
RetryAttempts++;
// iprintf("Retry for write = %d\r\n", RetryAttempts);
}
LastBytesWritten = BytesWritten;
}
if (RetryAttempts >= FileSysRetryLimit) { TransferError = 1; }
if (BytesRead < 1) { break; }
}
f_close(wfile);
if (TransferError)
{
if (TransferError == 1)
{
iprintf("There was an error writing %s/%s to file system\r\n", full_directory, file_name);
}
else if (TransferError == 2)
{
iprintf("There was an error reading %s/%s from the network\r\n", full_directory, file_name);
}
return (FTPD_FAIL);
}
else
{
return (FTPD_OK);
}
}
/**
* @brief Delete a file.
*/
int FTPD_DeleteFile(const char *current_directory, const char *file_name, void *pSession)
{
f_chdir("/");
if (*current_directory)
{
if (f_chdir((char *)current_directory)) { return (FTPD_FAIL); }
}
if (f_delete((char *)file_name)) { return (FTPD_FAIL); }
return (FTPD_OK);
}
/**
* @brief Get a file list.
*/
int FTPD_ListFile(const char *current_directory, void *pSession, FTPDCallBackReportFunct *pFunc, int socket)
{
F_FIND find;
char s[256];
int rc;
f_chdir("/");
if (*current_directory)
{
if (f_chdir((char *)current_directory)) { return (FTPD_FAIL); }
}
rc = f_findfirst("*.*", &find);
if (rc == 0)
{
// iprintf("\r\nDisplaying time/date information\r\n");
do
{
if (!(find.attr & F_ATTR_DIR))
{
getdirstring(&find, s);
gettimedate(&find);
pFunc(socket, s);
}
} while (!f_findnext(&find));
// iprintf("\r\n");
}
// else
//{
// sniprintf( s, 256, "Card error: %d", rc );
// pFunc( socket, s );
//}
return (FTPD_OK);
}
/**
* @brief Rename a file.
*/
int FTPD_Rename(const char *full_directory, const char *old_file_name, const char *new_file_name, void *pSession)
{
f_chdir("/");
if (*full_directory)
{
if (f_chdir((char *)full_directory)) { return (FTPD_FAIL); }
}
if (f_rename(old_file_name, new_file_name)) { return FTPD_FAIL; }
return FTPD_OK;
}