From 7bf3c0202135f5b725a1ae93f7c7d4f73567c398 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 6 Nov 2014 21:01:22 +0100 Subject: core: add nm_ip4_config_get_subnet_for_host() function And nm_ip6_config_get_subnet_for_host(). Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 26 ++++++++++++++++++++++++-- src/nm-ip4-config.h | 1 + src/nm-ip6-config.c | 31 +++++++++++++++++++++++++++++-- src/nm-ip6-config.h | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index b053f4fe86..313b4729b7 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -1196,12 +1196,12 @@ const NMPlatformIP4Route * nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); - int i; + guint i; NMPlatformIP4Route *best_route = NULL; g_return_val_if_fail (host, NULL); - for (i = 0; i < priv->routes->len; i++ ) { + for (i = 0; i < priv->routes->len; i++) { NMPlatformIP4Route *item = &g_array_index (priv->routes, NMPlatformIP4Route, i); if (item->gateway != 0) @@ -1222,6 +1222,28 @@ nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host return best_route; } +const NMPlatformIP4Address * +nm_ip4_config_get_subnet_for_host (const NMIP4Config *config, guint32 host) +{ + NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); + guint i; + NMPlatformIP4Address *subnet = NULL; + + g_return_val_if_fail (host, NULL); + + for (i = 0; i < priv->addresses->len; i++) { + NMPlatformIP4Address *item = &g_array_index (priv->addresses, NMPlatformIP4Address, i); + + if (subnet && subnet->plen >= item->plen) + continue; + if (nm_utils_ip4_address_clear_host_address (host, item->plen) != nm_utils_ip4_address_clear_host_address (item->address, item->plen)) + continue; + subnet = item; + } + + return subnet; +} + /******************************************************************/ void diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h index 48aaeaf01e..5bd7fa3ce9 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -93,6 +93,7 @@ guint32 nm_ip4_config_get_num_routes (const NMIP4Config *config); const NMPlatformIP4Route *nm_ip4_config_get_route (const NMIP4Config *config, guint32 i); const NMPlatformIP4Route *nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host); +const NMPlatformIP4Address *nm_ip4_config_get_subnet_for_host (const NMIP4Config *config, guint32 host); /* Nameservers */ void nm_ip4_config_reset_nameservers (NMIP4Config *config); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 1f27072716..98a4b01965 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -1203,13 +1203,13 @@ const NMPlatformIP6Route * nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct in6_addr *host) { NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config); - int i; + guint i; struct in6_addr network2, host2; NMPlatformIP6Route *best_route = NULL; g_return_val_if_fail (host && !IN6_IS_ADDR_UNSPECIFIED (host), NULL); - for (i = 0; i < priv->routes->len; i++ ) { + for (i = 0; i < priv->routes->len; i++) { NMPlatformIP6Route *item = &g_array_index (priv->routes, NMPlatformIP6Route, i); if (!IN6_IS_ADDR_UNSPECIFIED (&item->gateway)) @@ -1234,6 +1234,33 @@ nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct return best_route; } +const NMPlatformIP6Address * +nm_ip6_config_get_subnet_for_host (const NMIP6Config *config, const struct in6_addr *host) +{ + NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config); + guint i; + NMPlatformIP6Address *subnet = NULL; + struct in6_addr subnet2, host2; + + g_return_val_if_fail (host && !IN6_IS_ADDR_UNSPECIFIED (host), NULL); + + for (i = 0; i < priv->addresses->len; i++) { + NMPlatformIP6Address *item = &g_array_index (priv->addresses, NMPlatformIP6Address, i); + + if (subnet && subnet->plen >= item->plen) + continue; + + nm_utils_ip6_address_clear_host_address (&host2, host, item->plen); + nm_utils_ip6_address_clear_host_address (&subnet2, &item->address, item->plen); + + if (IN6_ARE_ADDR_EQUAL (&subnet2, &host2)) + subnet = item; + } + + return subnet; +} + + /******************************************************************/ void diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h index 7abf57a779..4036bfc3a1 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -94,6 +94,7 @@ guint32 nm_ip6_config_get_num_routes (const NMIP6Config *config); const NMPlatformIP6Route *nm_ip6_config_get_route (const NMIP6Config *config, guint32 i); const NMPlatformIP6Route *nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct in6_addr *host); +const NMPlatformIP6Address *nm_ip6_config_get_subnet_for_host (const NMIP6Config *config, const struct in6_addr *host); /* Nameservers */ void nm_ip6_config_reset_nameservers (NMIP6Config *config); -- cgit v1.2.1