summaryrefslogtreecommitdiff
path: root/server.c
blob: df51032129bf0d550aa7e3bd6f3d837cf30edcac (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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "icmp_server.h"
#include "util.h"


int main(void) {
	int sock = icmp_server_open_socket();
	if (sock < 0) {
		perror("icmp_server_open_socket");
		return 1;
	}

	while (true) {
		struct icmp_incoming msg = icmp_server_receive(sock);
		if (msg.data == NULL) {
			perror("icmp_server_communicate");
			return 1;
		}

		printf("Received: id %d   seqnum %d   data:\n", msg.id, msg.seqnum);
		xxd(msg.data, msg.length);

		char data[msg.length + 1];
		for (size_t i = 0; i < msg.length; i++) {
			data[i] = toupper(msg.data[i]);
		}
		data[msg.length] = '!';

		if (icmp_server_send_reply(sock, msg.source_addr, msg.id, msg.seqnum, data, msg.length + 1) < 0) {
			perror("icmp_server_send_reply");
		}
	}

	close(sock);
}