refactor: extract send error handling to separate function

This commit is contained in:
lohhiiccc 2026-01-27 21:35:47 +01:00
parent 06305696da
commit 1c60852520

View file

@ -1,9 +1,13 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h" #include "internal/icmp_send.h"
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
/* Forward declarations */
static int handle_send_error(struct icmp_handle *h);
/* -------------------- */
int int
send_packet(struct icmp_handle *h, const void *buffer, size_t len, send_packet(struct icmp_handle *h, const void *buffer, size_t len,
struct sockaddr_in *dest) struct sockaddr_in *dest)
@ -14,19 +18,7 @@ send_packet(struct icmp_handle *h, const void *buffer, size_t len,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if (bytes_sent < 0) if (bytes_sent < 0)
{ return handle_send_error(h);
int saved_errno = errno;
if (EAGAIN == saved_errno || EWOULDBLOCK == saved_errno)
{
icmp_set_error_fmt(h, ICMP_ERR_EAGAIN,
"Send would block (buffer full, retry later)");
return -1;
}
icmp_set_error_fmt(h, ICMP_ERR_SEND, "sendto() failed: %s",
strerror(saved_errno));
return -1;
}
if ((size_t)bytes_sent != len) if ((size_t)bytes_sent != len)
{ {
@ -37,3 +29,20 @@ send_packet(struct icmp_handle *h, const void *buffer, size_t len,
return 0; return 0;
} }
static int
handle_send_error(struct icmp_handle *h)
{
int saved_errno;
saved_errno = errno;
if (EAGAIN == saved_errno || EWOULDBLOCK == saved_errno)
{
icmp_set_error_fmt(h, ICMP_ERR_EAGAIN,
"Send would block (buffer full, retry later)");
return -1;
}
icmp_set_error_fmt(h, ICMP_ERR_SEND, "sendto() failed: %s",
strerror(saved_errno));
return -1;
}