feat(error): add icmp_should_retry for non-blocking retry support

This commit is contained in:
lohhiiccc 2026-01-25 01:40:06 +01:00
parent b7be251a18
commit 01e66a34fb
6 changed files with 60 additions and 0 deletions

2
.gitignore vendored
View file

@ -14,6 +14,8 @@ test.out
*.swp *.swp
*.swo *.swo
*.bak *.bak
.cache/
compile_commands.json
# Logs # Logs
*.log *.log

View file

View file

@ -43,5 +43,6 @@ int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata);
/* Error handling */ /* Error handling */
const char *icmp_strerror(const icmp_handle_t *h); const char *icmp_strerror(const icmp_handle_t *h);
int icmp_should_retry(const icmp_handle_t *h);
#endif /* ICMP_H */ #endif /* ICMP_H */

View file

@ -16,6 +16,7 @@ struct icmp_handle {
#define ICMP_ERR_RECV -4 #define ICMP_ERR_RECV -4
#define ICMP_ERR_INVALID -5 #define ICMP_ERR_INVALID -5
#define ICMP_ERR_ENOMEM -6 #define ICMP_ERR_ENOMEM -6
#define ICMP_ERR_AGAIN -7
/* Internal error handling functions */ /* Internal error handling functions */
void icmp_set_error(struct icmp_handle *h, int code, const char *msg); void icmp_set_error(struct icmp_handle *h, int code, const char *msg);

13
src/error/should_retry.c Normal file
View file

@ -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;
}

View file

@ -56,3 +56,46 @@ Test(error, strerror_no_error)
cr_assert_str_eq(msg, "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");
}