summaryrefslogtreecommitdiff
path: root/server.c
blob: db269e5ad431ce3288e14a77e668d362a1bc3773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include "icmp.h"
#include "util.h"


struct state {
	int socket;
};

#if 0
static int icmp_callback(struct nflog_data *ldata, void *state_) {
	(void)gh; (void)nfmsg;

	struct state *state = (struct state*)state_;

	uint8_t *ip_start;  // received packet, starting at the IP buffer
	int ip_len = nflog_get_payload(ldata, (char**)&ip_start);

	struct iphdr *hdr = (struct iphdr*)ip_start;
	int hdr_len = hdr->ihl * 4;
	uint32_t saddr = hdr->saddr;

	struct icmp_echo *msg = (struct icmp_echo*)(ip_start + hdr_len);
	int msg_len = ip_len - hdr_len;
	printf("Received: type %u   code %u   id %hu   seqnum %hu   payload:\n",
			(unsigned)msg->type, (unsigned)msg->code, msg->id, msg->seqnum);
	xxd(msg->payload, msg_len - offsetof(struct icmp_echo, payload));

	uint8_t *saddr_bytes = (uint8_t*)&saddr;
	char ip_address[16];
	sprintf(ip_address, "%u.%u.%u.%u", saddr_bytes[0], saddr_bytes[1], saddr_bytes[2], saddr_bytes[3]);
	if (icmp_send_echo_reply(ip_address, msg->id, msg->seqnum, "dank je wel", 11) < 0) {
		perror("icmp_send_echo_reply");
	}

	return 0;
}
#endif


int main(void) {
	// struct state state;
	// state.socket = icmp_open_socket();
	// if (state.socket < 0) {
	//     perror("icmp_open_socket");
	//     return 1;
	// }

	int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

	struct icmp_echo msg;

	char buf[MAX_IP_PACKET_SIZE];
	while (true) {
		struct iovec iov1;
		memset(&iov1, 0, sizeof iov1);
		iov1.iov_base = buf;
		iov1.iov_len = sizeof buf;

		struct sockaddr_in addr;

		struct msghdr recv_msghdr;
		memset(&recv_msghdr, 0, sizeof recv_msghdr);
		recv_msghdr.msg_name = &addr;
		recv_msghdr.msg_namelen = sizeof addr;
		recv_msghdr.msg_iov = &iov1;
		recv_msghdr.msg_iovlen = 1;

		ssize_t nr = recvmsg(sock, &recv_msghdr, 0);
		if (nr < 0) break;

		// buf now contains received data starting at the IP header

		printf("Full packet received:\n");
		xxd(buf, nr);

		struct iphdr *hdr = (struct iphdr*)buf;
		int hdr_len = hdr->ihl * 4;
		uint32_t saddr = hdr->saddr;

		struct icmp_echo *msg = (struct icmp_echo*)(buf + hdr_len);
		int msg_len = nr - hdr_len;
		printf("Received: type %u   code %u   id %hu   seqnum %hu   payload:\n",
				(unsigned)msg->type, (unsigned)msg->code, msg->id, msg->seqnum);
		xxd(msg->payload, msg_len - offsetof(struct icmp_echo, payload));

		if (msg->type != ICMP_ECHO) {
			printf("Not an ICMP_ECHO, ignoring\n");
			continue;
		}

		uint8_t *saddr_bytes = (uint8_t*)&saddr;
		char ip_address[16];
		sprintf(ip_address, "%u.%u.%u.%u", saddr_bytes[0], saddr_bytes[1], saddr_bytes[2], saddr_bytes[3]);
		if (icmp_send_echo_reply(ip_address, msg->id, msg->seqnum, "dank je wel", 11) < 0) {
			perror("icmp_send_echo_reply");
		}
	}
}