Skip to content

Commit b1df7cf

Browse files
committed
net/icmpv4: Allow for arbitrary payload data in ICMP echo
Allow for including arbitrary data in net_icmpv4_send_echo_request() that will be echoed verbatim by the receiver. This allows to use ICMP echo for diagnostic use cases, e.g. by testing packet framentation (large payload) or measuring round-trip-time. Signed-off-by: Benjamin Valentin <[email protected]>
1 parent 8a966ec commit b1df7cf

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

subsys/net/ip/icmpv4.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ static enum net_verdict icmpv4_handle_echo_request(struct net_pkt *pkt,
129129
int net_icmpv4_send_echo_request(struct net_if *iface,
130130
struct in_addr *dst,
131131
u16_t identifier,
132-
u16_t sequence)
132+
u16_t sequence,
133+
const void *data,
134+
size_t data_size)
133135
{
134136
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(icmpv4_access,
135137
struct net_icmpv4_echo_req);
@@ -146,7 +148,8 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
146148
src = &iface->config.ip.ipv4->unicast[0].address.in_addr;
147149

148150
pkt = net_pkt_alloc_with_buffer(iface,
149-
sizeof(struct net_icmpv4_echo_req),
151+
sizeof(struct net_icmpv4_echo_req)
152+
+ data_size,
150153
AF_INET, IPPROTO_ICMP,
151154
PKT_WAIT_TIME);
152155
if (!pkt) {
@@ -168,6 +171,7 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
168171
echo_req->sequence = htons(sequence);
169172

170173
net_pkt_set_data(pkt, &icmpv4_access);
174+
net_pkt_write(pkt, data, data_size);
171175

172176
net_pkt_cursor_init(pkt);
173177

subsys/net/ip/icmpv4.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ int net_icmpv4_send_error(struct net_pkt *pkt, u8_t type, u8_t code);
6262
* to this Echo Request. May be zero.
6363
* @param sequence A sequence number to aid in matching Echo Replies
6464
* to this Echo Request. May be zero.
65+
* @param data Arbitrary payload data that will be included in the
66+
* Echo Reply verbatim. May be zero.
67+
* @param data_size Size of the Payload Data in bytes. May be zero.
6568
*
6669
* @return Return 0 if the sending succeed, <0 otherwise.
6770
*/
6871
int net_icmpv4_send_echo_request(struct net_if *iface,
6972
struct in_addr *dst,
7073
u16_t identifier,
71-
u16_t sequence);
74+
u16_t sequence,
75+
const void *data,
76+
size_t data_size);
7277

7378
void net_icmpv4_register_handler(struct net_icmpv4_handler *handler);
7479

subsys/net/ip/net_shell.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,9 @@ static int ping_ipv4(const struct shell *shell, char *host)
27632763
net_if_ipv4_select_src_iface(&ipv4_target),
27642764
&ipv4_target,
27652765
sys_rand32_get(),
2766-
sys_rand32_get());
2766+
sys_rand32_get(),
2767+
NULL,
2768+
0);
27672769
if (ret) {
27682770
remove_ipv4_ping_handler();
27692771
} else {

0 commit comments

Comments
 (0)