[or-cvs] Use getaddrinfo and gethostbyname_r where available. Note ...

Nick Mathewson nickm at seul.org
Thu Feb 3 21:31:06 UTC 2005


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

Modified Files:
	compat.c 
Log Message:
Use getaddrinfo and gethostbyname_r where available.  Note that these are not necessarily threadsafe: this needs more thinking. Perhaps we should back down on this multithreading idea.

Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- compat.c	3 Feb 2005 19:59:10 -0000	1.32
+++ compat.c	3 Feb 2005 21:31:04 -0000	1.33
@@ -497,7 +497,6 @@
    * something.
    */
   struct in_addr iaddr;
-  struct hostent *ent;
   tor_assert(addr);
   if (!*name) {
     /* Empty address is an error. */
@@ -507,7 +506,34 @@
     memcpy(addr, &iaddr.s_addr, 4);
     return 0;
   } else {
-    ent = gethostbyname(name);
+#ifdef HAVE_GETADDRINFO
+    int err;
+    struct addrinfo *res, *res_p;
+    struct addrinfo hints;
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+    err = getaddrinfo(name, NULL, &hints, &res);
+    if (!err) {
+      for (res_p = res; res_p; res_p = res_p->ai_next) {
+        if (res_p->ai_family == PF_INET &&
+            res_p->ai_addrlen == 4) {
+          memcpy(addr, res_p->ai_addr, 4);
+          freeaddrinfo(res);
+          return 0;
+        }
+      }
+      return -1;
+    }
+
+    return (err == EAI_AGAIN) ? 1 : -1;
+#else
+    struct hostent *ent;
+#ifdef HAVE_GETHOSTBYNAME_R
+    ent = gethostbyname_r(name);
+#else
+    struct hostent *ent;
+#endif
     if (ent) {
       /* break to remind us if we move away from IPv4 */
       tor_assert(ent->h_length == 4);
@@ -520,6 +546,7 @@
 #else
     return (h_errno == TRY_AGAIN) ? 1 : -1;
 #endif
+#endif
   }
 }
 



More information about the tor-commits mailing list