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