host: deal with host/peer null pointers in debug messages
[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 struct network_peer peer;
40 };
41
42 struct network_group {
43 struct avl_node node;
44 const char *name;
45
46 int n_members;
47 struct network_host **members;
48 };
49
50 static inline const char *network_host_name(struct network_host *host)
51 {
52 if (!host)
53 return "(none)";
54
55 return host->node.key;
56 }
57
58 static inline const char *network_peer_name(struct network_peer *peer)
59 {
60 struct network_host *host;
61
62 if (!peer)
63 return "(none)";
64
65 host = container_of(peer, struct network_host, peer);
66 return network_host_name(host);
67 }
68
69 void network_hosts_update_start(struct network *net);
70 void network_hosts_update_done(struct network *net);
71 void network_hosts_add(struct network *net, struct blob_attr *hosts);
72
73 void network_hosts_init(struct network *net);
74 void network_hosts_free(struct network *net);
75
76 #endif