style: indent and include order
This commit is contained in:
parent
803d8e3eb5
commit
1a4a007dbb
31 changed files with 141 additions and 126 deletions
|
|
@ -55,13 +55,11 @@ int icmp_get_fd(const icmp_handle_t *h);
|
|||
|
||||
/* Send functions */
|
||||
int icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
|
||||
uint32_t header_rest,
|
||||
const void *payload, size_t len, struct in_addr dest,
|
||||
uint8_t ttl);
|
||||
uint32_t header_rest, const void *payload, size_t len,
|
||||
struct in_addr dest, uint8_t ttl);
|
||||
|
||||
int icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
||||
uint16_t seq, uint8_t ttl,
|
||||
const void *payload, size_t payload_len);
|
||||
uint16_t seq, uint8_t ttl, const void *payload, size_t payload_len);
|
||||
|
||||
/* Receive function */
|
||||
int icmp_process(icmp_handle_t *h, icmp_callback_t cb, void *userdata,
|
||||
|
|
|
|||
|
|
@ -6,13 +6,12 @@
|
|||
#include <netinet/in.h>
|
||||
|
||||
int icmp_build_packet(void *buffer, size_t buffer_len, uint8_t type,
|
||||
uint8_t code, uint32_t header_rest,
|
||||
const void *payload, size_t payload_len);
|
||||
uint8_t code, uint32_t header_rest, const void *payload,
|
||||
size_t payload_len);
|
||||
|
||||
int 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);
|
||||
size_t *ip_hdr_len, struct in_addr *dst_addr, uint8_t *protocol);
|
||||
|
||||
int icmp_parse_icmp_payload(const void *buffer, size_t buffer_len,
|
||||
size_t ip_hdr_len, uint8_t *type, uint8_t *code,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
#ifndef ICMP_RECV_H
|
||||
#define ICMP_RECV_H
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
|
||||
/* Validation helpers */
|
||||
int recv_validate_params(struct icmp_handle *h, icmp_callback_t cb);
|
||||
int recv_handle_receive_error(struct icmp_handle *h);
|
||||
|
|
@ -14,17 +15,14 @@ int recv_handle_receive_error(struct icmp_handle *h);
|
|||
ssize_t recv_receive_packet(int fd, void *buffer, size_t buffer_len,
|
||||
struct sockaddr_in *from);
|
||||
|
||||
int recv_parse_packet(const void *buffer, size_t buffer_len,
|
||||
uint8_t *type, uint8_t *code,
|
||||
uint8_t *ttl, struct in_addr *src_addr,
|
||||
const void **payload, size_t *payload_len,
|
||||
size_t *ip_hdr_len);
|
||||
int recv_parse_packet(const void *buffer, size_t buffer_len, uint8_t *type,
|
||||
uint8_t *code, uint8_t *ttl, struct in_addr *src_addr,
|
||||
const void **payload, size_t *payload_len, size_t *ip_hdr_len);
|
||||
|
||||
/* Reply building */
|
||||
void recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code,
|
||||
uint8_t ttl, struct in_addr src_addr,
|
||||
const void *payload, size_t payload_len,
|
||||
const void *buffer, size_t ip_hdr_len);
|
||||
uint8_t ttl, struct in_addr src_addr, const void *payload,
|
||||
size_t payload_len, const void *buffer, size_t ip_hdr_len);
|
||||
|
||||
/* Process single packet */
|
||||
void recv_process_single_packet(uint8_t *buffer, ssize_t len,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#ifndef ICMP_SEND_H
|
||||
#define ICMP_SEND_H
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
|
||||
void send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest);
|
||||
int send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl);
|
||||
int send_packet(struct icmp_handle *h, const void *buffer, size_t len,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
|
||||
void
|
||||
icmp_set_error(struct icmp_handle *h, int code, const char *msg)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
|
||||
void
|
||||
icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_socket.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Forward declarations */
|
||||
static int alloc_icmp_handle(struct icmp_handle **h);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
|
||||
void
|
||||
icmp_destroy(icmp_handle_t *h)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
#include "internal/icmp_utils.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
#include "internal/icmp_utils.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static void write_icmp_header(struct icmp_header *hdr, uint8_t type,
|
||||
uint8_t code, uint32_t header_rest);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* ICMP header size */
|
||||
#define ICMP_HEADER_SIZE 8
|
||||
|
|
@ -15,8 +16,8 @@ static void extract_icmp_fields(const struct icmp_header *hdr, uint8_t *type,
|
|||
|
||||
int
|
||||
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 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;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
#include <netinet/in.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
|
||||
/* Minimum IP header size */
|
||||
#define MIN_IP_HEADER_SIZE 20
|
||||
|
||||
|
|
@ -15,8 +16,8 @@ static size_t extract_ip_header_length(uint8_t version_ihl);
|
|||
|
||||
int
|
||||
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)
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include <netinet/in.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "icmp_types.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "icmp_types.h"
|
||||
#include "internal/icmp_packet.h"
|
||||
#include "internal/icmp_packet_internal.h"
|
||||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MIN_ERROR_PAYLOAD_LEN 28
|
||||
#define MIN_ICMP_HEADER_LEN 8
|
||||
|
|
@ -13,8 +14,7 @@ static int is_error_type(uint8_t type);
|
|||
static int is_echo_type(uint8_t type);
|
||||
static int is_frag_needed(uint8_t type, uint8_t code);
|
||||
static int parse_embedded_ip(const icmp_reply_t *reply,
|
||||
icmp_offending_packet_t *offending,
|
||||
size_t *ip_hdr_len);
|
||||
icmp_offending_packet_t *offending, size_t *ip_hdr_len);
|
||||
static const struct icmp_header *get_embedded_icmp(const icmp_reply_t *reply,
|
||||
size_t ip_hdr_len);
|
||||
static void extract_icmp_fields(const struct icmp_header *hdr,
|
||||
|
|
@ -22,8 +22,8 @@ static void extract_icmp_fields(const struct icmp_header *hdr,
|
|||
/* -------------------- */
|
||||
|
||||
int
|
||||
icmp_error_extract_offending(const icmp_reply_t *reply,
|
||||
icmp_offending_packet_t *offending)
|
||||
icmp_error_extract_offending(const icmp_reply_t *reply, icmp_offending_packet_t
|
||||
*offending)
|
||||
{
|
||||
size_t ip_hdr_len;
|
||||
const struct icmp_header *hdr;
|
||||
|
|
@ -72,22 +72,24 @@ is_frag_needed(uint8_t type, uint8_t code)
|
|||
}
|
||||
|
||||
static int
|
||||
parse_embedded_ip(const icmp_reply_t *reply, icmp_offending_packet_t *offending,
|
||||
size_t *ip_hdr_len)
|
||||
parse_embedded_ip(const icmp_reply_t *reply, icmp_offending_packet_t
|
||||
*offending, size_t *ip_hdr_len)
|
||||
{
|
||||
return icmp_parse_ip_header(reply->payload, reply->payload_len,
|
||||
NULL, &offending->src, ip_hdr_len,
|
||||
&offending->dst, &offending->protocol);
|
||||
return icmp_parse_ip_header(reply->payload, reply->payload_len, NULL,
|
||||
&offending->src, ip_hdr_len, &offending->dst,
|
||||
&offending->protocol);
|
||||
}
|
||||
|
||||
static const struct icmp_header *
|
||||
get_embedded_icmp(const icmp_reply_t *reply, size_t ip_hdr_len)
|
||||
{
|
||||
return (const struct icmp_header *)((const uint8_t *)reply->payload + ip_hdr_len);
|
||||
return (const struct icmp_header *)((const uint8_t *)reply->payload +
|
||||
ip_hdr_len);
|
||||
}
|
||||
|
||||
static void
|
||||
extract_icmp_fields(const struct icmp_header *hdr, icmp_offending_packet_t *offending)
|
||||
extract_icmp_fields(const struct icmp_header *hdr,
|
||||
icmp_offending_packet_t *offending)
|
||||
{
|
||||
offending->icmp_type = hdr->type;
|
||||
offending->icmp_code = hdr->code;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_recv.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* Buffer for incoming packets: IP header + ICMP */
|
||||
#define RECV_BUFFER_SIZE 1500
|
||||
|
|
|
|||
|
|
@ -2,18 +2,16 @@
|
|||
#include "internal/icmp_packet_internal.h"
|
||||
|
||||
int
|
||||
recv_parse_packet(const void *buffer, size_t buffer_len,
|
||||
uint8_t *type, uint8_t *code,
|
||||
uint8_t *ttl, struct in_addr *src_addr,
|
||||
const void **payload, size_t *payload_len,
|
||||
size_t *ip_hdr_len)
|
||||
recv_parse_packet(const void *buffer, size_t buffer_len, uint8_t *type,
|
||||
uint8_t *code, uint8_t *ttl, struct in_addr *src_addr,
|
||||
const void **payload, size_t *payload_len, size_t *ip_hdr_len)
|
||||
{
|
||||
if (icmp_parse_ip_header(buffer, buffer_len, ttl, src_addr,
|
||||
ip_hdr_len, NULL, NULL) < 0)
|
||||
if (icmp_parse_ip_header(buffer, buffer_len, ttl, src_addr, ip_hdr_len,
|
||||
NULL, NULL) < 0)
|
||||
return -1;
|
||||
|
||||
if (icmp_parse_icmp_payload(buffer, buffer_len, *ip_hdr_len,
|
||||
type, code, payload, payload_len) < 0)
|
||||
if (icmp_parse_icmp_payload(buffer, buffer_len, *ip_hdr_len, type, code,
|
||||
payload, payload_len) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
#include "internal/icmp_recv.h"
|
||||
|
||||
// TODO: check ssize_t -> size_t cast
|
||||
void
|
||||
recv_process_single_packet(uint8_t *buffer, ssize_t len, icmp_callback_t cb,
|
||||
void *userdata)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include "internal/icmp_recv.h"
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "internal/icmp_recv.h"
|
||||
|
||||
ssize_t
|
||||
recv_receive_packet(int fd, void *buffer, size_t buffer_len,
|
||||
struct sockaddr_in *from)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#include "internal/icmp_recv.h"
|
||||
#include "internal/icmp_utils.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_recv.h"
|
||||
#include "internal/icmp_utils.h"
|
||||
|
||||
void
|
||||
recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code,
|
||||
uint8_t ttl, struct in_addr src_addr,
|
||||
const void *payload, size_t payload_len,
|
||||
const void *buffer, size_t ip_hdr_len)
|
||||
recv_build_reply(icmp_reply_t *reply, uint8_t type, uint8_t code, uint8_t ttl,
|
||||
struct in_addr src_addr, const void *payload, size_t payload_len, const
|
||||
void *buffer, size_t ip_hdr_len)
|
||||
{
|
||||
memset(reply, 0, sizeof(icmp_reply_t));
|
||||
reply->type = type;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_recv.h"
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_recv.h"
|
||||
|
||||
int
|
||||
recv_handle_receive_error(struct icmp_handle *h)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include <stddef.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_recv.h"
|
||||
#include <stddef.h>
|
||||
|
||||
int
|
||||
recv_validate_params(struct icmp_handle *h, icmp_callback_t cb)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#include "icmp.h"
|
||||
#include "icmp_types.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "icmp_types.h"
|
||||
|
||||
int
|
||||
icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
||||
uint16_t seq, uint8_t ttl,
|
||||
const void *payload, size_t payload_len)
|
||||
uint16_t seq, uint8_t ttl, const void *payload, size_t payload_len)
|
||||
{
|
||||
union {
|
||||
struct { uint16_t id; uint16_t seq; } echo;
|
||||
|
|
@ -15,6 +15,6 @@ icmp_send_echo(icmp_handle_t *h, struct in_addr dest, uint16_t id,
|
|||
|
||||
hdr_rest.echo.id = htons(id);
|
||||
hdr_rest.echo.seq = htons(seq);
|
||||
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw,
|
||||
payload, payload_len, dest, ttl);
|
||||
return icmp_send_raw(h, ICMP_TYPE_ECHO_REQUEST, 0, hdr_rest.raw, payload,
|
||||
payload_len, dest, ttl);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#include <stddef.h>
|
||||
|
||||
#include "icmp.h"
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_packet_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/* MTU(1500) - IP header(20) - ICMP header(8) = 1472 */
|
||||
#define MAX_PAYLOAD_SIZE 1472
|
||||
|
|
@ -15,9 +16,8 @@ static int validate_payload(struct icmp_handle *h, const void *payload,
|
|||
|
||||
int
|
||||
icmp_send_raw(icmp_handle_t *h, uint8_t type, uint8_t code,
|
||||
uint32_t header_rest,
|
||||
const void *payload, size_t len, struct in_addr dest,
|
||||
uint8_t ttl)
|
||||
uint32_t header_rest, 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;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static int handle_send_error(struct icmp_handle *h);
|
||||
/* -------------------- */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include <netinet/in.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
int
|
||||
send_to_destination(struct icmp_handle *h, const void *packet, size_t len,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "internal/icmp_send.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_send.h"
|
||||
|
||||
void
|
||||
send_prepare_destination(struct sockaddr_in *addr, struct in_addr dest)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include "internal/icmp_send.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_send.h"
|
||||
|
||||
int
|
||||
send_set_socket_ttl(struct icmp_handle *h, uint8_t ttl)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include <stddef.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_send.h"
|
||||
#include <stddef.h>
|
||||
|
||||
int
|
||||
send_validate_handle(struct icmp_handle *h)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_socket.h"
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_socket.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static void handle_configure_error(struct icmp_handle *h, const char *func);
|
||||
/* -------------------- */
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_socket.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal/icmp_internal.h"
|
||||
#include "internal/icmp_socket.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static void handle_socket_error(struct icmp_handle *h, int err);
|
||||
/* -------------------- */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "internal/icmp_utils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include "internal/icmp_utils.h"
|
||||
|
||||
int
|
||||
icmp_get_time(struct timespec *ts)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue