From 01e66a34fb0ce47c610b94464c7cf6da33b77d16 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Sun, 25 Jan 2026 01:40:06 +0100 Subject: [PATCH] feat(error): add icmp_should_retry for non-blocking retry support --- .gitignore | 2 ++ includes/.gitkeep | 0 includes/icmp.h | 1 + includes/internal/icmp_internal.h | 1 + src/error/should_retry.c | 13 ++++++++++ tests/error/test_error.c | 43 +++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+) delete mode 100644 includes/.gitkeep create mode 100644 src/error/should_retry.c diff --git a/.gitignore b/.gitignore index fe3fbd4..47d9367 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ test.out *.swp *.swo *.bak +.cache/ +compile_commands.json # Logs *.log diff --git a/includes/.gitkeep b/includes/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/includes/icmp.h b/includes/icmp.h index edacd73..883f2d5 100644 --- a/includes/icmp.h +++ b/includes/icmp.h @@ -43,5 +43,6 @@ int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata); /* Error handling */ const char *icmp_strerror(const icmp_handle_t *h); +int icmp_should_retry(const icmp_handle_t *h); #endif /* ICMP_H */ diff --git a/includes/internal/icmp_internal.h b/includes/internal/icmp_internal.h index 7e389b8..cedd274 100644 --- a/includes/internal/icmp_internal.h +++ b/includes/internal/icmp_internal.h @@ -16,6 +16,7 @@ struct icmp_handle { #define ICMP_ERR_RECV -4 #define ICMP_ERR_INVALID -5 #define ICMP_ERR_ENOMEM -6 +#define ICMP_ERR_AGAIN -7 /* Internal error handling functions */ void icmp_set_error(struct icmp_handle *h, int code, const char *msg); diff --git a/src/error/should_retry.c b/src/error/should_retry.c new file mode 100644 index 0000000..cc5437d --- /dev/null +++ b/src/error/should_retry.c @@ -0,0 +1,13 @@ +#include "icmp.h" +#include "internal/icmp_internal.h" + +int +icmp_should_retry(const icmp_handle_t *h) +{ + const struct icmp_handle *handle = (const struct icmp_handle *)h; + + if (NULL == handle) + return 0; + + return (ICMP_ERR_AGAIN == handle->last_error) ? 1 : 0; +} diff --git a/tests/error/test_error.c b/tests/error/test_error.c index 85f3511..ecc821c 100644 --- a/tests/error/test_error.c +++ b/tests/error/test_error.c @@ -56,3 +56,46 @@ Test(error, strerror_no_error) cr_assert_str_eq(msg, "No error"); } + +/* Test 7: should_retry returns 1 for ICMP_ERR_AGAIN */ +Test(error, should_retry_with_eagain) +{ + struct icmp_handle h = {0}; + h.last_error = ICMP_ERR_AGAIN; + strcpy(h.error_msg, "Send would block"); + + int should_retry = icmp_should_retry((icmp_handle_t *)&h); + + cr_assert_eq(should_retry, 1, "Should return 1 for ICMP_ERR_AGAIN"); +} + +/* Test 8: should_retry returns 0 for other errors */ +Test(error, should_retry_with_other_error) +{ + struct icmp_handle h = {0}; + h.last_error = ICMP_ERR_SEND; + strcpy(h.error_msg, "sendto() failed"); + + int should_retry = icmp_should_retry((icmp_handle_t *)&h); + + cr_assert_eq(should_retry, 0, "Should return 0 for ICMP_ERR_SEND"); +} + +/* Test 9: should_retry returns 0 for ICMP_OK */ +Test(error, should_retry_no_error) +{ + struct icmp_handle h = {0}; + h.last_error = ICMP_OK; + + int should_retry = icmp_should_retry((icmp_handle_t *)&h); + + cr_assert_eq(should_retry, 0, "Should return 0 when no error"); +} + +/* Test 10: should_retry with NULL handle returns 0 */ +Test(error, should_retry_null_handle) +{ + int should_retry = icmp_should_retry(NULL); + + cr_assert_eq(should_retry, 0, "Should return 0 for NULL handle"); +}