- Added `socket_create`. - Added `socket_configure`. - Added socket error utilities - Added unit tests.
48 lines
881 B
C
48 lines
881 B
C
#include "internal/icmp_internal.h"
|
|
#include "internal/icmp_socket.h"
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
|
|
/* Forward declarations */
|
|
static void handle_socket_error(struct icmp_handle *h, int err);
|
|
/* -------------------- */
|
|
|
|
int
|
|
socket_create(struct icmp_handle *h)
|
|
{
|
|
int fd;
|
|
|
|
/* Create raw ICMP socket */
|
|
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
|
|
|
if (fd < 0)
|
|
{
|
|
handle_socket_error(h, errno);
|
|
return -1;
|
|
}
|
|
|
|
/* Store file descriptor in handle */
|
|
h->fd = fd;
|
|
|
|
/* Clear error state on success */
|
|
socket_clear_error(h);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
handle_socket_error(struct icmp_handle *h, int err)
|
|
{
|
|
if (EPERM == err)
|
|
{
|
|
socket_set_error(h, ICMP_ERR_PERMISSION,
|
|
"Raw socket requires CAP_NET_RAW or root");
|
|
}
|
|
else
|
|
{
|
|
socket_set_error(h, ICMP_ERR_SOCKET,
|
|
"socket() failed: %s", strerror(err));
|
|
}
|
|
}
|