90 lines
2.2 KiB
C
90 lines
2.2 KiB
C
#ifndef ICMP_H
|
|
#define ICMP_H
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
|
|
/* Opaque handle type */
|
|
typedef struct icmp_handle icmp_handle_t;
|
|
|
|
/* Reply structure passed to callback */
|
|
typedef struct icmp_reply {
|
|
uint8_t type;
|
|
uint8_t code;
|
|
struct in_addr from;
|
|
uint8_t ttl;
|
|
struct timespec timestamp;
|
|
void *payload;
|
|
size_t payload_len;
|
|
void *ip_payload;
|
|
size_t ip_payload_len;
|
|
} icmp_reply_t;
|
|
|
|
/* Offending packet structure for error messages */
|
|
typedef struct icmp_offending_packet {
|
|
struct in_addr src;
|
|
struct in_addr dst;
|
|
uint8_t protocol;
|
|
uint8_t icmp_type;
|
|
uint8_t icmp_code;
|
|
uint16_t next_mtu; /* MTU from outer ICMP (type=3, code=4) */
|
|
union {
|
|
struct {
|
|
uint16_t id;
|
|
uint16_t seq;
|
|
} echo;
|
|
uint32_t gateway;
|
|
struct {
|
|
uint16_t unused;
|
|
uint16_t mtu;
|
|
} frag;
|
|
uint8_t raw[4];
|
|
} rest;
|
|
} icmp_offending_packet_t;
|
|
|
|
/* Callback type for received packets */
|
|
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);
|
|
|
|
/* Send functions */
|
|
int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
|
|
uint32_t header_rest, const void *payload, size_t len,
|
|
struct in_addr dest, uint8_t ttl);
|
|
|
|
int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
|
uint16_t seq, uint8_t ttl, const void *payload, size_t payload_len);
|
|
|
|
/* Receive function */
|
|
int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
|
size_t max_packets);
|
|
|
|
/* ID/Sequence helpers */
|
|
int icmp_reply_id_seq(const icmp_reply_t *reply, uint16_t *id,
|
|
uint16_t *seq);
|
|
|
|
/* Error message helpers */
|
|
int icmp_error_extract_offending(const icmp_reply_t *reply,
|
|
icmp_offending_packet_t *offending);
|
|
|
|
/* Socket options */
|
|
int icmp_set_dont_fragment(icmp_handle_t *h);
|
|
int icmp_set_tos(icmp_handle_t *h, uint8_t tos);
|
|
|
|
/* Error handling */
|
|
const char *icmp_strerror(const icmp_handle_t *h);
|
|
int icmp_should_retry(const icmp_handle_t *h);
|
|
|
|
/* Time helpers */
|
|
int icmp_get_time(struct timespec *ts);
|
|
int64_t icmp_time_diff_ns(const struct timespec *start,
|
|
const struct timespec *end);
|
|
|
|
#endif
|