feat: add echo payload option
This commit is contained in:
parent
69a6a633d4
commit
87a4650840
4 changed files with 13 additions and 11 deletions
|
|
@ -59,7 +59,8 @@ int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
|
||||||
uint8_t ttl);
|
uint8_t ttl);
|
||||||
|
|
||||||
int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
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 */
|
/* Receive function */
|
||||||
int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@
|
||||||
|
|
||||||
int
|
int
|
||||||
icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
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 {
|
union {
|
||||||
struct { uint16_t id; uint16_t seq; } echo;
|
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.id = htons(id);
|
||||||
hdr_rest.echo.seq = htons(seq);
|
hdr_rest.echo.seq = htons(seq);
|
||||||
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw,
|
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw,
|
||||||
NULL, 0, dest, ttl);
|
payload, payload_len, dest, ttl);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ Test(process, echo_loopback, .disabled = true)
|
||||||
h = icmp_create();
|
h = icmp_create();
|
||||||
cr_assert_not_null(h);
|
cr_assert_not_null(h);
|
||||||
inet_pton(AF_INET, "127.0.0.1", &dest);
|
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);
|
cr_assert_eq(ret, 0);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ Test(send_echo, simple_echo)
|
||||||
struct in_addr dest;
|
struct in_addr dest;
|
||||||
dest.s_addr = inet_addr("127.0.0.1");
|
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_eq(ret, 0, "Expected success (0)");
|
||||||
cr_assert_str_eq(icmp_strerror(h), "No error");
|
cr_assert_str_eq(icmp_strerror(h), "No error");
|
||||||
|
|
@ -32,7 +32,7 @@ Test(send_echo, null_handle)
|
||||||
struct in_addr dest;
|
struct in_addr dest;
|
||||||
dest.s_addr = inet_addr("127.0.0.1");
|
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");
|
cr_assert_eq(ret, -1, "Expected -1 for NULL handle");
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +46,7 @@ Test(send_echo, invalid_fd)
|
||||||
struct in_addr dest;
|
struct in_addr dest;
|
||||||
dest.s_addr = inet_addr("127.0.0.1");
|
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");
|
cr_assert_eq(ret, -1, "Expected -1 for invalid fd");
|
||||||
}
|
}
|
||||||
|
|
@ -65,10 +65,10 @@ Test(send_echo, different_ttl)
|
||||||
struct in_addr dest;
|
struct in_addr dest;
|
||||||
dest.s_addr = inet_addr("127.0.0.1");
|
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");
|
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");
|
cr_assert_eq(ret2, 0, "Expected success with TTL=255");
|
||||||
|
|
||||||
icmp_destroy(h);
|
icmp_destroy(h);
|
||||||
|
|
@ -88,10 +88,10 @@ Test(send_echo, different_id_seq)
|
||||||
struct in_addr dest;
|
struct in_addr dest;
|
||||||
dest.s_addr = inet_addr("127.0.0.1");
|
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");
|
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");
|
cr_assert_eq(ret2, 0, "Expected success with id=0, seq=0");
|
||||||
|
|
||||||
icmp_destroy(h);
|
icmp_destroy(h);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue