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

[or-cvs] Snarf some logic from python, adapted to our own needs, to ...



Update of /home/or/cvsroot/tor/src/common
In directory moria.mit.edu:/tmp/cvs-serv22707/src/common

Modified Files:
	compat.c 
Log Message:
Snarf some logic from python, adapted to our own needs, to handle gethostbyname_r correctly across platforms.

Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- compat.c	22 Feb 2005 02:51:55 -0000	1.36
+++ compat.c	22 Feb 2005 04:50:31 -0000	1.37
@@ -530,10 +530,29 @@
     return (err == EAI_AGAIN) ? 1 : -1;
 #else
     struct hostent *ent;
-#ifdef HAVE_GETHOSTBYNAME_R
-    ent = gethostbyname_r(name);
+    int err;
+#ifdef HAVE_GETHOSTBYNAME_R_6_ARG
+    char buf[2048];
+    struct hostent hostent;
+    int r;
+    r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
+#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG);
+    char buf[2048];
+    struct hostent hostent;
+    ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
+#elif defined(HAVE_GETHOSTBYNAME_R_3_ARG);
+    struct hostent_data data;
+    struct hostent hent;
+    memset(&data, 0, sizeof(data));
+    err = gethostbyname_r(name, &hent, &data);
+    ent = err ? NULL : &hent;
 #else
     ent = gethostbyname(name);
+#ifdef MS_WINDOWS
+    err = WSAGetLastError();
+#else
+    err = h_errno;
+#endif
 #endif
     if (ent) {
       /* break to remind us if we move away from IPv4 */
@@ -543,9 +562,9 @@
     }
     memset(addr, 0, 4);
 #ifdef MS_WINDOWS
-    return (WSAGetLastError() == WSATRY_AGAIN) ? 1 : -1;
+    return (err == WSATRY_AGAIN) ? 1 : -1;
 #else
-    return (h_errno == TRY_AGAIN) ? 1 : -1;
+    return (err == TRY_AGAIN) ? 1 : -1;
 #endif
 #endif
   }