unet-cli: strip initial newline in usage message
[project/unetd.git] / pex.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
4 */
5 #ifndef __UNETD_PEX_H
6 #define __UNETD_PEX_H
7
8 #include <sys/socket.h>
9 #include <libubox/uloop.h>
10 #include "stun.h"
11
12 #define NETWORK_PEX_HOSTS_LIMIT 128
13
14 struct network;
15
16 struct network_pex_host {
17 struct list_head list;
18 uint64_t timeout;
19 uint64_t last_active;
20 uint64_t last_ping;
21 union network_endpoint endpoint;
22 };
23
24 struct network_pex {
25 struct uloop_fd fd;
26 struct list_head hosts;
27 int num_hosts;
28 struct uloop_timeout request_update_timer;
29 };
30
31 enum network_stun_state {
32 STUN_STATE_IDLE,
33 STUN_STATE_PEX_QUERY_WAIT,
34 STUN_STATE_STUN_QUERY_SEND,
35 STUN_STATE_STUN_QUERY_WAIT,
36 };
37
38 struct network_stun_server {
39 struct list_head list;
40
41 struct avl_node pending_node;
42 struct stun_request req;
43
44 const char *host;
45 uint8_t seq;
46 bool req_auth_port;
47 bool pending;
48 };
49
50 struct network_stun {
51 struct list_head servers;
52 struct avl_tree pending;
53
54 struct uloop_timeout timer;
55
56 enum network_stun_state state;
57 bool wgport_disabled;
58
59 uint16_t auth_port_ext;
60 uint16_t port_local;
61 uint16_t port_ext;
62
63 int retry;
64
65 struct uloop_fd socket;
66 };
67
68 enum pex_event {
69 PEX_EV_HANDSHAKE,
70 PEX_EV_ENDPOINT_CHANGE,
71 PEX_EV_QUERY,
72 PEX_EV_PING,
73 };
74
75 void network_pex_init(struct network *net);
76 int network_pex_open(struct network *net);
77 void network_pex_close(struct network *net);
78 void network_pex_free(struct network *net);
79
80 void network_pex_event(struct network *net, struct network_peer *peer,
81 enum pex_event ev);
82 void network_pex_create_host(struct network *net, union network_endpoint *ep,
83 unsigned int timeout);
84
85 void network_stun_init(struct network *net);
86 void network_stun_free(struct network *net);
87 void network_stun_server_add(struct network *net, const char *host);
88 void network_stun_rx_packet(struct network *net, const void *data, size_t len);
89 void network_stun_update_port(struct network *net, bool auth, uint16_t val);
90 void network_stun_start(struct network *net);
91
92 static inline bool network_pex_active(struct network_pex *pex)
93 {
94 return pex->fd.fd >= 0;
95 }
96
97 int global_pex_open(const char *unix_path);
98
99 #endif