feat: icmp_create_block()
This commit is contained in:
parent
a9bdaf33f2
commit
5713e343aa
3 changed files with 32 additions and 10 deletions
19
README.md
19
README.md
|
|
@ -1,7 +1,7 @@
|
||||||
# libicmp
|
# libicmp
|
||||||
|
|
||||||
Non-blocking ICMPv4 library for building ping, traceroute, and network
|
ICMPv4 library for building ping, traceroute, and network diagnostic tools.
|
||||||
diagnostic tools.
|
Supports both non-blocking (default) and blocking socket modes.
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
|
|
@ -28,12 +28,14 @@ make test
|
||||||
|
|
||||||
```c
|
```c
|
||||||
icmp_handle_t *icmp_create(void);
|
icmp_handle_t *icmp_create(void);
|
||||||
|
icmp_handle_t *icmp_create_block(void);
|
||||||
void icmp_destroy(icmp_handle_t *h);
|
void icmp_destroy(icmp_handle_t *h);
|
||||||
int icmp_get_fd(const 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_create()` opens a raw ICMP socket in non-blocking mode and returns an
|
||||||
`icmp_get_fd()` exposes the underlying file descriptor for use with
|
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()`.
|
`select()` or `poll()`. Errors are retrieved via `icmp_strerror()`.
|
||||||
|
|
||||||
### Send
|
### Send
|
||||||
|
|
@ -71,12 +73,13 @@ on error.
|
||||||
## Modules
|
## Modules
|
||||||
|
|
||||||
### handle/
|
### handle/
|
||||||
Handle lifecycle: `icmp_create()`, `icmp_destroy()`, `icmp_get_fd()`.
|
Handle lifecycle: `icmp_create()`, `icmp_create_block()`, `icmp_destroy()`,
|
||||||
Owns the opaque `icmp_handle_t` struct and the error state attached to it.
|
`icmp_get_fd()`. Owns the opaque `icmp_handle_t` struct and the error state
|
||||||
|
attached to it.
|
||||||
|
|
||||||
### socket/
|
### socket/
|
||||||
Raw socket creation, non-blocking configuration, and the DF (Don't
|
Raw socket creation, optional non-blocking configuration (`O_NONBLOCK`), and
|
||||||
Fragment) flag via `IP_MTU_DISCOVER`.
|
the DF (Don't Fragment) flag via `IP_MTU_DISCOVER`.
|
||||||
|
|
||||||
### send/
|
### send/
|
||||||
Packet building and transmission. Assembles the ICMP header and payload,
|
Packet building and transmission. Assembles the ICMP header and payload,
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ typedef void (*icmp_callback_t)(const icmp_reply_t *reply, void *userdata);
|
||||||
|
|
||||||
/* Handle lifecycle */
|
/* Handle lifecycle */
|
||||||
icmp_handle_t *icmp_create(void);
|
icmp_handle_t *icmp_create(void);
|
||||||
|
icmp_handle_t *icmp_create_block(void);
|
||||||
void icmp_destroy(icmp_handle_t *h);
|
void icmp_destroy(icmp_handle_t *h);
|
||||||
int icmp_get_fd(const icmp_handle_t *h);
|
int icmp_get_fd(const icmp_handle_t *h);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,11 @@
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static int alloc_icmp_handle(struct icmp_handle **h);
|
static int alloc_icmp_handle(struct icmp_handle **h);
|
||||||
static void init_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 *
|
static struct icmp_handle *
|
||||||
icmp_create(void)
|
create_handle(void)
|
||||||
{
|
{
|
||||||
struct icmp_handle *h;
|
struct icmp_handle *h;
|
||||||
|
|
||||||
|
|
@ -26,6 +27,17 @@ icmp_create(void)
|
||||||
return NULL;
|
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))
|
if (-1 == socket_configure(h))
|
||||||
{
|
{
|
||||||
close(h->fd);
|
close(h->fd);
|
||||||
|
|
@ -36,6 +48,12 @@ icmp_create(void)
|
||||||
return (icmp_handle_t *)h;
|
return (icmp_handle_t *)h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
icmp_handle_t *
|
||||||
|
icmp_create_block(void)
|
||||||
|
{
|
||||||
|
return (icmp_handle_t *)create_handle();
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
alloc_icmp_handle(struct icmp_handle **h)
|
alloc_icmp_handle(struct icmp_handle **h)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue