refactor: standardize code style and improve clarity

This commit is contained in:
lohhiiccc 2026-02-04 23:09:36 +01:00
parent ace00ea0f2
commit 6cda2e8db5
6 changed files with 14 additions and 10 deletions

View file

@ -8,7 +8,7 @@
/* Checksum calculation (RFC 1071) */
uint16_t icmp_checksum(const void *data, size_t len);
/* Time helpers - return 0 on success, -1 on error */
/* Time helpers */
int icmp_get_time(struct timespec *ts);
int64_t icmp_time_diff_ns(const struct timespec *start,
const struct timespec *end);

View file

@ -24,7 +24,7 @@ icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type, uint8_t code,
write_icmp_header(h, type, code, header_rest);
if (payload_len > 0)
memcpy((unsigned char*)buffer + sizeof(struct icmp_header), payload,
memcpy((uint8_t *)buffer + sizeof(struct icmp_header), payload,
payload_len);
h->checksum = htons(icmp_checksum(h, required_len));

View file

@ -3,13 +3,14 @@
#include "internal/icmp_recv.h"
#include <stdint.h>
#define MAX_PACKET_SIZE 1500
/* Buffer for incoming packets: IP header + ICMP */
#define RECV_BUFFER_SIZE 1500
int
icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
size_t max_packets)
{
uint8_t buffer[MAX_PACKET_SIZE];
uint8_t buffer[RECV_BUFFER_SIZE];
struct sockaddr_in from;
ssize_t recv_len;
int processed;

View file

@ -15,6 +15,9 @@ recv_receive_packet(int fd, void *buffer, size_t buffer_len,
n = recvfrom(fd, buffer, buffer_len, 0, (struct sockaddr *)from,
&from_len);
if (n < 0)
return (EAGAIN == errno || EWOULDBLOCK == errno) ? 0 : -1;
{
int saved_errno = errno;
return (EAGAIN == saved_errno || EWOULDBLOCK == saved_errno) ? 0 : -1;
}
return n;
}

View file

@ -4,9 +4,9 @@
#include "internal/icmp_send.h"
#include <stddef.h>
/* Maximum payload size for simplicity */
#define MAX_PAYLOAD_SIZE 1024
#define MAX_PACKET_SIZE (8 + MAX_PAYLOAD_SIZE)
/* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */
#define MAX_PAYLOAD_SIZE 1472
#define ICMP_HEADER_SIZE 8
/* Forward declarations */
static int validate_payload(struct icmp_handle *h, const void *payload,
@ -25,7 +25,7 @@ icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
if (0 == validate_payload(h, payload, len))
return -1;
uint8_t buffer[MAX_PACKET_SIZE];
uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE];
int packet_len = icmp_build_packet(buffer, sizeof(buffer),
type, code, header_rest, payload, len);