#ifndef ICMP_H #define ICMP_H #include #include #include #include #include /* 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; 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); 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); /* 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); /* 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