platform: don't use gateway as key attribute for routes

On Linux, the gateway attribute is not a key attribute and therefore is
not necessary for functions that just need to identify a route. This may
be revisited when porting to other platforms but for now I want to keep
things simple.
This commit is contained in:
Pavel Šimerda
2013-05-02 08:06:08 +02:00
parent b041877dc1
commit e2009e3fe3
5 changed files with 55 additions and 75 deletions

View File

@@ -691,8 +691,7 @@ ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int p
}
static NMPlatformIP4Route *
ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
in_addr_t gateway, int metric)
ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
int i;
@@ -703,7 +702,6 @@ ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
if (route->ifindex == ifindex
&& route->network == network
&& route->plen == plen
&& route->gateway == gateway
&& route->metric == metric)
return route;
}
@@ -712,8 +710,7 @@ ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
}
static NMPlatformIP6Route *
ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int plen,
struct in6_addr gateway, int metric)
ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
int i;
@@ -724,7 +721,6 @@ ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int p
if (route->ifindex == ifindex
&& IN6_ARE_ADDR_EQUAL (&route->network, &network)
&& route->plen == plen
&& IN6_ARE_ADDR_EQUAL (&route->gateway, &gateway)
&& route->metric == metric)
return route;
}
@@ -733,10 +729,9 @@ ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int p
}
static gboolean
ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
in_addr_t gateway, int metric)
ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
NMPlatformIP4Route *route = ip4_route_get (platform, ifindex, network, plen, gateway, metric);
NMPlatformIP4Route *route = ip4_route_get (platform, ifindex, network, plen, metric);
NMPlatformIP4Route deleted_route;
g_assert (route);
@@ -749,10 +744,9 @@ ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen
}
static gboolean
ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen,
struct in6_addr gateway, int metric)
ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
NMPlatformIP6Route *route = ip6_route_get (platform, ifindex, network, plen, gateway, metric);
NMPlatformIP6Route *route = ip6_route_get (platform, ifindex, network, plen, metric);
NMPlatformIP6Route deleted_route;
g_assert (route);
@@ -765,17 +759,15 @@ ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, in
}
static gboolean
ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
in_addr_t gateway, int metric)
ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
return !!ip4_route_get (platform, ifindex, network, plen, gateway, metric);
return !!ip4_route_get (platform, ifindex, network, plen, metric);
}
static gboolean
ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen,
struct in6_addr gateway, int metric)
ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
return !!ip6_route_get (platform, ifindex, network, plen, gateway, metric);
return !!ip6_route_get (platform, ifindex, network, plen, metric);
}
/******************************************************************/

View File

@@ -1382,9 +1382,9 @@ build_rtnl_route (int family, int ifindex, gconstpointer network, int plen, gcon
struct rtnl_nexthop *nexthop = rtnl_route_nh_alloc ();
int addrlen = (family == AF_INET) ? sizeof (in_addr_t) : sizeof (struct in6_addr);
auto_nl_addr struct nl_addr *dst = nl_addr_build (family, network, addrlen);
auto_nl_addr struct nl_addr *gw = nl_addr_build (family, gateway, addrlen);
auto_nl_addr struct nl_addr *gw = gateway ? nl_addr_build (family, gateway, addrlen) : NULL;
g_assert (rtnlroute && dst && gw && nexthop);
g_assert (rtnlroute && dst && nexthop);
nl_addr_set_prefixlen (dst, plen);
@@ -1394,7 +1394,7 @@ build_rtnl_route (int family, int ifindex, gconstpointer network, int plen, gcon
rtnl_route_set_priority (rtnlroute, metric);
rtnl_route_nh_set_ifindex (nexthop, ifindex);
if (!nl_addr_iszero (gw))
if (gw && !nl_addr_iszero (gw))
rtnl_route_nh_set_gateway (nexthop, gw);
rtnl_route_add_nexthop (rtnlroute, nexthop);
@@ -1417,25 +1417,26 @@ ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int p
}
static gboolean
ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
in_addr_t gateway, int metric)
ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
in_addr_t gateway = 0;
return delete_object (platform, build_rtnl_route (AF_INET, ifindex, &network, plen, &gateway, metric, 0));
}
static gboolean
ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen,
struct in6_addr gateway, int metric)
ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
struct in6_addr gateway = in6addr_any;
return delete_object (platform, build_rtnl_route (AF_INET6, ifindex, &network, plen, &gateway, metric, 0));
}
static gboolean
ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network, int plen,
gpointer gateway, int metric)
ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network, int plen, int metric)
{
auto_nl_object struct nl_object *object = build_rtnl_route (
family, ifindex, network, plen, gateway, metric, 0);
family, ifindex, network, plen, INADDR_ANY, metric, 0);
auto_nl_object struct nl_object *cached_object = nl_cache_search (
choose_cache (platform, object), object);
@@ -1443,17 +1444,15 @@ ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network
}
static gboolean
ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
in_addr_t gateway, int metric)
ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
return ip_route_exists (platform, AF_INET, ifindex, &network, plen, &gateway, metric);
return ip_route_exists (platform, AF_INET, ifindex, &network, plen, metric);
}
static gboolean
ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen,
struct in6_addr gateway, int metric)
ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
return ip_route_exists (platform, AF_INET6, ifindex, &network, plen, &gateway, metric);
return ip_route_exists (platform, AF_INET6, ifindex, &network, plen, metric);
}
/******************************************************************/

View File

@@ -884,7 +884,7 @@ nm_platform_ip4_route_add (int ifindex,
g_return_val_if_fail (mss >= 0, FALSE);
g_return_val_if_fail (klass->ip4_route_add, FALSE);
if (nm_platform_ip4_route_exists (ifindex, network, plen, gateway, metric)) {
if (nm_platform_ip4_route_exists (ifindex, network, plen, metric)) {
debug ("route already exists");
platform->error = NM_PLATFORM_ERROR_EXISTS;
return FALSE;
@@ -903,7 +903,7 @@ nm_platform_ip6_route_add (int ifindex,
g_return_val_if_fail (mss >= 0, FALSE);
g_return_val_if_fail (klass->ip6_route_add, FALSE);
if (nm_platform_ip6_route_exists (ifindex, network, plen, gateway, metric)) {
if (nm_platform_ip6_route_exists (ifindex, network, plen, metric)) {
debug ("route already exists");
platform->error = NM_PLATFORM_ERROR_EXISTS;
return FALSE;
@@ -913,63 +913,60 @@ nm_platform_ip6_route_add (int ifindex,
}
gboolean
nm_platform_ip4_route_delete (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric)
nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metric)
{
reset_error ();
g_return_val_if_fail (platform, FALSE);
g_return_val_if_fail (klass->ip4_route_delete, FALSE);
if (!nm_platform_ip4_route_exists (ifindex, network, plen, gateway, metric)) {
if (!nm_platform_ip4_route_exists (ifindex, network, plen, metric)) {
debug ("route not found");
platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
return FALSE;
}
return klass->ip4_route_delete (platform,ifindex, network, plen, gateway, metric);
return klass->ip4_route_delete (platform, ifindex, network, plen, metric);
}
gboolean
nm_platform_ip6_route_delete (int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric)
struct in6_addr network, int plen, int metric)
{
reset_error ();
g_return_val_if_fail (platform, FALSE);
g_return_val_if_fail (klass->ip6_route_delete, FALSE);
if (!nm_platform_ip6_route_exists (ifindex, network, plen, gateway, metric)) {
if (!nm_platform_ip6_route_exists (ifindex, network, plen, metric)) {
debug ("route not found");
platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
return FALSE;
}
return klass->ip6_route_delete (platform, ifindex, network, plen, gateway, metric);
return klass->ip6_route_delete (platform, ifindex, network, plen, metric);
}
gboolean
nm_platform_ip4_route_exists (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric)
nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metric)
{
reset_error ();
g_return_val_if_fail (platform, FALSE);
g_return_val_if_fail (klass->ip4_route_exists, FALSE);
return klass->ip4_route_exists (platform,ifindex, network, plen, gateway, metric);
return klass->ip4_route_exists (platform,ifindex, network, plen, metric);
}
gboolean
nm_platform_ip6_route_exists (int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric)
nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, int metric)
{
reset_error ();
g_return_val_if_fail (platform, FALSE);
g_return_val_if_fail (klass->ip6_route_exists, FALSE);
return klass->ip6_route_exists (platform, ifindex, network, plen, gateway, metric);
return klass->ip6_route_exists (platform, ifindex, network, plen, metric);
}
/******************************************************************/

View File

@@ -180,14 +180,10 @@ typedef struct {
in_addr_t network, int plen, in_addr_t gateway, int prio, int mss);
gboolean (*ip6_route_add) (NMPlatform *, int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int prio, int mss);
gboolean (*ip4_route_delete) (NMPlatform *, int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric);
gboolean (*ip6_route_delete) (NMPlatform *, int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric);
gboolean (*ip4_route_exists) (NMPlatform *, int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric);
gboolean (*ip6_route_exists) (NMPlatform *, int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric);
gboolean (*ip4_route_delete) (NMPlatform *, int ifindex, in_addr_t network, int plen, int metric);
gboolean (*ip6_route_delete) (NMPlatform *, int ifindex, struct in6_addr network, int plen, int metric);
gboolean (*ip4_route_exists) (NMPlatform *, int ifindex, in_addr_t network, int plen, int metric);
gboolean (*ip6_route_exists) (NMPlatform *, int ifindex, struct in6_addr network, int plen, int metric);
} NMPlatformClass;
/* NMPlatform signals
@@ -281,14 +277,10 @@ gboolean nm_platform_ip4_route_add (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric, int mss);
gboolean nm_platform_ip6_route_add (int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric, int mss);
gboolean nm_platform_ip4_route_delete (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric);
gboolean nm_platform_ip6_route_delete (int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric);
gboolean nm_platform_ip4_route_exists (int ifindex,
in_addr_t network, int plen, in_addr_t gateway, int metric);
gboolean nm_platform_ip6_route_exists (int ifindex,
struct in6_addr network, int plen, struct in6_addr gateway, int metric);
gboolean nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metric);
gboolean nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, int metric);
gboolean nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metric);
gboolean nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, int metric);
#define auto_g_free __attribute__((cleanup(put_g_free)))
static void __attribute__((unused))

View File

@@ -60,9 +60,9 @@ test_ip4_route ()
accept_signal (route_added);
/* Add route */
g_assert (!nm_platform_ip4_route_exists (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (!nm_platform_ip4_route_exists (ifindex, network, plen, metric)); no_error ();
g_assert (nm_platform_ip4_route_add (ifindex, network, plen, gateway, metric, mss)); no_error ();
g_assert (nm_platform_ip4_route_exists (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (nm_platform_ip4_route_exists (ifindex, network, plen, metric)); no_error ();
accept_signal (route_added);
/* Add route again */
@@ -87,12 +87,12 @@ test_ip4_route ()
g_array_unref (routes);
/* Remove route */
g_assert (nm_platform_ip4_route_delete (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (!nm_platform_ip4_route_exists (ifindex, network, plen, gateway, metric));
g_assert (nm_platform_ip4_route_delete (ifindex, network, plen, metric)); no_error ();
g_assert (!nm_platform_ip4_route_exists (ifindex, network, plen, metric));
accept_signal (route_removed);
/* Remove route again */
g_assert (!nm_platform_ip4_route_delete (ifindex, network, plen, gateway, metric));
g_assert (!nm_platform_ip4_route_delete (ifindex, network, plen, metric));
error (NM_PLATFORM_ERROR_NOT_FOUND);
free_signal (route_added);
@@ -121,9 +121,9 @@ test_ip6_route ()
accept_signal (route_added);
/* Add route */
g_assert (!nm_platform_ip6_route_exists (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (!nm_platform_ip6_route_exists (ifindex, network, plen, metric)); no_error ();
g_assert (nm_platform_ip6_route_add (ifindex, network, plen, gateway, metric, mss)); no_error ();
g_assert (nm_platform_ip6_route_exists (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (nm_platform_ip6_route_exists (ifindex, network, plen, metric)); no_error ();
accept_signal (route_added);
/* Add route again */
@@ -148,12 +148,12 @@ test_ip6_route ()
g_array_unref (routes);
/* Remove route */
g_assert (nm_platform_ip6_route_delete (ifindex, network, plen, gateway, metric)); no_error ();
g_assert (!nm_platform_ip6_route_exists (ifindex, network, plen, gateway, metric));
g_assert (nm_platform_ip6_route_delete (ifindex, network, plen, metric)); no_error ();
g_assert (!nm_platform_ip6_route_exists (ifindex, network, plen, metric));
accept_signal (route_removed);
/* Remove route again */
g_assert (!nm_platform_ip6_route_delete (ifindex, network, plen, gateway, metric));
g_assert (!nm_platform_ip6_route_delete (ifindex, network, plen, metric));
error (NM_PLATFORM_ERROR_NOT_FOUND);
free_signal (route_added);