refactor: standardize code style and improve clarity
This commit is contained in:
parent
ace00ea0f2
commit
6cda2e8db5
6 changed files with 14 additions and 10 deletions
|
|
@ -8,7 +8,7 @@
|
||||||
/* Checksum calculation (RFC 1071) */
|
/* Checksum calculation (RFC 1071) */
|
||||||
uint16_t icmp_checksum(const void *data, size_t len);
|
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);
|
int icmp_get_time(struct timespec *ts);
|
||||||
int64_t icmp_time_diff_ns(const struct timespec *start,
|
int64_t icmp_time_diff_ns(const struct timespec *start,
|
||||||
const struct timespec *end);
|
const struct timespec *end);
|
||||||
|
|
|
||||||
|
|
@ -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);
|
write_icmp_header(h, type, code, header_rest);
|
||||||
if (payload_len > 0)
|
if (payload_len > 0)
|
||||||
memcpy((unsigned char*)buffer + sizeof(struct icmp_header), payload,
|
memcpy((uint8_t *)buffer + sizeof(struct icmp_header), payload,
|
||||||
payload_len);
|
payload_len);
|
||||||
|
|
||||||
h->checksum = htons(icmp_checksum(h, required_len));
|
h->checksum = htons(icmp_checksum(h, required_len));
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,14 @@
|
||||||
#include "internal/icmp_recv.h"
|
#include "internal/icmp_recv.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define MAX_PACKET_SIZE 1500
|
/* Buffer for incoming packets: IP header + ICMP */
|
||||||
|
#define RECV_BUFFER_SIZE 1500
|
||||||
|
|
||||||
int
|
int
|
||||||
icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
||||||
size_t max_packets)
|
size_t max_packets)
|
||||||
{
|
{
|
||||||
uint8_t buffer[MAX_PACKET_SIZE];
|
uint8_t buffer[RECV_BUFFER_SIZE];
|
||||||
struct sockaddr_in from;
|
struct sockaddr_in from;
|
||||||
ssize_t recv_len;
|
ssize_t recv_len;
|
||||||
int processed;
|
int processed;
|
||||||
|
|
|
||||||
|
|
@ -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,
|
n = recvfrom(fd, buffer, buffer_len, 0, (struct sockaddr *)from,
|
||||||
&from_len);
|
&from_len);
|
||||||
if (n < 0)
|
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;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@
|
||||||
#include "internal/icmp_send.h"
|
#include "internal/icmp_send.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/* Maximum payload size for simplicity */
|
/* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */
|
||||||
#define MAX_PAYLOAD_SIZE 1024
|
#define MAX_PAYLOAD_SIZE 1472
|
||||||
#define MAX_PACKET_SIZE (8 + MAX_PAYLOAD_SIZE)
|
#define ICMP_HEADER_SIZE 8
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static int validate_payload(struct icmp_handle *h, const void *payload,
|
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))
|
if (0 == validate_payload(h, payload, len))
|
||||||
return -1;
|
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),
|
int packet_len = icmp_build_packet(buffer, sizeof(buffer),
|
||||||
type, code, header_rest, payload, len);
|
type, code, header_rest, payload, len);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue