summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2005-02-03 21:31:04 +0000
committerNick Mathewson <nickm@torproject.org>2005-02-03 21:31:04 +0000
commita035032f09de213ae7f3137cfd3262067f4635a5 (patch)
treeafc0726b5284a3b1a2b80d1716a062eb45247081 /src/common
parent4174bf9cbdaaab1b86dc7c051ebf11f53412272f (diff)
downloadtor-a035032f09de213ae7f3137cfd3262067f4635a5.tar.gz
tor-a035032f09de213ae7f3137cfd3262067f4635a5.zip
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.
svn:r3522
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index fa30cd8739..c6c218881b 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -497,7 +497,6 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
* something.
*/
struct in_addr iaddr;
- struct hostent *ent;
tor_assert(addr);
if (!*name) {
/* Empty address is an error. */
@@ -507,7 +506,34 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
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 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
#else
return (h_errno == TRY_AGAIN) ? 1 : -1;
#endif
+#endif
}
}