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 <benjamin.valentin@ml-pa.com>
This commit is contained in:
Benjamin Valentin 2019-03-05 18:35:50 +01:00 committed by Jukka Rissanen
parent b938324345
commit dd65cfb533
3 changed files with 15 additions and 4 deletions

View file

@ -129,7 +129,9 @@ drop:
int net_icmpv4_send_echo_request(struct net_if *iface,
struct in_addr *dst,
u16_t identifier,
u16_t sequence)
u16_t sequence,
const void *data,
size_t data_size)
{
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(icmpv4_access,
struct net_icmpv4_echo_req);
@ -146,7 +148,8 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
src = &iface->config.ip.ipv4->unicast[0].address.in_addr;
pkt = net_pkt_alloc_with_buffer(iface,
sizeof(struct net_icmpv4_echo_req),
sizeof(struct net_icmpv4_echo_req)
+ data_size,
AF_INET, IPPROTO_ICMP,
PKT_WAIT_TIME);
if (!pkt) {
@ -168,6 +171,7 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
echo_req->sequence = htons(sequence);
net_pkt_set_data(pkt, &icmpv4_access);
net_pkt_write(pkt, data, data_size);
net_pkt_cursor_init(pkt);

View file

@ -62,13 +62,18 @@ int net_icmpv4_send_error(struct net_pkt *pkt, u8_t type, u8_t code);
* to this Echo Request. May be zero.
* @param sequence A sequence number to aid in matching Echo Replies
* to this Echo Request. May be zero.
* @param data Arbitrary payload data that will be included in the
* Echo Reply verbatim. May be zero.
* @param data_size Size of the Payload Data in bytes. May be zero.
*
* @return Return 0 if the sending succeed, <0 otherwise.
*/
int net_icmpv4_send_echo_request(struct net_if *iface,
struct in_addr *dst,
u16_t identifier,
u16_t sequence);
u16_t sequence,
const void *data,
size_t data_size);
void net_icmpv4_register_handler(struct net_icmpv4_handler *handler);

View file

@ -2763,7 +2763,9 @@ static int ping_ipv4(const struct shell *shell, char *host)
net_if_ipv4_select_src_iface(&ipv4_target),
&ipv4_target,
sys_rand32_get(),
sys_rand32_get());
sys_rand32_get(),
NULL,
0);
if (ret) {
remove_ipv4_ping_handler();
} else {