refactor: extract send error handling to separate function
This commit is contained in:
parent
06305696da
commit
1c60852520
1 changed files with 22 additions and 13 deletions
|
|
@ -1,9 +1,13 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Forward declarations */
|
||||
static int handle_send_error(struct icmp_handle *h);
|
||||
/* -------------------- */
|
||||
int
|
||||
send_packet(struct icmp_handle *h, const void *buffer, size_t len,
|
||||
struct sockaddr_in *dest)
|
||||
|
|
@ -14,19 +18,7 @@ send_packet(struct icmp_handle *h, const void *buffer, size_t len,
|
|||
sizeof(struct sockaddr_in));
|
||||
|
||||
if (bytes_sent < 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
return handle_send_error(h);
|
||||
|
||||
if ((size_t)bytes_sent != len)
|
||||
{
|
||||
|
|
@ -37,3 +29,20 @@ send_packet(struct icmp_handle *h, const void *buffer, size_t len,
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue