summaryrefslogtreecommitdiff
path: root/server.c
blob: ee93a24c64f2959412a4bb1f3de9e2d511fb2736 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sched.h>
#include <poll.h>
#include <errno.h>
#include <sched.h>

#include "main.h"
#include "uring.h"
#include "server.h"
#include "proxy.h"
#include "utils.h"
#include "idle.h"
#include "rcon.h"
#include "systemd.h"

struct server_local {
	struct sockaddr_in46 addr;
	char addrstr[ADDRSTRLEN];
	struct sockaddr_in46 peer;
	struct uring_task task;
	struct list_head list;
};

static bool
set_property(struct cfg *cfg, struct server *scfg, char **property, const char *value)
{
	if (!cfg || !scfg || empty_str(value) || *property)
		return false;

	*property = strdup(value);
	if (!*property) {
		perror("strdup");
		return false;
	}

	return true;
}

void
server_refdump(struct server *server)
{
	struct server_local *local;
	struct server_proxy *proxy;

	uring_task_refdump(&server->task);
	list_for_each_entry(local, &server->locals, list)
		uring_task_refdump(&local->task);
	list_for_each_entry(proxy, &server->proxys, list)
		proxy_refdump(proxy);
	if (server->idle)
		idle_refdump(server->idle);
}

static void
server_free(struct uring_task *task)
{
	struct server *scfg = container_of(task, struct server, task);

	fprintf(stderr, "Freeing scfg %s\n", scfg->name);
	list_del(&scfg->list);
	free(scfg->pretty_name);
	free(scfg->start_exec);
	free(scfg->stop_exec);
	free(scfg->systemd_service);
	free(scfg->systemd_obj);
	free(scfg->name);
	free(scfg);
}

void
server_delete(struct cfg *cfg, struct server *scfg)
{
	struct server_local *local, *ltmp;
	struct sockaddr_in46 *remote, *rtmp;

	fprintf(stderr, "Removing server cfg: %s\n", scfg->name);

	idle_delete(cfg, scfg);
	rcon_delete(cfg, scfg);

	list_for_each_entry_safe(remote, rtmp, &scfg->remotes, list) {
		list_del(&remote->list);
		free(remote);
	}

	list_for_each_entry_safe(local, ltmp, &scfg->locals, list) {
		uring_cancel(cfg, &local->task);
	}

	uring_poll_cancel(cfg, &scfg->exec_task);
	uring_task_put(cfg, &scfg->exec_task);
	uring_task_put(cfg, &scfg->task);
}

void
server_delete_by_name(struct cfg *cfg, const char *name)
{
	struct server *scfg;

	if (!cfg || empty_str(name))
		return;

	list_for_each_entry(scfg, &cfg->servers, list) {
		if (!strcmp(scfg->name, name)) {
			server_delete(cfg, scfg);
			return;
		}
	}
}

static void
server_dump(struct server *scfg)
{
	struct server_local *local;
	struct sockaddr_in46 *remote;
	char abuf[ADDRSTRLEN];

	fprintf(stderr, "Dumping server %s\n", scfg->name);
	switch (scfg->type) {
	case SERVER_TYPE_ANNOUNCE:
		fprintf(stderr, "  * Type: announce\n");
		break;
	case SERVER_TYPE_PROXY:
		fprintf(stderr, "  * Type: proxy\n");
		break;
	default:
		fprintf(stderr, "  * Type: unknown\n");
		break;
	}
	fprintf(stderr, "  * Pretty name: %s\n", scfg->pretty_name ? scfg->pretty_name : "<undefined>");
	fprintf(stderr, "  * Announce port: %" PRIu16 "\n", scfg->announce_port); 
	fprintf(stderr, "  * Listening:\n");
	list_for_each_entry(local, &scfg->locals, list)
		fprintf(stderr, "    * %s\n", local->addrstr);
	fprintf(stderr, "  * Remote:\n");
	list_for_each_entry(remote, &scfg->remotes, list)
		fprintf(stderr, "    * %s\n", sockaddr_to_str(remote, abuf, sizeof(abuf)));
	fprintf(stderr, "  * RCon:\n");
	list_for_each_entry(remote, &scfg->rcons, list)
		fprintf(stderr, "    * %s\n", sockaddr_to_str(remote, abuf, sizeof(abuf)));
}

static void
server_local_free(struct uring_task *task)
{
	struct server_local *local = container_of(task, struct server_local, task);

	fprintf(stderr, "%s called: task 0x%p\n", __func__, task);

	list_del(&local->list);
	free(local);
}

static void
server_local_accept(struct cfg *cfg, struct uring_task *task, int res)
{
	struct server_local *local = container_of(task, struct server_local, task);
	struct server *scfg = container_of(task->parent, struct server, task);
	struct server_proxy *proxy;
	char abuf[ADDRSTRLEN];

	fprintf(stderr, "%s called: task 0x%p and res %i\n", __func__, task, res);
	fprintf(stderr, "%s called: scfg name is %s\n", __func__, scfg->name);

	if (task->dead) {
		fprintf(stderr, "Task dead!\n");
		uring_task_put(cfg, task);
		return;
	}

	if (res < 0) {
		fprintf(stderr, "%s: result was %i\n", __func__, res);
		goto out;
	}

	sockaddr_to_str(&local->peer, abuf, sizeof(abuf));
	fprintf(stderr, "%s: incoming proxy connection: %s -> %s\n", scfg->name, abuf, local->addrstr);

	if (list_empty(&scfg->remotes)) {
		error("scfg->remotes empty!\n");
		uring_close(cfg, NULL, res, NULL);
		goto out;
	}

	proxy = proxy_new(cfg, scfg, &local->peer, res);
	if (!proxy)
		uring_close(cfg, NULL, res, NULL);

out:
	uring_accept(cfg, &local->task, &local->peer, server_local_accept);
}

static bool
server_local_open(struct cfg *cfg, struct server *scfg, struct server_local *local)
{
	int sfd;
	int enable = 1;
	int r;

	sfd = socket(local->addr.storage.ss_family, SOCK_STREAM, 0);
	if (sfd < 0) {
		perror("socket");
		goto out;
	}

	if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
		perror("setsockopt");
		goto out;
	}

	r = bind(sfd, (struct sockaddr *)&local->addr.storage, local->addr.addrlen);
	if (r < 0) {
		perror("bind");
		goto out;
	}

	r = listen(sfd, 100);
	if (r < 0) {
		perror("listen");
		goto out;
	}

	uring_task_set_fd(&local->task, sfd);
	uring_accept(cfg, &local->task, &local->peer, server_local_accept);
	fprintf(stderr, "** Opened listening socket: %s\n", local->addrstr);
	return true;

out:
	if (sfd >= 0)
		close(sfd);
	return false;
}

static void
server_exec_free(struct uring_task *task)
{
	//struct server *scfg = container_of(task, struct server, exec_task);

	fprintf(stderr, "%s called\n", __func__);
}

#ifndef P_PIDFD
#define P_PIDFD 3
#endif

static void
server_exec_done(struct cfg *cfg, struct uring_task *task, int res)
{
	struct server *scfg = container_of(task, struct server, exec_task);
	int r;
	siginfo_t info;

	if (task->dead)
		goto out;

	if (!(res & POLLIN)) {
		fprintf(stderr, "%s: unexpected result: %i\n", __func__, res);
		goto out;
	}

	r = waitid(P_PIDFD, scfg->exec_task.fd, &info, WEXITED);
        if (r < 0) {
                perror("waitid");
		goto out;
        }

	fprintf(stderr, "Command executed, return value: %i\n", info.si_status);
out:
	uring_task_close_fd(cfg, &scfg->exec_task);
}

static int
server_exec_child(void *ptr)
{
	const char *cmd = ptr;

	execl(cmd, cmd, NULL);
	return errno;
}

#ifndef CLONE_PIDFD
#define CLONE_PIDFD 0x00001000
#endif

static bool
server_exec(struct cfg *cfg, struct server *scfg, const char *cmd)
{
	char stack[4096]; /* Beautiful/horrible hack :) */
	int pidfd;
	int r;

	if (!cfg || !scfg || !cmd)
		return false;

	if (scfg->exec_task.fd >= 0)
		return false;

        r = clone(server_exec_child, stack + sizeof(stack),
                  CLONE_VM | CLONE_VFORK | CLONE_PIDFD | SIGCHLD,
                  (void *)cmd, &pidfd);
	if (r < 0) {
		perror("clone");
		return false;
	}

	uring_task_set_fd(&scfg->exec_task, pidfd);
	uring_poll(cfg, &scfg->exec_task, POLLIN, server_exec_done);
	return true;
}

bool
server_start(struct cfg *cfg, struct server *scfg)
{
	if (!cfg || !scfg)
		return false;

	switch (scfg->start_method) {

	case SERVER_START_METHOD_EXEC:
		return server_exec(cfg, scfg, scfg->start_exec);

	case SERVER_START_METHOD_SYSTEMD:
		return systemd_service_start(cfg, scfg);

	case SERVER_START_METHOD_UNDEFINED:
	default:
		break;
	}

	return false;
}

bool
server_stop(struct cfg *cfg, struct server *scfg)
{
	if (!cfg || !scfg)
		return false;

	switch (scfg->stop_method) {

	case SERVER_STOP_METHOD_EXEC:
		return server_exec(cfg, scfg, scfg->stop_exec);

	case SERVER_STOP_METHOD_SYSTEMD:
		return systemd_service_stop(cfg, scfg);

	case SERVER_STOP_METHOD_RCON:
		rcon_init(cfg, scfg);
		return true;

	case SERVER_STOP_METHOD_UNDEFINED:
	default:
		break;
	}

	return false;
}

bool
server_commit(struct cfg *cfg, struct server *scfg)
{
	struct server_local *local;
	uint16_t port;

	if (!scfg || !scfg->name) {
		fprintf(stderr, "%s: called with invalid parameters\n", __func__);
		return false;
	}

	if (!list_empty(&scfg->proxys)) {
		fprintf(stderr, "%s(%s): proxys not empty?\n", __func__, scfg->name);
		return false;
	}

	/* FIXME: running? */

	if (scfg->stop_method == SERVER_STOP_METHOD_RCON &&
	    (list_empty(&scfg->rcons) || !scfg->rcon_password)) {
		fprintf(stderr, "%s(%s): rcon stop method but missing rcon password\n", __func__, scfg->name); 
		return false;
	}

	if ((scfg->start_method == SERVER_START_METHOD_SYSTEMD ||
	     scfg->stop_method == SERVER_STOP_METHOD_SYSTEMD) &&
	    !scfg->systemd_service) {
		fprintf(stderr, "%s(%s): systemd start/stop method but missing systemd service\n", __func__, scfg->name); 
		return false;
	}

	if (scfg->systemd_service && !scfg->systemd_obj) {
		scfg->systemd_obj = systemd_service_object_path(cfg,
								scfg->systemd_service);
		if (!scfg->systemd_obj) {
			fprintf(stderr, "%s(%s): failed to create systemd object path (%s)\n", __func__, scfg->name, scfg->systemd_service);
			return false;
		}
	}

	if (scfg->idle_timeout > 0 &&
	    scfg->stop_method == SERVER_STOP_METHOD_UNDEFINED) {
		fprintf(stderr, "%s(%s): idle_timeout set but missing stop method\n", __func__, scfg->name); 
		return false;
	}

	switch (scfg->type) {
	case SERVER_TYPE_ANNOUNCE:
		if (scfg->announce_port < 1) {
			fprintf(stderr, "%s(%s): missing announce port\n", __func__, scfg->name); 
			return false;
		}

		if (scfg->start_method != SERVER_START_METHOD_UNDEFINED) {
			fprintf(stderr, "%s(%s): can't set start_method for announce server\n", __func__, scfg->name);
			return false;
		}

		if (!list_empty(&scfg->locals)) {
			fprintf(stderr, "%s(%s): can't set local addresses for announce server\n", __func__, scfg->name);
			return false;
		}

		if (!list_empty(&scfg->remotes)) {
			fprintf(stderr, "%s(%s): can't set remote addresses for announce server\n", __func__, scfg->name);
			return false;
		}

		break;

	case SERVER_TYPE_PROXY:
		if (scfg->announce_port >= 1) {
			fprintf(stderr, "%s(%s): can't set announce port for proxy server\n", __func__, scfg->name); 
			return false;
		}

		if (list_empty(&scfg->locals)) {
			fprintf(stderr, "%s(%s): missing local addresses for proxy server\n", __func__, scfg->name);
			return false;
		}

		if (list_empty(&scfg->remotes)) {
			fprintf(stderr, "%s(%s): missing remote addresses for proxy server\n", __func__, scfg->name);
			return false;
		}

		list_for_each_entry(local, &scfg->locals, list) {
			port = sockaddr_port(&local->addr);

			if (port == 0) {
				fprintf(stderr, "%s(%s): invalid local port\n", __func__, scfg->name);
				return false;
			}

			if (scfg->announce_port < 1)
				scfg->announce_port = port;

			if (scfg->announce_port != port) {
				fprintf(stderr, "%s(%s): multiple local ports\n", __func__, scfg->name);
				return false;
			}
		}

		if (scfg->announce_port < 1) {
			fprintf(stderr, "%s(%s): can't determine which port to announce\n", __func__, scfg->name);
			return false;
		}

		break;

	default:
		fprintf(stderr, "%s(%s): can't determine server type\n", __func__, scfg->name);
		return false;
	}

	if (!scfg->pretty_name) {
		char *suffix;
		
		suffix = strrchr(scfg->name, '.');
		if (!suffix || suffix == scfg->name) {
			fprintf(stderr, "%s(%s): invalid server name\n", __func__, scfg->name);
			return false;
		}

		scfg->pretty_name = strndup(scfg->name, suffix - scfg->name);
		if (!scfg->pretty_name) {
			fprintf(stderr, "%s(%s): failed to create display name\n", __func__, scfg->name);
			return false;
		}
	}

	/* FIXME: config, dont reread config if server running, make sure fd is available before this is called */
	server_dump(scfg);

	list_for_each_entry(local, &scfg->locals, list) {
		server_local_open(cfg, scfg, local);
	}

	idle_init(cfg, scfg);

	if (scfg->systemd_service) {
		fprintf(stderr, "Checking if systemd service is running\n");
		systemd_service_running(cfg, scfg);
	}

	return true;
}

bool
server_add_remote(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *addr)
{
	if (!scfg || !addr)
		return false;

	list_add(&addr->list, &scfg->remotes);
	return true;
}

bool
server_add_local(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *addr)
{
	struct server_local *local;

	if (!scfg || !addr)
		return false;

	local = zmalloc(sizeof(*local));
	if (!local)
		return false;

	local->addr.storage = addr->storage;
	local->addr.addrlen = addr->addrlen;
	uring_task_init(&local->task, "local", &scfg->task, server_local_free);
	sockaddr_to_str(&local->addr, local->addrstr, sizeof(local->addrstr));
	fprintf(stderr, "Adding local: %s\n", local->addrstr);
	list_add(&local->list, &scfg->locals);
	free(addr);
	return true;
}

bool
server_add_rcon(struct cfg *cfg, struct server *scfg,
		struct sockaddr_in46 *addr)
{
	if (!scfg || !addr)
		return false;

	list_add(&addr->list, &scfg->rcons);
	return true;
}

bool
server_set_rcon_password(struct cfg *cfg, struct server *scfg,
			 const char *password)
{
	return set_property(cfg, scfg, &scfg->rcon_password, password);
}

bool
server_set_systemd_service(struct cfg *cfg, struct server *scfg,
			   const char *service)
{
	const char *suffix;
	char *tmp;

	if (!cfg || !scfg || empty_str(service) || scfg->systemd_service)
		return false;

	suffix = strrchr(service, '.');
	if (!suffix || strcmp(suffix, ".service")) {
		tmp = malloc(strlen(service) + strlen(".service") + 1);
		if (tmp)
			sprintf(tmp, "%s.service", service);
	} else
		tmp = strdup(service);

	if (!tmp) {
		perror("malloc/strdup");
		return false;
	}

	scfg->systemd_service = tmp;
	return true;
}

bool
server_set_stop_method(struct cfg *cfg, struct server *scfg,
		       enum server_stop_method stop_method)
{
	if (scfg->stop_method != SERVER_STOP_METHOD_UNDEFINED ||
	    stop_method == SERVER_STOP_METHOD_UNDEFINED)
		return false;

	scfg->stop_method = stop_method;
	return true;
}

bool
server_set_start_method(struct cfg *cfg, struct server *scfg,
			enum server_start_method start_method)
{
	if (scfg->start_method != SERVER_START_METHOD_UNDEFINED ||
	    start_method == SERVER_START_METHOD_UNDEFINED)
		return false;

	scfg->start_method = start_method;
	return true;
}

bool
server_set_stop_exec(struct cfg *cfg, struct server *scfg, const char *cmd)
{
	return set_property(cfg, scfg, &scfg->stop_exec, cmd);
}

bool
server_set_start_exec(struct cfg *cfg, struct server *scfg, const char *cmd)
{
	return set_property(cfg, scfg, &scfg->start_exec, cmd);
}

bool
server_set_idle_timeout(struct cfg *cfg, struct server *scfg, uint16_t timeout)
{
	if (!scfg || scfg->idle_timeout != 0)
		return false;

	scfg->idle_timeout = timeout;
	return true;
}

bool
server_set_port(struct cfg *cfg, struct server *scfg, uint16_t port)
{
	if (!scfg || scfg->announce_port != 0)
		return false;

	scfg->announce_port = htons(port);
	return true;
}

bool
server_set_type(struct cfg *cfg, struct server *scfg, enum server_type type)
{
	if (!scfg || scfg->type != SERVER_TYPE_UNDEFINED)
		return false;

	switch (type) {
	case SERVER_TYPE_ANNOUNCE:
		scfg->type = SERVER_TYPE_ANNOUNCE;
		break;
	case SERVER_TYPE_PROXY:
		scfg->type = SERVER_TYPE_PROXY;
		break;
	default:
		return false;
	}

	return true;
}

bool
server_set_pretty_name(struct cfg *cfg, struct server *scfg, const char *pretty_name)
{
	return set_property(cfg, scfg, &scfg->pretty_name, pretty_name);
}

struct server *
server_new(struct cfg *cfg, const char *name)
{
	struct server *scfg;

	list_for_each_entry(scfg, &cfg->servers, list) {
		if (strcmp(name, scfg->name))
			continue;
		debug(2, "Server already exists: %s\n", name);
		return scfg;
	}

	debug(2, "Would add server cfg: %s\n", name);
	scfg = zmalloc(sizeof(*scfg));
	if (!scfg) {
		error("malloc");
		return NULL;
	}

	scfg->type = SERVER_TYPE_UNDEFINED;
	scfg->name = strdup(name);
	scfg->running = false;
	scfg->stop_method = SERVER_STOP_METHOD_UNDEFINED;
	scfg->start_method = SERVER_START_METHOD_UNDEFINED;
	uring_task_init(&scfg->task, "scfg", uring_parent(cfg), server_free);
	uring_task_init(&scfg->exec_task, "exec", &scfg->task, server_exec_free);
	list_init(&scfg->remotes);
	list_init(&scfg->locals);
	list_init(&scfg->proxys);
	list_init(&scfg->rcons);
	memset(&scfg->mcast_iov, 0, sizeof(scfg->mcast_iov));
	scfg->mcast_iov.iov_base = scfg->mcast_buf;
	memset(&scfg->mcast_msg, 0, sizeof(scfg->mcast_msg));
	scfg->mcast_msg.msg_iov = &scfg->mcast_iov;
	scfg->mcast_msg.msg_iovlen = 1;
	scfg->idle_timeout = 0;
	list_add(&scfg->list, &cfg->servers);

	return scfg;
}