feat: icmp_create_block()

This commit is contained in:
lohhiiccc 2026-04-22 23:58:41 +02:00 committed by lohhiiccc
parent 23c5256c76
commit 70988a801f
3 changed files with 32 additions and 10 deletions

View file

@ -1,7 +1,7 @@
# libicmp
Non-blocking ICMPv4 library for building ping, traceroute, and network
diagnostic tools.
ICMPv4 library for building ping, traceroute, and network diagnostic tools.
Supports both non-blocking (default) and blocking socket modes.
## Dependencies
@ -28,12 +28,14 @@ make test
```c
icmp_handle_t *icmp_create(void);
icmp_handle_t *icmp_create_block(void);
void icmp_destroy(icmp_handle_t *h);
int icmp_get_fd(const icmp_handle_t *h);
```
`icmp_create()` opens a raw ICMP socket and returns an opaque handle.
`icmp_get_fd()` exposes the underlying file descriptor for use with
`icmp_create()` opens a raw ICMP socket in non-blocking mode and returns an
opaque handle. `icmp_create_block()` does the same but leaves the socket in
blocking mode`icmp_get_fd()` exposes the underlying file descriptor for use with
`select()` or `poll()`. Errors are retrieved via `icmp_strerror()`.
### Send
@ -71,12 +73,13 @@ on error.
## Modules
### handle/
Handle lifecycle: `icmp_create()`, `icmp_destroy()`, `icmp_get_fd()`.
Owns the opaque `icmp_handle_t` struct and the error state attached to it.
Handle lifecycle: `icmp_create()`, `icmp_create_block()`, `icmp_destroy()`,
`icmp_get_fd()`. Owns the opaque `icmp_handle_t` struct and the error state
attached to it.
### socket/
Raw socket creation, non-blocking configuration, and the DF (Don't
Fragment) flag via `IP_MTU_DISCOVER`.
Raw socket creation, optional non-blocking configuration (`O_NONBLOCK`), and
the DF (Don't Fragment) flag via `IP_MTU_DISCOVER`.
### send/
Packet building and transmission. Assembles the ICMP header and payload,

View file

@ -50,6 +50,7 @@ typedef void (*icmp_callback_t)(const icmp_reply_t *reply, void *userdata);
/* Handle lifecycle */
icmp_handle_t *icmp_create(void);
icmp_handle_t *icmp_create_block(void);
void icmp_destroy(icmp_handle_t *h);
int icmp_get_fd(const icmp_handle_t *h);

View file

@ -8,10 +8,11 @@
/* Forward declarations */
static int alloc_icmp_handle(struct icmp_handle **h);
static void init_icmp_handle(struct icmp_handle *h);
static struct icmp_handle *create_handle(void);
/* -------------------- */
icmp_handle_t *
icmp_create(void)
static struct icmp_handle *
create_handle(void)
{
struct icmp_handle *h;
@ -26,6 +27,17 @@ icmp_create(void)
return NULL;
}
return h;
}
icmp_handle_t *
icmp_create(void)
{
struct icmp_handle *h = create_handle();
if (NULL == h)
return NULL;
if (-1 == socket_configure(h))
{
close(h->fd);
@ -36,6 +48,12 @@ icmp_create(void)
return (icmp_handle_t *)h;
}
icmp_handle_t *
icmp_create_block(void)
{
return (icmp_handle_t *)create_handle();
}
static int
alloc_icmp_handle(struct icmp_handle **h)
{