diff --git a/src/packet/build.c b/src/packet/build.c index 773ded8..1b4b743 100644 --- a/src/packet/build.c +++ b/src/packet/build.c @@ -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)); diff --git a/src/packet/parse_icmp.c b/src/packet/parse_icmp.c index f92a5c5..ebba51c 100644 --- a/src/packet/parse_icmp.c +++ b/src/packet/parse_icmp.c @@ -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; diff --git a/src/packet/parse_ip.c b/src/packet/parse_ip.c index c301c08..a09c98f 100644 --- a/src/packet/parse_ip.c +++ b/src/packet/parse_ip.c @@ -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; diff --git a/src/send/api/raw.c b/src/send/api/raw.c index 2c8e973..e46fa87 100644 --- a/src/send/api/raw.c +++ b/src/send/api/raw.c @@ -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) { diff --git a/src/send/core/send_to_destination.c b/src/send/core/send_to_destination.c index 3f6ee95..bb1b288 100644 --- a/src/send/core/send_to_destination.c +++ b/src/send/core/send_to_destination.c @@ -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) diff --git a/src/send/helpers/set_socket_ttl.c b/src/send/helpers/set_socket_ttl.c index dada3d4..427cd00 100644 --- a/src/send/helpers/set_socket_ttl.c +++ b/src/send/helpers/set_socket_ttl.c @@ -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; diff --git a/src/socket/configure.c b/src/socket/configure.c index d534a65..5a5b1fa 100644 --- a/src/socket/configure.c +++ b/src/socket/configure.c @@ -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; diff --git a/src/socket/create.c b/src/socket/create.c index 12a9f5f..8d8a415 100644 --- a/src/socket/create.c +++ b/src/socket/create.c @@ -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; diff --git a/src/utils/checksum.c b/src/utils/checksum.c index d124c59..da5c8df 100644 --- a/src/utils/checksum.c +++ b/src/utils/checksum.c @@ -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)