summaryrefslogtreecommitdiffstats
path: root/pex.h
blob: f0173a65ca3a32a729e290053e816560263e91b3 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
 */
#ifndef __UNETD_PEX_H
#define __UNETD_PEX_H

#include <sys/socket.h>
#include <libubox/uloop.h>
#include "stun.h"

#define NETWORK_PEX_HOSTS_LIMIT	128

struct network;

struct network_pex_host {
	struct list_head list;
	uint64_t timeout;
	uint64_t last_active;
	uint64_t last_ping;
	bool interface;
	union network_endpoint endpoint;
};

struct network_pex {
	struct uloop_fd fd;
	struct list_head hosts;
	int num_hosts;
	struct uloop_timeout request_update_timer;
};

enum network_stun_state {
	STUN_STATE_IDLE,
	STUN_STATE_PEX_QUERY_WAIT,
	STUN_STATE_STUN_QUERY_SEND,
	STUN_STATE_STUN_QUERY_WAIT,
};

struct network_stun_server {
	struct list_head list;

	struct avl_node pending_node;
	struct stun_request req;

	const char *host;
	uint8_t seq;
	bool req_auth_port;
	bool pending;
};

struct network_stun {
	struct list_head servers;
	struct avl_tree pending;

	struct uloop_timeout timer;

	enum network_stun_state state;
	bool wgport_disabled;

	uint16_t auth_port_ext;
	uint16_t port_local;
	uint16_t port_ext;

	int retry;

	struct uloop_fd socket;
};

enum pex_event {
	PEX_EV_HANDSHAKE,
	PEX_EV_ENDPOINT_CHANGE,
	PEX_EV_QUERY,
	PEX_EV_PING,
};

void network_pex_init(struct network *net);
int network_pex_open(struct network *net);
void network_pex_close(struct network *net);
void network_pex_free(struct network *net);
void network_pex_reload();

void network_pex_event(struct network *net, struct network_peer *peer,
		       enum pex_event ev);
struct network_pex_host *
network_pex_create_host(struct network *net, union network_endpoint *ep,
			unsigned int timeout);

void network_stun_init(struct network *net);
void network_stun_free(struct network *net);
void network_stun_server_add(struct network *net, const char *host);
void network_stun_rx_packet(struct network *net, const void *data, size_t len);
void network_stun_update_port(struct network *net, bool auth, uint16_t val);
void network_stun_start(struct network *net);

static inline bool network_pex_active(struct network_pex *pex)
{
	return pex->fd.fd >= 0;
}

int global_pex_open(const char *unix_path);

#endif