]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
data types: move rest of the global variables to control structures
authorSami Kerola <kerolasa@iki.fi>
Sun, 28 Aug 2016 19:12:58 +0000 (20:12 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 28 Aug 2016 21:49:14 +0000 (22:49 +0100)
Many of these variables seem to related to printing.  One can reasonably
assume the control structure will need to be split to a library and
none-library parts.

12 files changed:
asn.c
asn.h
curses.c
display.c
display.h
gtk.c
mtr.h
net.c
net.h
report.c
select.c
split.c

diff --git a/asn.c b/asn.c
index 0b2eb725e0b19552f624701eb27da1cd67a30615..bb2bca1f2e7540221af207e8a60e1e697c0e14ce 100644 (file)
--- a/asn.c
+++ b/asn.c
 #define NAMELEN        127
 #define UNKN   "???"
 
-int  ipinfo_no = -1;
-int  ipinfo_max = -1;
 static int  iihash = 0;
-char fmtinfo[32];
+static char fmtinfo[32];
 
 // items width: ASN, Route, Country, Registry, Allocated 
-int iiwidth[] = { 7, 19, 4, 8, 11};    // item len + space
-int iiwidth_len = sizeof(iiwidth)/sizeof((iiwidth)[0]);
+static const int iiwidth[] = { 7, 19, 4, 8, 11 };    // item len + space
 
 typedef char* items_t[ITEMSMAX + 1];
 static items_t items_a;                // without hash: items
@@ -161,7 +158,7 @@ static char* trimsep(char *s) {
 }
 
 // originX.asn.cymru.com txtrec:    ASN | Route | Country | Registry | Allocated
-static char* split_txtrec(char *txt_rec) {
+static char* split_txtrec(struct mtr_ctl *ctl, char *txt_rec) {
     if (!txt_rec)
        return NULL;
     if (iihash) {
@@ -191,14 +188,14 @@ static char* split_txtrec(char *txt_rec) {
     for (j = i;  j <= ITEMSMAX; j++)
         (*items)[j] = NULL;
 
-    if (i > ipinfo_max)
-        ipinfo_max = i;
-    if (ipinfo_no >= i) {
-        if (ipinfo_no >= ipinfo_max)
-            ipinfo_no = 0;
+    if (i > ctl->ipinfo_max)
+        ctl->ipinfo_max = i;
+    if (ctl->ipinfo_no >= i) {
+        if (ctl->ipinfo_no >= ctl->ipinfo_max)
+            ctl->ipinfo_no = 0;
        return (*items)[0];
     } else
-       return (*items)[ipinfo_no];
+       return (*items)[ctl->ipinfo_no];
 }
 
 #ifdef ENABLE_IPV6
@@ -244,7 +241,7 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
         item.key = key;;
         ENTRY *found_item;
         if ((found_item = hsearch(item, FIND))) {
-            if (!(val = (*((items_t*)found_item->data))[ipinfo_no]))
+            if (!(val = (*((items_t*)found_item->data))[ctl->ipinfo_no]))
                 val = (*((items_t*)found_item->data))[0];
         DEB_syslog(LOG_INFO, "Found (hashed): %s", val);
         }
@@ -252,7 +249,7 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
 
     if (!val) {
         DEB_syslog(LOG_INFO, "Lookup: %s", key);
-        if ((val = split_txtrec(ipinfo_lookup(lookup_key)))) {
+        if ((val = split_txtrec(ctl, ipinfo_lookup(lookup_key)))) {
             DEB_syslog(LOG_INFO, "Looked up: %s", key);
             if (iihash)
                 if ((item.key = strdup(key))) {
@@ -266,32 +263,34 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
     return val;
 }
 
-extern int get_iiwidth(void) {
-    return (ipinfo_no < iiwidth_len) ? iiwidth[ipinfo_no] : iiwidth[ipinfo_no % iiwidth_len];
+extern int get_iiwidth (struct mtr_ctl *ctl) {
+  return (ctl->ipinfo_no <
+         ctl->iiwidth_len) ? iiwidth[ctl->ipinfo_no] :
+                             iiwidth[ctl->ipinfo_no % ctl->iiwidth_len];
 }
 
 extern char *fmt_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
     char *ipinfo = get_ipinfo(ctl, addr);
     char fmt[8];
-    snprintf(fmt, sizeof(fmt), "%s%%-%ds", ipinfo_no?"":"AS", get_iiwidth());
+    snprintf(fmt, sizeof(fmt), "%s%%-%ds", ctl->ipinfo_no?"":"AS", get_iiwidth(ctl));
     snprintf(fmtinfo, sizeof(fmtinfo), fmt, ipinfo?ipinfo:UNKN);
     return fmtinfo;
 }
 
-extern int is_printii(void) {
-    return ((ipinfo_no >= 0) && (ipinfo_no != ipinfo_max));
+extern int is_printii(struct mtr_ctl *ctl) {
+    return ((ctl->ipinfo_no >= 0) && (ctl->ipinfo_no != ctl->ipinfo_max));
 }
 
-extern void asn_open(void) {
-    if (ipinfo_no >= 0) {
+extern void asn_open(struct mtr_ctl *ctl) {
+    if (ctl->ipinfo_no >= 0) {
         DEB_syslog(LOG_INFO, "hcreate(%d)", IIHASH_HI);
         if (!(iihash = hcreate(IIHASH_HI)))
             error(0, errno, "ipinfo hash");
     }
 }
 
-extern void asn_close(void) {
-    if ((ipinfo_no >= 0) && iihash) {
+extern void asn_close(struct mtr_ctl *ctl) {
+    if ((ctl->ipinfo_no >= 0) && iihash) {
         DEB_syslog(LOG_INFO, "hdestroy()");
         hdestroy();
         iihash = 0;
diff --git a/asn.h b/asn.h
index 9ca113f533695f2788a12e48ff2e5a8b7f48dd77..76fd0e4a747acc9e9684beb520119090a8fef851 100644 (file)
--- a/asn.h
+++ b/asn.h
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-extern int ipinfo_no;
-extern int ipinfo_max;
-extern int iiwidth_len;
-extern void asn_open();
-extern void asn_close();
+extern void asn_open(struct mtr_ctl *ctl);
+extern void asn_close(struct mtr_ctl *ctl);
 extern char *fmt_ipinfo(struct mtr_ctl *ctl, ip_t *addr);
-extern int get_iiwidth(void);
-extern int is_printii(void);
+extern int get_iiwidth(struct mtr_ctl *ctl);
+extern int is_printii(struct mtr_ctl *ctl);
index a39fa91c5149593deac789dab8840d5ce03664f1..1f7d634f3a80f8b02bd359774f8520b10744034a 100644 (file)
--- a/curses.c
+++ b/curses.c
@@ -371,17 +371,17 @@ static void mtr_curses_hosts(struct mtr_ctl *ctl, int startstat)
 
   max = net_max(ctl);
 
-  for(at = net_min(ctl) + display_offset; at < max; at++) {
+  for(at = net_min(ctl) + ctl->display_offset; at < max; at++) {
     printw("%2d. ", at + 1);
     addr = net_addr(at);
     mpls = net_mpls(at);
 
-    if( addrcmp( (void *) addr, (void *) &unspec_addr, ctl->af ) != 0 ) {
+    if( addrcmp( (void *) addr, (void *) &ctl->unspec_addr, ctl->af ) != 0 ) {
       name = dns_lookup(ctl, addr);
       if (! net_up(at))
        attron(A_BOLD);
 #ifdef HAVE_IPINFO
-      if (is_printii())
+      if (is_printii(ctl))
         printw(fmt_ipinfo(ctl, addr));
 #endif
       if(name != NULL) {
@@ -424,13 +424,13 @@ static void mtr_curses_hosts(struct mtr_ctl *ctl, int startstat)
         addrs = net_addrs(at, i);
         mplss = net_mplss(at, i);
        if ( addrcmp( (void *) addrs, (void *) addr, ctl->af ) == 0 ) continue;
-       if ( addrcmp( (void *) addrs, (void *) &unspec_addr, ctl->af ) == 0 ) break;
+       if ( addrcmp( (void *) addrs, (void *) &ctl->unspec_addr, ctl->af ) == 0 ) break;
 
         name = dns_lookup(ctl, addrs);
         if (! net_up(at)) attron(A_BOLD);
         printw("\n    ");
 #ifdef HAVE_IPINFO
-        if (is_printii())
+        if (is_printii(ctl))
           printw(fmt_ipinfo(ctl, addrs));
 #endif
         if (name != NULL) {
@@ -471,7 +471,7 @@ static void mtr_gen_scale(struct mtr_ctl *ctl)
                scale[i] = 0;
        }
        max = net_max(ctl);
-       for (at = display_offset; at < max; at++) {
+       for (at = ctl->display_offset; at < max; at++) {
                saved = net_saved_pings(at);
                for (i = 0; i < SAVED_PINGS; i++) {
                        if (saved[i] < 0) continue;
@@ -581,7 +581,7 @@ static void mtr_curses_graph(struct mtr_ctl *ctl, int startstat, int cols)
 
        max = net_max(ctl);
 
-       for (at = display_offset; at < max; at++) {
+       for (at = ctl->display_offset; at < max; at++) {
                printw("%2d. ", at+1);
 
                addr = net_addr(at);
@@ -592,9 +592,9 @@ static void mtr_curses_graph(struct mtr_ctl *ctl, int startstat, int cols)
 
                if (! net_up(at))
                        attron(A_BOLD);
-               if (addrcmp((void *) addr, (void *) &unspec_addr, ctl->af)) {
+               if (addrcmp((void *) addr, (void *) &ctl->unspec_addr, ctl->af)) {
 #ifdef HAVE_IPINFO
-                       if (is_printii())
+                       if (is_printii(ctl))
                                printw(fmt_ipinfo(ctl, addr));
 #endif
                        name = dns_lookup(ctl, addr);
@@ -685,8 +685,8 @@ extern void mtr_curses_redraw(struct mtr_ctl *ctl)
     char msg[80];
     int padding = 30;
 #ifdef HAVE_IPINFO
-    if (is_printii())
-      padding += get_iiwidth();
+    if (is_printii(ctl))
+      padding += get_iiwidth(ctl);
 #endif
     int max_cols = maxx<=SAVED_PINGS+padding ? maxx-padding : SAVED_PINGS;
     startstat = padding - 2;
index 972136fe305f5111178a538df3ec7eaeec7d1492..b66cd83c9b0e77296572ff8c010f7eb64757dd10 100644 (file)
--- a/display.c
+++ b/display.c
@@ -90,7 +90,7 @@ extern void display_open(struct mtr_ctl *ctl)
   case DisplayCurses:
     mtr_curses_open(ctl);
 #ifdef HAVE_IPINFO
-    asn_open();
+    asn_open(ctl);
 #endif
     break;
 #endif
@@ -101,7 +101,7 @@ extern void display_open(struct mtr_ctl *ctl)
   case DisplayGTK:
     gtk_open(ctl);
 #ifdef HAVE_IPINFO
-    asn_open();
+    asn_open(ctl);
 #endif
     break;
 #endif
@@ -130,7 +130,7 @@ extern void display_close(struct mtr_ctl *ctl, time_t now)
 #ifdef HAVE_NCURSES
   case DisplayCurses:
 #ifdef HAVE_IPINFO
-    asn_close();
+    asn_close(ctl);
 #endif
     mtr_curses_close();
     break;
index fe994a845cfd4d3e2e74073727f869419783e00d..f26e1e13c66ece66a03f8e18cc16d198b4dfe3e1 100644 (file)
--- a/display.h
+++ b/display.h
@@ -60,5 +60,3 @@ extern void display_rawhost(struct mtr_ctl *ctl, int hostnum, ip_t *ip_addr);
 extern int display_keyaction(struct mtr_ctl *ctl);
 extern void display_loop(struct mtr_ctl *ctl);
 extern void display_clear(struct mtr_ctl *ctl);
-
-extern int display_offset; /* only used in text mode */
diff --git a/gtk.c b/gtk.c
index 23ed77cbf899b2f15ff1946995d3878d735e0f4f..e7f71afa437fe869b4c60286fc50f6025ff067b2 100644 (file)
--- a/gtk.c
+++ b/gtk.c
@@ -370,7 +370,7 @@ static void TreeViewCreate(struct mtr_ctl *ctl)
                    G_CALLBACK(ReportTreeView_clicked), ctl);
 
 #ifdef HAVE_IPINFO
-  if (is_printii()) {
+  if (is_printii(ctl)) {
     renderer = gtk_cell_renderer_text_new ();
     column = gtk_tree_view_column_new_with_attributes ("ASN",
       renderer,
@@ -472,7 +472,7 @@ static void update_tree_row(struct mtr_ctl *ctl, int row, GtkTreeIter *iter)
   char str[256]="???", *name=str;
 
   addr = net_addr(row);
-  if (addrcmp( (void *) addr, (void *) &unspec_addr, ctl->af)) {
+  if (addrcmp( (void *) addr, (void *) &ctl->unspec_addr, ctl->af)) {
     if ((name = dns_lookup(ctl, addr))) {
       if (ctl->show_ips) {
         snprintf(str, sizeof(str), "%s (%s)", name, strlongip(ctl, addr));
@@ -498,7 +498,7 @@ static void update_tree_row(struct mtr_ctl *ctl, int row, GtkTreeIter *iter)
 
     -1);
 #ifdef HAVE_IPINFO
-  if (is_printii())
+  if (is_printii(ctl))
     gtk_list_store_set(ReportStore, iter, COL_ASN, fmt_ipinfo(ctl, addr), -1);
 #endif
 }
diff --git a/mtr.h b/mtr.h
index 11d8a00fb6c44c1ef4edb524d9c182c441b3f4c9..6ef297ddccecad5ccb00bda5fd087131ce13fd60 100644 (file)
--- a/mtr.h
+++ b/mtr.h
@@ -22,6 +22,7 @@
 
 #include <stdint.h>
 #include <sys/socket.h>
+#include <arpa/inet.h>
 
 /* Typedefs */
 
@@ -74,7 +75,7 @@ typedef struct in_addr ip_t;
 #define MAXFLD 20              /* max stats fields to display */
 
 #ifndef HAVE_SOCKLEN_T
-typedef int socklen_t; 
+typedef int socklen_t;
 #endif
 
 extern char *
@@ -88,12 +89,15 @@ struct mtr_ctl {
   char *InterfaceAddress;
   char LocalHostname[128];
   int ipinfo_no;
+  int ipinfo_max;
+  int iiwidth_len;
   int cpacketsize;             /* packet size used by ping */
   int bitpattern;              /* packet bit pattern used by ping */
   int tos;                     /* type of service set in ping packet*/
 #ifdef SO_MARK
   uint32_t mark;
 #endif
+  ip_t unspec_addr;
   int af;                      /* address family of remote target */
   int mtrtype;                 /* type of query packet used */
   int fstTTL;                  /* initial hub(ttl) to ping byMin */
@@ -105,6 +109,7 @@ struct mtr_ctl {
   unsigned char fld_active[2 * MAXFLD];        /* SO_MARK to set for ping packet*/
   int fld_index[256];          /* default display field (defined by key in net.h) and order */
   char available_options[MAXFLD];
+  int display_offset;          /* only used in text mode */
   void *gtk_data;              /* pointer to hold arbitrary gtk data */
   unsigned int                 /* bit field to hold named booleans */
     ForceMaxPing:1,
diff --git a/net.c b/net.c
index 0e238c55be98f7081952aeb7c123021864ecd193..6779da277555a7d076f31e435ac45614bfcf6641 100644 (file)
--- a/net.c
+++ b/net.c
@@ -185,7 +185,6 @@ static int    sendsock6_udp;
 static int    recvsock6;
 static int    sendsock;
 static int    recvsock;
-ip_t   unspec_addr;
 
 #ifdef ENABLE_IPV6
 static struct sockaddr_storage sourcesockaddr_struct;
@@ -774,7 +773,7 @@ static void net_process_ping(struct mtr_ctl *ctl, int seq, struct mplslen mpls,
   /* impossible? if( totusec < 0 ) totusec = 0 */;
 
   if ( addrcmp( (void *) &(host[index].addr),
-               (void *) &unspec_addr, ctl->af ) == 0 ) {
+               (void *) &ctl->unspec_addr, ctl->af ) == 0 ) {
     /* should be out of if as addr can change */
     addrcpy( (void *) &(host[index].addr), addrcopy, ctl->af );
     host[index].mpls = mpls;
@@ -788,7 +787,7 @@ static void net_process_ping(struct mtr_ctl *ctl, int seq, struct mplslen mpls,
       if( addrcmp( (void *) &(host[index].addrs[i]), (void *) &addrcopy,
                    ctl->af ) == 0 ||
           addrcmp( (void *) &(host[index].addrs[i]),
-                  (void *) &unspec_addr, ctl->af ) == 0 ) break;
+                  (void *) &ctl->unspec_addr, ctl->af ) == 0 ) break;
       i++;
     }
     if( addrcmp( (void *) &(host[index].addrs[i]), addrcopy, ctl->af ) != 0 && 
@@ -1206,7 +1205,7 @@ extern int net_max(struct mtr_ctl *ctl)
                   (void *) remoteaddress, ctl->af ) == 0 ) {
       return at + 1;
     } else if ( addrcmp( (void *) &(host[at].addr),
-                        (void *) &unspec_addr, ctl->af ) != 0 ) {
+                        (void *) &ctl->unspec_addr, ctl->af ) != 0 ) {
       max = at + 2;
     }
   }
@@ -1286,7 +1285,7 @@ extern int net_send_batch(struct mtr_ctl *ctl)
   net_send_query(ctl, batch_at);
 
   for (i=ctl->fstTTL-1;i<batch_at;i++) {
-    if ( addrcmp( (void *) &(host[i].addr), (void *) &unspec_addr, ctl->af ) == 0 )
+    if ( addrcmp( (void *) &(host[i].addr), (void *) &ctl->unspec_addr, ctl->af ) == 0 )
       n_unknown++;
 
     /* The second condition in the next "if" statement was added in mtr-0.56, 
diff --git a/net.h b/net.h
index d123be752e9708f74e3f86810801d4869d23f107..66040f2578503de94eb375e0b945b034a1314e80 100644 (file)
--- a/net.h
+++ b/net.h
@@ -113,9 +113,6 @@ struct fields {
 
 extern struct fields data_fields[MAXFLD];
 
-/* keys: the value in the array is the index number in data_fields[] */
-extern ip_t unspec_addr;
-
 /* MPLS label object */
 struct mplslen {
   unsigned long label[MAXLABELS]; /* label value */
index 6c7bddb76bedb16e4354af76401ed170f2970c46..61b09e43f44055febd2ab8ff05a8d26613d48808 100644 (file)
--- a/report.c
+++ b/report.c
@@ -50,7 +50,7 @@ extern void report_open(void)
 
 static size_t snprint_addr(struct mtr_ctl *ctl, char *dst, size_t dst_len, ip_t *addr)
 {
-  if(addrcmp((void *) addr, (void *) &unspec_addr, ctl->af)) {
+  if(addrcmp((void *) addr, (void *) &ctl->unspec_addr, ctl->af)) {
     struct hostent *host = ctl->dns ? addr2host((void *) addr, ctl->af) : NULL;
     if (!host) return snprintf(dst, dst_len, "%s", strlongip(ctl, addr));
     else if (ctl->dns && ctl->show_ips)
@@ -97,12 +97,12 @@ extern void report_close(struct mtr_ctl *ctl)
   
 #ifdef HAVE_IPINFO
   int len_tmp = len_hosts;
-  if (ipinfo_no >= 0) {
-    ipinfo_no %= iiwidth_len;
+  if (ctl->ipinfo_no >= 0) {
+    ctl->ipinfo_no %= ctl->iiwidth_len;
     if (ctl->reportwide) {
       len_hosts++;    // space
-      len_tmp   += get_iiwidth();
-      if (!ipinfo_no)
+      len_tmp   += get_iiwidth(ctl);
+      if (!ctl->ipinfo_no)
         len_tmp += 2; // align header: AS
     }
   }
@@ -130,7 +130,7 @@ extern void report_close(struct mtr_ctl *ctl)
     snprint_addr(ctl, name, sizeof(name), addr);
 
 #ifdef HAVE_IPINFO
-    if (is_printii()) {
+    if (is_printii(ctl)) {
       snprintf(fmt, sizeof(fmt), " %%2d. %%s%%-%zus", len_hosts);
       snprintf(buf, sizeof(buf), fmt, at+1, fmt_ipinfo(ctl, addr), name);
     } else {
@@ -164,7 +164,7 @@ extern void report_close(struct mtr_ctl *ctl)
       addr2 = net_addrs(at, z);
       mplss = net_mplss(at, z);
       int found = 0;
-      if ((addrcmp ((void *) &unspec_addr, (void *) addr2, ctl->af)) == 0)
+      if ((addrcmp ((void *) &ctl->unspec_addr, (void *) addr2, ctl->af)) == 0)
         break;
       for (w = 0; w < z; w++)
         /* Ok... checking if there are ips repeated on same hop */
@@ -176,7 +176,7 @@ extern void report_close(struct mtr_ctl *ctl)
       if (!found) {
   
 #ifdef HAVE_IPINFO
-        if (is_printii()) {
+        if (is_printii(ctl)) {
           if (mpls->labels && z == 1 && ctl->enablempls)
             print_mpls(mpls);
           snprint_addr(ctl, name, sizeof(name), addr2);
@@ -212,7 +212,7 @@ extern void report_close(struct mtr_ctl *ctl)
 
     /* No multipath */
 #ifdef HAVE_IPINFO
-    if (is_printii()) {
+    if (is_printii(ctl)) {
       if (mpls->labels && z == 1 && ctl->enablempls)
         print_mpls(mpls);
     } else {
@@ -427,7 +427,7 @@ extern void csv_close(struct mtr_ctl *ctl, time_t now)
     if (at == net_min(ctl)) {
       printf("Mtr_Version,Start_Time,Status,Host,Hop,Ip,");
 #ifdef HAVE_IPINFO
-      if(!ipinfo_no) {
+      if(!ctl->ipinfo_no) {
        printf("Asn,");
       }
 #endif
@@ -440,7 +440,7 @@ extern void csv_close(struct mtr_ctl *ctl, time_t now)
     }
 
 #ifdef HAVE_IPINFO
-    if(!ipinfo_no) {
+    if(!ctl->ipinfo_no) {
       char* fmtinfo = fmt_ipinfo(ctl, addr);
       fmtinfo = trim(fmtinfo);
       printf("MTR.%s,%lld,%s,%s,%d,%s,%s", PACKAGE_VERSION, (long long)now, "OK", ctl->Hostname,
index ff3ca994969e3beb68a49117674e3998f439e3e0..208ccbe37d44b0a25cd211de2966a9dfc53101e0 100644 (file)
--- a/select.c
+++ b/select.c
@@ -236,12 +236,12 @@ extern void select_loop(struct mtr_ctl *ctl){
        break;
 #ifdef HAVE_IPINFO
       case ActionII:
-       ipinfo_no++;
-       if (ipinfo_no > ipinfo_max)
-         ipinfo_no = 0;
+       ctl->ipinfo_no++;
+       if (ctl->ipinfo_no > ctl->ipinfo_max)
+         ctl->ipinfo_no = 0;
        break;
       case ActionAS:
-       ipinfo_no = ipinfo_no?0:ipinfo_max;
+       ctl->ipinfo_no = ctl->ipinfo_no ? 0 : ctl->ipinfo_max;
        break;
 #endif
 
diff --git a/split.c b/split.c
index e97bbd99ba3ca899d2ab64e04f610987bc52832f..7917a3f1a38480e39dac4ef389cf8b589bcc0b3e 100644 (file)
--- a/split.c
+++ b/split.c
@@ -90,7 +90,7 @@ extern void split_redraw(struct mtr_ctl *ctl)
    */
   for(at = 0; at < max; at++) {
     addr = net_addr(at);
-    if(addrcmp((void*)addr, (void*)&unspec_addr, ctl->af)) {
+    if(addrcmp((void*)addr, (void*)&ctl->unspec_addr, ctl->af)) {
       char str[256], *name;
       if (!(name = dns_lookup(ctl, addr)))
         name = strlongip(ctl, addr);