aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 6bebabba8b663def3fd81667a8f41cd050cda1e5 (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <openssl/ssl.h>
#include <string.h>

#include "network.h"
#include "config.h"
#include "types.h"
#include "tls.h"
#include "types.h"

int main(void) {
	initservernetwork();

	struct string full_msg = {malloc(0), 0};
	while (1) {
		uint8_t data[512];
		uint64_t new_len = SSL_read(ssl, data, 512);

		if (new_len == 0) {
			puts("Disconnected.");
			return 0;
		}

		uint8_t found = 0;
		uint64_t msg_len;
		for (uint64_t i = 0; i < new_len; i++) {
			if (data[i] == '\n') {
				found = 1;
				msg_len = i + full_msg.len;
				break;
			}
		}

		void *tmp = realloc(full_msg.data, full_msg.len+new_len);
		if (tmp == 0 && full_msg.len+new_len != 0) {
			puts("OOM... currently just exiting bc there's no automatic reconnect in here yet, and the only sane solution to this is resyncing.");
			return 1;
		}
		full_msg.data = tmp;

		memcpy(full_msg.data+full_msg.len, data, new_len);

		full_msg.len += new_len;

		if (!found)
			continue;

		while (1) {
			WRITES(1, STRING("Recvd: "));
			write(1, full_msg.data, msg_len+1); // +1: \n

			uint64_t offset = 0;
			while (offset < msg_len && full_msg.data[offset] == ' ')
				offset++;

			if (msg_len == offset) {
				puts("Protocol violation: Empty message.");
				return 2;
			}

			struct string source;
			if (full_msg.data[0] == ':') {
				source.data = full_msg.data + 1;
				found = 0;
				for (uint64_t i = offset + 1; i < msg_len; i++) {
					if (full_msg.data[i] == ' ') {
						found = 1;
						offset = i + 1;
						source.len = i - 1;
						break;
					}
				}
				if (!found || source.len + 1 == msg_len) {
					puts("Protocol violation: Sender but no command.");
					return 2;
				}
			} else {
				source = (struct string){0};
				offset = 0;
			}

			while (offset < msg_len && full_msg.data[offset] == ' ')
				offset++;

			if (offset == msg_len) {
				puts("Protocol violation: No command.");
				return 2;
			}

			struct string command;
			command.data = full_msg.data+offset;
			found = 0;
			for (uint64_t i = offset; i < msg_len; i++) {
				if (full_msg.data[i] == ' ') {
					found = 1;
					command.len = i - offset;
					offset = i;
					break;
				}
			}
			if (!found) {
				command.len = msg_len - offset;
				offset = msg_len;
			}

			while (offset < msg_len && full_msg.data[offset] == ' ')
				offset++;

			uint64_t argc = 0;
			uint64_t old_offset = offset;
			if (offset < msg_len) {
				while (offset < msg_len) {
					if (full_msg.data[offset] == ':') {
						argc++;
						break;
					}

					while (offset < msg_len && full_msg.data[offset] != ' ')
						offset++;

					argc++;

					while (offset < msg_len && full_msg.data[offset] == ' ')
						offset++;
				}
			}
			offset = old_offset;

			struct string argv[argc];
			if (offset < msg_len) {
				uint64_t i = 0;
				while (offset < msg_len) {
					if (full_msg.data[offset] == ':') {
						argv[i].data = full_msg.data+offset+1;
						argv[i].len = msg_len - offset - 1;
						break;
					}

					argv[i].data = full_msg.data+offset;
					uint64_t start = offset;

					while (offset < msg_len && full_msg.data[offset] != ' ')
						offset++;

					argv[i].len = offset - start;

					while (offset < msg_len && full_msg.data[offset] == ' ')
						offset++;

					i++;
				}
			}

			int (*func)(struct string source, uint64_t argc, struct string *argv) = get_table_index(network_commands, command);

			if (func == 0) {
				WRITES(1, STRING("WARNING: Command is unknown, ignoring...\n"));
			} else {
				write(1, "\n", 1);
				func(source, argc, argv);
			}
			write(1, "\n", 1);

			memmove(full_msg.data, full_msg.data+msg_len+1, full_msg.len - msg_len - 1);
			full_msg.len -= msg_len+1;

			found = 0;
			for (uint64_t i = 0; i < full_msg.len; i++) {
				if (full_msg.data[i] == '\n') {
					found = 1;
					msg_len = i;
					break;
				}
			}

			if (found == 0) {
				void *tmp = realloc(full_msg.data, full_msg.len);
				if (tmp == 0 && full_msg.len != 0) {
					puts("AAAAAAAAA (OOM shrinking allocated data?)");
					return 1;
				}
				full_msg.data = tmp;

				break;
			}
		}
	}

	return 0;
}