diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index adc2780615..b9f2ea7f0d 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -1210,12 +1210,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) @@ -1236,6 +1236,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 a7e88d97a0..afdd8d09a2 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -97,6 +97,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 9959268e30..2899b722f6 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -1217,13 +1217,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)) @@ -1248,6 +1248,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 fc908b6180..f1d2dc8dd9 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -99,6 +99,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);