summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7534e76fbc1a7a816101b186452727ff9cf53ec4 (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
#include "proto.h"
#include "log.h"

#include <time.h>
#include <assert.h>
#include <errno.h>

void flush_messages(int sfd, struct message* cursor, const struct message* const end) {
	dbg("Flushing %ld pending messages", end - cursor);
	for(; cursor < end; cursor++) {
		//now reply the client with the same data
		int rc = sendto(sfd, cursor->payload, cursor->payload_len, 0, (const struct sockaddr*)&cursor->dest, cursor->dest_len);
		if (rc < 0) {
			fatal("Failed to send message");
		}
	}
}

int main(int argc, char** argv) {
	struct message outbuff[10] = {0};

	struct dht dht;
	dht.self = (struct nodeid){.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}};
	struct message* message_cursor = outbuff;
	proto_begin(&dht, time(NULL), &message_cursor, outbuff+10);
	flush_messages(dht.sfd, outbuff, message_cursor);


	char buff_storage[2049];
	int rc = 0;
	while(rc == 0) {
		char* buff = buff_storage;
		printf("Waiting for data...");
		fflush(stdout);

		bool timedout = false;
		if(!dht.pause){
			struct entry* oldest;
			routing_oldest(&oldest);
			if(oldest != NULL) {
				dbg("Set timeout to %ld", oldest->expire - time(NULL));
				struct timeval tv = {
					.tv_sec = oldest->expire - time(NULL),
					.tv_usec = 0,
				};
				if(tv.tv_sec <= 0) {
					timedout = true;
				} else {
					setsockopt(dht.sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
				}
			}
		} else {
			printf("DHT timeout is paused");
		}
		// Try to receive some data, this is a blocking call
		struct sockaddr_storage remote;
		socklen_t remote_len = sizeof(remote);
		ssize_t recv_len;
		if(!timedout) {
			recv_len = recvfrom(dht.sfd, buff, 2048, 0, (struct sockaddr *)&remote, &remote_len);
			if(recv_len == -1) {
				// This is really strange. The man pages say we should be getting
				// an ETIMEDOUT here, but instead linux gives us this.
				if(errno == EAGAIN) {
					buff = NULL;
					recv_len = 0;
				} else {
					fatal("RECV failed %d %m", errno, errno);
				}
			} else if(recv_len >= 2048) {
				fatal("Receive buffer too small");
			}
			// Null terminate the packet
			if(buff != NULL) {
				buff[recv_len] = '\0';
			}
		} else {
			buff = NULL;
			recv_len = 0;
		}

		struct message* message_cursor = outbuff;
		time_t now = time(NULL);
		rc = proto_run(&dht, buff, recv_len, (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+10);
		flush_messages(dht.sfd, outbuff, message_cursor);
	}

	proto_end(&dht);

	return rc;
}