aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: e95d750cb2c3f1ead13f92ec29274bc55251611a (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Main loop part of HaxServ
//
// Written by: Test_User <hax@andrewyu.org>
//
// This is free and unencumbered software released into the public
// domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <gnutls/gnutls.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.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;
		{
			int len;
			do {
				len = gnutls_record_recv(session, data, 512);
			} while (len == GNUTLS_E_AGAIN || len == GNUTLS_E_INTERRUPTED);
			if (len < 0)
				new_len = 0;
			else
				new_len = len;
		}

		if (new_len == 0) {
			WRITES(1, STRING("Disconnected.\n"));
			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) {
			WRITES(2, STRING("OOM... currently just exiting bc there's no automatic reconnect in here yet, and the only sane solution to this is resyncing.\n"));
			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(2, STRING("WARNING: Command is unknown, ignoring...\n"));
			} else {
				write(1, "\n", 1);
				int err = func(source, argc, argv);
				if (err) {
					WRITES(1, STRING("Disconnecting by result of the network command handler...\n"));
					return 0;
				}
			}
			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;
}