From 70988a801f650c45914ee759ed807ad15060dd0c Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Wed, 22 Apr 2026 23:58:41 +0200 Subject: [PATCH] feat: icmp_create_block() --- README.md | 19 +++++++++++-------- includes/icmp.h | 1 + src/handle/create.c | 22 ++++++++++++++++++++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f671a68..3eeeb23 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/includes/icmp.h b/includes/icmp.h index f06f9a6..2a11fe9 100644 --- a/includes/icmp.h +++ b/includes/icmp.h @@ -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); diff --git a/src/handle/create.c b/src/handle/create.c index fb0b4f7..c831647 100644 --- a/src/handle/create.c +++ b/src/handle/create.c @@ -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) {