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