diff --git a/components/console_cmd_ping/README.md b/components/console_cmd_ping/README.md index 169885489..2e6f640ba 100644 --- a/components/console_cmd_ping/README.md +++ b/components/console_cmd_ping/README.md @@ -48,7 +48,7 @@ For more details refer [IDF Component Manager](https://docs.espressif.com/projec ### ping: ``` -ping [-W ] [-i ] [-s ] [-c ] [-Q ] [-T ] +ping [-W ] [-i ] [-s ] [-c ] [-Q ] [-T ] [-I ] send ICMP ECHO_REQUEST to network hosts -W, --timeout= Time to wait for a response, in seconds -i, --interval= Wait interval seconds between sending each packet @@ -56,6 +56,7 @@ ping [-W ] [-i ] [-s ] [-c ] [-Q ] [-T ] -c, --count= Stop after sending count packets -Q, --tos= Set Type of Service related bits in IP datagrams -T, --ttl= Set Time to Live related bits in IP datagrams + -I, --interface= Set Interface number 0=no-interface selected, >0 netif number + 1 (1 is usually 'lo0') Host address getaddrinfo [-f ] [-F ]... [-p ] @@ -121,8 +122,8 @@ getaddrinfo -f AF_INET -F AI_PASSIVE www.example.com ping www.example.com ``` -2. To specify additional options, such as timeout, interval, packet size, etc.: +2. To specify additional options, such as timeout, interval, packet size, interface, etc.: ``` -ping -W 5 -i 1 -s 64 -c 4 -Q 0x10 -T 64 www.example.com +ping -W 5 -i 1 -s 64 -c 4 -Q 0x10 -T 64 -I 0 www.example.com ``` diff --git a/components/console_cmd_ping/console_ping.c b/components/console_cmd_ping/console_ping.c index 501f282fd..c5e830138 100644 --- a/components/console_cmd_ping/console_ping.c +++ b/components/console_cmd_ping/console_ping.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -99,6 +99,7 @@ static struct { struct arg_int *count; struct arg_int *tos; struct arg_int *ttl; + struct arg_int *interface; struct arg_str *host; struct arg_end *end; } ping_args; @@ -137,6 +138,10 @@ static int do_ping_cmd(int argc, char **argv) config.ttl = (uint32_t)(ping_args.ttl->ival[0]); } + if (ping_args.interface->count > 0) { + config.interface = (uint32_t)(ping_args.interface->ival[0]); + } + // parse IP address struct sockaddr_in6 sock_addr6; ip_addr_t target_addr = {0}; @@ -155,12 +160,12 @@ static int do_ping_cmd(int argc, char **argv) } if (res->ai_family == AF_INET) { #if CONFIG_LWIP_IPV4 - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); #endif } else { #if CONFIG_LWIP_IPV6 - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; + struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); #endif } @@ -204,6 +209,7 @@ esp_err_t console_cmd_ping_register(void) ping_args.count = arg_int0("c", "count", "", "Stop after sending count packets"); ping_args.tos = arg_int0("Q", "tos", "", "Set Type of Service related bits in IP datagrams"); ping_args.ttl = arg_int0("T", "ttl", "", "Set Time to Live related bits in IP datagrams"); + ping_args.interface = arg_int0("I", "interface", "", "Set Interface number"); ping_args.host = arg_str1(NULL, NULL, "", "Host address"); ping_args.end = arg_end(1); const esp_console_cmd_t ping_cmd = {