[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r11155: Try to fix bug 482: do not rely on s6_addr16 or s6_addr32 on (in tor/trunk: . src/common src/or)



Author: nickm
Date: 2007-08-17 16:44:54 -0400 (Fri, 17 Aug 2007)
New Revision: 11155

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/src/common/compat.h
   tor/trunk/src/common/util.c
   tor/trunk/src/or/test.c
Log:
 r14634@catbus:  nickm | 2007-08-17 16:43:49 -0400
 Try to fix bug 482: do not rely on s6_addr16 or s6_addr32 on MSVC.  How ugly.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14634] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-08-17 20:44:51 UTC (rev 11154)
+++ tor/trunk/ChangeLog	2007-08-17 20:44:54 UTC (rev 11155)
@@ -8,6 +8,8 @@
 
   o Major bugfixes (compilation):
     - Try to fix win32 compilation again: Improve checking for ipv6 types.
+    - Try to fix MSVC compilation: build correctly on platforms that
+      do not define s6_addr16 or s6_addr32.
     - Fix compile on platforms without getaddrinfo: bug found by Li-Hui
       Zhou.
 

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2007-08-17 20:44:51 UTC (rev 11154)
+++ tor/trunk/src/common/compat.h	2007-08-17 20:44:54 UTC (rev 11155)
@@ -298,6 +298,16 @@
 typedef uint16_t sa_family_t;
 #endif
 
+#ifndef _MSC_VER
+/* Apparently, MSVC doesn't define s6_addr16 or s6_addr32. How dumb. */
+/* XXXX020 detect with autoconf. */
+#define S6_ADDR16(x) ((x).s6_addr16)
+#define S6_ADDR32(x) ((x).s6_addr32)
+#else
+#define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
+#define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
+#endif
+
 /* XXXX020 detect sockaddr_in6 correctly on ms_windows; this is also a hack. */
 #if !defined(HAVE_STRUCT_SOCKADDR_IN6) && !defined(MS_WINDOWS)
 struct sockaddr_in6 {
@@ -367,6 +377,9 @@
     return a->sa6.sin6_port;
 }
 
+#define IN6_ADDRESS16(x) S6_ADDR16(*IN6_ADDRESS(x))
+#define IN6_ADDRESS32(x) S6_ADDR32(*IN6_ADDRESS(x))
+
 #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
 #define TOR_ADDR_BUF_LEN 46 /* ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255 */
 

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2007-08-17 20:44:51 UTC (rev 11154)
+++ tor/trunk/src/common/util.c	2007-08-17 20:44:54 UTC (rev 11155)
@@ -1876,15 +1876,15 @@
   } else if (v_family == AF_INET6) {
     if (tor_addr_is_v4(addr)) { /* v4-mapped */
       v_family = AF_INET;
-      iph4 = ntohl(IN6_ADDRESS(addr)->s6_addr32[3]);
+      iph4 = ntohl(IN6_ADDRESS32(addr)[3]);
     }
   }
 
   if (v_family == AF_INET6) {
-    iph6[0] = ntohl(IN6_ADDRESS(addr)->s6_addr32[0]);
-    iph6[1] = ntohl(IN6_ADDRESS(addr)->s6_addr32[1]);
-    iph6[2] = ntohl(IN6_ADDRESS(addr)->s6_addr32[2]);
-    iph6[3] = ntohl(IN6_ADDRESS(addr)->s6_addr32[3]);
+    iph6[0] = ntohl(IN6_ADDRESS32(addr)[0]);
+    iph6[1] = ntohl(IN6_ADDRESS32(addr)[1]);
+    iph6[2] = ntohl(IN6_ADDRESS32(addr)[2]);
+    iph6[3] = ntohl(IN6_ADDRESS32(addr)[3]);
     if (for_listening && !iph6[0] && !iph6[1] && !iph6[2] && !iph6[3]) /* :: */
       return 0;
 
@@ -2396,9 +2396,9 @@
     return 1;
 
   if (IN_FAMILY(addr) == AF_INET6) { /* First two don't need to be ordered */
-    if ((IN6_ADDRESS(addr)->s6_addr32[0] == 0) &&
-        (IN6_ADDRESS(addr)->s6_addr32[1] == 0) &&
-        (ntohl(IN6_ADDRESS(addr)->s6_addr32[2]) == 0x0000ffffu))
+    if ((IN6_ADDRESS32(addr)[0] == 0) &&
+        (IN6_ADDRESS32(addr)[1] == 0) &&
+        (ntohl(IN6_ADDRESS32(addr)[2]) == 0x0000ffffu))
       return 1;
   }
 
@@ -2415,10 +2415,10 @@
 
   switch (IN_FAMILY(addr)) {
     case AF_INET6:
-      return (!IN6_ADDRESS(addr)->s6_addr32[0] &&
-              !IN6_ADDRESS(addr)->s6_addr32[1] &&
-              !IN6_ADDRESS(addr)->s6_addr32[2] &&
-              !IN6_ADDRESS(addr)->s6_addr32[3]);
+      return (!IN6_ADDRESS32(addr)[0] &&
+              !IN6_ADDRESS32(addr)[1] &&
+              !IN6_ADDRESS32(addr)[2] &&
+              !IN6_ADDRESS32(addr)[3]);
     case AF_INET:
       return (!IN4_ADDRESS(addr)->s_addr);
     default:
@@ -2540,8 +2540,8 @@
       return 1;
     return 0;
   } else if (v_family[0] == AF_INET6) { /* Real IPv6 */
-    const uint32_t *a1 = IN6_ADDRESS(addr1)->s6_addr32;
-    const uint32_t *a2 = IN6_ADDRESS(addr2)->s6_addr32;
+    const uint32_t *a1 = IN6_ADDRESS32(addr1);
+    const uint32_t *a2 = IN6_ADDRESS32(addr2);
     for (idx = 0; idx < 4; ++idx) {
       uint32_t masked_a = ntohl(a1[idx]);
       uint32_t masked_b = ntohl(a2[idx]);
@@ -2633,7 +2633,7 @@
     sock = tor_open_socket(PF_INET6,SOCK_DGRAM,IPPROTO_UDP);
     my_addr_len = sizeof(struct sockaddr_in6);
     sin6->sin6_family = AF_INET6;
-    sin6->sin6_addr.s6_addr16[0] = htons(0x2002); /* 2002:: */
+    S6_ADDR16(sin6->sin6_addr)[0] = htons(0x2002); /* 2002:: */
   } else if (family == AF_INET) {
     struct sockaddr_in *sin = (struct sockaddr_in*)&target_addr;
     sock = tor_open_socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2007-08-17 20:44:51 UTC (rev 11154)
+++ tor/trunk/src/or/test.c	2007-08-17 20:44:54 UTC (rev 11155)
@@ -1069,10 +1069,10 @@
  * conventions of the other test macros. */
 #define test_addr_parse_check(ip1, ip2, ip3, ip4, mm, pt1, pt2) STMT_BEGIN  \
     test_assert(r>=0);                                     \
-    test_eq(htonl(ip1), IN6_ADDRESS(&t1)->s6_addr32[0]);   \
-    test_eq(htonl(ip2), IN6_ADDRESS(&t1)->s6_addr32[1]);   \
-    test_eq(htonl(ip3), IN6_ADDRESS(&t1)->s6_addr32[2]);   \
-    test_eq(htonl(ip4), IN6_ADDRESS(&t1)->s6_addr32[3]);   \
+    test_eq(htonl(ip1), IN6_ADDRESS32(&t1)[0]);            \
+    test_eq(htonl(ip2), IN6_ADDRESS32(&t1)[1]);            \
+    test_eq(htonl(ip3), IN6_ADDRESS32(&t1)[2]);            \
+    test_eq(htonl(ip4), IN6_ADDRESS32(&t1)[3]);            \
     test_eq(mask, mm);                                     \
     test_eq(port1, pt1);                                   \
     test_eq(port2, pt2);                                   \