]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Replace 32-bit-based add_in6_addr() implementation by an 8-bit based one
authorGert Doering <gert@greenie.muc.de>
Sat, 28 May 2011 20:50:40 +0000 (22:50 +0200)
committerDavid Sommerseth <davids@redhat.com>
Thu, 25 Aug 2011 18:12:37 +0000 (20:12 +0200)
Windows has no 32-bit accessor to the union inside "struct in6_addr",
and the 8-bit accessor is the only common denominator across BSD, Solaris,
Linux and Windows...

Signed-off-by: Gert Doering <gert@greenie.muc.de>
Acked-by: Samuli Seppänen <samuli@openvpn.net>
Signed-off-by: David Sommerseth <davids@redhat.com>
socket.c

index 364585d1715d82b7d38c383202c24580a6df12f5..302c39f8382a4953fc65a2cd0d0fd2febacf1253 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -2620,32 +2620,30 @@ print_in6_addr (struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
   return BSTR (&out);
 }
 
+#ifndef UINT8_MAX
+# define UINT8_MAX 0xff
+#endif
+
 /* add some offset to an ipv6 address
- * (add in steps of 32 bits, taking overflow into next round)
+ * (add in steps of 8 bits, taking overflow into next round)
  */
-#ifndef s6_addr32
-# ifdef TARGET_SOLARIS
-#  define s6_addr32 _S6_un._S6_u32
-# else
-#  define s6_addr32 __u6_addr.__u6_addr32
-# endif
-#endif
-#ifndef UINT32_MAX
-# define UINT32_MAX (4294967295U)
-#endif
 struct in6_addr add_in6_addr( struct in6_addr base, uint32_t add )
 {
     int i;
-    uint32_t h;
 
-    for( i=3; i>=0 && add > 0 ; i-- )
+    for( i=15; i>=0 && add > 0 ; i-- )
     {
-       h = ntohl( base.s6_addr32[i] );
-       base.s6_addr32[i] = htonl( (h+add) & UINT32_MAX );
-       /* 32-bit overrun? 
-        * caveat: can't do "h+add > UINT32_MAX" with 32bit math!
+       register int carry;
+       register uint32_t h;
+
+       h = (unsigned char) base.s6_addr[i];
+       base.s6_addr[i] = (h+add) & UINT8_MAX;
+
+       /* using explicit carry for the 8-bit additions will catch
+         * 8-bit and(!) 32-bit overruns nicely
          */
-       add = ( h > UINT32_MAX - add )?  1: 0;
+       carry = ((h & 0xff)  + (add & 0xff)) >> 8;
+       add = (add>>8) + carry;
     }
     return base;
 }