Unify backend implementations for blocking hostname lookup

We have a getaddrinfo() implementation that we prefer, and a
gethostbyname*() implementation that we fall back on.  Give them
both the same interface, and let them be called by the same name.

This is a preparatory step for making them both mockable.
This commit is contained in:
Nick Mathewson 2019-10-02 21:22:53 -04:00
parent bca30bcb90
commit ac8f6d51f4

View File

@ -71,9 +71,9 @@ tor_lookup_hostname,(const char *name, uint32_t *addr))
* See tor_addr_lookup() for details.
*/
static int
tor_addr_lookup_host_getaddrinfo(const char *name,
uint16_t family,
tor_addr_t *addr)
tor_addr_lookup_host_impl(const char *name,
uint16_t family,
tor_addr_t *addr)
{
int err;
struct addrinfo *res=NULL, *res_p;
@ -120,15 +120,17 @@ tor_addr_lookup_host_getaddrinfo(const char *name,
#else /* !defined(HAVE_GETADDRINFO) */
/* Host lookup helper for tor_addr_lookup(), which calls getaddrinfo().
* Used when gethostbyname() is not available on this system.
/* Host lookup helper for tor_addr_lookup(), which calls gethostbyname().
* Used when getaddrinfo() is not available on this system.
*
* See tor_addr_lookup() for details.
*/
static int
tor_addr_lookup_host_gethostbyname(const char *name,
tor_addr_t *addr)
tor_addr_lookup_host_impl(const char *name,
uint16_t family,
tor_addr_t *addr)
{
(void) family;
struct hostent *ent;
int err;
#ifdef HAVE_GETHOSTBYNAME_R_6_ARG
@ -215,13 +217,8 @@ tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
} else {
/* Clear the address after a failed tor_addr_parse(). */
memset(addr, 0, sizeof(tor_addr_t));
#ifdef HAVE_GETADDRINFO
result = tor_addr_lookup_host_getaddrinfo(name, family, addr);
result = tor_addr_lookup_host_impl(name, family, addr);
goto done;
#else /* !(defined(HAVE_GETADDRINFO)) */
result = tor_addr_lookup_host_gethostbyname(name, addr);
goto done;
#endif /* defined(HAVE_GETADDRINFO) */
}
/* If we weren't successful, and haven't already set the result,