ftp_server.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "ftp_server.h"
  2. #include "fly_param.h"
  3. #include "ftpd.h"
  4. #include "os_cpu.h"
  5. #include "sockets.h"
  6. #include <stdio.h>
  7. INT32S g_ftp_cmd_socket = -1; /* 创建与ftp交互的socket */
  8. INT32S g_ftp_data_socket = -1; /* 创建与ftp交互的socket */
  9. void ftp_server_cmd_task(void)
  10. {
  11. /* socket 创建 */
  12. INT8U buf[500];
  13. INT32S ret = 0;
  14. static INT32S ftp_cmd_sockfd;
  15. static struct sockaddr_in svr_addr = {0};
  16. struct sockaddr_in clt_addr;
  17. INT32U non_block = 1;
  18. INT32U len;
  19. INT32U i_ctrl_cmd;
  20. static const char ftp_reply_unkown[] = "500 Unknown command\r\n";
  21. ftp_cmd_t *cmd_match;
  22. ftp_mbox_t msgbox;
  23. msgbox.current_dir[0] = 0; // 路径为空,即根目录
  24. svr_addr.sin_family = AF_INET;
  25. svr_addr.sin_port = htons(FTP_CMD_PORT);
  26. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  27. while (1)
  28. {
  29. ftp_cmd_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  30. if (ftp_cmd_sockfd < 0)
  31. {
  32. continue;
  33. }
  34. ret = bind(ftp_cmd_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  35. if (ret < 0)
  36. {
  37. lwip_close(ftp_cmd_sockfd);
  38. ftp_cmd_sockfd = -1;
  39. continue;
  40. }
  41. ret = listen(ftp_cmd_sockfd, 1);
  42. if (ret < 0)
  43. {
  44. lwip_close(ftp_cmd_sockfd);
  45. continue;
  46. }
  47. g_ftp_cmd_socket = accept(ftp_cmd_sockfd, NULL, NULL);
  48. while (-1 != g_ftp_cmd_socket)
  49. {
  50. msgbox.cmd_socket = g_ftp_cmd_socket; // 当前 ftp 控制端口连接句柄
  51. send(g_ftp_cmd_socket, msg200, sizeof(msg200) - 1, 0); // 控制端口反馈
  52. while (-1 != g_ftp_cmd_socket)
  53. {
  54. ret = recv(g_ftp_cmd_socket, buf, sizeof(buf) - 1, 0);
  55. if (ret <= 0)
  56. {
  57. lwip_close(g_ftp_cmd_socket);
  58. g_ftp_cmd_socket = -1;
  59. break;
  60. }
  61. i_ctrl_cmd = FTP_STR2ID(buf);
  62. if (buf[3] < 'A' || buf[3] > 'z') // 有些命令只有三个字节,需要判断
  63. {
  64. i_ctrl_cmd &= 0x00ffffff;
  65. msgbox.arg = buf + 4;
  66. msgbox.arglen = sizeof(buf) - 1 - 4;
  67. }
  68. else
  69. {
  70. msgbox.arg = buf + 5;
  71. msgbox.arglen = sizeof(buf) - 1 - 5;
  72. }
  73. cmd_match = ftp_search_command(i_ctrl_cmd); // 匹配命令号
  74. if (NULL == cmd_match)
  75. send(msgbox.cmd_socket, ftp_reply_unkown, sizeof(ftp_reply_unkown) - 1, 0);
  76. else
  77. cmd_match->func(&msgbox);
  78. }
  79. lwip_close(g_ftp_cmd_socket);
  80. g_ftp_cmd_socket = -1;
  81. break;
  82. }
  83. lwip_close(ftp_cmd_sockfd);
  84. ftp_cmd_sockfd = -1;
  85. }
  86. }
  87. void ftp_server_data_task(void)
  88. {
  89. /* socket 创建 */
  90. INT8U buf[500];
  91. INT32S ret = 0;
  92. static INT32S ftp_data_sockfd;
  93. static struct sockaddr_in svr_addr = {0};
  94. struct sockaddr_in clt_addr;
  95. INT32U len;
  96. ftp_mbox_t *msgbox;
  97. char *ctrl_msg;
  98. INT8U err = 0;
  99. svr_addr.sin_family = AF_INET;
  100. svr_addr.sin_port = htons(FTP_DATA_PORT);
  101. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  102. while (1)
  103. {
  104. ftp_data_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  105. if (ftp_data_sockfd < 0)
  106. {
  107. continue;
  108. }
  109. ret = bind(ftp_data_sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  110. if (ret < 0)
  111. {
  112. lwip_close(ftp_data_sockfd);
  113. ftp_data_sockfd = -1;
  114. continue;
  115. }
  116. ret = listen(ftp_data_sockfd, 1);
  117. if (ret < 0)
  118. {
  119. lwip_close(ftp_data_sockfd);
  120. continue;
  121. }
  122. // len = sizeof(clt_addr);
  123. // g_ftp_data_socket = accept(ftp_data_sockfd, (struct sockaddr *)&clt_addr, len);
  124. g_ftp_data_socket = accept(ftp_data_sockfd, NULL, NULL);
  125. // printf("1111111\r\n");
  126. while (-1 != g_ftp_data_socket)
  127. {
  128. msgbox = (ftp_mbox_t *)OSMboxPend(ftp_mbox, 50, &err);
  129. // ret = recv(g_ftp_data_socket, buf, sizeof(buf) - 1, 0);
  130. // if (ret <= 0)
  131. // {
  132. // lwip_close(g_ftp_data_socket);
  133. // g_ftp_data_socket = -1;
  134. // break;
  135. // }
  136. // if (msgbox->arglen >= 2)
  137. // {
  138. if (err == 0)
  139. {
  140. switch (msgbox->event) // 根据不同的操作命令进行操作
  141. {
  142. case FTP_LIST:
  143. ctrl_msg = data_port_list_file(msgbox);
  144. break;
  145. // case FTP_SEND_FILE_DATA:
  146. // ctrl_msg = data_port_send_file(data_port_conn, msgbox);
  147. // break;
  148. // case FTP_RECV_FILE:
  149. // ctrl_msg = data_port_recv_file(msgbox);
  150. // break;
  151. default:;
  152. }
  153. send(msgbox->cmd_socket, ctrl_msg, strlen(ctrl_msg), 0); // 控制端口反馈
  154. // }
  155. }
  156. else
  157. {
  158. lwip_close(g_ftp_data_socket);
  159. g_ftp_data_socket = -1;
  160. break;
  161. }
  162. }
  163. lwip_close(ftp_data_sockfd);
  164. ftp_data_sockfd = -1;
  165. }
  166. }