refactor: use union instead of bit shifting for echo header

This commit is contained in:
lohhiiccc 2026-01-27 21:36:26 +01:00
parent 1c60852520
commit 29904cbec6

View file

@ -1,5 +1,4 @@
#include "icmp.h"
#include "internal/icmp_send.h"
#include "icmp_types.h"
#include <stdint.h>
@ -7,11 +6,13 @@ int
icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
uint16_t seq, uint8_t ttl)
{
if (0 == send_validate_handle(h))
return -1;
union {
struct { uint16_t id; uint16_t seq; } echo;
uint32_t raw;
} hdr_rest;
uint32_t header_rest = (uint32_t)id | ((uint32_t)seq << 16);
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, header_rest,
hdr_rest.echo.id = id;
hdr_rest.echo.seq = seq;
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw,
NULL, 0, dest, ttl);
}