summaryrefslogtreecommitdiff
path: root/proxy.c
blob: 05117fd40347ff6a001b84eff0c7df3e1128c6a7 (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
#include <stdio.h>
#include <time.h>

#include "main.h"
#include "uring.h"
#include "server.h"
#include "proxy.h"
#include "utils.h"

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
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 cfg *cfg, struct server_proxy *proxy)
{
	debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name, proxy);

	assert_return(cfg && proxy);

	uring_task_destroy(cfg, &proxy->servertask);
	uring_task_destroy(cfg, &proxy->clienttask);
	uring_task_destroy(cfg, &proxy->task);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	if (!connected) {
		error("%s: proxy connection to remote server failed",
		      proxy->server->name);
		proxy_delete(cfg, proxy);
		return;
	}

	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);

	uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in);
	uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in);
}

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

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

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

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

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

	uring_task_init(cfg, &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(cfg, &proxy->servertask, "proxy_server", &proxy->task,
			proxy_server_free);
	uring_task_set_buf(&proxy->servertask, &proxy->serverbuf);

	list_add(&proxy->list, &server->proxys);
	connect_any(cfg, &proxy->servertask, &server->remotes,
		    &proxy->server_conn, proxy_connected_cb);

	return proxy;
}