summaryrefslogtreecommitdiff
path: root/minecproxy/server-proxy.c
blob: 9d7b59e21e3776b6d3b16b0ef8bd2c554b99cbb1 (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
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <unistd.h>

#include "main.h"
#include "uring.h"
#include "ptimer.h"
#include "server.h"
#include "server-proxy.h"

static void format_bytes(char *buf, size_t len, uint64_t val)
{
	uint64_t tmp;
	const char *suffix = "B";

	assert_return(buf && len > 0);

	tmp = val * 10;
	if (val > 1152921504606846976ULL) {
		tmp = val / 115292150460684697ULL;
		suffix = "EiB";
	} else if (val > 1125899906842624ULL) {
		tmp /= 1125899906842624ULL;
		suffix = "PiB";
	} else if (val > 1099511627776ULL) {
		tmp /= 1099511627776ULL;
		suffix = "TiB";
	} else if (val > 1073741824ULL) {
		tmp /= 1073741824ULL;
		suffix = "GiB";
	} else if (val > 1048576) {
		tmp /= 1048576;
		suffix = "MiB";
	} else if (val > 1024) {
		tmp /= 1024;
		suffix = "KiB";
	}

	snprintf(buf, len, "%lu.%lu %s", tmp / 10, tmp % 10, suffix);
}

static void format_time(char *buf, size_t len, time_t diff)
{
	unsigned hh, mm, ss;

	assert_return(buf && len > 0);

	hh = diff / 3600;
	diff %= 3600;
	mm = diff / 60;
	diff %= 60;
	ss = diff;

	snprintf(buf, len, "%02u:%02u:%02u", hh, mm, ss);
}

static void proxy_free(struct uring_task *task)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, task);
	char cts[100];
	char stc[100];
	char duration[100];

	assert_return(task);

	debug(DBG_PROXY, "server: %s, src: %s, dst: %s", proxy->server->name,
	      proxy->client_conn.remote.addrstr,
	      proxy->server_conn.remote.addrstr);

	if (proxy->begin > 0) {
		format_time(duration, sizeof(duration),
			    time(NULL) - proxy->begin);
		format_bytes(cts, sizeof(cts), proxy->client_bytes);
		format_bytes(stc, sizeof(stc), proxy->server_bytes);

		info("%s: proxy connection %s -> %s closed "
		     "(CtS: %s, StC: %s), duration %s",
		     proxy->server->name, proxy->client_conn.remote.addrstr,
		     proxy->server_conn.remote.addrstr, cts, stc, duration);
	}

	list_del(&proxy->list);
	xfree(proxy);
}

static void proxy_client_free(struct uring_task *task)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, clienttask);

	assert_return(task);

	debug(DBG_PROXY, "%s: client connection closed", proxy->server->name);
}

static void proxy_server_free(struct uring_task *task)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, servertask);

	assert_return(task);

	debug(DBG_PROXY, "%s: server connection closed", proxy->server->name);
}

void proxy_delete(struct server_proxy *proxy)
{
	debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name,
	      proxy);

	assert_return(proxy);

	ptimer_del_task(&proxy->ptask);

	if (cfg->splice_supported) {
		uring_close(&proxy->server->task, proxy->cpipe[PIPE_RD]);
		uring_close(&proxy->server->task, proxy->cpipe[PIPE_WR]);
		uring_close(&proxy->server->task, proxy->spipe[PIPE_RD]);
		uring_close(&proxy->server->task, proxy->spipe[PIPE_WR]);
	}

	uring_task_set_fd(&proxy->servertask, proxy->sfd);
	uring_task_destroy(&proxy->servertask);
	uring_task_set_fd(&proxy->clienttask, proxy->cfd);
	uring_task_destroy(&proxy->clienttask);
	uring_task_destroy(&proxy->task);
}

/*
 * These four functions provide the fallback read-write mode
 */
static void proxy_client_read_in(struct uring_task *task, int res);

static void proxy_client_written_out(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, clienttask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	proxy->client_bytes += res;
	uring_task_set_fd(&proxy->clienttask, proxy->cfd);
	uring_tbuf_read(task, proxy_client_read_in);
}

static void proxy_client_read_in(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, clienttask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	uring_task_set_fd(&proxy->clienttask, proxy->sfd);
	uring_tbuf_write(task, proxy_client_written_out);
}

static void proxy_server_read_in(struct uring_task *task, int res);

static void proxy_server_written_out(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, servertask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	proxy->server_bytes += res;
	uring_task_set_fd(&proxy->servertask, proxy->sfd);
	uring_tbuf_read(&proxy->servertask, proxy_server_read_in);
}

static void proxy_server_read_in(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, servertask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	uring_task_set_fd(&proxy->servertask, proxy->cfd);
	uring_tbuf_write(task, proxy_server_written_out);
}

/*
 * These four functions provide the splice fd->pipe->fd mode
 */
static void proxy_client_spliced_in(struct uring_task *task, int res);

static void proxy_client_spliced_out(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, clienttask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	proxy->client_bytes += res;
	uring_splice(task, proxy->cfd, proxy->cpipe[PIPE_WR],
		     proxy_client_spliced_in);
}

static void proxy_client_spliced_in(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, clienttask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	uring_splice(task, proxy->cpipe[PIPE_RD], proxy->sfd,
		     proxy_client_spliced_out);
}

static void proxy_server_spliced_in(struct uring_task *task, int res);

static void proxy_server_spliced_out(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, servertask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	proxy->server_bytes += res;
	uring_splice(task, proxy->sfd, proxy->spipe[PIPE_WR],
		     proxy_server_spliced_in);
}

static void proxy_server_spliced_in(struct uring_task *task, int res)
{
	struct server_proxy *proxy =
		container_of(task, struct server_proxy, servertask);

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	if (res <= 0) {
		debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res);
		proxy_delete(proxy);
		return;
	}

	uring_splice(task, proxy->spipe[PIPE_RD], proxy->cfd,
		     proxy_server_spliced_out);
}

static void proxy_connected_cb(struct connection *conn, bool connected)
{
	struct server_proxy *proxy =
		container_of(conn, struct server_proxy, server_conn);

	assert_return(conn);
	assert_task_alive(DBG_PROXY, &proxy->clienttask);
	assert_task_alive(DBG_PROXY, &proxy->servertask);

	proxy->connecting = false;
	if (!connected) {
		error("%s: proxy connection to remote server failed",
		      proxy->server->name);
		if (!proxy->ptask.active)
			proxy_delete(proxy);
		return;
	}

	ptimer_del_task(&proxy->ptask);

	proxy->sfd = proxy->servertask.fd;
	verbose("%s: proxy connection %s -> %s opened", proxy->server->name,
		proxy->client_conn.remote.addrstr,
		proxy->server_conn.remote.addrstr);
	proxy->begin = time(NULL);

	if (cfg->splice_supported) {
		debug(DBG_PROXY, "handling proxy connection with splice");
		uring_splice(&proxy->clienttask, proxy->cfd,
			     proxy->cpipe[PIPE_WR], proxy_client_spliced_in);
		uring_splice(&proxy->servertask, proxy->sfd,
			     proxy->spipe[PIPE_WR], proxy_server_spliced_in);
	} else {
		debug(DBG_PROXY, "handling proxy connection with read-write");
		uring_tbuf_read(&proxy->clienttask, proxy_client_read_in);
		uring_tbuf_read(&proxy->servertask, proxy_server_read_in);
	}
}

void proxy_refdump(struct server_proxy *proxy)
{
	assert_return(proxy);

	uring_task_refdump(&proxy->task);
	uring_task_refdump(&proxy->clienttask);
	uring_task_refdump(&proxy->servertask);
}

static void proxy_connect_timer_cb(struct ptimer_task *ptask)
{
	struct server_proxy *proxy =
		container_of(ptask, struct server_proxy, ptask);

	assert_return(ptask);

	if (proxy->connecting)
		return;

	proxy->connecting = true;
	connect_any(&proxy->servertask, &proxy->server->scfg.remotes,
		    &proxy->server_conn, proxy_connected_cb);
}

struct server_proxy *proxy_new(struct server *server, struct saddr *client,
			       int fd)
{
	struct server_proxy *proxy;

	assert_return(server && client && fd > 0, NULL);

	proxy = zmalloc(sizeof(*proxy));
	if (!proxy) {
		error("malloc: %m");
		goto out;
	}

	if (cfg->splice_supported) {
		if (pipe2(proxy->cpipe, O_CLOEXEC) < 0) {
			error("pipe2: %m");
			goto out_free;
		}

		if (pipe2(proxy->spipe, O_CLOEXEC) < 0) {
			error("pipe2: %m");
			goto out_close_cpipe;
		}
	}

	proxy->sfd = -1;
	proxy->cfd = fd;
	proxy->server = server;
	uring_task_init(&proxy->task, "proxy", &server->task, proxy_free);

	connection_set_local(&proxy->client_conn, fd);
	connection_set_remote(&proxy->client_conn, client);

	uring_task_init(&proxy->clienttask, "proxy_client", &proxy->task,
			proxy_client_free);
	uring_task_set_buf(&proxy->clienttask, &proxy->clientbuf);
	uring_task_set_fd(&proxy->clienttask, fd);

	uring_task_init(&proxy->servertask, "proxy_server", &proxy->task,
			proxy_server_free);
	uring_task_set_buf(&proxy->servertask, &proxy->serverbuf);

	list_add(&proxy->list, &server->proxys);

	if (server->state != SERVER_STATE_RUNNING) {
		if (server_start(server) &&
		    cfg->proxy_connection_interval > 0 &&
		    cfg->proxy_connection_attempts > 0) {
			ptask_init(&proxy->ptask,
				   cfg->proxy_connection_interval,
				   cfg->proxy_connection_attempts,
				   proxy_connect_timer_cb);
			ptimer_add_task(&proxy->ptask);
		}
	}

	proxy->connecting = true;
	connect_any(&proxy->servertask, &server->scfg.remotes,
		    &proxy->server_conn, proxy_connected_cb);

	return proxy;

out_close_cpipe:
	uring_close(&server->task, proxy->cpipe[PIPE_RD]);
	uring_close(&server->task, proxy->cpipe[PIPE_WR]);
out_free:
	xfree(proxy);
out:
	return NULL;
}

static void local_accept(struct uring_task *task, int res)
{
	struct server_local *local =
		container_of(task, struct server_local, task);
	struct server *server = container_of(task->parent, struct server, task);
	struct server_proxy *proxy;

	assert_return(task);
	assert_task_alive(DBG_PROXY, task);

	debug(DBG_PROXY, "task %p, res %i, server %s", task, res, server->name);

	if (res < 0) {
		error("res: %i", res);
		goto out;
	}

	saddr_set_addrstr(&local->client);

	verbose("%s: incoming proxy connection: %s -> %s", server->name,
		local->client.addrstr, local->local.addrstr);

	if (list_empty(&server->scfg.remotes)) {
		/* This shouldn't be possible, checked before opening local */
		error("server->scfg.remotes empty!");
		uring_close(&local->task, res);
		goto out;
	}

	proxy = proxy_new(server, &local->client, res);
	if (!proxy)
		uring_close(&local->task, res);

out:
	uring_accept(&local->task, &local->client, local_accept);
}

bool local_open(struct server_local *local)
{
	int sfd;
	int option;
	int r;

	assert_return(local && local->server, false);

	sfd = socket(local->local.st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
	if (sfd < 0) {
		error("socket: %m");
		goto error;
	}

	option = true;
	if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) <
	    0) {
		error("setsockopt: %m");
		goto error;
	}

	/* The MC protocol expects the client to send data first */
	if (cfg->socket_defer) {
		option = true;
		if (setsockopt(sfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &option,
			       sizeof(option)) < 0)
			error("setsockopt: %m");
	}

	/*
	 * This has the advantage that interfaces don't need to be up but
	 * it means that cfg errors will not be caught.
	 */
	if (cfg->socket_freebind) {
		option = true;
		if (setsockopt(sfd, IPPROTO_IP, IP_FREEBIND, &option,
			       sizeof(option)) < 0)
			error("setsockopt: %m");
	}

	socket_set_low_latency(sfd, cfg->socket_keepalive, cfg->socket_iptos,
			       cfg->socket_nodelay);

	r = bind(sfd, (struct sockaddr *)&local->local.st,
		 local->local.addrlen);
	if (r < 0) {
		error("bind: %m");
		goto error;
	}

	r = listen(sfd, 100);
	if (r < 0) {
		error("listen: %m");
		goto error;
	}

	uring_task_set_fd(&local->task, sfd);
	uring_accept(&local->task, &local->client, local_accept);
	return true;

error:
	if (sfd >= 0)
		uring_close(&local->task, sfd);
	return false;
}

void local_refdump(struct server_local *local)
{
	assert_return(local);

	uring_task_refdump(&local->task);
}

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

	assert_return(task);

	debug(DBG_PROXY, "task %p, local %p", task, local);
	list_del(&local->list);
	xfree(local);
}

void local_delete(struct server_local *local)
{
	assert_return(local);

	uring_task_destroy(&local->task);
}

struct server_local *local_new(struct server *server, struct saddr *saddr)
{
	struct server_local *local;

	assert_return(server && saddr, NULL);

	local = zmalloc(sizeof(*local));
	if (!local) {
		error("malloc: %m");
		xfree(saddr);
		return NULL;
	}

	debug(DBG_PROXY, "%s adding local: %s", server->name, saddr->addrstr);
	local->local = *saddr;
	local->server = server;
	uring_task_init(&local->task, "local", &server->task, local_free);
	xfree(saddr);
	return local;
}