From 0f048bb0f49a060ea0fd89048d3dda276306b8e2 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 11 Mar 2010 18:03:01 +0000 Subject: [PATCH] Compare addresses if netmasks are equal in the qsort comparison function for routes, to ensure that routes keep their same relative order. This idea is from http://www.gnu.org/s/libc/manual/html_node/Array-Sort-Function.html. --- tcpip.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tcpip.cc b/tcpip.cc index d04c403b0..0b944e10c 100644 --- a/tcpip.cc +++ b/tcpip.cc @@ -3259,8 +3259,16 @@ pcap_if_t *getpcapinterfaces() { static int nmaskcmp(const void *a, const void *b) { struct sys_route *r1 = (struct sys_route *) a; struct sys_route *r2 = (struct sys_route *) b; - if (r1->netmask == r2->netmask) - return 0; + if (r1->netmask == r2->netmask) { + /* Compare addresses of equal elements to make the sort stable, as suggested + by the Glibc manual. */ + if (a < b) + return -1; + else if (a > b) + return 1; + else + return 0; + } if (ntohl(r1->netmask) > ntohl(r2->netmask)) return -1; else @@ -3523,7 +3531,6 @@ struct sys_route *getsysroutes(int *howmany) { static struct sys_route *routes = NULL; static int numroutes = 0; FILE *routefp; - int i; if (!howmany) fatal("NULL howmany ptr passed to %s()", __func__); @@ -3546,15 +3553,7 @@ struct sys_route *getsysroutes(int *howmany) { numroutes = *howmany; /* Ensure that the route array is sorted by netmask */ - for (i = 1; i < numroutes; i++) { - if (ntohl(routes[i].netmask) > ntohl(routes[i - 1].netmask)) - break; - } - - if (i < numroutes) { - /* they're not sorted ... better take care of that */ - qsort(routes, numroutes, sizeof(routes[0]), nmaskcmp); - } + qsort(routes, numroutes, sizeof(routes[0]), nmaskcmp); return routes; }