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,13 +55,11 @@ int icmp_get_fd(const icmp_handle_t *h);
/* Send functions */ /* Send functions */
int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code, int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
uint32_t header_rest, uint32_t header_rest, const void *payload, size_t len,
const void *payload, size_t len, struct in_addr dest, struct in_addr dest, uint8_t ttl);
uint8_t ttl);
int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id, int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
uint16_t seq, uint8_t ttl, uint16_t seq, uint8_t ttl, const void *payload, size_t payload_len);
const void *payload, size_t payload_len);
/* Receive function */ /* Receive function */
int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata, int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,

View file

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

View file

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

View file

@ -1,9 +1,10 @@
#ifndef ICMP_SEND_H #ifndef ICMP_SEND_H
#define ICMP_SEND_H #define ICMP_SEND_H
#include "internal/icmp_internal.h"
#include <netinet/in.h> #include <netinet/in.h>
#include "internal/icmp_internal.h"
void send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest); 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_set_socket_ttl(struct icmp_handle *h, uint8_t ttl);
int send_packet(struct icmp_handle *h, const void *buffer, size_t len, int send_packet(struct icmp_handle *h, const void *buffer, size_t len,

View file

@ -1,6 +1,7 @@
#include "internal/icmp_internal.h"
#include <stdio.h> #include <stdio.h>
#include "internal/icmp_internal.h"
void void
icmp_set_error(struct icmp_handle *h, int code, const char *msg) 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 <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "internal/icmp_internal.h"
void void
icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...) 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 "icmp.h"
#include "internal/icmp_internal.h" #include "internal/icmp_internal.h"
#include "internal/icmp_socket.h" #include "internal/icmp_socket.h"
#include <stdlib.h>
#include <unistd.h>
/* Forward declarations */ /* Forward declarations */
static int alloc_icmp_handle(struct icmp_handle **h); 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 <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
void void
icmp_destroy(icmp_handle_t *h) icmp_destroy(icmp_handle_t *h)
{ {

View file

@ -1,10 +1,11 @@
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include "internal/icmp_utils.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "internal/icmp_packet_internal.h"
#include "internal/icmp_packet.h"
#include "internal/icmp_utils.h"
/* Forward declarations */ /* Forward declarations */
static void write_icmp_header(struct icmp_header *hdr, uint8_t type, 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);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,6 +1,5 @@
#include "internal/icmp_recv.h" #include "internal/icmp_recv.h"
// TODO: check ssize_t -> size_t cast
void void
recv_process_single_packet(uint8_t *buffer, ssize_t len, icmp_callback_t cb, recv_process_single_packet(uint8_t *buffer, ssize_t len, icmp_callback_t cb,
void *userdata) void *userdata)

View file

@ -1,8 +1,9 @@
#include "internal/icmp_recv.h"
#include <sys/socket.h> #include <sys/socket.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "internal/icmp_recv.h"
ssize_t ssize_t
recv_receive_packet(int fd, void *buffer, size_t buffer_len, recv_receive_packet(int fd, void *buffer, size_t buffer_len,
struct sockaddr_in *from) struct sockaddr_in *from)

View file

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

View file

@ -1,8 +1,9 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_recv.h"
int int
recv_handle_receive_error(struct icmp_handle *h) recv_handle_receive_error(struct icmp_handle *h)
{ {

View file

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

View file

@ -1,8 +1,9 @@
#include <stddef.h>
#include "icmp.h" #include "icmp.h"
#include "internal/icmp_internal.h" #include "internal/icmp_internal.h"
#include "internal/icmp_packet_internal.h" #include "internal/icmp_packet_internal.h"
#include "internal/icmp_send.h" #include "internal/icmp_send.h"
#include <stddef.h>
/* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */ /* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */
#define MAX_PAYLOAD_SIZE 1472 #define MAX_PAYLOAD_SIZE 1472
@ -15,9 +16,8 @@ static int validate_payload(struct icmp_handle *h, const void *payload,
int int
icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code, icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
uint32_t header_rest, uint32_t header_rest, const void *payload, size_t len,
const void *payload, size_t len, struct in_addr dest, struct in_addr dest, uint8_t ttl)
uint8_t ttl)
{ {
uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE]; uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE];
int packet_len; int packet_len;

View file

@ -1,10 +1,11 @@
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_send.h"
/* Forward declarations */ /* Forward declarations */
static int handle_send_error(struct icmp_handle *h); static int handle_send_error(struct icmp_handle *h);
/* -------------------- */ /* -------------------- */

View file

@ -1,6 +1,7 @@
#include <netinet/in.h>
#include "internal/icmp_internal.h" #include "internal/icmp_internal.h"
#include "internal/icmp_send.h" #include "internal/icmp_send.h"
#include <netinet/in.h>
int int
send_to_destination(struct icmp_handle *h, const void *packet, size_t len, send_to_destination(struct icmp_handle *h, const void *packet, size_t len,

View file

@ -1,6 +1,7 @@
#include "internal/icmp_send.h"
#include <string.h> #include <string.h>
#include "internal/icmp_send.h"
void void
send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest) 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 <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "internal/icmp_send.h"
int int
send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl) send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl)
{ {

View file

@ -1,6 +1,7 @@
#include <stddef.h>
#include "internal/icmp_internal.h" #include "internal/icmp_internal.h"
#include "internal/icmp_send.h" #include "internal/icmp_send.h"
#include <stddef.h>
int int
send_validate_handle(struct icmp_handle *h) 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 <fcntl.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
/* Forward declarations */ /* Forward declarations */
static void handle_configure_error(struct icmp_handle *h, const char *func); 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 <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
/* Forward declarations */ /* Forward declarations */
static void handle_socket_error(struct icmp_handle *h, int err); 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 <stdint.h>
#include "internal/icmp_utils.h"
int int
icmp_get_time(struct timespec *ts) icmp_get_time(struct timespec *ts)
{ {