style: indent and include order

This commit is contained in:
lohhiiccc 2026-03-19 02:43:46 +01:00
parent 803d8e3eb5
commit 1a4a007dbb
31 changed files with 141 additions and 126 deletions

View file

@ -55,25 +55,23 @@ 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);
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);
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);
size_t max_packets);
/* ID/Sequence helpers */
int icmp_reply_id_seq(const icmp_reply_t *reply, uint16_t *id,
uint16_t *seq);
uint16_t *seq);
/* Error message helpers */
int icmp_error_extract_offending(const icmp_reply_t *reply,
icmp_offending_packet_t *offending);
icmp_offending_packet_t *offending);
/* Socket options */
int icmp_set_dont_fragment(icmp_handle_t *h);
@ -85,6 +83,6 @@ 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);
const struct timespec *end);
#endif

View file

@ -6,16 +6,15 @@
#include <netinet/in.h>
int icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type,
uint8_t code, uint32_t header_rest,
const void *payload, size_t payload_len);
uint8_t code, uint32_t header_rest, const void *payload,
size_t payload_len);
int icmp_parse_ip_header(const void *buffer, size_t buffer_len,
uint8_t *ttl, struct in_addr *src_addr,
size_t *ip_hdr_len, struct in_addr *dst_addr,
uint8_t *protocol);
uint8_t *ttl, struct in_addr *src_addr,
size_t *ip_hdr_len, struct in_addr *dst_addr, uint8_t *protocol);
int icmp_parse_icmp_payload(const void *buffer, size_t buffer_len,
size_t ip_hdr_len, uint8_t *type, uint8_t *code,
const void **payload, size_t *payload_len);
size_t ip_hdr_len, uint8_t *type, uint8_t *code,
const void **payload, size_t *payload_len);
#endif

View file

@ -1,11 +1,12 @@
#ifndef ICMP_RECV_H
#define ICMP_RECV_H
#include "icmp.h"
#include "internal/icmp_internal.h"
#include <netinet/in.h>
#include <sys/types.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
/* Validation helpers */
int recv_validate_params(struct icmp_handle *h, icmp_callback_t cb);
int recv_handle_receive_error(struct icmp_handle *h);
@ -14,20 +15,17 @@ int recv_handle_receive_error(struct icmp_handle *h);
ssize_t recv_receive_packet(int fd, void *buffer, size_t buffer_len,
struct sockaddr_in *from);
int recv_parse_packet(const void *buffer, size_t buffer_len,
uint8_t *type, uint8_t *code,
uint8_t *ttl, struct in_addr *src_addr,
const void **payload, size_t *payload_len,
size_t *ip_hdr_len);
int recv_parse_packet(const void *buffer, size_t buffer_len, uint8_t *type,
uint8_t *code, uint8_t *ttl, struct in_addr *src_addr,
const void **payload, size_t *payload_len, size_t *ip_hdr_len);
/* Reply building */
void recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code,
uint8_t ttl, struct in_addr src_addr,
const void *payload, size_t payload_len,
const void *buffer, size_t ip_hdr_len);
uint8_t ttl, struct in_addr src_addr, const void *payload,
size_t payload_len, const void *buffer, size_t ip_hdr_len);
/* Process single packet */
void recv_process_single_packet(uint8_t *buffer, ssize_t len,
icmp_callback_t cb, void *userdata);
icmp_callback_t cb, void *userdata);
#endif

View file

@ -1,16 +1,17 @@
#ifndef ICMP_SEND_H
#define ICMP_SEND_H
#include "internal/icmp_internal.h"
#include <netinet/in.h>
#include "internal/icmp_internal.h"
void send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest);
int send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl);
int send_packet(struct icmp_handle *h, const void *buffer, size_t len,
struct sockaddr_in *dest);
struct sockaddr_in *dest);
int send_validate_handle(struct icmp_handle *h);
int send_to_destination(struct icmp_handle *h, const void *packet, size_t len,
struct in_addr dest, uint8_t ttl);
struct in_addr dest, uint8_t ttl);
#endif

View file

@ -11,6 +11,6 @@ uint16_t icmp_checksum(const void *data, size_t len);
/* Time helpers */
int icmp_get_time(struct timespec *ts);
int64_t icmp_time_diff_ns(const struct timespec *start,
const struct timespec *end);
const struct timespec *end);
#endif

View file

@ -1,6 +1,7 @@
#include "internal/icmp_internal.h"
#include <stdio.h>
#include "internal/icmp_internal.h"
void
icmp_set_error(struct icmp_handle *h, int code, const char *msg)
{

View file

@ -1,7 +1,8 @@
#include "internal/icmp_internal.h"
#include <stdarg.h>
#include <stdio.h>
#include "internal/icmp_internal.h"
void
icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...)
{

View file

@ -1,8 +1,9 @@
#include <stdlib.h>
#include <unistd.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
#include <stdlib.h>
#include <unistd.h>
/* Forward declarations */
static int alloc_icmp_handle(struct icmp_handle **h);

View file

@ -1,8 +1,9 @@
#include "icmp.h"
#include "internal/icmp_internal.h"
#include <unistd.h>
#include <stdlib.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
void
icmp_destroy(icmp_handle_t *h)
{

View file

@ -1,19 +1,20 @@
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include "internal/icmp_utils.h"
#include <stddef.h>
#include <string.h>
#include <arpa/inet.h>
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include "internal/icmp_utils.h"
/* Forward declarations */
static void write_icmp_header(struct icmp_header *hdr, uint8_t type,
uint8_t code, uint32_t header_rest);
uint8_t code, uint32_t header_rest);
/* -------------------- */
int
icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type, uint8_t code,
uint32_t header_rest, const void *payload,
size_t payload_len)
uint32_t header_rest, const void *payload,
size_t payload_len)
{
const size_t required_len = sizeof(struct icmp_header) + payload_len;
struct icmp_header *h;
@ -37,7 +38,7 @@ icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type, uint8_t code,
static void
write_icmp_header(struct icmp_header *hdr, uint8_t type, uint8_t code,
uint32_t header_rest)
uint32_t header_rest)
{
hdr->type = type;
hdr->code = code;

View file

@ -1,6 +1,7 @@
#include <stdint.h>
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include <stdint.h>
/* ICMP header size */
#define ICMP_HEADER_SIZE 8
@ -8,15 +9,15 @@
/* Forward declarations */
static int validate_icmp_size(size_t buffer_len, size_t ip_hdr_len);
static const struct icmp_header *get_icmp_header(const void *buffer,
size_t ip_hdr_len);
size_t ip_hdr_len);
static void extract_icmp_fields(const struct icmp_header *hdr, uint8_t *type,
uint8_t *code);
uint8_t *code);
/* -------------------- */
int
icmp_parse_icmp_payload(const void *buffer, size_t buffer_len,
size_t ip_hdr_len, uint8_t *type, uint8_t *code,
const void **payload, size_t *payload_len)
size_t ip_hdr_len, uint8_t *type, uint8_t *code, const void **payload,
size_t *payload_len)
{
size_t payload_offset;
const struct icmp_header *hdr;
@ -47,7 +48,7 @@ get_icmp_header(const void *buffer, size_t ip_hdr_len)
static void
extract_icmp_fields(const struct icmp_header *hdr, uint8_t *type,
uint8_t *code)
uint8_t *code)
{
*type = hdr->type;
*code = hdr->code;

View file

@ -1,10 +1,11 @@
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include <netinet/in.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
/* Minimum IP header size */
#define MIN_IP_HEADER_SIZE 20
@ -15,8 +16,8 @@ static size_t extract_ip_header_length(uint8_t version_ihl);
int
icmp_parse_ip_header(const void *buffer, size_t buffer_len, uint8_t *ttl,
struct in_addr *src_addr, size_t *ip_hdr_len,
struct in_addr *dst_addr, uint8_t *protocol)
struct in_addr *src_addr, size_t *ip_hdr_len, struct in_addr *dst_addr,
uint8_t *protocol)
{
const struct ip_header *h;
size_t ihl_bytes;

View file

@ -1,4 +1,5 @@
#include <netinet/in.h>
#include "icmp.h"
#include "icmp_types.h"
#include "internal/icmp_packet.h"

View file

@ -1,9 +1,10 @@
#include <netinet/in.h>
#include <stdint.h>
#include "icmp.h"
#include "icmp_types.h"
#include "internal/icmp_packet.h"
#include "internal/icmp_packet_internal.h"
#include <netinet/in.h>
#include <stdint.h>
#define MIN_ERROR_PAYLOAD_LEN 28
#define MIN_ICMP_HEADER_LEN 8
@ -13,17 +14,16 @@ static int is_error_type(uint8_t type);
static int is_echo_type(uint8_t type);
static int is_frag_needed(uint8_t type, uint8_t code);
static int parse_embedded_ip(const icmp_reply_t *reply,
icmp_offending_packet_t *offending,
size_t *ip_hdr_len);
icmp_offending_packet_t *offending, size_t *ip_hdr_len);
static const struct icmp_header *get_embedded_icmp(const icmp_reply_t *reply,
size_t ip_hdr_len);
size_t ip_hdr_len);
static void extract_icmp_fields(const struct icmp_header *hdr,
icmp_offending_packet_t *offending);
icmp_offending_packet_t *offending);
/* -------------------- */
int
icmp_error_extract_offending(const icmp_reply_t *reply,
icmp_offending_packet_t *offending)
icmp_error_extract_offending(const icmp_reply_t *reply, icmp_offending_packet_t
*offending)
{
size_t ip_hdr_len;
const struct icmp_header *hdr;
@ -72,22 +72,24 @@ is_frag_needed(uint8_t type, uint8_t code)
}
static int
parse_embedded_ip(const icmp_reply_t *reply, icmp_offending_packet_t *offending,
size_t *ip_hdr_len)
parse_embedded_ip(const icmp_reply_t *reply, icmp_offending_packet_t
*offending, size_t *ip_hdr_len)
{
return icmp_parse_ip_header(reply->payload, reply->payload_len,
NULL, &offending->src, ip_hdr_len,
&offending->dst, &offending->protocol);
return icmp_parse_ip_header(reply->payload, reply->payload_len, NULL,
&offending->src, ip_hdr_len, &offending->dst,
&offending->protocol);
}
static const struct icmp_header *
get_embedded_icmp(const icmp_reply_t *reply, size_t ip_hdr_len)
{
return (const struct icmp_header *)((const uint8_t *)reply->payload + ip_hdr_len);
return (const struct icmp_header *)((const uint8_t *)reply->payload +
ip_hdr_len);
}
static void
extract_icmp_fields(const struct icmp_header *hdr, icmp_offending_packet_t *offending)
extract_icmp_fields(const struct icmp_header *hdr,
icmp_offending_packet_t *offending)
{
offending->icmp_type = hdr->type;
offending->icmp_code = hdr->code;

View file

@ -1,7 +1,8 @@
#include <stdint.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
#include <stdint.h>
/* Buffer for incoming packets: IP header + ICMP */
#define RECV_BUFFER_SIZE 1500

View file

@ -2,18 +2,16 @@
#include "internal/icmp_packet_internal.h"
int
recv_parse_packet(const void *buffer, size_t buffer_len,
uint8_t *type, uint8_t *code,
uint8_t *ttl, struct in_addr *src_addr,
const void **payload, size_t *payload_len,
size_t *ip_hdr_len)
recv_parse_packet(const void *buffer, size_t buffer_len, uint8_t *type,
uint8_t *code, uint8_t *ttl, struct in_addr *src_addr,
const void **payload, size_t *payload_len, size_t *ip_hdr_len)
{
if (icmp_parse_ip_header(buffer, buffer_len, ttl, src_addr,
ip_hdr_len, NULL, NULL) < 0)
if (icmp_parse_ip_header(buffer, buffer_len, ttl, src_addr, ip_hdr_len,
NULL, NULL) < 0)
return -1;
if (icmp_parse_icmp_payload(buffer, buffer_len, *ip_hdr_len,
type, code, payload, payload_len) < 0)
if (icmp_parse_icmp_payload(buffer, buffer_len, *ip_hdr_len, type, code,
payload, payload_len) < 0)
return -1;
return 0;

View file

@ -1,9 +1,8 @@
#include "internal/icmp_recv.h"
// TODO: check ssize_t -> size_t cast
void
recv_process_single_packet(uint8_t *buffer, ssize_t len, icmp_callback_t cb,
void *userdata)
void *userdata)
{
icmp_reply_t reply;
uint8_t type, code, ttl;
@ -12,10 +11,10 @@ recv_process_single_packet(uint8_t *buffer, ssize_t len, icmp_callback_t cb,
size_t payload_len, ip_hdr_len;
if (recv_parse_packet(buffer, (size_t)(len), &type, &code, &ttl, &src_addr,
&payload, &payload_len, &ip_hdr_len) < 0)
&payload, &payload_len, &ip_hdr_len) < 0)
return;
recv_build_reply(&reply, type, code, ttl, src_addr, payload, payload_len,
buffer, ip_hdr_len);
buffer, ip_hdr_len);
reply.ip_payload_len = (size_t)(len) - ip_hdr_len;
cb(&reply, userdata);
}

View file

@ -1,11 +1,12 @@
#include "internal/icmp_recv.h"
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include "internal/icmp_recv.h"
ssize_t
recv_receive_packet(int fd, void *buffer, size_t buffer_len,
struct sockaddr_in *from)
struct sockaddr_in *from)
{
socklen_t from_len;
ssize_t n;

View file

@ -1,12 +1,12 @@
#include "internal/icmp_recv.h"
#include "internal/icmp_utils.h"
#include <string.h>
#include "internal/icmp_recv.h"
#include "internal/icmp_utils.h"
void
recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code,
uint8_t ttl, struct in_addr src_addr,
const void *payload, size_t payload_len,
const void *buffer, size_t ip_hdr_len)
recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code, uint8_t ttl,
struct in_addr src_addr, const void *payload, size_t payload_len, const
void *buffer, size_t ip_hdr_len)
{
memset(reply, 0, sizeof(icmp_reply_t));
reply->type = type;

View file

@ -1,8 +1,9 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
#include <errno.h>
#include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
int
recv_handle_receive_error(struct icmp_handle *h)
{
@ -10,6 +11,6 @@ recv_handle_receive_error(struct icmp_handle *h)
saved_errno = errno;
icmp_set_error_fmt(h, ICMP_ERR_RECV, "recvfrom() failed: %s",
strerror(saved_errno));
strerror(saved_errno));
return -1;
}

View file

@ -1,6 +1,7 @@
#include <stddef.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
#include <stddef.h>
int
recv_validate_params(struct icmp_handle *h, icmp_callback_t cb)

View file

@ -1,12 +1,12 @@
#include "icmp.h"
#include "icmp_types.h"
#include <arpa/inet.h>
#include <stdint.h>
#include "icmp.h"
#include "icmp_types.h"
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)
uint16_t seq, uint8_t ttl, const void *payload, size_t payload_len)
{
union {
struct { uint16_t id; uint16_t seq; } echo;
@ -15,6 +15,6 @@ icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
hdr_rest.echo.id = htons(id);
hdr_rest.echo.seq = htons(seq);
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw,
payload, payload_len, dest, ttl);
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw, payload,
payload_len, dest, ttl);
}

View file

@ -1,8 +1,9 @@
#include <stddef.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_send.h"
#include <stddef.h>
/* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */
#define MAX_PAYLOAD_SIZE 1472
@ -10,14 +11,13 @@
/* Forward declarations */
static int validate_payload(struct icmp_handle *h, const void *payload,
size_t len);
size_t len);
/* -------------------- */
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)
uint32_t header_rest, const void *payload, size_t len,
struct in_addr dest, uint8_t ttl)
{
uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE];
int packet_len;
@ -53,7 +53,7 @@ validate_payload(struct icmp_handle *h, const void *payload, size_t len)
if (len > MAX_PAYLOAD_SIZE)
{
icmp_set_error_fmt(h, ICMP_ERR_INVALID,
"Payload too large (max %d bytes)", MAX_PAYLOAD_SIZE);
"Payload too large (max %d bytes)", MAX_PAYLOAD_SIZE);
return 0;
}
return 1;

View file

@ -1,21 +1,22 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
/* Forward declarations */
static int handle_send_error(struct icmp_handle *h);
/* -------------------- */
int
send_packet(struct icmp_handle *h, const void *buffer, size_t len,
struct sockaddr_in *dest)
struct sockaddr_in *dest)
{
ssize_t bytes_sent;
bytes_sent = sendto(h->fd, buffer, len, 0, (struct sockaddr *)dest,
sizeof(struct sockaddr_in));
sizeof(struct sockaddr_in));
if (bytes_sent < 0)
return handle_send_error(h);
@ -23,7 +24,7 @@ send_packet(struct icmp_handle *h, const void *buffer, size_t len,
if ((size_t)bytes_sent != len)
{
icmp_set_error_fmt(h, ICMP_ERR_SEND, "Partial send (%zd/%zu bytes)",
bytes_sent, len);
bytes_sent, len);
return -1;
}
@ -39,10 +40,10 @@ handle_send_error(struct icmp_handle *h)
if (EAGAIN == saved_errno || EWOULDBLOCK == saved_errno)
{
icmp_set_error_fmt(h, ICMP_ERR_EAGAIN,
"Send would block (buffer full, retry later)");
"Send would block (buffer full, retry later)");
return -1;
}
icmp_set_error_fmt(h, ICMP_ERR_SEND, "sendto() failed: %s",
strerror(saved_errno));
strerror(saved_errno));
return -1;
}

View file

@ -1,10 +1,11 @@
#include <netinet/in.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
#include <netinet/in.h>
int
send_to_destination(struct icmp_handle *h, const void *packet, size_t len,
struct in_addr dest, uint8_t ttl)
struct in_addr dest, uint8_t ttl)
{
struct sockaddr_in addr;

View file

@ -1,6 +1,7 @@
#include "internal/icmp_send.h"
#include <string.h>
#include "internal/icmp_send.h"
void
send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest)
{

View file

@ -1,9 +1,10 @@
#include "internal/icmp_send.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include "internal/icmp_send.h"
int
send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl)
{
@ -14,7 +15,7 @@ send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl)
{
saved_errno = errno;
icmp_set_error_fmt(h, ICMP_ERR_SOCKET, "Failed to set TTL: %s",
strerror(saved_errno));
strerror(saved_errno));
return -1;
}

View file

@ -1,6 +1,7 @@
#include <stddef.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
#include <stddef.h>
int
send_validate_handle(struct icmp_handle *h)

View file

@ -1,9 +1,10 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
/* Forward declarations */
static void handle_configure_error(struct icmp_handle *h, const char *func);
/* -------------------- */

View file

@ -1,10 +1,11 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
/* Forward declarations */
static void handle_socket_error(struct icmp_handle *h, int err);
/* -------------------- */

View file

@ -1,6 +1,7 @@
#include "internal/icmp_utils.h"
#include <stdint.h>
#include "internal/icmp_utils.h"
int
icmp_get_time(struct timespec *ts)
{
@ -13,5 +14,5 @@ int64_t
icmp_time_diff_ns(const struct timespec *start, const struct timespec *end)
{
return (end->tv_sec - start->tv_sec) * 1000000000LL +
(end->tv_nsec - start->tv_nsec);
(end->tv_nsec - start->tv_nsec);
}