add support for overriding peer-exchange-port for individual hosts
[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
18 struct {
19 int connect_attempt;
20 bool connected;
21 bool handshake;
22 bool has_local_ep_addr;
23 union network_addr local_ep_addr;
24 union network_endpoint endpoint;
25
26 union network_endpoint next_endpoint;
27 uint64_t last_ep_update;
28
29 uint64_t rx_bytes;
30 uint64_t last_handshake;
31 uint64_t last_request;
32 int idle;
33 int num_net_queries;
34 } state;
35 };
36
37 struct network_host {
38 struct avl_node node;
39
40 const char *gateway;
41 struct network_peer peer;
42 };
43
44 struct network_group {
45 struct avl_node node;
46 const char *name;
47
48 int n_members;
49 struct network_host **members;
50 };
51
52 static inline const char *network_host_name(struct network_host *host)
53 {
54 if (!host)
55 return "(none)";
56
57 return host->node.key;
58 }
59
60 static inline bool network_host_is_peer(struct network_host *host)
61 {
62 return !!host->peer.node.avl.key;
63 }
64
65 static inline const char *network_peer_name(struct network_peer *peer)
66 {
67 struct network_host *host;
68
69 if (!peer)
70 return "(none)";
71
72 host = container_of(peer, struct network_host, peer);
73 return network_host_name(host);
74 }
75
76
77 static inline bool
78 network_host_uses_peer_route(struct network_host *host, struct network *net,
79 struct network_peer *peer)
80 {
81 if (&host->peer == peer || host == net->net_config.local_host)
82 return false;
83
84 if (net->net_config.local_host->gateway &&
85 !strcmp(net->net_config.local_host->gateway, network_peer_name(peer)))
86 return true;
87
88 if (!host->gateway)
89 return false;
90
91 return !strcmp(host->gateway, network_peer_name(peer));
92 }
93
94 #define for_each_routed_host(cur_host, net, peer) \
95 avl_for_each_element(&(net)->hosts, cur_host, node) \
96 if (network_host_uses_peer_route(host, net, peer))
97
98
99 void network_hosts_update_start(struct network *net);
100 void network_hosts_update_done(struct network *net);
101 void network_hosts_add(struct network *net, struct blob_attr *hosts);
102
103 void network_hosts_init(struct network *net);
104 void network_hosts_free(struct network *net);
105
106 #endif