From 87a4650840d30faa63b787d0335afc0dcfd7453c Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Sun, 8 Mar 2026 16:32:04 +0100 Subject: [PATCH] feat: add echo payload option --- includes/icmp.h | 3 ++- src/send/api/echo.c | 5 +++-- tests/recv/test_process.c | 2 +- tests/send/test_send_echo.c | 14 +++++++------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/includes/icmp.h b/includes/icmp.h index 4c848d3..ad2bcd3 100644 --- a/includes/icmp.h +++ b/includes/icmp.h @@ -59,7 +59,8 @@ int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code, uint8_t ttl); int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id, - uint16_t seq, uint8_t ttl); + uint16_t seq, uint8_t ttl, + const void *payload, size_t payload_len); /* Receive function */ int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata, diff --git a/src/send/api/echo.c b/src/send/api/echo.c index 8812005..172113e 100644 --- a/src/send/api/echo.c +++ b/src/send/api/echo.c @@ -5,7 +5,8 @@ int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id, - uint16_t seq, uint8_t ttl) + uint16_t seq, uint8_t ttl, + const void *payload, size_t payload_len) { union { struct { uint16_t id; uint16_t seq; } echo; @@ -15,5 +16,5 @@ icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id, hdr_rest.echo.id = htons(id); hdr_rest.echo.seq = htons(seq); return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw, - NULL, 0, dest, ttl); + payload, payload_len, dest, ttl); } diff --git a/tests/recv/test_process.c b/tests/recv/test_process.c index e5189c6..b752c78 100644 --- a/tests/recv/test_process.c +++ b/tests/recv/test_process.c @@ -71,7 +71,7 @@ Test(process, echo_loopback, .disabled = true) h = icmp_create(); cr_assert_not_null(h); inet_pton(AF_INET, "127.0.0.1", &dest); - ret = icmp_send_echo(h, dest, 1234, 1, 64); + ret = icmp_send_echo(h, dest, 1234, 1, 64, NULL, 0); cr_assert_eq(ret, 0); sleep(1); count = 0; diff --git a/tests/send/test_send_echo.c b/tests/send/test_send_echo.c index 46935cc..8841cca 100644 --- a/tests/send/test_send_echo.c +++ b/tests/send/test_send_echo.c @@ -18,7 +18,7 @@ Test(send_echo, simple_echo) struct in_addr dest; dest.s_addr = inet_addr("127.0.0.1"); - int ret = icmp_send_echo(h, dest, 0x1234, 1, 64); + int ret = icmp_send_echo(h, dest, 0x1234, 1, 64, NULL, 0); cr_assert_eq(ret, 0, "Expected success (0)"); cr_assert_str_eq(icmp_strerror(h), "No error"); @@ -32,7 +32,7 @@ Test(send_echo, null_handle) struct in_addr dest; dest.s_addr = inet_addr("127.0.0.1"); - int ret = icmp_send_echo(NULL, dest, 1, 1, 64); + int ret = icmp_send_echo(NULL, dest, 1, 1, 64, NULL, 0); cr_assert_eq(ret, -1, "Expected -1 for NULL handle"); } @@ -46,7 +46,7 @@ Test(send_echo, invalid_fd) struct in_addr dest; dest.s_addr = inet_addr("127.0.0.1"); - int ret = icmp_send_echo((icmp_handle_t *)&h, dest, 1, 1, 64); + int ret = icmp_send_echo((icmp_handle_t *)&h, dest, 1, 1, 64, NULL, 0); cr_assert_eq(ret, -1, "Expected -1 for invalid fd"); } @@ -65,10 +65,10 @@ Test(send_echo, different_ttl) struct in_addr dest; dest.s_addr = inet_addr("127.0.0.1"); - int ret1 = icmp_send_echo(h, dest, 1, 1, 1); + int ret1 = icmp_send_echo(h, dest, 1, 1, 1, NULL, 0); cr_assert_eq(ret1, 0, "Expected success with TTL=1"); - int ret2 = icmp_send_echo(h, dest, 1, 2, 255); + int ret2 = icmp_send_echo(h, dest, 1, 2, 255, NULL, 0); cr_assert_eq(ret2, 0, "Expected success with TTL=255"); icmp_destroy(h); @@ -88,10 +88,10 @@ Test(send_echo, different_id_seq) struct in_addr dest; dest.s_addr = inet_addr("127.0.0.1"); - int ret1 = icmp_send_echo(h, dest, 0xABCD, 42, 64); + int ret1 = icmp_send_echo(h, dest, 0xABCD, 42, 64, NULL, 0); cr_assert_eq(ret1, 0, "Expected success with id=0xABCD, seq=42"); - int ret2 = icmp_send_echo(h, dest, 0, 0, 64); + int ret2 = icmp_send_echo(h, dest, 0, 0, 64, NULL, 0); cr_assert_eq(ret2, 0, "Expected success with id=0, seq=0"); icmp_destroy(h);