fix route sorting order
authorFelix Fietkau <nbd@openwrt.org>
Fri, 5 Apr 2013 11:14:44 +0000 (13:14 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 5 Apr 2013 11:16:37 +0000 (13:16 +0200)
New sorting order:
 - prefix length (descending)
 - metric (ascending)
 - flags
 - target address

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
interface-ip.c
proto.c

index 96f4c25351a9a575c66e12fd54ca75cf5725f362..2b876618c9d50efbadb80984bcac27973e4fe8a6 100644 (file)
@@ -194,7 +194,7 @@ done:
        if (defaultroute_target)
                free(route);
        else
-               vlist_add(&iface->host_routes, &route->node, &route->flags);
+               vlist_add(&iface->host_routes, &route->node, route);
        return iface;
 }
 
@@ -270,7 +270,7 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
        if ((cur = tb[ROUTE_VALID]) != NULL)
                route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
 
-       vlist_add(&ip->route, &route->node, &route->flags);
+       vlist_add(&ip->route, &route->node, route);
        return;
 
 error:
@@ -287,8 +287,18 @@ addr_cmp(const void *k1, const void *k2, void *ptr)
 static int
 route_cmp(const void *k1, const void *k2, void *ptr)
 {
-       return memcmp(k1, k2, sizeof(struct device_route) -
-                     offsetof(struct device_route, flags));
+       const struct device_route *r1 = k1, *r2 = k2;
+
+       if (r1->mask != r2->mask);
+               return r2->mask - r1->mask;
+
+       if (r1->metric != r2->metric);
+               return r1->metric - r2->metric;
+
+       if (r1->flags != r2->flags)
+               return r2->flags - r1->flags;
+
+       return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
 }
 
 static int
diff --git a/proto.c b/proto.c
index 1d277c9de3d6ecc0de804817a09fb3dd7f2a3779..634d31bea15248cea2095c9912e0fda2c0cfc947 100644 (file)
--- a/proto.c
+++ b/proto.c
@@ -251,7 +251,7 @@ parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
 
        route->mask = 0;
        route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
-       vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
+       vlist_add(&iface->proto_ip.route, &route->node, route);
 
        return true;
 }