feat: add max_packets parameter to make icmp_process non-blocking
This commit is contained in:
parent
bd367b97dc
commit
e57d43de33
3 changed files with 14 additions and 9 deletions
|
|
@ -41,7 +41,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);
|
||||||
|
|
||||||
/* 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,
|
||||||
|
size_t max_packets);
|
||||||
|
|
||||||
/* Error handling */
|
/* Error handling */
|
||||||
const char *icmp_strerror(const icmp_handle_t *h);
|
const char *icmp_strerror(const icmp_handle_t *h);
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,18 @@
|
||||||
#define MAX_PACKET_SIZE 1500
|
#define MAX_PACKET_SIZE 1500
|
||||||
|
|
||||||
int
|
int
|
||||||
icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata)
|
icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
||||||
|
size_t max_packets)
|
||||||
{
|
{
|
||||||
uint8_t buffer[MAX_PACKET_SIZE];
|
uint8_t buffer[MAX_PACKET_SIZE];
|
||||||
struct sockaddr_in from;
|
struct sockaddr_in from;
|
||||||
ssize_t recv_len;
|
ssize_t recv_len;
|
||||||
|
int processed;
|
||||||
|
|
||||||
if (0 == recv_validate_params(h, cb))
|
if (0 == recv_validate_params(h, cb))
|
||||||
return -1;
|
return -1;
|
||||||
while (1)
|
processed = 0;
|
||||||
|
while (max_packets == 0 || (size_t)processed < max_packets)
|
||||||
{
|
{
|
||||||
recv_len = recv_receive_packet(h->fd, buffer, sizeof(buffer), &from);
|
recv_len = recv_receive_packet(h->fd, buffer, sizeof(buffer), &from);
|
||||||
if (0 == recv_len)
|
if (0 == recv_len)
|
||||||
|
|
@ -22,7 +25,8 @@ icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata)
|
||||||
if (recv_len < 0)
|
if (recv_len < 0)
|
||||||
return recv_handle_receive_error(h);
|
return recv_handle_receive_error(h);
|
||||||
recv_process_single_packet(buffer, recv_len, cb, userdata);
|
recv_process_single_packet(buffer, recv_len, cb, userdata);
|
||||||
|
processed++;
|
||||||
}
|
}
|
||||||
icmp_clear_error(h);
|
icmp_clear_error(h);
|
||||||
return 0;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ Test(process, null_handle)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = icmp_process(NULL, dummy_callback, NULL);
|
ret = icmp_process(NULL, dummy_callback, NULL, 0);
|
||||||
cr_assert_eq(ret, -1);
|
cr_assert_eq(ret, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ Test(process, null_callback)
|
||||||
cr_skip("Test requires CAP_NET_RAW or root privileges");
|
cr_skip("Test requires CAP_NET_RAW or root privileges");
|
||||||
h = icmp_create();
|
h = icmp_create();
|
||||||
cr_assert_not_null(h);
|
cr_assert_not_null(h);
|
||||||
ret = icmp_process(h, NULL, NULL);
|
ret = icmp_process(h, NULL, NULL, 0);
|
||||||
cr_assert_eq(ret, -1);
|
cr_assert_eq(ret, -1);
|
||||||
cr_assert_str_eq(icmp_strerror(h), "Callback cannot be NULL");
|
cr_assert_str_eq(icmp_strerror(h), "Callback cannot be NULL");
|
||||||
icmp_destroy(h);
|
icmp_destroy(h);
|
||||||
|
|
@ -54,7 +54,7 @@ Test(process, no_packets_available)
|
||||||
cr_skip("Test requires CAP_NET_RAW or root privileges");
|
cr_skip("Test requires CAP_NET_RAW or root privileges");
|
||||||
h = icmp_create();
|
h = icmp_create();
|
||||||
cr_assert_not_null(h);
|
cr_assert_not_null(h);
|
||||||
ret = icmp_process(h, dummy_callback, NULL);
|
ret = icmp_process(h, dummy_callback, NULL, 0);
|
||||||
cr_assert_eq(ret, 0);
|
cr_assert_eq(ret, 0);
|
||||||
icmp_destroy(h);
|
icmp_destroy(h);
|
||||||
}
|
}
|
||||||
|
|
@ -75,8 +75,8 @@ Test(process, echo_loopback, .disabled = true)
|
||||||
cr_assert_eq(ret, 0);
|
cr_assert_eq(ret, 0);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
count = 0;
|
count = 0;
|
||||||
ret = icmp_process(h, counter_callback, &count);
|
ret = icmp_process(h, counter_callback, &count, 0);
|
||||||
cr_assert_eq(ret, 0);
|
cr_assert_geq(ret, 0);
|
||||||
cr_assert_geq(count, 1, "Should receive at least one packet");
|
cr_assert_geq(count, 1, "Should receive at least one packet");
|
||||||
icmp_destroy(h);
|
icmp_destroy(h);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue