resolve: split sub-functions out of tor_addr_lookup()

And remove the practracker exception for tor_addr_lookup().

Cleanup after 30721.
This commit is contained in:
teor 2019-06-02 18:42:01 +10:00 committed by Nick Mathewson
parent 29cf64c838
commit fb93646c1c
2 changed files with 134 additions and 88 deletions

View File

@ -272,7 +272,6 @@ problem function-size /src/lib/math/prob_distr.c:sample_uniform_interval() 145
problem function-size /src/lib/net/address.c:tor_addr_parse_mask_ports() 198
problem function-size /src/lib/net/address.c:tor_addr_compare_masked() 111
problem function-size /src/lib/net/inaddr.c:tor_inet_pton() 107
problem function-size /src/lib/net/resolve.c:tor_addr_lookup() 110
problem function-size /src/lib/net/socketpair.c:tor_ersatz_socketpair() 102
problem function-size /src/lib/osinfo/uname.c:get_uname() 116
problem function-size /src/lib/process/process_unix.c:process_unix_exec() 220

View File

@ -58,51 +58,18 @@ tor_lookup_hostname,(const char *name, uint32_t *addr))
return -1;
}
/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
* *<b>addr</b> to the proper IP address and family. The <b>family</b>
* argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
* <i>preferred</i> family, though another one may be returned if only one
* family is implemented for this address.
*
* Like tor_addr_parse(), this function accepts IPv6 addresses with or without
* square brackets.
*
* Return 0 on success, -1 on failure; 1 on transient failure.
*/
MOCK_IMPL(int,
tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
{
/* Perhaps eventually this should be replaced by a tor_getaddrinfo or
* something.
*/
int parsed_family = 0;
tor_assert(name);
tor_assert(addr);
tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
if (!*name) {
/* Empty address is an error. */
memset(addr, 0, sizeof(tor_addr_t));
return -1;
}
/* Is it an IP address? */
parsed_family = tor_addr_parse(addr, name);
if (parsed_family >= 0) {
/* If the IP address family matches, or was unspecified */
if (parsed_family == family || family == AF_UNSPEC) {
return 0;
} else {
/* Clear the address before returning an error. */
memset(addr, 0, sizeof(tor_addr_t));
return -1;
}
} else {
/* Clear the address after a failed tor_addr_parse(). */
memset(addr, 0, sizeof(tor_addr_t));
#ifdef HAVE_GETADDRINFO
/* Host lookup helper for tor_addr_lookup(), when getaddrinfo() is
* available on this system.
*
* See tor_addr_lookup() for details.
*/
static int
tor_addr_lookup_host_getaddrinfo(const char *name,
uint16_t family,
tor_addr_t *addr)
{
int err;
struct addrinfo *res=NULL, *res_p;
struct addrinfo *best=NULL;
@ -144,7 +111,19 @@ tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
return result;
}
return (err == EAI_AGAIN) ? 1 : -1;
}
#else /* !(defined(HAVE_GETADDRINFO)) */
/* Host lookup helper for tor_addr_lookup(), which calls getaddrinfo().
* Used when gethostbyname() 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)
{
struct hostent *ent;
int err;
#ifdef HAVE_GETHOSTBYNAME_R_6_ARG
@ -168,7 +147,7 @@ tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
err = WSAGetLastError();
#else
err = h_errno;
#endif
#endif /* defined(_WIN32) */
#endif /* defined(HAVE_GETHOSTBYNAME_R_6_ARG) || ... */
if (ent) {
if (ent->h_addrtype == AF_INET) {
@ -185,8 +164,76 @@ tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
#else
return (err == TRY_AGAIN) ? 1 : -1;
#endif
}
#endif /* defined(HAVE_GETADDRINFO) */
/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
* *<b>addr</b> to the proper IP address and family. The <b>family</b>
* argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
* <i>preferred</i> family, though another one may be returned if only one
* family is implemented for this address.
*
* Like tor_addr_parse(), this function accepts IPv6 addresses with or without
* square brackets.
*
* Return 0 on success, -1 on failure; 1 on transient failure.
*/
MOCK_IMPL(int,
tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
{
/* Perhaps eventually this should be replaced by a tor_getaddrinfo or
* something.
*/
int parsed_family = 0;
int result = -1;
tor_assert(name);
tor_assert(addr);
tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
if (!*name) {
/* Empty address is an error. */
goto permfail;
}
/* Is it an IP address? */
parsed_family = tor_addr_parse(addr, name);
if (parsed_family >= 0) {
/* If the IP address family matches, or was unspecified */
if (parsed_family == family || family == AF_UNSPEC) {
goto success;
} else {
goto permfail;
}
} 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);
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,
* assume it's a permanent failure */
permfail:
result = -1;
goto done;
success:
result = 0;
/* We have set the result, now it's time to clean up */
done:
if (result) {
/* Clear the address on error */
memset(addr, 0, sizeof(tor_addr_t));
}
return result;
}
/** Parse an address or address-port combination from <b>s</b>, resolve the