ping 过程如何选择网卡 路由(routing)就是通过互联的网络把信息从源地址传输到目的地址的活动,发送端必然需要找到一个网卡将数据报发送出去,ping 的过程中需要通过 ip4_route 发现合适的网卡;
LwIP 中发现网卡的默认规则是,遍历挂接的网卡设备, 比对远程主机的ip和网卡的ip是否在同一网段,如果是,则返回同一网段的网卡; 如果 定义了钩子函数LWIP_HOOK_IP4_ROUTE_SRC,如果上面无法找到对应网卡时,则会 通过该钩子函数查找网卡; 如果1,2均未找到网卡,返回默认网卡; 实现过程 rt-threadcomponents
et
etdevsrc
etdev.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.
"); 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.
"); 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.
", 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.
", netdev->name); return -RT_ERROR; } } rt_kprintf("network interface: %s
", 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
", (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
", (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
", 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
", 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; } rt-threadcomponents
et
etdevsrc
etdev.c修改ping命令,增加网卡参数; int netdev_ping(int argc, char **argv) { if (argc == 1) { rt_kprintf("Please input: ping
"); } 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); rt-threadcomponents
etlwip-2.0.2srclwipopts.h 使能钩子函数 #define LWIP_HOOK_IP4_ROUTE_SRC(dest, src) lwip_ip4_route_src(dest, src rt-threadcomponents
etlwip-2.0.2srcarchsys_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
”, 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
”, netif->name); if (ip4_addr_cmp(src, netif_ip4_addr(netif))) { rt_kprintf(“found ip4 route src %s
”, netif->name); return netif; } } } } netif = netif_default; rt_kprintf(“use ip4 route src %s
”, netif->name); return netif; } 5. `rt-threadcomponents
etlwip-2.0.2src
etifethernetif.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
"); rt_kprintf("==========
"); 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
", 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;
原作者:yukelab
|