From cda78884aa373cf93555f41a022a80d73f26ce74 Mon Sep 17 00:00:00 2001 From: yukelab Date: Tue, 7 Sep 2021 10:20:58 +0800 Subject: [PATCH 1/3] ping cmd with specified netif in lwip-2.1.2 using LWIP_HOOK_IP4_ROUTE_SRC hook find specified netif route, using cmd `ping 192.168.xx.xx e0`, ping dest using e0 netif. if not found netif, using default netif, the effect is same as the cmd `ping 192.168.xx.xx` that only ping with default netif. --- components/net/lwip-2.1.2/src/arch/sys_arch.c | 24 +++++++++++++++++++ components/net/lwip-2.1.2/src/lwipopts.h | 1 + .../net/lwip-2.1.2/src/netif/ethernetif.c | 7 ++++++ components/net/netdev/src/netdev.c | 20 +++++++++++++--- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/components/net/lwip-2.1.2/src/arch/sys_arch.c b/components/net/lwip-2.1.2/src/arch/sys_arch.c index c97609a4ea..2f612bd6b3 100644 --- a/components/net/lwip-2.1.2/src/arch/sys_arch.c +++ b/components/net/lwip-2.1.2/src/arch/sys_arch.c @@ -779,6 +779,30 @@ void ppp_trace(int level, const char *format, ...) } #endif +struct netif *lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src) +{ + struct netif *netif; + + /* 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) + { + if (ip4_addr_cmp(src, netif_ip4_addr(netif))) + { + return netif; + } + } + } + } + netif = netif_default; + return netif; +} + /* * export bsd socket symbol for RT-Thread Application Module */ diff --git a/components/net/lwip-2.1.2/src/lwipopts.h b/components/net/lwip-2.1.2/src/lwipopts.h index 81409a9d0a..23e4a69b64 100644 --- a/components/net/lwip-2.1.2/src/lwipopts.h +++ b/components/net/lwip-2.1.2/src/lwipopts.h @@ -648,4 +648,5 @@ #endif +#define LWIP_HOOK_IP4_ROUTE_SRC(dest, src) lwip_ip4_route_src(dest, src) #endif /* __LWIPOPTS_H__ */ diff --git a/components/net/lwip-2.1.2/src/netif/ethernetif.c b/components/net/lwip-2.1.2/src/netif/ethernetif.c index e9c2a20a15..536a339301 100644 --- a/components/net/lwip-2.1.2/src/netif/ethernetif.c +++ b/components/net/lwip-2.1.2/src/netif/ethernetif.c @@ -201,6 +201,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, 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); @@ -227,6 +228,12 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, return -RT_ERROR; } + local.sin_len = sizeof(local); + local.sin_family = AF_INET; + local.sin_port = 0; + local.sin_addr.s_addr = (netif->ip_addr.addr); + lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in)); + lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout)); if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK) diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index 4c5d524510..231c6e523d 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -1052,7 +1052,7 @@ MSH_CMD_EXPORT_ALIAS(netdev_ifconfig, ifconfig, list the information of all netw #endif /* NETDEV_USING_IFCONFIG */ #ifdef NETDEV_USING_PING -int netdev_cmd_ping(char* target_name, rt_uint32_t times, rt_size_t size) +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 */ @@ -1073,6 +1073,16 @@ int netdev_cmd_ping(char* target_name, rt_uint32_t times, rt_size_t size) 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 */ @@ -1146,9 +1156,13 @@ int netdev_ping(int argc, char **argv) { rt_kprintf("Please input: ping \n"); } - else + else if (argc == 2) { - netdev_cmd_ping(argv[1], 4, 0); + netdev_cmd_ping(argv[1], RT_NULL, 4, 0); + } + else if (argc == 3) + { + netdev_cmd_ping(argv[1], argv[2], 4, 0); } return 0; From be2006f3255f0786398b9b7db3f7cc36dfa50fc2 Mon Sep 17 00:00:00 2001 From: yukelab Date: Thu, 9 Sep 2021 14:29:23 +0800 Subject: [PATCH 2/3] fix sys_arch.c --- components/net/lwip-2.1.2/src/arch/sys_arch.c | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/components/net/lwip-2.1.2/src/arch/sys_arch.c b/components/net/lwip-2.1.2/src/arch/sys_arch.c index 2f612bd6b3..5107708a8a 100644 --- a/components/net/lwip-2.1.2/src/arch/sys_arch.c +++ b/components/net/lwip-2.1.2/src/arch/sys_arch.c @@ -786,21 +786,21 @@ struct netif *lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src) /* 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) - { - if (ip4_addr_cmp(src, netif_ip4_addr(netif))) + /* 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))) { - return netif; + /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */ + if (src != NULL) + { + if (ip4_addr_cmp(src, netif_ip4_addr(netif))) + { + return netif; + } + } } } - } - } - netif = netif_default; - return netif; + netif = netif_default; + return netif; } /* From 8e518f234af316ce86770dff11bad08f2eaefc65 Mon Sep 17 00:00:00 2001 From: liuxianliang Date: Mon, 15 Nov 2021 15:58:31 +0800 Subject: [PATCH 3/3] [fix] format --- .../net/lwip-2.1.2/src/netif/ethernetif.c | 78 +++++++++---------- components/net/netdev/src/netdev.c | 2 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/components/net/lwip-2.1.2/src/netif/ethernetif.c b/components/net/lwip-2.1.2/src/netif/ethernetif.c index 536a339301..0625ff74b0 100644 --- a/components/net/lwip-2.1.2/src/netif/ethernetif.c +++ b/components/net/lwip-2.1.2/src/netif/ethernetif.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. - * COPYRIGHT (C) 2006-2018, RT-Thread Development Team + * COPYRIGHT (C) 2006-2021, RT-Thread Development Team * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -167,16 +167,16 @@ static int lwip_netdev_set_dns_server(struct netdev *netif, uint8_t dns_num, ip_ static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled) { netdev_low_level_set_dhcp_status(netif, is_enabled); - + if(RT_TRUE == is_enabled) { dhcp_start((struct netif *)netif->user_data); } else { - dhcp_stop((struct netif *)netif->user_data); + dhcp_stop((struct netif *)netif->user_data); } - + return ERR_OK; } #endif /* RT_LWIP_DHCP */ @@ -186,7 +186,7 @@ static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled) extern int lwip_ping_recv(int s, int *ttl); extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size); -int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, +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; @@ -202,7 +202,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, struct sockaddr_in *h = RT_NULL; struct in_addr ina; struct sockaddr_in local; - + RT_ASSERT(netif); RT_ASSERT(host); RT_ASSERT(ping_resp); @@ -221,7 +221,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len, return -RT_ERROR; } SMEMCPY(&(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) { @@ -295,7 +295,7 @@ const struct netdev_ops lwip_netdev_ops = lwip_netdev_set_addr_info, #ifdef RT_LWIP_DNS lwip_netdev_set_dns_server, -#else +#else NULL, #endif /* RT_LWIP_DNS */ @@ -334,7 +334,7 @@ static int netdev_add(struct netif *lwip_netif) { return -ERR_IF; } - + #ifdef SAL_USING_LWIP extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev); /* set the lwIP network interface device protocol family information */ @@ -343,7 +343,7 @@ static int netdev_add(struct netif *lwip_netif) rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN); result = netdev_register(netdev, name, (void *)lwip_netif); - + /* Update netdev info after registered */ netdev->flags = lwip_netif->flags; netdev->mtu = lwip_netif->mtu; @@ -386,7 +386,7 @@ static int netdev_flags_sync(struct netif *lwip_netif) { return -ERR_IF; } - + netdev->mtu = lwip_netif->mtu; netdev->flags |= lwip_netif->flags; @@ -448,7 +448,7 @@ static err_t eth_netif_device_init(struct netif *netif) /* copy device flags to netif flags */ netif->flags = (ethif->flags & 0xff); netif->mtu = ETHERNET_MTU; - + /* set output */ netif->output = etharp_output; @@ -541,7 +541,7 @@ rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_ /* set linkoutput */ netif->linkoutput = ethernetif_linkoutput; - + /* get hardware MAC address */ rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr); @@ -561,7 +561,7 @@ rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_ ipaddr.addr = inet_addr(RT_LWIP_IPADDR); gw.addr = inet_addr(RT_LWIP_GWADDR); netmask.addr = inet_addr(RT_LWIP_MSKADDR); -#else +#else IP4_ADDR(&ipaddr, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); @@ -726,7 +726,7 @@ static void eth_rx_thread_entry(void* parameter) while (1) { if(device->eth_rx == RT_NULL) break; - + p = device->eth_rx(&(device->parent)); if (p != RT_NULL) { @@ -749,9 +749,9 @@ static void eth_rx_thread_entry(void* parameter) } #endif -/* this function does not need, - * use eth_system_device_init_private() - * call by lwip_system_init(). +/* this function does not need, + * use eth_system_device_init_private() + * call by lwip_system_init(). */ int eth_system_device_init(void) { @@ -894,27 +894,27 @@ void list_if(void) rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw))); rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask))); #if LWIP_IPV6 - { - ip6_addr_t *addr; - int addr_state; - int i; - - addr = (ip6_addr_t *)&netif->ip6_addr[0]; - addr_state = netif->ip6_addr_state[0]; - - rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr), - addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID"); - - for(i=1; iip6_addr[i]; - addr_state = netif->ip6_addr_state[i]; - - rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr), - addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID"); - } - - } + { + ip6_addr_t *addr; + int addr_state; + int i; + + addr = (ip6_addr_t *)&netif->ip6_addr[0]; + addr_state = netif->ip6_addr_state[0]; + + rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr), + addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID"); + + for(i=1; iip6_addr[i]; + addr_state = netif->ip6_addr_state[i]; + + rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr), + addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID"); + } + + } rt_kprintf("\r\n"); #endif /* LWIP_IPV6 */ netif = netif->next; diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index 231c6e523d..f121f52c19 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -1154,7 +1154,7 @@ int netdev_ping(int argc, char **argv) { if (argc == 1) { - rt_kprintf("Please input: ping \n"); + rt_kprintf("Please input: ping [netdev name] \n"); } else if (argc == 2) {