network: add support for specifying a host gateway
[project/unetd.git] / host.h
diff --git a/host.h b/host.h
index 985ac4569e8f4361cbe8a4da7267e85840cf75fa..b802d776c7088c63c0f99b92cacada22b620b13f 100644 (file)
--- a/host.h
+++ b/host.h
@@ -36,6 +36,7 @@ struct network_peer {
 struct network_host {
        struct avl_node node;
 
+       const char *gateway;
        struct network_peer peer;
 };
 
@@ -55,6 +56,11 @@ static inline const char *network_host_name(struct network_host *host)
        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;
@@ -66,6 +72,29 @@ static inline const char *network_peer_name(struct network_peer *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);