refactor: move variable declarations to function beginnings

This commit is contained in:
lohhiiccc 2026-02-07 20:38:19 +01:00
parent 85543f3d19
commit 865f9d0ccb
9 changed files with 23 additions and 16 deletions

View file

@ -16,16 +16,19 @@ icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type, uint8_t code,
size_t payload_len)
{
const size_t required_len = sizeof(struct icmp_header) + payload_len;
struct icmp_header *h;
if (buffer_len < required_len)
return -1;
struct icmp_header *h = (struct icmp_header *)buffer;
h = (struct icmp_header *)buffer;
write_icmp_header(h, type, code, header_rest);
if (payload_len > 0)
{
memcpy((uint8_t *)buffer + sizeof(struct icmp_header), payload,
payload_len);
}
h->checksum = htons(icmp_checksum(h, required_len));

View file

@ -18,12 +18,15 @@ icmp_parse_icmp_payload(const void *buffer, size_t buffer_len,
size_t ip_hdr_len, uint8_t *type, uint8_t *code,
const void **payload, size_t *payload_len)
{
size_t payload_offset;
const struct icmp_header *hdr;
if (validate_icmp_size(buffer_len, ip_hdr_len) != 0)
return -1;
const struct icmp_header *hdr = get_icmp_header(buffer, ip_hdr_len);
hdr = get_icmp_header(buffer, ip_hdr_len);
extract_icmp_fields(hdr, type, code);
size_t payload_offset = ip_hdr_len + ICMP_HEADER_SIZE;
payload_offset = ip_hdr_len + ICMP_HEADER_SIZE;
*payload_len = buffer_len - payload_offset;
*payload = (const uint8_t *)buffer + payload_offset;

View file

@ -18,11 +18,14 @@ 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 *dst_addr, uint8_t *protocol)
{
const struct ip_header *h;
size_t ihl_bytes;
if (buffer_len < MIN_IP_HEADER_SIZE)
return -1;
const struct ip_header *h = (const struct ip_header *)buffer;
size_t ihl_bytes = extract_ip_header_length(h->version_ihl);
h = (const struct ip_header *)buffer;
ihl_bytes = extract_ip_header_length(h->version_ihl);
if (0 != validate_ip_header(h, buffer_len))
return -1;

View file

@ -19,15 +19,17 @@ icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
const void *payload, size_t len, struct in_addr dest,
uint8_t ttl)
{
uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE];
int packet_len;
if (0 == send_validate_handle(h))
return -1;
if (0 == validate_payload(h, payload, len))
return -1;
uint8_t buffer[ICMP_HEADER_SIZE + MAX_PAYLOAD_SIZE];
int packet_len = icmp_build_packet(buffer, sizeof(buffer),
packet_len = icmp_build_packet(buffer, sizeof(buffer),
type, code, header_rest, payload, len);
if (-1 == packet_len)
{

View file

@ -6,10 +6,11 @@ int
send_to_destination(struct icmp_handle *h, const void *packet, size_t len,
struct in_addr dest, uint8_t ttl)
{
struct sockaddr_in addr;
if (send_set_socket_ttl(h, ttl) < 0)
return -1;
struct sockaddr_in addr;
send_prepare_destination(&addr, dest);
if (send_packet(h, packet, len, &addr) < 0)

View file

@ -8,10 +8,11 @@ int
send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl)
{
int ttl_val = ttl;
int saved_errno;
if (setsockopt(h->fd, IPPROTO_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) < 0)
{
int saved_errno = errno;
saved_errno = errno;
icmp_set_error_fmt(h, ICMP_ERR_SOCKET, "Failed to set TTL: %s",
strerror(saved_errno));
return -1;

View file

@ -13,7 +13,6 @@ socket_configure(struct icmp_handle *h)
{
int flags;
/* Get current file status flags */
flags = fcntl(h->fd, F_GETFL, 0);
if (-1 == flags)
{
@ -21,14 +20,12 @@ socket_configure(struct icmp_handle *h)
return -1;
}
/* Set non-blocking flag */
if (-1 == fcntl(h->fd, F_SETFL, flags | O_NONBLOCK))
{
handle_configure_error(h, "fcntl(F_SETFL)");
return -1;
}
/* Clear error state on success */
icmp_clear_error(h);
return 0;

View file

@ -14,7 +14,6 @@ socket_create(struct icmp_handle *h)
{
int fd;
/* Create raw ICMP socket */
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd < 0)
@ -23,10 +22,8 @@ socket_create(struct icmp_handle *h)
return -1;
}
/* Store file descriptor in handle */
h->fd = fd;
/* Clear error state on success */
icmp_clear_error(h);
return 0;

View file

@ -24,8 +24,8 @@ static uint32_t
sum_words(const uint8_t *data, size_t len)
{
const uint8_t *ptr = data;
const size_t words = len >> 1;
uint32_t sum = 0;
size_t words = len >> 1;
/* Duff's device: unroll loop by 4 */
if (words > 0)