]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Add -z / --show-ip support
authorTravis Cross <tc@traviscross.com>
Fri, 1 Feb 2013 01:58:14 +0000 (01:58 +0000)
committerTravis Cross <tc@traviscross.com>
Mon, 4 Feb 2013 07:44:43 +0000 (07:44 +0000)
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 <tc@traviscross.com>
curses.c
dns.c
gtk.c
mtr.8
mtr.c
mtr.h
report.c
split.c

index 7f47676bb9701d4eb6d228280f64fc17c8a709ce..86f8785c0107b0ae0b725335fa990ad93fc4ef03 100644 (file)
--- 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 6a76bc199dbfdde059fa35005f7a1130677c0c54..56c937f4c6c04f12d074f39a9f3821b38c8bd56f 100644 (file)
--- 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 e42f4f62e7153dca7ff9d9afa34d3f954758100e..839bbbdd2d9a66199eb326a1379f6bf9d8bf6bca 100644 (file)
--- 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 694c631ff6ab2d2972a2d8e050c575f2f47b42bb..e78f3065be59e1b31228d9394fdc2ba788ccbcfc 100644 (file)
--- 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 d73ccfd87d0a6be32081542885796bb8ea37b212..42dfc13da8500021c9dfa800629f25032aadaa4c 100644 (file)
--- 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 9cfee0bf17360adc9868a9a3a92bd454fd155a67..ec76190813df220d69cb8cc606df98b60efbe6df 100644 (file)
--- 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__
index 98341282a20f375db533e1834c6cffaf3fa6f886..368dc2bfae892b380c83e45e0ff150f2a41067f4 100644 (file)
--- 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("<MTR SRC=%s DST=%s", LocalHostname, Hostname);
   printf(" TOS=0x%X", tos);
@@ -228,19 +218,7 @@ void xml_close(void)
   at  = net_min();
   for(; at < max; at++) {
     addr = net_addr(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 {
-       sprintf(name, "%s", strlongip( addr ) );
-      }
-    }
+    snprint_addr(name, sizeof(name), addr);
 
     printf("    <HUB COUNT=%d HOST=%s>\n", at+1, name);
     for( i=0; i<MAXFLD; i++ ) {
@@ -279,7 +257,6 @@ void csv_close(void)
   int i, j, at, max;
   ip_t *addr;
   char name[81];
-  struct hostent *host;
 
   /* Caption */
   printf("<SRC=%s DST=%s", LocalHostname, Hostname);
@@ -310,19 +287,7 @@ void csv_close(void)
   at  = net_min();
   for(; at < max; at++) {
     addr = net_addr(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 {
-       sprintf(name, "%s", strlongip( addr ) );
-      }
-    }
+    snprint_addr(name, sizeof(name), addr);
 
     printf("%d, %s", at+1, name);
     for( i=0; i<MAXFLD; i++ ) {
diff --git a/split.c b/split.c
index 4453dac2b8c73f8483e671547f41867f8153526a..642353ed5bf48905c6d83987c0c519a0ea3fc5ba 100644 (file)
--- a/split.c
+++ b/split.c
@@ -74,7 +74,6 @@ void split_redraw(void)
   int   max;
   int   at;
   ip_t *addr;
-  char *name;
   char  newLine[MAX_LINE_SIZE];
   int   i;
 
@@ -97,24 +96,20 @@ void split_redraw(void)
    */
   for(at = 0; at < max; at++) {
     addr = net_addr(at);
-    
-    if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) {
-      name = dns_lookup(addr);
-      if(name != NULL) {
-       /* May be we should test name's length */
-       snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", name,
-               net_loss(at),
-               net_returned(at), net_xmit(at),
-               net_best(at) /1000, net_avg(at)/1000, 
-               net_worst(at)/1000);
-      } else {
-       snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", 
-               strlongip( addr ),
-               net_loss(at),
-               net_returned(at), net_xmit(at),
-               net_best(at) /1000, net_avg(at)/1000, 
-               net_worst(at)/1000);
+    if(addrcmp((void*)addr, (void*)&unspec_addr, af)) {
+      char str[256], *name;
+      if (!(name = dns_lookup(addr)))
+        name = strlongip(addr);
+      if (show_ips) {
+        snprintf(str, sizeof(str), "%s %s", name, strlongip(addr));
+        name = str;
       }
+      /* May be we should test name's length */
+      snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", name,
+               net_loss(at),
+               net_returned(at), net_xmit(at),
+               net_best(at) /1000, net_avg(at)/1000,
+               net_worst(at)/1000);
     } else {
       sprintf(newLine, "???");
     }