diff options
author | Nick Mathewson <nickm@torproject.org> | 2005-02-22 04:50:31 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2005-02-22 04:50:31 +0000 |
commit | d060f845f22689e3a3d13ab37f3e0bd0641f7e4c (patch) | |
tree | a3c46854432672df5926b8dc2c851ffccc2e4e9e /src/common | |
parent | a68fcbf807106417fb2410ce6a2592befaebe328 (diff) | |
download | tor-d060f845f22689e3a3d13ab37f3e0bd0641f7e4c.tar tor-d060f845f22689e3a3d13ab37f3e0bd0641f7e4c.tar.gz |
Snarf some logic from python, adapted to our own needs, to handle gethostbyname_r correctly across platforms.
svn:r3649
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/compat.c | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index f80e1b4b4..b827b8175 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -530,10 +530,29 @@ int tor_lookup_hostname(const char *name, uint32_t *addr) 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 @@ int tor_lookup_hostname(const char *name, uint32_t *addr) } 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 } |