summaryrefslogtreecommitdiff
path: root/proxy.c
blob: 00090b109b8e0efea87d2fc104c6c159dd995f23 (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
#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->task);
}

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

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

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

	fprintf(stderr, "%s: connected %i\n", __func__, res);
	return;
}

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

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

	remote = list_first_entry(&scfg->remotes, struct sockaddr_in46, list);
	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");
		uring_close(cfg, NULL, fd, NULL);
		free(proxy);
		return NULL;
	}

	proxy->client = *client;
	sockaddr_to_str(&proxy->client, proxy->clientstr, sizeof(proxy->clientstr));
	uring_task_init(&proxy->task, "server_proxy", &scfg->task, proxy_free);
	uring_task_set_fd(&proxy->task, sfd);
	list_add(&proxy->list, &scfg->proxys);
	uring_connect(cfg, &proxy->task, &proxy->server, proxy_connected);

	return proxy;
}