Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
LWIP
ping
组件 lwip v2.0.3 版本使用 netdev网卡的 ping 功能
发布于 2024-12-04 09:27:16 浏览:131
订阅该版
[tocm] # 一、版本说明 rt-thread 版本是 v4.1.1 lwip 版本:v2.0.3 # 二、ping 指令的使用 原代码: ``` int netdev_ping(int argc, char **argv) { if (argc == 1) { rt_kprintf("Please input: ping [netdev name]
\n"); } else if (argc == 2) { netdev_cmd_ping(argv[1], RT_NULL, 4, 0); } else if (argc == 3) { netdev_cmd_ping(argv[1], argv[2], 4, 0); } return 0; } MSH_CMD_EXPORT_ALIAS(netdev_ping, ping, ping network host); ``` 输入 ping 指令,提示 ``` Please input: ping [netdev name]
``` 通过查看 netdev_cmd_ping 函数,可知实际应该是 ``` ping [host address]
``` netdev_cmd_ping 函数如下: ``` #ifdef NETDEV_USING_PING int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_size_t size) { #define NETDEV_PING_DATA_SIZE 32 /** ping receive timeout - in milliseconds */ #define NETDEV_PING_RECV_TIMEO (2 * RT_TICK_PER_SECOND) /** ping delay - in milliseconds */ #define NETDEV_PING_DELAY (1 * RT_TICK_PER_SECOND) /* check netdev ping options */ #define NETDEV_PING_IS_COMMONICABLE(netdev) \ ((netdev) && (netdev)->ops && (netdev)->ops->ping && \ netdev_is_up(netdev) && netdev_is_link_up(netdev)) \ struct netdev *netdev = RT_NULL; struct netdev_ping_resp ping_resp; rt_uint32_t index; int ret = 0; if (size == 0) { size = NETDEV_PING_DATA_SIZE; } if (netdev_name != RT_NULL) { netdev = netdev_get_by_name(netdev_name); if (netdev == RT_NULL) { netdev = netdev_default; rt_kprintf("ping: not found specified netif, using default netdev %s.\n", netdev->name); } } if (NETDEV_PING_IS_COMMONICABLE(netdev_default)) { /* using default network interface device for ping */ netdev = netdev_default; } else { /* using first internet up status network interface device */ netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP); if (netdev == RT_NULL) { rt_kprintf("ping: not found available network interface device.\n"); return -RT_ERROR; } else if (netdev->ops == RT_NULL || netdev->ops->ping == RT_NULL) { rt_kprintf("ping: network interface device(%s) not support ping feature.\n", netdev->name); return -RT_ERROR; } else if (!netdev_is_up(netdev) || !netdev_is_link_up(netdev)) { rt_kprintf("ping: network interface device(%s) status error.\n", netdev->name); return -RT_ERROR; } } for (index = 0; index < times; index++) { int delay_tick = 0; rt_tick_t start_tick = 0; rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp)); start_tick = rt_tick_get(); ret = netdev->ops->ping(netdev, (const char *)target_name, size, NETDEV_PING_RECV_TIMEO, &ping_resp); if (ret == -RT_ETIMEOUT) { rt_kprintf("ping: from %s icmp_seq=%d timeout\n", (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr), index); } else if (ret == -RT_ERROR) { rt_kprintf("ping: unknown %s %s\n", (ip_addr_isany(&(ping_resp.ip_addr))) ? "host" : "address", (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr)); } else { if (ping_resp.ttl == 0) { rt_kprintf("%d bytes from %s icmp_seq=%d time=%d ms\n", ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index, ping_resp.ticks); } else { rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index, ping_resp.ttl, ping_resp.ticks); } } /* if the response time is more than NETDEV_PING_DELAY, no need to delay */ delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY; rt_thread_delay(delay_tick); } return RT_EOK; } ``` # 二、多网卡指定网卡ping 多网卡指定网卡进行 ping ,发现使用的是默认网卡,查看 netdev_cmd_ping 函数 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20241203/e047e10323b3c85165fa316f858b8425.png) 判断的是默认网卡,修改如下 ``` if(NETDEV_PING_IS_COMMONICABLE(netdev))// netdev_default { /* using default network interface device for ping */ //netdev = netdev_default; if (netdev == RT_NULL) { netdev = netdev_default; rt_kprintf("ping: not found specified netif, using default netdev %s.\n", netdev->name); } } ``` 完整代码: ``` int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_size_t size) { #define NETDEV_PING_DATA_SIZE 32 /** ping receive timeout - in milliseconds */ #define NETDEV_PING_RECV_TIMEO (2 * RT_TICK_PER_SECOND) /** ping delay - in milliseconds */ #define NETDEV_PING_DELAY (1 * RT_TICK_PER_SECOND) /* check netdev ping options */ #define NETDEV_PING_IS_COMMONICABLE(netdev) \ ((netdev) && (netdev)->ops && (netdev)->ops->ping && \ netdev_is_up(netdev) && netdev_is_link_up(netdev)) \ struct netdev *netdev = RT_NULL; struct netdev_ping_resp ping_resp; rt_uint32_t index; int ret = 0; if (size == 0) { size = NETDEV_PING_DATA_SIZE; } if (netdev_name != RT_NULL) { netdev = netdev_get_by_name(netdev_name); if (netdev == RT_NULL) { netdev = netdev_default; rt_kprintf("ping: not found specified netif, using default netdev %s.\n", netdev->name); } } if (NETDEV_PING_IS_COMMONICABLE(netdev))// netdev_default { /* using default network interface device for ping */ //netdev = netdev_default; if (netdev == RT_NULL) { netdev = netdev_default; rt_kprintf("ping: not found specified netif, using default netdev %s.\n", netdev->name); } } else { /* using first internet up status network interface device */ netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP); if (netdev == RT_NULL) { rt_kprintf("ping: not found available network interface device.\n"); return -RT_ERROR; } else if (netdev->ops == RT_NULL || netdev->ops->ping == RT_NULL) { rt_kprintf("ping: network interface device(%s) not support ping feature.\n", netdev->name); return -RT_ERROR; } else if (!netdev_is_up(netdev) || !netdev_is_link_up(netdev)) { rt_kprintf("ping: network interface device(%s) status error.\n", netdev->name); return -RT_ERROR; } } rt_kprintf("default netif %s.\n", netdev_default->name); rt_kprintf("ping: found specified netif, using netdev %s.\n", netdev->name); for (index = 0; index < times; index++) { int delay_tick = 0; rt_tick_t start_tick = 0; rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp)); start_tick = rt_tick_get(); ret = netdev->ops->ping(netdev, (const char *)target_name, size, NETDEV_PING_RECV_TIMEO, &ping_resp); if (ret == -RT_ETIMEOUT) { rt_kprintf("ping: from %s icmp_seq=%d timeout\n", (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr), index); } else if (ret == -RT_ERROR) { rt_kprintf("ping: unknown %s %s\n", (ip_addr_isany(&(ping_resp.ip_addr))) ? "host" : "address", (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr)); } else { if (ping_resp.ttl == 0) { rt_kprintf("%d bytes from %s icmp_seq=%d time=%d ms\n", ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index, ping_resp.ticks); } else { rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index, ping_resp.ttl, ping_resp.ticks); } } /* if the response time is more than NETDEV_PING_DELAY, no need to delay */ delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY; rt_thread_delay(delay_tick); } return RT_EOK; } ``` 修改完,可使用指定网卡进行 ping 功能。
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
YZRD
这家伙很懒,什么也没写!
文章
22
回答
220
被采纳
19
关注TA
发私信
相关文章
1
RT-THREAD在STM32H747平台上移植lwip
2
{lwip}使能RT_LWIP_DHCP时可以获取到ip
3
stm32f103 LWIP 2.0.2 TCP收发问题
4
lwip2.1不重启修改IP
5
关于网络协议栈的测试
6
可否将LWIP升级到2.1.2 和 2.0.3?
7
socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
8
tcpclient 插拔网线问题?
9
两个tcpclient同时通讯可以吗?
10
SO_BINDTODEVICE 未定义该如何解决
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
freemodbus
PWM
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
xiaorui
1
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部