From b415564df9531103dec10930f43dc557f9c2f2c3 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 30 Jun 2013 17:38:15 +0000 Subject: [PATCH] Sort routes first by netmask, then by metric. Metric is used to break ties between routes with the same size netmask. --- CHANGELOG | 5 +++++ libnetutil/netutil.cc | 14 +++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7a64415e5..5ca0e0b72 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,10 @@ # Nmap Changelog ($Id$); -*-text-*- +o Nmap's routing table is now sorted first by netmask, then by metric. + Previously it was the other way around, which could cause a very + general route with a low metric to be preferred over a specific + route with a higher metric. + o [Ncat] The -i option (idle timeout) now works in listen mode as well as connect mode. [Tomas Hozza] diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index 687384599..e7d5a5bfc 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -1200,7 +1200,7 @@ void tcppacketoptinfo(u8 *optp, int len, char *result, int bufsize) { -/* A trivial function used with qsort to sort the routes by metric and netmask */ +/* A trivial function used with qsort to sort the routes by netmask and metric */ static int routecmp(const void *a, const void *b) { struct sys_route *r1 = (struct sys_route *) a; struct sys_route *r2 = (struct sys_route *) b; @@ -1209,16 +1209,16 @@ static int routecmp(const void *a, const void *b) { else if (r1->dest.ss_family > r2->dest.ss_family) return 1; - if (r1->metric < r2->metric) - return -1; - else if (r1->metric > r2->metric) - return 1; - if (r1->netmask_bits < r2->netmask_bits) return 1; else if (r1->netmask_bits > r2->netmask_bits) return -1; + if (r1->metric < r2->metric) + return -1; + else if (r1->metric > r2->metric) + return 1; + /* Compare addresses of equal elements to make the sort stable, as suggested by the Glibc manual. */ if (a < b) @@ -1698,7 +1698,7 @@ struct sys_route *getsysroutes(int *howmany, char *errstr, size_t errstrlen) { return NULL; }else{ numroutes = *howmany; - /* Ensure that the route array is sorted by metric and netmask */ + /* Ensure that the route array is sorted by netmask and metric */ qsort(routes, numroutes, sizeof(routes[0]), routecmp); } return routes;