2023_open_source_contest_preliminary_1st_issue3 from 华东师范大学_bdislab_final战队_杨龙
it is OK
|
@ -134,7 +134,8 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
|||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_FTPCLIENT),y)
|
||||
SRC_FILES +=
|
||||
SRC_FILES += test_ftpclient/test_ftpclient.c test_ftpclient/ftp_client/ftp_client.c\
|
||||
test_ftpclient/ftp_client/my_socket.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_LORA_P2P),y)
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
# 初赛一级赛题3:基于矽璓已实现的Lwip,在ARM上实现FTP协议的Client功能
|
||||
|
||||
## 1. 简介
|
||||
本项目是基于矽璓已实现的Lwip,在ARM上实现FTP协议的Client功能
|
||||
test_ftpclient.h声明了下载10个文件的测试函数
|
||||
test_ftpclient.c实现了下载10个文件的测试函数
|
||||
ftp_client文件夹定义了ftp_client的相关类库其中my_socket.h,my_socket.c定义了socket抽象层,并基于
|
||||
Lwip实现了该抽象层,ftp_client.h,ftp_client.c实现了ftp登录,获取文件大小,下载文件等功能
|
||||
|
||||
## 2. 数据结构设计说明
|
||||
- ftp_client.c 的设计
|
||||
分别定义了发送命令和接收数据的socket和相应的缓冲区并且实现了登录、发送命令、接收响应数据、查找文件大小、进入被动模式、下载文件、关闭ftp客户端等操作
|
||||
```c
|
||||
static int m_socket_cmd; // 发送命令的socket文件描述符
|
||||
static int m_socket_data; // 接收ftp服务器文件的socket文件描述符
|
||||
static char m_send_buffer[1024]; // 发送缓冲区
|
||||
static char m_recv_buffer[1024]; // 接收缓冲区
|
||||
```
|
||||
## 3. 测试程序说明
|
||||
- test_ftpclient.c用于测试下载10个文件
|
||||
连接电脑上的ftp服务器下载10个4KB文件,通过终端日志打印确定文件是否下载成功,以及追踪通信流程和下载进度
|
||||
```c
|
||||
void TestFtpClient(int argc, char* argv[])
|
||||
{
|
||||
FtpInitCmd();
|
||||
int ret = FtpLogin("192.168.0.248", 21, "anonymous", "anonymous");
|
||||
int size;
|
||||
char *buf;
|
||||
for(int i = 1;i <= 10;i++){
|
||||
char fileName[20] = "/file";
|
||||
char temp[5] = "";
|
||||
sprintf(temp,"%d",i);
|
||||
strcat(fileName,temp);
|
||||
size = FtpFileSize(fileName);
|
||||
buf = malloc(size);
|
||||
FtpInitData(); // data socket 每次下载都要重新创建,下载完都要关闭
|
||||
ret = FtpDownload(fileName, buf, size);
|
||||
free(buf);
|
||||
}
|
||||
FtpQuit();
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
## 4. 运行结果(##需结合运行测试截图按步骤说明##)
|
||||
1. 配置开启BSP_USING_LWIP、USER_TEST_FTPCLIENT
|
||||

|
||||

|
||||
2. 编译
|
||||

|
||||
3. 烧写
|
||||

|
||||
4. xshell连接串口终端
|
||||

|
||||
6. 配置ip
|
||||

|
||||
7. 运行TestFtpClient,开始下载文件
|
||||

|
||||

|
||||

|
|
@ -0,0 +1,256 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: ftp_client.c
|
||||
* @brief: ftp client tool
|
||||
* @version: 1.0
|
||||
* @author: bdislab_final
|
||||
* @date: 2023/7/25
|
||||
* @reference https://kerndev.blog.csdn.net/article/details/89383888
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include "my_socket.h"
|
||||
#include "ftp_client.h"
|
||||
|
||||
static int m_socket_cmd;
|
||||
static int m_socket_data;
|
||||
static char m_send_buffer[1024];
|
||||
static char m_recv_buffer[1024];
|
||||
|
||||
static int FtpSendCommand(char *cmd)
|
||||
{
|
||||
int ret;
|
||||
printf("send command: %s\r\n", cmd);
|
||||
ret = SocketSend(m_socket_cmd, cmd, (int)strlen(cmd));
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("failed to send command: %s\r\n",cmd);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int FtpRecvRespond(char *resp, int len)
|
||||
{
|
||||
int ret;
|
||||
int off;
|
||||
len -= 1;
|
||||
for(off=0; off<len; off+=ret)
|
||||
{
|
||||
ret = SocketRecv(m_socket_cmd, &resp[off], 1);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("recv respond error(ret=%d)!\r\n", ret);
|
||||
return 0;
|
||||
}
|
||||
if(resp[off] == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
resp[off+1] = 0;
|
||||
printf("respond:%s\r\n", resp);
|
||||
return atoi(resp);
|
||||
}
|
||||
|
||||
static int FtpEnterPasv(char *ipaddr, int *port)
|
||||
{
|
||||
int ret;
|
||||
char *find;
|
||||
int a,b,c,d;
|
||||
int pa,pb;
|
||||
ret = FtpSendCommand("PASV\r\n");
|
||||
if(ret != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 227)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
find = strrchr(m_recv_buffer, '(');
|
||||
sscanf(find, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &pa, &pb);
|
||||
sprintf(ipaddr, "%d.%d.%d.%d", a, b, c, d);
|
||||
*port = pa * 256 + pb;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int FtpDownload(char *name, char *buf, int len)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
char ipaddr[32];
|
||||
int port;
|
||||
|
||||
//查询数据地址
|
||||
ret = FtpEnterPasv(ipaddr, &port);
|
||||
if(ret != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
//连接数据端口
|
||||
ret = SocketConnect(m_socket_data, ipaddr, port);
|
||||
if(ret != 1)
|
||||
{
|
||||
printf("failed to connect data port\r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//准备下载
|
||||
sprintf(m_send_buffer, "RETR %s\r\n", name);
|
||||
ret = FtpSendCommand(m_send_buffer);
|
||||
if(ret != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 150)
|
||||
{
|
||||
SocketClose(m_socket_data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//开始下载,读取完数据后,服务器会自动关闭连接
|
||||
for(i=0; i<len; i+=ret)
|
||||
{
|
||||
ret = SocketRecv(m_socket_data, ((char *)buf) + i, len);
|
||||
printf("download %d/%d.\r\n", i + ret, len);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("download was interrupted.\r\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//下载完成
|
||||
printf("download %d/%d bytes complete.\r\n", i, len);
|
||||
printf("====file content====\n");
|
||||
buf[100] = '\0';
|
||||
printf("%s\n",buf);
|
||||
printf("====================\n");
|
||||
// FILE *fp = NULL;
|
||||
|
||||
// fp = fopen(name+1, "wb");
|
||||
// fwrite(buf,len,1,fp);
|
||||
// fclose(fp);
|
||||
|
||||
SocketClose(m_socket_data);
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
return (ret==226);
|
||||
}
|
||||
|
||||
int FtpFileSize(char *name)
|
||||
{
|
||||
int ret;
|
||||
int size;
|
||||
sprintf(m_send_buffer,"SIZE %s\r\n",name);
|
||||
ret = FtpSendCommand(m_send_buffer);
|
||||
if(ret != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 213)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size = atoi(m_recv_buffer + 4);
|
||||
return size;
|
||||
}
|
||||
|
||||
int FtpLogin(char *addr, int port, char *username, char *password)
|
||||
{
|
||||
int ret;
|
||||
printf("connect...\r\n");
|
||||
ret = SocketConnect(m_socket_cmd, addr, port);
|
||||
if(ret != 1)
|
||||
{
|
||||
printf("connect server failed!\r\n");
|
||||
return 0;
|
||||
}
|
||||
printf("connect ok.\r\n");
|
||||
//等待欢迎信息
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 220)
|
||||
{
|
||||
printf("bad server, ret=%d!\r\n", ret);
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("login...\r\n");
|
||||
//发送USER
|
||||
sprintf(m_send_buffer, "USER %s\r\n", username);
|
||||
ret = FtpSendCommand(m_send_buffer);
|
||||
if(ret != 1)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 331)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//发送PASS
|
||||
sprintf(m_send_buffer, "PASS %s\r\n", password);
|
||||
ret = FtpSendCommand(m_send_buffer);
|
||||
if(ret != 1)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 230)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
printf("login success.\r\n");
|
||||
|
||||
//设置为二进制模式
|
||||
ret = FtpSendCommand("TYPE I\r\n");
|
||||
if(ret != 1)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
ret = FtpRecvRespond(m_recv_buffer, 1024);
|
||||
if(ret != 200)
|
||||
{
|
||||
SocketClose(m_socket_cmd);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FtpQuit(void)
|
||||
{
|
||||
FtpSendCommand("QUIT\r\n");
|
||||
SocketClose(m_socket_cmd);
|
||||
}
|
||||
|
||||
void FtpInitCmd(void)
|
||||
{
|
||||
m_socket_cmd = SocketCreate();
|
||||
}
|
||||
|
||||
void FtpInitData(void){
|
||||
m_socket_data= SocketCreate();
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: ftp_client.h
|
||||
* @brief: ftp client tool
|
||||
* @version: 1.0
|
||||
* @author: bdislab_final
|
||||
* @date: 2023/7/25
|
||||
*/
|
||||
|
||||
#ifndef FTP_CLIENT_H
|
||||
#define FTP_CLIENT_H
|
||||
|
||||
/* init ftp cmd socket */
|
||||
void FtpInitCmd(void);
|
||||
|
||||
/* init ftp data socket */
|
||||
void FtpInitData(void);
|
||||
|
||||
/* quit ftp */
|
||||
void FtpQuit(void);
|
||||
|
||||
/* fpt login */
|
||||
int FtpLogin(char *addr, int port, char *username, char *password);
|
||||
|
||||
/* get file size */
|
||||
int FtpFileSize(char *name);
|
||||
|
||||
/* ftp download file*/
|
||||
int FtpDownload(char *name, char *buf, int len);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: my_socket.c
|
||||
* @brief: a abstract socket api
|
||||
* @version: 1.0
|
||||
* @author: bdislab_final
|
||||
* @date: 2023/7/25
|
||||
* @reference https://kerndev.blog.csdn.net/article/details/89383888
|
||||
*/
|
||||
|
||||
#include "my_socket.h"
|
||||
#include <sockets.h>
|
||||
#include <def.h>
|
||||
#include <errno.h>
|
||||
|
||||
int SocketCreate(void){
|
||||
return socket(AF_INET,SOCK_STREAM,0);
|
||||
}
|
||||
|
||||
int SocketConnect(int sock, const char *addr, int port){
|
||||
// unsigned int iRemoteAddr = 0;
|
||||
struct sockaddr_in stRemoteAddr = {0}; //对端,即目标地址信息
|
||||
|
||||
stRemoteAddr.sin_family = AF_INET;
|
||||
stRemoteAddr.sin_port = htons(port);
|
||||
stRemoteAddr.sin_addr.s_addr = inet_addr(addr);
|
||||
// inet_pton(AF_INET, addr, &iRemoteAddr);
|
||||
// stRemoteAddr.sin_addr.s_addr=iRemoteAddr;
|
||||
int res = connect(sock,(struct sockaddr *)&stRemoteAddr,sizeof(stRemoteAddr));
|
||||
if(res == -1){
|
||||
printf("error:%d\n",errno);
|
||||
}
|
||||
return res == 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
int SocketSend(int sock, void *data, int len){
|
||||
return send(sock,data,len,0);
|
||||
}
|
||||
|
||||
int SocketRecv(int sock, void *data, int len){
|
||||
return recv(sock,data,len,0);
|
||||
}
|
||||
|
||||
void SocketClose(int sock){
|
||||
close(sock);
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file my_socket.h
|
||||
* @brief a abstract socket api
|
||||
* @version 1.0
|
||||
* @author bdislab_final
|
||||
* @date 2023/7/25
|
||||
*/
|
||||
|
||||
#ifndef MYSOCKET_H
|
||||
#define MYSOCKET_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/* create a socket */
|
||||
int SocketCreate(void);
|
||||
|
||||
/* connect a socket */
|
||||
int SocketConnect(int sock, const char *addr, int port);
|
||||
|
||||
/* send data through socket*/
|
||||
int SocketSend(int sock, void *data, int len);
|
||||
|
||||
/* receive data from socket*/
|
||||
int SocketRecv(int sock, void *data, int len);
|
||||
|
||||
/* close socket*/
|
||||
void SocketClose(int sock);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
After Width: | Height: | Size: 214 KiB |
After Width: | Height: | Size: 121 KiB |
After Width: | Height: | Size: 108 KiB |
After Width: | Height: | Size: 117 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 73 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 136 KiB |
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: test_ftpclient.c
|
||||
* @brief: a ftpClient test sample
|
||||
* @version: 1.0
|
||||
* @author: bdislab_final
|
||||
* @date: 2023/7/25
|
||||
*/
|
||||
|
||||
#include "test_ftpclient.h"
|
||||
#include "ftp_client/ftp_client.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <transform.h>
|
||||
|
||||
|
||||
/* test for ftp client */
|
||||
void TestFtpClient(int argc, char* argv[])
|
||||
{
|
||||
FtpInitCmd();
|
||||
int ret = FtpLogin("192.168.1.248", 21, "anonymous", "anonymous");
|
||||
int size;
|
||||
char *buf;
|
||||
for(int i = 1;i <= 10;i++){
|
||||
char fileName[20] = "/file";
|
||||
char temp[5] = "";
|
||||
sprintf(temp,"%d",i-1);
|
||||
strcat(fileName,temp);
|
||||
size = FtpFileSize(fileName);
|
||||
buf = malloc(size);
|
||||
FtpInitData(); // data socket 每次下载都要重新创建,下载完都要关闭
|
||||
ret = FtpDownload(fileName, buf, size);
|
||||
free(buf);
|
||||
}
|
||||
FtpQuit();
|
||||
return;
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(TestFtpClient, a ftpClient test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file test_ftpclient.h
|
||||
* @brief a ftpClient test sample
|
||||
* @version 1.0
|
||||
* @author bdislab_final
|
||||
* @date 2023/7/25
|
||||
*/
|
||||
|
||||
#ifndef TEST_FTPCLIENT_H
|
||||
#define TEST_FTPCLIENT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* test for ftp client */
|
||||
void TestFtpClient(int argc, char* argv[]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|