summaryrefslogtreecommitdiff
path: root/proxy.c
blob: e5a4effb8fa0db399569125c2cd4350ef8667d25 (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
#include "main.h"
#include "uring.h"
#include "server.h"
#include "proxy.h"
#include "utils.h"

void
proxy_refdump(struct server_proxy *proxy)
{
	uring_task_refdump(&proxy->clienttask);
	uring_task_refdump(&proxy->servertask);
}

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

	fprintf(stderr, "%s: %s\n", __func__, proxy->scfg->name);
	/*
	list_del(&proxy->list);
	free(proxy);
	*/
}

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

	fprintf(stderr, "%s: %s\n", __func__, proxy->scfg->name);
	/*
	list_del(&proxy->list);
	free(proxy);
	*/
}

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

	fprintf(stderr, "%s: result was %i\n", __func__, res);
	if (res <= 0)
		return;

	if (res != proxy->clientlen) {
		fprintf(stderr, "%s: short write\n", __func__);
		return;
	}

	uring_task_set_fd(&proxy->clienttask, proxy->cfd);
	uring_read(cfg, task, proxy->clientbuf, sizeof(proxy->clientbuf), 0, 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);

	fprintf(stderr, "%s: result was %i\n", __func__, res);
	if (res <= 0)
		return;

	proxy->clientlen = res;
	uring_task_set_fd(&proxy->clienttask, proxy->sfd);
	uring_write(cfg, task, proxy->clientbuf, res, 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);

	fprintf(stderr, "%s: result was %i\n", __func__, res);
	if (res <= 0)
		return;

	if (res != proxy->serverlen) {
		fprintf(stderr, "%s: short write\n", __func__);
		return;
	}

	uring_task_set_fd(&proxy->servertask, proxy->sfd);
	uring_read(cfg, task, proxy->serverbuf, sizeof(proxy->serverbuf), 0, 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);

	fprintf(stderr, "%s: result was %i\n", __func__, res);
	if (res <= 0)
		return;

	proxy->serverlen = res;
	uring_task_set_fd(&proxy->servertask, proxy->cfd);
	uring_write(cfg, task, proxy->serverbuf, res, proxy_server_data_out);
}

static void proxy_connect_next_remote(struct cfg *cfg, struct server_proxy *proxy);

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

	fprintf(stderr, "%s: connected %i\n", __func__, res);
	if (res < 0) {
		proxy_connect_next_remote(cfg, proxy);
		return;
	}

	uring_read(cfg, &proxy->clienttask, proxy->clientbuf, sizeof(proxy->clientbuf), 0, proxy_client_data_in);
	uring_read(cfg, &proxy->servertask, proxy->serverbuf, sizeof(proxy->serverbuf), 0, proxy_server_data_in);
}

static void
proxy_connect_next_remote(struct cfg *cfg, struct server_proxy *proxy)
{
	struct sockaddr_in46 *remote, *tmp;
	struct server *scfg = proxy->scfg;
	int sfd;
	unsigned i = 0;

again:
	remote = NULL;
	list_for_each_entry(tmp, &scfg->remotes, list) {
		if (i == proxy->next_remote) {
			remote = tmp;
			break;
		}
		i++;
	}

	if (!remote) {
		fprintf(stderr, "No more remote addresses to attempt\n");
		/* FIXME: put tasks */
		return;
	}

	proxy->next_remote++;
	proxy->server = *remote;
	sockaddr_to_str(&proxy->server, proxy->serverstr, sizeof(proxy->serverstr));
	fprintf(stderr, "%s: attempting proxy connection to %s (len %u)\n",
		scfg->name, proxy->serverstr, proxy->server.addrlen);

	sfd = socket(proxy->server.storage.ss_family, SOCK_STREAM, 0);
	if (sfd < 0) {
		perror("socket");
		goto again;
	}

	proxy->sfd = sfd;
	uring_task_set_fd(&proxy->servertask, sfd);
	uring_connect(cfg, &proxy->servertask, &proxy->server, proxy_server_connected);
}

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

	proxy = zmalloc(sizeof(*proxy));
	if (!proxy) {
		perror("malloc");
		return NULL;
	}

	proxy->sfd = -1;
	proxy->cfd = fd;
	proxy->scfg = scfg;
	proxy->client = *client;
	sockaddr_to_str(&proxy->client, proxy->clientstr, sizeof(proxy->clientstr));
	uring_task_init(&proxy->clienttask, "proxy_client", &scfg->task, proxy_client_free);
	uring_task_init(&proxy->servertask, "proxy_server", &scfg->task, proxy_server_free);
	uring_task_set_fd(&proxy->clienttask, fd);
	list_add(&proxy->list, &scfg->proxys);
	proxy_connect_next_remote(cfg, proxy);

	return proxy;
}