Skip to content

Commit c078c36

Browse files
authored
Merge pull request #886 from david-cermak/feat/console_ping_interface
[console_ping]: Add support for interface argument
2 parents cee3bde + 90ddb04 commit c078c36

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

components/console_cmd_ping/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ For more details refer [IDF Component Manager](https://docs.espressif.com/projec
4848

4949
### ping:
5050
```
51-
ping [-W <t>] [-i <t>] [-s <n>] [-c <n>] [-Q <n>] [-T <n>] <host>
51+
ping [-W <t>] [-i <t>] [-s <n>] [-c <n>] [-Q <n>] [-T <n>] [-I <n>] <host>
5252
send ICMP ECHO_REQUEST to network hosts
5353
-W, --timeout=<t> Time to wait for a response, in seconds
5454
-i, --interval=<t> Wait interval seconds between sending each packet
5555
-s, --size=<n> Specify the number of data bytes to be sent
5656
-c, --count=<n> Stop after sending count packets
5757
-Q, --tos=<n> Set Type of Service related bits in IP datagrams
5858
-T, --ttl=<n> Set Time to Live related bits in IP datagrams
59+
-I, --interface=<n> Set Interface number 0=no-interface selected, >0 netif number + 1 (1 is usually 'lo0')
5960
<host> Host address
6061
6162
getaddrinfo [-f <AF>] [-F <FLAGS>]... [-p <port>] <hostname>
@@ -121,8 +122,8 @@ getaddrinfo -f AF_INET -F AI_PASSIVE www.example.com
121122
ping www.example.com
122123
```
123124

124-
2. To specify additional options, such as timeout, interval, packet size, etc.:
125+
2. To specify additional options, such as timeout, interval, packet size, interface, etc.:
125126

126127
```
127-
ping -W 5 -i 1 -s 64 -c 4 -Q 0x10 -T 64 www.example.com
128+
ping -W 5 -i 1 -s 64 -c 4 -Q 0x10 -T 64 -I 0 www.example.com
128129
```

components/console_cmd_ping/console_ping.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -99,6 +99,7 @@ static struct {
9999
struct arg_int *count;
100100
struct arg_int *tos;
101101
struct arg_int *ttl;
102+
struct arg_int *interface;
102103
struct arg_str *host;
103104
struct arg_end *end;
104105
} ping_args;
@@ -137,6 +138,10 @@ static int do_ping_cmd(int argc, char **argv)
137138
config.ttl = (uint32_t)(ping_args.ttl->ival[0]);
138139
}
139140

141+
if (ping_args.interface->count > 0) {
142+
config.interface = (uint32_t)(ping_args.interface->ival[0]);
143+
}
144+
140145
// parse IP address
141146
struct sockaddr_in6 sock_addr6;
142147
ip_addr_t target_addr = {0};
@@ -155,12 +160,12 @@ static int do_ping_cmd(int argc, char **argv)
155160
}
156161
if (res->ai_family == AF_INET) {
157162
#if CONFIG_LWIP_IPV4
158-
struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
163+
struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr;
159164
inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
160165
#endif
161166
} else {
162167
#if CONFIG_LWIP_IPV6
163-
struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr;
168+
struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr;
164169
inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6);
165170
#endif
166171
}
@@ -204,6 +209,7 @@ esp_err_t console_cmd_ping_register(void)
204209
ping_args.count = arg_int0("c", "count", "<n>", "Stop after sending count packets");
205210
ping_args.tos = arg_int0("Q", "tos", "<n>", "Set Type of Service related bits in IP datagrams");
206211
ping_args.ttl = arg_int0("T", "ttl", "<n>", "Set Time to Live related bits in IP datagrams");
212+
ping_args.interface = arg_int0("I", "interface", "<n>", "Set Interface number");
207213
ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address");
208214
ping_args.end = arg_end(1);
209215
const esp_console_cmd_t ping_cmd = {

0 commit comments

Comments
 (0)