Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
LWIP
双网口_多网口_多IP
LwIP 指定网卡ping
发布于 2021-07-14 19:16:13 浏览:3406
订阅该版
[tocm] # ping 过程如何选择网卡 路由(routing)就是通过互联的网络把信息从源地址传输到目的地址的活动,发送端必然需要找到一个网卡将数据报发送出去,ping 的过程中需要通过 ip4_route 发现合适的网卡; LwIP 中发现网卡的默认规则是,遍历挂接的网卡设备, 1. 比对远程主机的`ip`和网卡的`ip`是否在同一网段,如果是,则返回同一网段的网卡; 2. 如果 定义了钩子函数`LWIP_HOOK_IP4_ROUTE_SRC`,如果上面无法找到对应网卡时,则会 通过该钩子函数查找网卡; 3. 如果1,2均未找到网卡,返回默认网卡; # 实现过程 1. `rt-thread\components\net\netdev\src\netdev.c`增加查找网卡动作; ```c int netdev_cmd_ping(char* target_name, rt_uint32_t times, rt_size_t size, char *netdev_name) { #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; int index, 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) { rt_kprintf("ping: not found network interface device, using default.\n"); netdev = netdev_default; } } if (!NETDEV_PING_IS_COMMONICABLE(netdev)) { /* 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("network interface: %s\r\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; } ``` 2. `rt-thread\components\net\netdev\src\netdev.c`修改ping命令,增加网卡参数; ```c int netdev_ping(int argc, char **argv) { if (argc == 1) { rt_kprintf("Please input: ping
\n"); } else if (argc == 3) { netdev_cmd_ping(argv[1], 4, 0, argv[2]); } else { netdev_cmd_ping(argv[1], 4, 0, RT_NULL); } return 0; } FINSH_FUNCTION_EXPORT_ALIAS(netdev_ping, __cmd_ping, ping network host); ``` 3. `rt-thread\components\net\lwip-2.0.2\src\lwipopts.h` 使能钩子函数 ```c #define LWIP_HOOK_IP4_ROUTE_SRC(dest, src) lwip_ip4_route_src(dest, src ``` 4. `rt-thread\components\net\lwip-2.0.2\src\arch\sys_arch.c` 增加 Lwip 钩子函数; ```c struct netif * lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src) { struct netif *netif; rt_kprintf("addr 0x%x 0x%x\r\n", dest->addr, src->addr); /* iterate through netifs */ for (netif = netif_list; netif != NULL; netif = netif->next) { /* is the netif up, does it have a link and a valid address? */ if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */ if (src != NULL) { rt_kprintf("iter netif %s\r\n", netif->name); if (ip4_addr_cmp(src, netif_ip4_addr(netif))) { rt_kprintf("found ip4 route src %s\r\n", netif->name); return netif; } } } } netif = netif_default; rt_kprintf("use ip4 route src %s\r\n", netif->name); return netif; } ``` 5. `rt-thread\components\net\lwip-2.0.2\src\netif\ethernetif.c`中增加绑定本地 ip; ```c int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp) { int s, ttl, recv_len, result = 0; int elapsed_time; rt_tick_t recv_start_tick; #if LWIP_VERSION_MAJOR >= 2U struct timeval recv_timeout = { timeout / RT_TICK_PER_SECOND, timeout % RT_TICK_PER_SECOND }; #else int recv_timeout = timeout * 1000UL / RT_TICK_PER_SECOND; #endif ip_addr_t target_addr; struct addrinfo hint, *res = RT_NULL; struct sockaddr_in *h = RT_NULL; struct in_addr ina; struct sockaddr_in local; RT_ASSERT(netif); RT_ASSERT(host); RT_ASSERT(ping_resp); rt_kprintf("lwip ping\r\n"); rt_kprintf("==========\r\n"); rt_memset(&hint, 0x00, sizeof(hint)); /* convert URL to IP */ if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0) { return -RT_ERROR; } rt_memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *)); rt_memcpy(&ina, &h->sin_addr, sizeof(ina)); lwip_freeaddrinfo(res); if (inet_aton(inet_ntoa(ina), &target_addr) == 0) { return -RT_ERROR; } rt_memcpy(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t)); /* new a socket */ if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { return -RT_ERROR; } local.sin_len = sizeof(local); local.sin_family = AF_INET; local.sin_port = 0; // local.sin_addr.s_addr = htons(IP_ADDR_ANY); // local.sin_addr.s_addr = htonl(netif->ip_addr.addr); local.sin_addr.s_addr = (netif->ip_addr.addr); lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in)); rt_kprintf("bind to %x\r\n", local.sin_addr.s_addr); lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout)); if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK) { recv_start_tick = rt_tick_get(); if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0) { elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND; ping_resp->data_len = recv_len; ping_resp->ttl = ttl; ping_resp->ticks = elapsed_time; } else { result = -RT_ETIMEOUT; goto __exit; } } else { result = -RT_ETIMEOUT; goto __exit; } __exit: lwip_close(s); return result; } ``` ![aa3aac26994d4058a8ccde7ea5746f76.png](https://oss-club.rt-thread.org/uploads/20210714/92c3b02dfe0f9447835cf0ce11ce9c9d.png) ![f2b28e83c2bb48c18061a2804ae8c356.png](https://oss-club.rt-thread.org/uploads/20210714/3ad322cd1550dd3818e452fddbdba3dd.png) ![1083ebb7cf9143c7b0b02074ddb3773f.png](https://oss-club.rt-thread.org/uploads/20210714/c045613f656cb3a6ba85dd0933ae8430.png)
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
yukelab
这家伙很懒,什么也没写!
文章
4
回答
47
被采纳
1
关注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
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
10
个答案
1
次被采纳
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
YZRD
2
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部