summaryrefslogtreecommitdiff
path: root/config.c
blob: 86723fefbf711ad34f1061194b1cc078b0c06853 (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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include "main.h"
#include "uring.h"
#include "config.h"
#include "server.h"

static void
eat_whitespace_and_comments(char **pos)
{
	while (true) {
		while (isspace(**pos))
			(*pos)++;

		if (**pos == '#') {
			while (**pos != '\r' && **pos != '\n' && **pos != '\0')
				(*pos)++;
			continue;
		}
		
		return;
	}
}

static char *
get_line(char **pos)
{
	char *begin = *pos;
	char *end;

	while (isspace(*begin))
		begin++;

	if (*begin == '\0')
		return NULL;

	end = begin;
	while (*end != '\n' && *end != '\0')
		end++;

	if (*end == '\0')
		*pos = end;
	else
		*pos = end + 1;

	while (isspace(*end)) {
		*end = '\0';
		end--;
	}

	return begin;
}

static bool
strtosockaddrs(const char *str, struct list_head *list)
{
	char *tmp;
	uint16_t port;
	int r;
	struct sockaddr_in46 *addr;

	if (!str || *str == '\0' || !list)
		return false;
	list_init(list);

	if (*str == '[') {
		/* IPv6, [a:b:c...h]:p or [*]:p */
		fprintf(stderr, "Attempting to parse an IPv6 addr\n");
		str++;
		tmp = strchr(str, ']');
		if (!tmp)
			goto out;
		*tmp = '\0';

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		list_add(&addr->list, list);

		if (!strcmp(str, "*"))
			addr->in6.sin6_addr = in6addr_any;
		else if (inet_pton(AF_INET6, str, &addr->in6.sin6_addr) <= 0)
			goto out;

		tmp++;
		if (*tmp != ':')
			goto out;

		tmp++;
		if (strtou16_strict(tmp, &port) < 0)
			goto out;

		addr->in6.sin6_family = AF_INET6;
		addr->in6.sin6_port = htons(port);
		addr->addrlen = sizeof(addr->in6);

	} else if (*str == '*') {
		/* IPv4, *:p */
		fprintf(stderr, "Attempting to parse an IPv4 wildcard\n");
		str++;
		if (*str != ':')
			goto out;

		str++;
		if (strtou16_strict(str, &port) < 0)
			goto out;

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		list_add(&addr->list, list);

		addr->in4.sin_family = AF_INET;
		addr->in4.sin_addr.s_addr = INADDR_ANY;
		addr->in4.sin_port = htons(port);
		addr->addrlen = sizeof(addr->in4);

	} else if ((tmp = strchr(str, ':'))) {
		/* IPv4, a.b.c.d:p or IPv4/6 hostname:p */
		fprintf(stderr, "Got an IPv4:port or hostname:port\n");
		*tmp = '\0';
		tmp++;
		if (strtou16_strict(tmp, &port) < 0)
			goto out;

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;

		if (inet_pton(AF_INET, str, &addr->in4.sin_addr) > 0) {
			fprintf(stderr, "...Got an IPv4:port (0x%p, list 0x%p)\n", addr, &addr->list);
			addr->in4.sin_family = AF_INET;
			addr->in4.sin_port = htons(port);
			addr->addrlen = sizeof(addr->in4);
			list_add(&addr->list, list);
			goto success;
		} else {
			xfree(addr);
		}

		struct addrinfo hints = {
			.ai_family = AF_UNSPEC,
			.ai_socktype = SOCK_STREAM,
			.ai_protocol = 0,
			.ai_flags = 0,
		};
		struct addrinfo *results, *ai;

		/* FIXME: This is completely synchronous but getaddrinfo_a is not very ergonomic */
		r = getaddrinfo(str, tmp, &hints, &results);
		if (r != 0) {
			fprintf(stderr, "gettaddrinfo(%s): %s\n", str, gai_strerror(r));
			goto out;
		}

		fprintf(stderr, "...Got an hostname:port\n");
		for (ai = results; ai; ai = ai->ai_next) {
			fprintf(stderr, "Got a result from getaddrinfo\n");

			addr = zmalloc(sizeof(*addr));
			if (!addr) {
				freeaddrinfo(results);
				goto out;
			}

			switch (ai->ai_family) {
			case AF_INET: {
				struct sockaddr_in *naddr = (struct sockaddr_in *)ai->ai_addr;

				addr->in4.sin_family = AF_INET;
				addr->in4.sin_addr = naddr->sin_addr;
				addr->in4.sin_port = naddr->sin_port;
				addr->addrlen = sizeof(addr->in4);
				list_add(&addr->list, list);
				break;
			}
			case AF_INET6: {
				struct sockaddr_in6 *naddr = (struct sockaddr_in6 *)ai->ai_addr;

				addr->in6.sin6_family = AF_INET6;
				addr->in6.sin6_addr = naddr->sin6_addr;
				addr->in6.sin6_port = naddr->sin6_port;
				addr->addrlen = sizeof(addr->in6);
				list_add(&addr->list, list);
				break;
			}
			default:
				fprintf(stderr, "  Fam: Unknown (%i)\n", ai->ai_family);
				xfree(addr);
				continue;
			}
		}

		freeaddrinfo(results);

	} else if (strtou16_strict(tmp, &port) == 0) {
		/* Port */

		fprintf(stderr, "Attempting to parse a port\n");
		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		addr->in6.sin6_family = AF_INET6;
		addr->in6.sin6_addr = in6addr_any;
		addr->in6.sin6_port = htons(port);
		addr->addrlen = sizeof(addr->in6);
		list_add(&addr->list, list);

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		addr->in4.sin_family = AF_INET;
		addr->in4.sin_addr.s_addr = INADDR_ANY;
		addr->in4.sin_port = htons(port);
		addr->addrlen = sizeof(addr->in4);
		list_add(&addr->list, list);
	} else {
		goto out;
	}

success:
	if (list_empty(list)) {
		fprintf(stderr, "Success but empty list!?\n");
		return false;
	} else {
		int i = 0;
		struct list_head *pos;

		list_for_each(pos, list)
			i++;
		fprintf(stderr, "Success, %i entries\n", i);
	}

	return true;

out:
	if (!list_empty(list)) {
		struct sockaddr_in46 *tmpaddr;

		list_for_each_entry_safe(addr, tmpaddr, list, list) {
			list_del(&addr->list);
			xfree(addr);
		}
	}
	return false;
}

/* Returns true if theres data left to parse in buf */
bool
config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap,
		  int *rkey, union cfg_value *rvalue)
{
	char *line, *tmp, *key;
	int i;

	if (!cfg || !buf || !*buf || !kvmap || !rkey || !rvalue)
		die("%s: called with invalid parameters\n", __func__);

	eat_whitespace_and_comments(buf);
	line = get_line(buf);
	if (!line)
		return false;

	printf("%s: examining line: %s\n", __func__, line);

	tmp = line;
	while (isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		goto out;

	key = tmp;
	while (*tmp != '\0' && !isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		goto out;

	*tmp = '\0';
	tmp++;

	while (isspace(*tmp))
		tmp++;

	if (*tmp != '=')
		goto out;

	tmp++;
	while (isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		goto out;

	for (i = 0; kvmap[i].key_name; i++) {
		if (strcmp(kvmap[i].key_name, key))
			continue;

		switch (kvmap[i].value_type) {

		case CFG_VAL_TYPE_STRING:
			rvalue->str = tmp;
			break;

		case CFG_VAL_TYPE_UINT16: {
			uint16_t v;

			if (strtou16_strict(tmp, &v) < 0)
				goto out;
			rvalue->uint16 = v;
			break;
		}

		case CFG_VAL_TYPE_ADDRS: {
			if (!strtosockaddrs(tmp, &rvalue->addr_list))
				goto out;
			if (list_empty(&rvalue->addr_list)) {
				fprintf(stderr, "CFG_VAL_TYPE_ADDRS with zero list!?\n");
				goto out;
			} else {
				int i = 0;
				struct list_head *pos;

				list_for_each(pos, &rvalue->addr_list)
					i++;
				fprintf(stderr, "CFG_VAL_TYPE_ADDRS with list %i entries\n", i);
			}
			break;
		}

		case CFG_VAL_TYPE_INVALID:
			/* fall through */
		default:
			goto out;
		}

		*rkey = kvmap[i].key_value;
		return true;
	}

out:
	fprintf(stderr, "Invalid line\n");
	*rkey = 0;
	return true;
}

bool
config_parse_header(struct cfg *cfg, const char *title, char **buf)
{
	char *line;

	if (!cfg || !title || !buf || !*buf)
		return false;

	eat_whitespace_and_comments(buf);

	line = get_line(buf);
	if (!line) {
		printf("Cfg: premature EOF\n");
		return false;
	} else {
		char titlehdr[strlen(title) + 3];

		sprintf(titlehdr, "[%s]", title);
		if (strcmp(line, titlehdr)) {
			printf("Invalid header: %s\n", line);
			return false;
		}
	}

	return true;
}