[Iftop-users] alternative, hopefully more portable patch for s6_addr32

Scott Bertilson ssb at umn.edu
Sun, 5 Jun 2011 12:02:00 -0500


This patch doesn't require extra fiddling on either Linux (Gentoo) or
FreeBSD 6.1. =A0Since both have the 8 bit array for V6 addresses,
neither requires any additional OS-specific hacks. =A0I think this might
be more generally portable.

This is a diff against the CVS archive as of today.

Index: addr_hash.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/pdw/vcvs/repos/iftop/addr_hash.c,v
retrieving revision 1.6
diff -u -r1.6 addr_hash.c
--- addr_hash.c 27 Nov 2010 11:06:12 -0000      1.6
+++ addr_hash.c 5 Jun 2011 16:57:30 -0000
@@ -43,7 +43,7 @@
     addr_pair* ap =3D (addr_pair*)key;

     if (ap->af =3D=3D AF_INET6) {
-        uint32_t* addr6 =3D ap->src6.s6_addr32;
+        uint32_t* addr6 =3D (uint32_t*)ap->src6.s6_addr;

         hash =3D ( hash_uint32(addr6[0])
                 + hash_uint32(addr6[1])
@@ -51,7 +51,7 @@
                 + hash_uint32(addr6[3])
                 + ap->src_port) % 0xFF;

-        addr6 =3D ap->dst6.s6_addr32;
+        addr6 =3D (uint32_t*)ap->dst6.s6_addr;
         hash =3D ( hash + hash_uint32(addr6[0])
                 + hash_uint32(addr6[1])
                 + hash_uint32(addr6[2])

Index: iftop.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/pdw/vcvs/repos/iftop/iftop.c,v
retrieving revision 1.56
diff -u -r1.56 iftop.c
--- iftop.c     27 Nov 2010 11:06:12 -0000      1.56
+++ iftop.c     5 Jun 2011 16:57:30 -0000
@@ -328,11 +328,11 @@
         /* First reduce the participating addresses using the netfilter pr=
efix.
          * We need scratch pads to do this.
          */
-        for (j=3D0; j < 4; ++j) {
-            scribdst.s6_addr32[j] =3D ip6tr->ip6_dst.s6_addr32[j]
-                                        & options.netfilter6mask.s6_addr32=
[j];
-            scribsrc.s6_addr32[j] =3D ip6tr->ip6_src.s6_addr32[j]
-                                        & options.netfilter6mask.s6_addr32=
[j];
+        for (j=3D0; j < 16; ++j) {
+            scribdst.s6_addr[j] =3D ip6tr->ip6_dst.s6_addr[j]
+                                        & options.netfilter6mask.s6_addr[j=
];
+            scribsrc.s6_addr[j] =3D ip6tr->ip6_src.s6_addr[j]
+                                        & options.netfilter6mask.s6_addr[j=
];
         }

         /* Now look for any hits. */

Index: ns_hash.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/pdw/vcvs/repos/iftop/ns_hash.c,v
retrieving revision 1.4
diff -u -r1.4 ns_hash.c
--- ns_hash.c   27 Nov 2010 11:06:12 -0000      1.4
+++ ns_hash.c   5 Jun 2011 16:57:30 -0000
@@ -29,7 +29,7 @@

 int ns_hash_hash(void* key) {
     int hash;
-    uint32_t* addr6 =3D ((struct in6_addr *) key)->s6_addr32;
+    uint32_t* addr6 =3D (uint32_t*)((struct in6_addr *) key)->s6_addr;

     hash =3D ( hash_uint32(addr6[0])
             + hash_uint32(addr6[1])

Index: options.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/pdw/vcvs/repos/iftop/options.c,v
retrieving revision 1.46
diff -u -r1.46 options.c
--- options.c   27 Nov 2010 11:06:12 -0000      1.46
+++ options.c   5 Jun 2011 16:57:30 -0000
@@ -538,16 +538,16 @@
             }
             else {
                 int bl, rem;
-                const uint32_t mm =3D 0xffffffff;
-                uint32_t part =3D mm;
+                const uint8_t mm =3D 0xff;
+                uint8_t part =3D mm;

-                bl =3D n / 32;
-                rem =3D n % 32;
-                part <<=3D 32 - rem;
+                bl =3D n / 8;
+                rem =3D n % 8;
+                part <<=3D 8 - rem;
                 for (j=3D0; j < bl; ++j)
-                    options.netfilter6mask.s6_addr32[j] =3D htonl(mm);
+                    options.netfilter6mask.s6_addr[j] =3D mm;

                 if (rem > 0)
-                    options.netfilter6mask.s6_addr32[bl] =3D htonl(part);
+                    options.netfilter6mask.s6_addr[bl] =3D part;
                 options.netfilter6 =3D 1;
             }
         }
@@ -560,8 +560,8 @@
             }
         }
         /* Prepare any comparison by masking the provided filtered net. */
-        for (j=3D0; j < 4; ++j)
-            options.netfilter6net.s6_addr32[j] &=3D
options.netfilter6mask.s6_addr32[j];
+        for (j=3D0; j < 16; ++j)
+            options.netfilter6net.s6_addr[j] &=3D
options.netfilter6mask.s6_addr[j];

         return 1;
     }