From 8bddb5b8c38e958efabf2d7c94c5d25154b754ac Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Fri, 1 Feb 2013 01:58:14 +0000 Subject: [PATCH] Add -z / --show-ip support This new option displays both the hostname and the IP address of a host in the path. This is particularly important when the hostname determined by a reverse lookup has no corresponding forward record. This is similar to the -b (both) option in tracepath, but -b was already taken in mtr for --bitpattern. Using this option also implies -w / --report-wide as this option isn't terribly useful without it in report mode. In general we endeavor to only show the IP address once per line, but in the split view we consistently add a separate field for IP address to the output. Signed-off-by: Travis Cross --- curses.c | 3 ++- dns.c | 6 ++--- gtk.c | 16 ++++++------- mtr.8 | 13 +++++++++++ mtr.c | 11 +++++++-- mtr.h | 1 + report.c | 71 ++++++++++++++------------------------------------------ split.c | 31 +++++++++++-------------- 8 files changed, 67 insertions(+), 85 deletions(-) diff --git a/curses.c b/curses.c index 7f47676..86f8785 100644 --- a/curses.c +++ b/curses.c @@ -314,7 +314,8 @@ void mtr_curses_hosts(int startstat) if (! net_up(at)) attron(A_BOLD); if(name != NULL) { - printw("%s", name); + if (show_ips) printw("%s (%s)", name, strlongip(addr)); + else printw("%s", name); } else { printw("%s", strlongip( addr ) ); } diff --git a/dns.c b/dns.c index 6a76bc1..56c937f 100644 --- a/dns.c +++ b/dns.c @@ -1376,9 +1376,9 @@ char *dns_lookup(ip_t * ip) { char *t; - if (!dns) return strlongip (ip); - t = dns_lookup2 (ip); - return (t&&use_dns)?t:strlongip(ip); + if (!dns) return NULL; + t = dns_lookup2(ip); + return (t && use_dns) ? t : NULL; } #ifdef ENABLE_IPV6 diff --git a/gtk.c b/gtk.c index e42f4f6..839bbbd 100644 --- a/gtk.c +++ b/gtk.c @@ -457,16 +457,16 @@ void TreeViewCreate(void) void update_tree_row(int row, GtkTreeIter *iter) { ip_t *addr; - char str[256], *name; + char str[256]="???", *name=str; addr = net_addr(row); - name = "???"; - if ( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { - name = dns_lookup(addr); - if(!name) { - sprintf(str, "%s", strlongip( addr )); - name = str; - } + if (addrcmp( (void *) addr, (void *) &unspec_addr, af)) { + if ((name = dns_lookup(addr))) { + if (show_ips) { + snprintf(str, sizeof(str), "%s (%s)", name, strlongip(addr)); + name = str; + } + } else name = strlongip(addr); } gtk_list_store_set(ReportStore, iter, diff --git a/mtr.8 b/mtr.8 index 694c631..e78f306 100644 --- a/mtr.8 +++ b/mtr.8 @@ -41,6 +41,9 @@ mtr \- a network diagnostic tool .B \-\-no-dns\c ] [\c +.B \-\-show-ips\c +] +[\c .B \-\-gtk\c ] [\c @@ -184,6 +187,16 @@ Use this option to force to display numeric IP numbers and not try to resolve the host names. +.TP +.B \-z +.TP +.B \-\-show-ips +.br +Use this option to tell +.B mtr +to display both the host names and numeric IP numbers. In split mode +this adds an extra field to the output. + .TP .B \-o\ fields\ order .TP diff --git a/mtr.c b/mtr.c index d73ccfd..42dfc13 100644 --- a/mtr.c +++ b/mtr.c @@ -60,6 +60,7 @@ char *Hostname = NULL; char *InterfaceAddress = NULL; char LocalHostname[128]; int dns = 1; +int show_ips = 0; int enablempls = 0; int cpacketsize = 64; /* default packet size */ int bitpattern = 0; @@ -144,6 +145,7 @@ void parse_arg (int argc, char **argv) { "tos", 1, 0, 'Q' }, /* typeof service (0,255) */ { "mpls", 0, 0, 'e' }, { "no-dns", 0, 0, 'n' }, + { "show-ips", 0, 0, 'z' }, { "address", 1, 0, 'a' }, { "first-ttl", 1, 0, 'f' }, /* -f & -m are borrowed from traceroute */ { "max-ttl", 1, 0, 'm' }, @@ -157,7 +159,7 @@ void parse_arg (int argc, char **argv) while(1) { /* added f:m:o: byMin */ opt = getopt_long(argc, argv, - "vhrwxtglpo:i:c:s:b:Q:ena:f:m:u46", long_options, NULL); + "vhrwxtglpo:i:c:s:b:Q:ena:f:m:uz46", long_options, NULL); if(opt == -1) break; @@ -267,6 +269,10 @@ void parse_arg (int argc, char **argv) case 'u': mtrtype = IPPROTO_UDP; break; + case 'z': + show_ips = 1; + reportwide = 1; + break; case '4': af = AF_INET; break; @@ -381,7 +387,8 @@ int main(int argc, char **argv) if (PrintHelp) { printf("usage: %s [-hvrwctglspniu46] [--help] [--version] [--report]\n" "\t\t[--report-wide] [--report-cycles=COUNT] [--curses] [--gtk]\n" - "\t\t[--raw] [--split] [--mpls] [--no-dns] [--address interface]\n" /* BL */ + "\t\t[--raw] [--split] [--mpls] [--no-dns] [--show-ips]\n" + "\t\t[--address interface]\n" /* BL */ "\t\t[--psize=bytes/-s bytes]\n" /* ok */ "\t\t[--report-wide|-w] [-u]\n" /* rew */ "\t\t[--interval=SECONDS] HOSTNAME [PACKETSIZE]\n", argv[0]); diff --git a/mtr.h b/mtr.h index 9cfee0b..ec76190 100644 --- a/mtr.h +++ b/mtr.h @@ -60,6 +60,7 @@ typedef struct in_addr ip_t; extern int enablempls; extern int dns; +extern int show_ips; extern int use_dns; #ifdef __GNUC__ diff --git a/report.c b/report.c index 9834128..368dc2b 100644 --- a/report.c +++ b/report.c @@ -49,6 +49,17 @@ void report_open(void) { } +static size_t snprint_addr(char *dst, size_t dst_len, ip_t *addr) +{ + if(addrcmp((void *) addr, (void *) &unspec_addr, af)) { + struct hostent *host = dns ? addr2host((void *) addr, af) : NULL; + if (!host) return snprintf(dst, dst_len, "%s", strlongip(addr)); + else if (dns && show_ips) + return snprintf(dst, dst_len, "%s (%s)", host->h_name, strlongip(addr)); + else return snprintf(dst, dst_len, "%s", host->h_name); + } else return snprintf(dst, dst_len, "%s", "???"); +} + void report_close(void) { @@ -61,7 +72,6 @@ void report_close(void) char fmt[16]; int len=0; int len_hosts = 33; - struct hostent *host; if (reportwide) { @@ -70,19 +80,11 @@ void report_close(void) max = net_max(); at = net_min(); for (; at < max; at++) { + int nlen; addr = net_addr(at); - if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { - host = dns ? addr2host( (void *) addr, af ) : NULL; - if (host != NULL) { - strncpy( name, host->h_name, (sizeof name) - 1 ); - name[ (sizeof name) - 1 ] = '\0'; - } else { - snprintf(name, sizeof(name), "%s", strlongip( addr ) ); - } - if (len_hosts < strlen(name)) { - len_hosts = strlen(name); - } - } + if ((nlen = snprint_addr(name, sizeof(name), addr))) + if (len_hosts < nlen) + len_hosts = nlen; } } @@ -104,18 +106,7 @@ void report_close(void) for(; at < max; at++) { addr = net_addr(at); mpls = net_mpls(at); - if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) == 0 ) { - sprintf(name, "???"); - } else { - host = dns ? addr2host( (void *) addr, af ) : NULL; - - if (host != NULL) { - strncpy( name, host->h_name, (sizeof name) - 1 ); - name[ (sizeof name) - 1 ] = '\0'; - } else { - snprintf(name, sizeof(name), "%s", strlongip( addr ) ); - } - } + snprint_addr(name, sizeof(name), addr); snprintf( fmt, sizeof(fmt), " %%2d.|-- %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, at+1, name); @@ -208,7 +199,6 @@ void xml_close(void) int i, j, at, max; ip_t *addr; char name[81]; - struct hostent *host; printf("h_name, (sizeof name) - 1 ); - name[ (sizeof name) - 1 ] = '\0'; - } else { - sprintf(name, "%s", strlongip( addr ) ); - } - } + snprint_addr(name, sizeof(name), addr); printf(" \n", at+1, name); for( i=0; ih_name, (sizeof name) - 1 ); - name[ (sizeof name) - 1 ] = '\0'; - } else { - sprintf(name, "%s", strlongip( addr ) ); - } - } + snprint_addr(name, sizeof(name), addr); printf("%d, %s", at+1, name); for( i=0; i