host: keep multiple endpoint candidates, one for each type
[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 enum peer_endpoint_type {
9 ENDPOINT_TYPE_STATIC,
10 ENDPOINT_TYPE_PEX,
11 ENDPOINT_TYPE_ENDPOINT_NOTIFY,
12 ENDPOINT_TYPE_ENDPOINT_PORT_NOTIFY,
13 __ENDPOINT_TYPE_MAX,
14 };
15
16 struct network_peer {
17 struct vlist_node node;
18 uint8_t key[CURVE25519_KEY_SIZE];
19 union network_addr local_addr;
20 const char *endpoint;
21 struct blob_attr *ipaddr;
22 struct blob_attr *subnet;
23 int port;
24 int pex_port;
25 bool dynamic;
26
27 struct {
28 int connect_attempt;
29 bool connected;
30 bool handshake;
31 bool has_local_ep_addr;
32 bool pinged;
33 union network_addr local_ep_addr;
34 union network_endpoint endpoint;
35
36 uint8_t next_endpoint_idx;
37 union network_endpoint next_endpoint[__ENDPOINT_TYPE_MAX];
38 uint64_t last_ep_update;
39
40 uint64_t rx_bytes;
41 uint64_t last_handshake;
42 uint64_t last_request;
43 int idle;
44 int num_net_queries;
45 } state;
46 };
47
48 struct network_dynamic_peer {
49 struct list_head list;
50
51 struct network_peer peer;
52 };
53
54 struct network_host {
55 struct avl_node node;
56
57 const char *gateway;
58 struct network_peer peer;
59 };
60
61 struct network_group {
62 struct avl_node node;
63 const char *name;
64
65 int n_members;
66 struct network_host **members;
67 };
68
69 static inline const char *network_host_name(struct network_host *host)
70 {
71 if (!host)
72 return "(none)";
73
74 return host->node.key;
75 }
76
77 static inline bool network_host_is_peer(struct network_host *host)
78 {
79 return !!host->peer.node.avl.key;
80 }
81
82 static inline const char *network_peer_name(struct network_peer *peer)
83 {
84 struct network_host *host;
85
86 if (!peer || peer->dynamic)
87 return "(none)";
88
89 host = container_of(peer, struct network_host, peer);
90 return network_host_name(host);
91 }
92
93
94 static inline bool
95 network_host_uses_peer_route(struct network_host *host, struct network *net,
96 struct network_peer *peer)
97 {
98 if (&host->peer == peer || host == net->net_config.local_host)
99 return false;
100
101 if (net->net_config.local_host->gateway &&
102 !strcmp(net->net_config.local_host->gateway, network_peer_name(peer)))
103 return true;
104
105 if (!host->gateway)
106 return false;
107
108 return !strcmp(host->gateway, network_peer_name(peer));
109 }
110
111 #define for_each_routed_host(cur_host, net, peer) \
112 avl_for_each_element(&(net)->hosts, cur_host, node) \
113 if (network_host_uses_peer_route(host, net, peer))
114
115
116 void network_hosts_update_start(struct network *net);
117 void network_hosts_update_done(struct network *net);
118 void network_hosts_add(struct network *net, struct blob_attr *hosts);
119 void network_hosts_reload_dynamic_peers(struct network *net);
120
121 void network_hosts_init(struct network *net);
122 void network_hosts_free(struct network *net);
123
124 #endif