ubus: add reload command
[project/unetd.git] / host.h
diff --git a/host.h b/host.h
index 76c52de8db6ead245967276578b45c1988261cd8..040c292f42fa7f086ce24b0cab64efea2609c8d3 100644 (file)
--- a/host.h
+++ b/host.h
@@ -13,6 +13,8 @@ struct network_peer {
        struct blob_attr *ipaddr;
        struct blob_attr *subnet;
        int port;
+       int pex_port;
+       bool dynamic;
 
        struct {
                int connect_attempt;
@@ -29,12 +31,20 @@ struct network_peer {
                uint64_t last_handshake;
                uint64_t last_request;
                int idle;
+               int num_net_queries;
        } state;
 };
 
+struct network_dynamic_peer {
+       struct list_head list;
+
+       struct network_peer peer;
+};
+
 struct network_host {
        struct avl_node node;
 
+       const char *gateway;
        struct network_peer peer;
 };
 
@@ -48,19 +58,55 @@ struct network_group {
 
 static inline const char *network_host_name(struct network_host *host)
 {
+       if (!host)
+               return "(none)";
+
        return host->node.key;
 }
 
+static inline bool network_host_is_peer(struct network_host *host)
+{
+       return !!host->peer.node.avl.key;
+}
+
 static inline const char *network_peer_name(struct network_peer *peer)
 {
-       struct network_host *host = container_of(peer, struct network_host, peer);
+       struct network_host *host;
+
+       if (!peer || peer->dynamic)
+               return "(none)";
 
+       host = container_of(peer, struct network_host, peer);
        return network_host_name(host);
 }
 
+
+static inline bool
+network_host_uses_peer_route(struct network_host *host, struct network *net,
+                           struct network_peer *peer)
+{
+       if (&host->peer == peer || host == net->net_config.local_host)
+               return false;
+
+       if (net->net_config.local_host->gateway &&
+           !strcmp(net->net_config.local_host->gateway, network_peer_name(peer)))
+               return true;
+
+       if (!host->gateway)
+               return false;
+
+       return !strcmp(host->gateway, network_peer_name(peer));
+}
+
+#define for_each_routed_host(cur_host, net, peer)                      \
+       avl_for_each_element(&(net)->hosts, cur_host, node)             \
+               if (network_host_uses_peer_route(host, net, peer))
+
+
 void network_hosts_update_start(struct network *net);
 void network_hosts_update_done(struct network *net);
 void network_hosts_add(struct network *net, struct blob_attr *hosts);
+void network_hosts_reload_dynamic_peers(struct network *net);
 
 void network_hosts_init(struct network *net);
 void network_hosts_free(struct network *net);