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