-
Notifications
You must be signed in to change notification settings - Fork 33
/
ftpproto.c
1379 lines (1181 loc) · 27.2 KB
/
ftpproto.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
#include "ftpproto.h"
#include "sysutil.h"
#include "str.h"
#include "ftpcodes.h"
#include "tunable.h"
#include "privsock.h"
void ftp_lreply(session_t *sess, int status, const char *text);
void handle_alarm_timeout(int sig);
void handle_sigalrm(int sig);
void handle_sigurg(int sig);
void start_cmdio_alarm(void);
void start_data_alarm(void);
void check_abor(session_t *sess);
int list_common(session_t *sess, int detail);
void limit_rate(session_t *sess, int bytes_transfered, int is_upload);
void upload_common(session_t *sess, int is_append);
int get_port_fd(session_t *sess);
int get_pasv_fd(session_t *sess);
int get_transfer_fd(session_t *sess);
int port_active(session_t *sess);
int pasv_active(session_t *sess);
static void do_user(session_t *sess);
static void do_pass(session_t *sess);
static void do_cwd(session_t *sess);
static void do_cdup(session_t *sess);
static void do_quit(session_t *sess);
static void do_port(session_t *sess);
static void do_pasv(session_t *sess);
static void do_type(session_t *sess);
//static void do_stru(session_t *sess);
//static void do_mode(session_t *sess);
static void do_retr(session_t *sess);
static void do_stor(session_t *sess);
static void do_appe(session_t *sess);
static void do_list(session_t *sess);
static void do_nlst(session_t *sess);
static void do_rest(session_t *sess);
static void do_abor(session_t *sess);
static void do_pwd(session_t *sess);
static void do_mkd(session_t *sess);
static void do_rmd(session_t *sess);
static void do_dele(session_t *sess);
static void do_rnfr(session_t *sess);
static void do_rnto(session_t *sess);
static void do_site(session_t *sess);
static void do_syst(session_t *sess);
static void do_feat(session_t *sess);
static void do_size(session_t *sess);
static void do_stat(session_t *sess);
static void do_noop(session_t *sess);
static void do_help(session_t *sess);
static void do_site_chmod(session_t *sess, char *chmod_arg);
static void do_site_umask(session_t *sess, char *umask_arg);
typedef struct ftpcmd
{
const char *cmd;
void (*cmd_handler)(session_t *sess);
} ftpcmd_t;
static ftpcmd_t ctrl_cmds[] = {
/* 访问控制命令 */
{"USER", do_user },
{"PASS", do_pass },
{"CWD", do_cwd },
{"XCWD", do_cwd },
{"CDUP", do_cdup },
{"XCUP", do_cdup },
{"QUIT", do_quit },
{"ACCT", NULL },
{"SMNT", NULL },
{"REIN", NULL },
/* 传输参数命令 */
{"PORT", do_port },
{"PASV", do_pasv },
{"TYPE", do_type },
{"STRU", /*do_stru*/NULL },
{"MODE", /*do_mode*/NULL },
/* 服务命令 */
{"RETR", do_retr },
{"STOR", do_stor },
{"APPE", do_appe },
{"LIST", do_list },
{"NLST", do_nlst },
{"REST", do_rest },
{"ABOR", do_abor },
{"\377\364\377\362ABOR", do_abor},
{"PWD", do_pwd },
{"XPWD", do_pwd },
{"MKD", do_mkd },
{"XMKD", do_mkd },
{"RMD", do_rmd },
{"XRMD", do_rmd },
{"DELE", do_dele },
{"RNFR", do_rnfr },
{"RNTO", do_rnto },
{"SITE", do_site },
{"SYST", do_syst },
{"FEAT", do_feat },
{"SIZE", do_size },
{"STAT", do_stat },
{"NOOP", do_noop },
{"HELP", do_help },
{"STOU", NULL },
{"ALLO", NULL }
};
session_t *p_sess;
void handle_alarm_timeout(int sig)
{
shutdown(p_sess->ctrl_fd, SHUT_RD);
ftp_reply(p_sess, FTP_IDLE_TIMEOUT, "Timeout.");
shutdown(p_sess->ctrl_fd, SHUT_WR);
exit(EXIT_FAILURE);
}
void handle_sigalrm(int sig)
{
if (!p_sess->data_process)
{
ftp_reply(p_sess, FTP_DATA_TIMEOUT, "Data timeout. Reconnect. Sorry.");
exit(EXIT_FAILURE);
}
// 否则,当前处于数据传输的状态收到了超时信号
p_sess->data_process = 0;
start_data_alarm();
}
void handle_sigurg(int sig)
{
if (p_sess->data_fd == -1)
{
return;
}
char cmdline[MAX_COMMAND_LINE] = {0};
int ret = readline(p_sess->ctrl_fd, cmdline, MAX_COMMAND_LINE);
if (ret <= 0)
{
ERR_EXIT("readline");
}
str_trim_crlf(cmdline);
if (strcmp(cmdline, "ABOR") == 0
|| strcmp(cmdline, "\377\364\377\362ABOR") == 0)
{
p_sess->abor_received = 1;
shutdown(p_sess->data_fd, SHUT_RDWR);
}
else
{
ftp_reply(p_sess, FTP_BADCMD, "Unknown command.");
}
}
void check_abor(session_t *sess)
{
if (sess->abor_received)
{
sess->abor_received = 0;
ftp_reply(p_sess, FTP_ABOROK, "ABOR successful.");
}
}
void start_cmdio_alarm(void)
{
if (tunable_idle_session_timeout > 0)
{
// 安装信号
signal(SIGALRM, handle_alarm_timeout);
// 启动闹钟
alarm(tunable_idle_session_timeout);
}
}
void start_data_alarm(void)
{
if (tunable_data_connection_timeout > 0)
{
// 安装信号
signal(SIGALRM, handle_sigalrm);
// 启动闹钟
alarm(tunable_data_connection_timeout);
}
else if (tunable_idle_session_timeout > 0)
{
// 关闭先前安装的闹钟
alarm(0);
}
}
void handle_child(session_t *sess)
{
ftp_reply(sess, FTP_GREET, "(miniftpd 0.1)");
int ret;
while (1)
{
memset(sess->cmdline, 0, sizeof(sess->cmdline));
memset(sess->cmd, 0, sizeof(sess->cmd));
memset(sess->arg, 0, sizeof(sess->arg));
start_cmdio_alarm();
ret = readline(sess->ctrl_fd, sess->cmdline, MAX_COMMAND_LINE);
if (ret == -1)
ERR_EXIT("readline");
else if (ret == 0)
exit(EXIT_SUCCESS);
printf("cmdline=[%s]\n", sess->cmdline);
// 去除\r\n
str_trim_crlf(sess->cmdline);
printf("cmdline=[%s]\n", sess->cmdline);
// 解析FTP命令与参数
str_split(sess->cmdline, sess->cmd, sess->arg, ' ');
printf("cmd=[%s] arg=[%s]\n", sess->cmd, sess->arg);
// 将命令转换为大写
str_upper(sess->cmd);
// 处理FTP命令
/*
if (strcmp("USER", sess->cmd) == 0)
{
do_user(sess);
}
else if (strcmp("PASS", sess->cmd) == 0)
{
do_pass(sess);
}
*/
int i;
int size = sizeof(ctrl_cmds) / sizeof(ctrl_cmds[0]);
for (i=0; i<size; i++)
{
if (strcmp(ctrl_cmds[i].cmd, sess->cmd) == 0)
{
if (ctrl_cmds[i].cmd_handler != NULL)
{
ctrl_cmds[i].cmd_handler(sess);
}
else
{
ftp_reply(sess, FTP_COMMANDNOTIMPL, "Unimplement command.");
}
break;
}
}
if (i == size)
{
ftp_reply(sess, FTP_BADCMD, "Unknown command.");
}
}
}
void ftp_reply(session_t *sess, int status, const char *text)
{
char buf[1024] = {0};
sprintf(buf, "%d %s\r\n", status, text);
writen(sess->ctrl_fd, buf, strlen(buf));
}
void ftp_lreply(session_t *sess, int status, const char *text)
{
char buf[1024] = {0};
sprintf(buf, "%d-%s\r\n", status, text);
writen(sess->ctrl_fd, buf, strlen(buf));
}
int list_common(session_t *sess, int detail)
{
DIR *dir = opendir(".");
if (dir == NULL)
{
return 0;
}
struct dirent *dt;
struct stat sbuf;
while ((dt = readdir(dir)) != NULL)
{
if (lstat(dt->d_name, &sbuf) < 0)
{
continue;
}
if (dt->d_name[0] == '.')
continue;
char buf[1024] = {0};
if (detail)
{
const char *perms = statbuf_get_perms(&sbuf);
int off = 0;
off += sprintf(buf, "%s ", perms);
off += sprintf(buf + off, " %3d %-8d %-8d ", sbuf.st_nlink, sbuf.st_uid, sbuf.st_gid);
off += sprintf(buf + off, "%8lu ", (unsigned long)sbuf.st_size);
const char *datebuf = statbuf_get_date(&sbuf);
off += sprintf(buf + off, "%s ", datebuf);
if (S_ISLNK(sbuf.st_mode))
{
char tmp[1024] = {0};
readlink(dt->d_name, tmp, sizeof(tmp));
off += sprintf(buf + off, "%s -> %s\r\n", dt->d_name, tmp);
}
else
{
off += sprintf(buf + off, "%s\r\n", dt->d_name);
}
}
else
{
sprintf(buf, "%s\r\n", dt->d_name);
}
//printf("%s", buf);
writen(sess->data_fd, buf, strlen(buf));
}
closedir(dir);
return 1;
}
void limit_rate(session_t *sess, int bytes_transfered, int is_upload)
{
sess->data_process = 1;
// 睡眠时间 = (当前传输速度 / 最大传输速度 – 1) * 当前传输时间;
long curr_sec = get_time_sec();
long curr_usec = get_time_usec();
double elapsed;
elapsed = (double)(curr_sec - sess->bw_transfer_start_sec);
elapsed += (double)(curr_usec - sess->bw_transfer_start_usec) / (double)1000000;
if (elapsed <= (double)0)
{
elapsed = (double)0.01;
}
// 计算当前传输速度
unsigned int bw_rate = (unsigned int)((double)bytes_transfered / elapsed);
double rate_ratio;
if (is_upload)
{
if (bw_rate <= sess->bw_upload_rate_max)
{
// 不需要限速
sess->bw_transfer_start_sec = curr_sec;
sess->bw_transfer_start_usec = curr_usec;
return;
}
rate_ratio = bw_rate / sess->bw_upload_rate_max;
}
else
{
if (bw_rate <= sess->bw_download_rate_max)
{
// 不需要限速
sess->bw_transfer_start_sec = curr_sec;
sess->bw_transfer_start_usec = curr_usec;
return;
}
rate_ratio = bw_rate / sess->bw_download_rate_max;
}
// 睡眠时间 = (当前传输速度 / 最大传输速度 – 1) * 当前传输时间;
double pause_time;
pause_time = (rate_ratio - (double)1) * elapsed;
nano_sleep(pause_time);
sess->bw_transfer_start_sec = get_time_sec();
sess->bw_transfer_start_usec = get_time_usec();
}
void upload_common(session_t *sess, int is_append)
{
// 创建数据连接
if (get_transfer_fd(sess) == 0)
{
return;
}
long long offset = sess->restart_pos;
sess->restart_pos = 0;
// 打开文件
int fd = open(sess->arg, O_CREAT | O_WRONLY, 0666);
if (fd == -1)
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
int ret;
// 加写锁
ret = lock_file_write(fd);
if (ret == -1)
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
// STOR
// REST+STOR
// APPE
if (!is_append && offset == 0) // STOR
{
ftruncate(fd, 0);
if (lseek(fd, 0, SEEK_SET) < 0)
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
}
else if (!is_append && offset != 0) // REST+STOR
{
if (lseek(fd, offset, SEEK_SET) < 0)
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
}
else if (is_append) // APPE
{
if (lseek(fd, 0, SEEK_END) < 0)
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
}
struct stat sbuf;
ret = fstat(fd, &sbuf);
if (!S_ISREG(sbuf.st_mode))
{
ftp_reply(sess, FTP_UPLOADFAIL, "Could not create file.");
return;
}
// 150
char text[1024] = {0};
if (sess->is_ascii)
{
sprintf(text, "Opening ASCII mode data connection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
else
{
sprintf(text, "Opening BINARY mode data connection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
ftp_reply(sess, FTP_DATACONN, text);
int flag = 0;
// 上传文件
char buf[1024];
sess->bw_transfer_start_sec = get_time_sec();
sess->bw_transfer_start_usec = get_time_usec();
while (1)
{
ret = read(sess->data_fd, buf, sizeof(buf));
if (ret == -1)
{
if (errno == EINTR)
{
continue;
}
else
{
flag = 2;
break;
}
}
else if (ret == 0)
{
flag = 0;
break;
}
limit_rate(sess, ret, 1);
if (sess->abor_received)
{
flag = 2;
break;
}
if (writen(fd, buf, ret) != ret)
{
flag = 1;
break;
}
}
// 关闭数据套接字
close(sess->data_fd);
sess->data_fd = -1;
close(fd);
if (flag == 0 && !sess->abor_received)
{
// 226
ftp_reply(sess, FTP_TRANSFEROK, "Transfer complete.");
}
else if (flag == 1)
{
// 451
ftp_reply(sess, FTP_BADSENDFILE, "Failure writting to local file.");
}
else if (flag == 2)
{
// 426
ftp_reply(sess, FTP_BADSENDNET, "Failure reading from network stream.");
}
check_abor(sess);
// 重新开启控制连接通道闹钟
start_cmdio_alarm();
}
int port_active(session_t *sess)
{
if (sess->port_addr)
{
if (pasv_active(sess))
{
fprintf(stderr, "both port an pasv are active");
exit(EXIT_FAILURE);
}
return 1;
}
return 0;
}
int pasv_active(session_t *sess)
{
/*
if (sess->pasv_listen_fd != -1)
{
if (port_active(sess))
{
fprintf(stderr, "both port an pasv are active");
exit(EXIT_FAILURE);
}
return 1;
}
*/
priv_sock_send_cmd(sess->child_fd, PRIV_SOCK_PASV_ACTIVE);
int active = priv_sock_get_int(sess->child_fd);
if (active)
{
if (port_active(sess))
{
fprintf(stderr, "both port an pasv are active");
exit(EXIT_FAILURE);
}
return 1;
}
return 0;
}
int get_port_fd(session_t *sess)
{
/*
向nobody发送PRIV_SOCK_GET_DATA_SOCK命令 1
向nobody发送一个整数port 4
向nobody发送一个字符串ip 不定长
*/
priv_sock_send_cmd(sess->child_fd, PRIV_SOCK_GET_DATA_SOCK);
unsigned short port = ntohs(sess->port_addr->sin_port);
char *ip = inet_ntoa(sess->port_addr->sin_addr);
priv_sock_send_int(sess->child_fd, (int)port);
priv_sock_send_buf(sess->child_fd, ip, strlen(ip));
char res = priv_sock_get_result(sess->child_fd);
if (res == PRIV_SOCK_RESULT_BAD)
{
return 0;
}
else if (res == PRIV_SOCK_RESULT_OK)
{
sess->data_fd = priv_sock_recv_fd(sess->child_fd);
}
return 1;
}
int get_pasv_fd(session_t *sess)
{
priv_sock_send_cmd(sess->child_fd, PRIV_SOCK_PASV_ACCEPT);
char res = priv_sock_get_result(sess->child_fd);
if (res == PRIV_SOCK_RESULT_BAD)
{
return 0;
}
else if (res == PRIV_SOCK_RESULT_OK)
{
sess->data_fd = priv_sock_recv_fd(sess->child_fd);
}
return 1;
}
int get_transfer_fd(session_t *sess)
{
// 检测是否收到PORT或者PASV命令
if (!port_active(sess) && !pasv_active(sess))
{
//printf("yyyyyyyyyyyyyyyy\n");
ftp_reply(sess, FTP_BADSENDCONN, "Use PORT or PASV first.");
return 0;
}
int ret = 1;
// 如果是主动模式
if (port_active(sess))
{
/*
socket
bind 20
connect
*/
// tcp_client(20);
/*
int fd = tcp_client(0);
if (connect_timeout(fd, sess->port_addr, tunable_connect_timeout) < 0)
{
close(fd);
return 0;
}
sess->data_fd = fd;
*/
if (get_port_fd(sess) == 0)
{
ret = 0;
}
}
if (pasv_active(sess))
{
/*
int fd = accept_timeout(sess->pasv_listen_fd, NULL, tunable_accept_timeout);
close(sess->pasv_listen_fd);
if (fd == -1)
{
return 0;
}
sess->data_fd = fd;
*/
if (get_pasv_fd(sess) == 0)
{
ret = 0;
}
}
if (sess->port_addr)
{
free(sess->port_addr);
sess->port_addr = NULL;
}
if (ret)
{
// 重新安装SIGALRM信号,并启动闹钟
start_data_alarm();
}
return ret;
}
static void do_user(session_t *sess)
{
//USER jjl
struct passwd *pw = getpwnam(sess->arg);
if (pw == NULL)
{
// 用户不存在
ftp_reply(sess, FTP_LOGINERR, "Login incorrect.");
return;
}
sess->uid = pw->pw_uid;
ftp_reply(sess, FTP_GIVEPWORD, "Please specify the password.");
}
static void do_pass(session_t *sess)
{
// PASS 123456
struct passwd *pw = getpwuid(sess->uid);
if (pw == NULL)
{
// 用户不存在
ftp_reply(sess, FTP_LOGINERR, "Login incorrect.");
return;
}
printf("name=[%s]\n", pw->pw_name);
struct spwd *sp = getspnam(pw->pw_name);
if (sp == NULL)
{
ftp_reply(sess, FTP_LOGINERR, "Login incorrect.");
return;
}
// 将明文进行加密
char *encrypted_pass = crypt(sess->arg, sp->sp_pwdp);
// 验证密码
if (strcmp(encrypted_pass, sp->sp_pwdp) != 0)
{
ftp_reply(sess, FTP_LOGINERR, "Login incorrect.");
return;
}
signal(SIGURG, handle_sigurg);
activate_sigurg(sess->ctrl_fd);
umask(tunable_local_umask);
setegid(pw->pw_gid);
seteuid(pw->pw_uid);
chdir(pw->pw_dir);
ftp_reply(sess, FTP_LOGINOK, "Login successful.");
}
static void do_cwd(session_t *sess)
{
if (chdir(sess->arg) < 0)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to change directory.");
return;
}
ftp_reply(sess, FTP_CWDOK, "Directory successfully changed.");
}
static void do_cdup(session_t *sess)
{
if (chdir("..") < 0)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to change directory.");
return;
}
ftp_reply(sess, FTP_CWDOK, "Directory successfully changed.");
}
static void do_quit(session_t *sess)
{
ftp_reply(sess, FTP_GOODBYE, "Goodbye.");
exit(EXIT_SUCCESS);
}
static void do_port(session_t *sess)
{
//PORT 192,168,0,100,123,233
unsigned int v[6];
sscanf(sess->arg, "%u,%u,%u,%u,%u,%u", &v[2], &v[3], &v[4], &v[5], &v[0], &v[1]);
sess->port_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
memset(sess->port_addr, 0, sizeof(struct sockaddr_in));
sess->port_addr->sin_family = AF_INET;
unsigned char *p = (unsigned char *)&sess->port_addr->sin_port;
p[0] = v[0];
p[1] = v[1];
p = (unsigned char *)&sess->port_addr->sin_addr;
p[0] = v[2];
p[1] = v[3];
p[2] = v[4];
p[3] = v[5];
ftp_reply(sess, FTP_PORTOK, "PORT command successful. Consider using PASV.");
}
static void do_pasv(session_t *sess)
{
//Entering Passive Mode (192,168,244,100,101,46).
char ip[16] = {0};
getlocalip(ip);
/*
sess->pasv_listen_fd = tcp_server(ip, 0);
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
if (getsockname(sess->pasv_listen_fd, (struct sockaddr *)&addr, &addrlen) < 0)
{
ERR_EXIT("getsockname");
}
unsigned short port = ntohs(addr.sin_port);
*/
priv_sock_send_cmd(sess->child_fd, PRIV_SOCK_PASV_LISTEN);
unsigned short port = (int)priv_sock_get_int(sess->child_fd);
unsigned int v[4];
sscanf(ip, "%u.%u.%u.%u", &v[0], &v[1], &v[2], &v[3]);
char text[1024] = {0};
sprintf(text, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).",
v[0], v[1], v[2], v[3], port>>8, port&0xFF);
ftp_reply(sess, FTP_PASVOK, text);
}
static void do_type(session_t *sess)
{
if (strcmp(sess->arg, "A") == 0)
{
sess->is_ascii = 1;
ftp_reply(sess, FTP_TYPEOK, "Switching to ASCII mode.");
}
else if (strcmp(sess->arg, "I") == 0)
{
sess->is_ascii = 0;
ftp_reply(sess, FTP_TYPEOK, "Switching to Binary mode.");
}
else
{
ftp_reply(sess, FTP_BADCMD, "Unrecognised TYPE command.");
}
}
/*
static void do_stru(session_t *sess)
{
}
static void do_mode(session_t *sess)
{
}
*/
static void do_retr(session_t *sess)
{
// 下载文件
// 断点续载
// 创建数据连接
if (get_transfer_fd(sess) == 0)
{
return;
}
long long offset = sess->restart_pos;
sess->restart_pos = 0;
// 打开文件
int fd = open(sess->arg, O_RDONLY);
if (fd == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
int ret;
// 加读锁
ret = lock_file_read(fd);
if (ret == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
// 判断是否是普通文件
struct stat sbuf;
ret = fstat(fd, &sbuf);
if (!S_ISREG(sbuf.st_mode))
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
if (offset != 0)
{
ret = lseek(fd, offset, SEEK_SET);
if (ret == -1)
{
ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
return;
}
}
//150 Opening BINARY mode data connection for /home/jjl/tmp/echocli.c (1085 bytes).
// 150
char text[1024] = {0};
if (sess->is_ascii)
{
sprintf(text, "Opening ASCII mode data connection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
else
{
sprintf(text, "Opening BINARY mode data connection for %s (%lld bytes).",
sess->arg, (long long)sbuf.st_size);
}
ftp_reply(sess, FTP_DATACONN, text);
int flag = 0;
// 下载文件
/*char buf[4096];
while (1)
{
ret = read(fd, buf, sizeof(buf));
if (ret == -1)
{
if (errno == EINTR)
{
continue;
}
else
{
flag = 1;
break;
}
}
else if (ret == 0)
{
flag = 0;
break;
}
if (writen(sess->data_fd, buf, ret) != ret)
{
flag = 2;
break;
}
}
*/
// ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
long long bytes_to_send = sbuf.st_size;
if (offset > bytes_to_send)
{
bytes_to_send = 0;
}
else
{
bytes_to_send -= offset;
}
sess->bw_transfer_start_sec = get_time_sec();
sess->bw_transfer_start_usec = get_time_usec();
while (bytes_to_send)
{
int num_this_time = bytes_to_send > 4096 ? 4096 : bytes_to_send;
ret = sendfile(sess->data_fd, fd, NULL, num_this_time);
if (ret == -1)
{
flag = 2;
break;
}
limit_rate(sess, ret, 0);
if (sess->abor_received)
{