fix: correct checksum byte order and use monotonic clock

This commit is contained in:
lohhiiccc 2026-01-26 20:11:15 +01:00
parent 8fb109e2a0
commit 10a10f6e0a
3 changed files with 10 additions and 3 deletions

View file

@ -4,6 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/types.h>
#include <time.h> #include <time.h>
/* Opaque handle type */ /* Opaque handle type */
@ -45,4 +46,9 @@ int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata);
const char *icmp_strerror(const icmp_handle_t *h); const char *icmp_strerror(const icmp_handle_t *h);
int icmp_should_retry(const icmp_handle_t *h); int icmp_should_retry(const icmp_handle_t *h);
#endif /* ICMP_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

View file

@ -3,6 +3,7 @@
#include "internal/icmp_utils.h" #include "internal/icmp_utils.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <arpa/inet.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,
@ -57,5 +58,5 @@ copy_payload(void *dest, const void *src, size_t len)
static void static void
compute_and_set_checksum(struct icmp_header *hdr, size_t total_len) compute_and_set_checksum(struct icmp_header *hdr, size_t total_len)
{ {
hdr->checksum = icmp_checksum(hdr, total_len); hdr->checksum = htons(icmp_checksum(hdr, total_len));
} }

View file

@ -4,7 +4,7 @@
int int
icmp_get_time(struct timespec *ts) icmp_get_time(struct timespec *ts)
{ {
if (0 != clock_gettime(CLOCK_REALTIME, ts)) if (0 != clock_gettime(CLOCK_MONOTONIC, ts))
return -1; return -1;
return 0; return 0;
} }