pex: keep active pex hosts after the specified timeout
[project/unetd.git] / host.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
4 */
5 #ifndef __UNETD_HOST_H
6 #define __UNETD_HOST_H
7
8 struct network_peer {
9 struct vlist_node node;
10 uint8_t key[CURVE25519_KEY_SIZE];
11 union network_addr local_addr;
12 const char *endpoint;
13 struct blob_attr *ipaddr;
14 struct blob_attr *subnet;
15 int port;
16 int pex_port;
17 bool dynamic;
18
19 struct {
20 int connect_attempt;
21 bool connected;
22 bool handshake;
23 bool has_local_ep_addr;
24 bool pinged;
25 union network_addr local_ep_addr;
26 union network_endpoint endpoint;
27
28 union network_endpoint next_endpoint;
29 uint64_t last_ep_update;
30
31 uint64_t rx_bytes;
32 uint64_t last_handshake;
33 uint64_t last_request;
34 int idle;
35 int num_net_queries;
36 } state;
37 };
38
39 struct network_dynamic_peer {
40 struct list_head list;
41
42 struct network_peer peer;
43 };
44
45 struct network_host {
46 struct avl_node node;
47
48 const char *gateway;
49 struct network_peer peer;
50 };
51
52 struct network_group {
53 struct avl_node node;
54 const char *name;
55
56 int n_members;
57 struct network_host **members;
58 };
59
60 static inline const char *network_host_name(struct network_host *host)
61 {
62 if (!host)
63 return "(none)";
64
65 return host->node.key;
66 }
67
68 static inline bool network_host_is_peer(struct network_host *host)
69 {
70 return !!host->peer.node.avl.key;
71 }
72
73 static inline const char *network_peer_name(struct network_peer *peer)
74 {
75 struct network_host *host;
76
77 if (!peer || peer->dynamic)
78 return "(none)";
79
80 host = container_of(peer, struct network_host, peer);
81 return network_host_name(host);
82 }
83
84
85 static inline bool
86 network_host_uses_peer_route(struct network_host *host, struct network *net,
87 struct network_peer *peer)
88 {
89 if (&host->peer == peer || host == net->net_config.local_host)
90 return false;
91
92 if (net->net_config.local_host->gateway &&
93 !strcmp(net->net_config.local_host->gateway, network_peer_name(peer)))
94 return true;
95
96 if (!host->gateway)
97 return false;
98
99 return !strcmp(host->gateway, network_peer_name(peer));
100 }
101
102 #define for_each_routed_host(cur_host, net, peer) \
103 avl_for_each_element(&(net)->hosts, cur_host, node) \
104 if (network_host_uses_peer_route(host, net, peer))
105
106
107 void network_hosts_update_start(struct network *net);
108 void network_hosts_update_done(struct network *net);
109 void network_hosts_add(struct network *net, struct blob_attr *hosts);
110 void network_hosts_reload_dynamic_peers(struct network *net);
111
112 void network_hosts_init(struct network *net);
113 void network_hosts_free(struct network *net);
114
115 #endif