platform: simplify getting routes and ignoring the default route

Most places except the tests don't want the default route when asking
the platform for all routes, so make that simpler by just adding a
parameter for including the default route or not.
This commit is contained in:
Dan Williams
2013-08-02 23:49:34 -05:00
parent c936f61a43
commit 7570832b20
10 changed files with 38 additions and 52 deletions

View File

@@ -123,15 +123,9 @@ nm_ip4_config_capture (int ifindex)
return NULL;
}
routes_array = nm_platform_ip4_route_get_all (ifindex);
routes_array = nm_platform_ip4_route_get_all (ifindex, FALSE);
routes = (NMPlatformIP4Route *)routes_array->data;
for (i = 0; i < routes_array->len; i++) {
/* Default route ignored; it's handled internally by NM and not
* tracked in the device's IP config.
*/
if (routes[i].plen == 0)
continue;
route = nm_ip4_route_new ();
nm_ip4_route_set_dest (route, routes[i].network);
nm_ip4_route_set_prefix (route, routes[i].plen);

View File

@@ -125,15 +125,9 @@ nm_ip6_config_capture (int ifindex)
return NULL;
}
routes_array = nm_platform_ip6_route_get_all (ifindex);
routes_array = nm_platform_ip6_route_get_all (ifindex, FALSE);
routes = (NMPlatformIP6Route *)routes_array->data;
for (i = 0; i < routes_array->len; i++) {
/* Default route ignored; it's handled internally by NM and not
* tracked in the device's IP config.
*/
if (routes[i].plen == 0)
continue;
route = nm_ip6_route_new ();
nm_ip6_route_set_dest (route, &routes[i].network);
nm_ip6_route_set_prefix (route, routes[i].plen);

View File

@@ -882,7 +882,7 @@ ip6_address_exists (NMPlatform *platform, int ifindex, struct in6_addr addr, int
/******************************************************************/
static GArray *
ip4_route_get_all (NMPlatform *platform, int ifindex)
ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
@@ -901,15 +901,17 @@ ip4_route_get_all (NMPlatform *platform, int ifindex)
/* Fill routes */
for (i = 0; i < priv->ip4_routes->len; i++) {
route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i);
if (route && route->ifindex == ifindex)
if (route && route->ifindex == ifindex) {
if (route->plen != 0 || include_default)
g_array_append_val (routes, *route);
}
}
return routes;
}
static GArray *
ip6_route_get_all (NMPlatform *platform, int ifindex)
ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
@@ -928,9 +930,11 @@ ip6_route_get_all (NMPlatform *platform, int ifindex)
/* Fill routes */
for (i = 0; i < priv->ip6_routes->len; i++) {
route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i);
if (route && route->ifindex == ifindex)
if (route && route->ifindex == ifindex) {
if (route->plen != 0 || include_default)
g_array_append_val (routes, *route);
}
}
return routes;
}

View File

@@ -2155,7 +2155,7 @@ ip_route_mark_all (NMPlatform *platform, int family, int ifindex)
}
static GArray *
ip4_route_get_all (NMPlatform *platform, int ifindex)
ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
@@ -2169,6 +2169,7 @@ ip4_route_get_all (NMPlatform *platform, int ifindex)
for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
if (nl_object_is_marked (object)) {
init_ip4_route (&route, (struct rtnl_route *) object);
if (route.plen != 0 || include_default)
g_array_append_val (routes, route);
nl_object_unmark (object);
}
@@ -2178,7 +2179,7 @@ ip4_route_get_all (NMPlatform *platform, int ifindex)
}
static GArray *
ip6_route_get_all (NMPlatform *platform, int ifindex)
ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
@@ -2192,6 +2193,7 @@ ip6_route_get_all (NMPlatform *platform, int ifindex)
for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
if (nl_object_is_marked (object)) {
init_ip6_route (&route, (struct rtnl_route *) object);
if (route.plen != 0 || include_default)
g_array_append_val (routes, route);
nl_object_unmark (object);
}

View File

@@ -1326,25 +1326,25 @@ nm_platform_address_flush (int ifindex)
/******************************************************************/
GArray *
nm_platform_ip4_route_get_all (int ifindex)
nm_platform_ip4_route_get_all (int ifindex, gboolean include_default)
{
reset_error ();
g_return_val_if_fail (ifindex > 0, NULL);
g_return_val_if_fail (klass->ip4_route_get_all, NULL);
return klass->ip4_route_get_all (platform, ifindex);
return klass->ip4_route_get_all (platform, ifindex, include_default);
}
GArray *
nm_platform_ip6_route_get_all (int ifindex)
nm_platform_ip6_route_get_all (int ifindex, gboolean include_default)
{
reset_error ();
g_return_val_if_fail (ifindex > 0, NULL);
g_return_val_if_fail (klass->ip6_route_get_all, NULL);
return klass->ip6_route_get_all (platform, ifindex);
return klass->ip6_route_get_all (platform, ifindex, include_default);
}
gboolean
@@ -1485,15 +1485,11 @@ nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes)
int i;
/* Delete unknown routes */
routes = nm_platform_ip4_route_get_all (ifindex);
routes = nm_platform_ip4_route_get_all (ifindex, FALSE);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP4Route, i);
route->ifindex = 0;
/* Ignore default route */
if (!route->plen)
continue;
if (!array_contains_ip4_route (known_routes, route))
nm_platform_ip4_route_delete (ifindex, route->network, route->plen, route->metric);
}
@@ -1535,15 +1531,11 @@ nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes)
int i;
/* Delete unknown routes */
routes = nm_platform_ip6_route_get_all (ifindex);
routes = nm_platform_ip6_route_get_all (ifindex, FALSE);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP6Route, i);
route->ifindex = 0;
/* Ignore default route */
if (!route->plen)
continue;
if (!array_contains_ip6_route (known_routes, route))
nm_platform_ip6_route_delete (ifindex, route->network, route->plen, route->metric);
}

View File

@@ -282,8 +282,8 @@ typedef struct {
gboolean (*ip4_address_exists) (NMPlatform *, int ifindex, in_addr_t address, int plen);
gboolean (*ip6_address_exists) (NMPlatform *, int ifindex, struct in6_addr address, int plen);
GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex);
GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex);
GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex, gboolean include_default);
GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, gboolean include_default);
gboolean (*ip4_route_add) (NMPlatform *, int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int prio, int mss);
gboolean (*ip6_route_add) (NMPlatform *, int ifindex,
@@ -404,8 +404,8 @@ gboolean nm_platform_ip4_address_sync (int ifindex, const GArray *known_addresse
gboolean nm_platform_ip6_address_sync (int ifindex, const GArray *known_addresses);
gboolean nm_platform_address_flush (int ifindex);
GArray *nm_platform_ip4_route_get_all (int ifindex);
GArray *nm_platform_ip6_route_get_all (int ifindex);
GArray *nm_platform_ip4_route_get_all (int ifindex, gboolean include_default);
GArray *nm_platform_ip6_route_get_all (int ifindex, gboolean include_default);
gboolean nm_platform_route_set_metric (int ifindex, int metric);
gboolean nm_platform_ip4_route_add (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric, int mss);

View File

@@ -85,8 +85,8 @@ dump_interface (NMPlatformLink *link)
g_array_unref (ip4_addresses);
g_array_unref (ip6_addresses);
ip4_routes = nm_platform_ip4_route_get_all (link->ifindex);
ip6_routes = nm_platform_ip6_route_get_all (link->ifindex);
ip4_routes = nm_platform_ip4_route_get_all (link->ifindex, TRUE);
ip6_routes = nm_platform_ip6_route_get_all (link->ifindex, TRUE);
g_assert (ip4_routes);
g_assert (ip6_routes);

View File

@@ -578,7 +578,7 @@ do_ip4_route_get_all (char **argv)
int i;
if (ifindex) {
routes = nm_platform_ip4_route_get_all (ifindex);
routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP4Route, i);
inet_ntop (AF_INET, &route->network, networkstr, sizeof (networkstr));
@@ -602,7 +602,7 @@ do_ip6_route_get_all (char **argv)
int i;
if (ifindex) {
routes = nm_platform_ip6_route_get_all (ifindex);
routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP6Route, i);
inet_ntop (AF_INET6, &route->network, networkstr, sizeof (networkstr));

View File

@@ -51,8 +51,8 @@ test_cleanup_internal ()
addresses4 = nm_platform_ip4_address_get_all (ifindex);
addresses6 = nm_platform_ip6_address_get_all (ifindex);
routes4 = nm_platform_ip4_route_get_all (ifindex);
routes6 = nm_platform_ip6_route_get_all (ifindex);
routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE);
routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE);
g_assert_cmpint (addresses4->len, ==, 1);
g_assert_cmpint (addresses6->len, ==, 1);
@@ -69,8 +69,8 @@ test_cleanup_internal ()
addresses4 = nm_platform_ip4_address_get_all (ifindex);
addresses6 = nm_platform_ip6_address_get_all (ifindex);
routes4 = nm_platform_ip4_route_get_all (ifindex);
routes6 = nm_platform_ip6_route_get_all (ifindex);
routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE);
routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE);
g_assert_cmpint (addresses4->len, ==, 0);
g_assert_cmpint (addresses6->len, ==, 0);

View File

@@ -83,7 +83,7 @@ test_ip4_route ()
accept_signal (route_changed);
/* Test route listing */
routes = nm_platform_ip4_route_get_all (ifindex);
routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
memset (rts, 0, sizeof (rts));
rts[0].network = gateway;
rts[0].plen = 32;
@@ -166,7 +166,7 @@ test_ip6_route ()
accept_signal (route_changed);
/* Test route listing */
routes = nm_platform_ip6_route_get_all (ifindex);
routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
memset (rts, 0, sizeof (rts));
rts[0].network = gateway;
rts[0].plen = 128;