123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include "ftp_server.h"
- #include "fly_param.h"
- #include "ftpd.h"
- #include "os_cpu.h"
- #include "sockets.h"
- #include <stdio.h>
- INT32S g_ftp_cmd_socket = -1; /* 创建与ftp交互的socket */
- INT32S g_ftp_data_socket = -1; /* 创建与ftp交互的socket */
- void ftp_server_cmd_task(void)
- {
- /* socket 创建 */
- INT8U buf[500];
- INT32S ret = 0;
- static INT32S ftp_cmd_sockfd;
- static struct sockaddr_in svr_addr = {0};
- struct sockaddr_in clt_addr;
- INT32U non_block = 1;
- INT32U len;
- INT32U i_ctrl_cmd;
- static const char ftp_reply_unkown[] = "500 Unknown command\r\n";
- ftp_cmd_t *cmd_match;
- ftp_mbox_t msgbox;
- msgbox.current_dir[0] = 0; // 路径为空,即根目录
- svr_addr.sin_family = AF_INET;
- svr_addr.sin_port = htons(FTP_CMD_PORT);
- svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
- while (1)
- {
- ftp_cmd_sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (ftp_cmd_sockfd < 0)
- {
- continue;
- }
- ret = bind(ftp_cmd_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
- if (ret < 0)
- {
- lwip_close(ftp_cmd_sockfd);
- ftp_cmd_sockfd = -1;
- continue;
- }
- ret = listen(ftp_cmd_sockfd, 1);
- if (ret < 0)
- {
- lwip_close(ftp_cmd_sockfd);
- continue;
- }
- g_ftp_cmd_socket = accept(ftp_cmd_sockfd, NULL, NULL);
- while (-1 != g_ftp_cmd_socket)
- {
- msgbox.cmd_socket = g_ftp_cmd_socket; // 当前 ftp 控制端口连接句柄
- send(g_ftp_cmd_socket, msg200, sizeof(msg200) - 1, 0); // 控制端口反馈
- while (-1 != g_ftp_cmd_socket)
- {
- ret = recv(g_ftp_cmd_socket, buf, sizeof(buf) - 1, 0);
- if (ret <= 0)
- {
- lwip_close(g_ftp_cmd_socket);
- g_ftp_cmd_socket = -1;
- break;
- }
- i_ctrl_cmd = FTP_STR2ID(buf);
- if (buf[3] < 'A' || buf[3] > 'z') // 有些命令只有三个字节,需要判断
- {
- i_ctrl_cmd &= 0x00ffffff;
- msgbox.arg = buf + 4;
- msgbox.arglen = sizeof(buf) - 1 - 4;
- }
- else
- {
- msgbox.arg = buf + 5;
- msgbox.arglen = sizeof(buf) - 1 - 5;
- }
- cmd_match = ftp_search_command(i_ctrl_cmd); // 匹配命令号
- if (NULL == cmd_match)
- send(msgbox.cmd_socket, ftp_reply_unkown, sizeof(ftp_reply_unkown) - 1, 0);
- else
- cmd_match->func(&msgbox);
- }
- lwip_close(g_ftp_cmd_socket);
- g_ftp_cmd_socket = -1;
- break;
- }
- lwip_close(ftp_cmd_sockfd);
- ftp_cmd_sockfd = -1;
- }
- }
- void ftp_server_data_task(void)
- {
- /* socket 创建 */
- INT8U buf[500];
- INT32S ret = 0;
- static INT32S ftp_data_sockfd;
- static struct sockaddr_in svr_addr = {0};
- struct sockaddr_in clt_addr;
- INT32U len;
- ftp_mbox_t *msgbox;
- char *ctrl_msg;
- INT8U err = 0;
- svr_addr.sin_family = AF_INET;
- svr_addr.sin_port = htons(FTP_DATA_PORT);
- svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
- while (1)
- {
- ftp_data_sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (ftp_data_sockfd < 0)
- {
- continue;
- }
- ret = bind(ftp_data_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
- if (ret < 0)
- {
- lwip_close(ftp_data_sockfd);
- ftp_data_sockfd = -1;
- continue;
- }
- ret = listen(ftp_data_sockfd, 1);
- if (ret < 0)
- {
- lwip_close(ftp_data_sockfd);
- continue;
- }
- // len = sizeof(clt_addr);
- // g_ftp_data_socket = accept(ftp_data_sockfd, (struct sockaddr *)&clt_addr, len);
- g_ftp_data_socket = accept(ftp_data_sockfd, NULL, NULL);
- // printf("1111111\r\n");
- while (-1 != g_ftp_data_socket)
- {
- msgbox = (ftp_mbox_t *)OSMboxPend(ftp_mbox, 50, &err);
- // ret = recv(g_ftp_data_socket, buf, sizeof(buf) - 1, 0);
- // if (ret <= 0)
- // {
- // lwip_close(g_ftp_data_socket);
- // g_ftp_data_socket = -1;
- // break;
- // }
- // if (msgbox->arglen >= 2)
- // {
- if (err == 0)
- {
- switch (msgbox->event) // 根据不同的操作命令进行操作
- {
- case FTP_LIST:
- ctrl_msg = data_port_list_file(msgbox);
- break;
- // case FTP_SEND_FILE_DATA:
- // ctrl_msg = data_port_send_file(data_port_conn, msgbox);
- // break;
- // case FTP_RECV_FILE:
- // ctrl_msg = data_port_recv_file(msgbox);
- // break;
- default:;
- }
- send(msgbox->cmd_socket, ctrl_msg, strlen(ctrl_msg), 0); // 控制端口反馈
- // }
- }
- else
- {
- lwip_close(g_ftp_data_socket);
- g_ftp_data_socket = -1;
- break;
- }
- }
- lwip_close(ftp_data_sockfd);
- ftp_data_sockfd = -1;
- }
- }
|