summaryrefslogtreecommitdiff
path: root/icmp.h
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-07-23 15:48:44 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-07-23 15:48:44 +0200
commit5722c47aa3402f1458da9eec332153a454a540b7 (patch)
treef1bfaa8c89dc39c6b9ae0bf7ff54eb524b1468eb /icmp.h
Initial version
Working ping back and forth with specified data, and data arrives at the other party. Currently, the server uses nflog to get the pings, which: 1. requires iptables settings to work; 2. buffers and collects messages for a second. Both are suboptimal, and I believe raw sockets can improve on this.
Diffstat (limited to 'icmp.h')
-rw-r--r--icmp.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/icmp.h b/icmp.h
new file mode 100644
index 0000000..3557fc3
--- /dev/null
+++ b/icmp.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <stdint.h>
+
+
+#define IP_HEADER_SIZE 40
+
+#define MIN_MTU 576
+#define MAX_IP_PACKET_SIZE 65535
+#define MAX_DATAGRAM_SIZE (MAX_IP_PACKET_SIZE - IP_HEADER_SIZE)
+
+#define ICMP_PAYLOAD_OFFSET 8
+
+struct __attribute__((packed)) icmp_echo {
+ uint8_t type;
+ uint8_t code;
+ uint16_t checksum;
+ uint16_t id;
+ uint16_t seqnum;
+ uint8_t payload[MAX_DATAGRAM_SIZE - ICMP_PAYLOAD_OFFSET];
+};
+
+#define ICMP_MAX_PAYLOAD_LENGTH (MAX_DATAGRAM_SIZE - ICMP_PAYLOAD_OFFSET)
+#define ICMP_SAFE_PAYLOAD_LENGTH (MIN_MTU - IP_HEADER_SIZE - ICMP_PAYLOAD_OFFSET)
+
+
+struct icmp_reply {
+ const uint8_t *data; // points to internal buffer
+ size_t length; // length of 'data'
+
+ int seqnum;
+};
+
+// Returns -1 on error with errno.
+int icmp_open_socket(void);
+
+// Only actual IPv4 addresses allowed. Sends data in 'data' with length 'length', and
+// returns pointer to internal buffer with reply data. Buffer is invalidated on next
+// call to the function.
+// Returns {.data=NULL} on error with errno.
+struct icmp_reply icmp_communicate(int sock, const char *ip_address, int seqnum, const void *data, size_t length);
+
+// Returns -1 on error with errno.
+int icmp_send_echo_reply(const char *ip_address, int id, int seqnum, const void *data, size_t length);