RPKI: Improve error handling of DNS resolver

This commit is contained in:
Ondrej Zajicek (work) 2021-03-17 17:24:00 +01:00
parent 0a3db4c680
commit 454ae30445

View file

@ -19,12 +19,13 @@
/** /**
* rpki_hostname_autoresolv - auto-resolve an IP address from a hostname * rpki_hostname_autoresolv - auto-resolve an IP address from a hostname
* @host: domain name of host, e.g. "rpki-validator.realmv6.org" * @host: domain name of host, e.g. "rpki-validator.realmv6.org"
* @err_msg: error message returned in case of errors
* *
* This function resolves an IP address from a hostname. * This function resolves an IP address from a hostname.
* Returns &ip_addr structure with IP address or |IPA_NONE|. * Returns &ip_addr structure with IP address or |IPA_NONE|.
*/ */
static ip_addr static ip_addr
rpki_hostname_autoresolv(const char *host) rpki_hostname_autoresolv(const char *host, const char **err_msg)
{ {
struct addrinfo *res; struct addrinfo *res;
struct addrinfo hints = { struct addrinfo hints = {
@ -33,13 +34,15 @@ rpki_hostname_autoresolv(const char *host)
.ai_flags = AI_ADDRCONFIG, .ai_flags = AI_ADDRCONFIG,
}; };
*err_msg = NULL;
if (!host) if (!host)
return IPA_NONE; return IPA_NONE;
int err_code = getaddrinfo(host, NULL, &hints, &res); int err_code = getaddrinfo(host, NULL, &hints, &res);
if (err_code != 0) if (err_code != 0)
{ {
log(L_DEBUG "getaddrinfo failed: %s", gai_strerror(err_code)); *err_msg = gai_strerror(err_code);
return IPA_NONE; return IPA_NONE;
} }
@ -83,12 +86,15 @@ rpki_tr_open(struct rpki_tr_sock *tr)
sk->tbsize = RPKI_TX_BUFFER_SIZE; sk->tbsize = RPKI_TX_BUFFER_SIZE;
sk->tos = IP_PREC_INTERNET_CONTROL; sk->tos = IP_PREC_INTERNET_CONTROL;
if (ipa_zero2(sk->daddr) && sk->host) if (ipa_zero(sk->daddr) && sk->host)
{ {
sk->daddr = rpki_hostname_autoresolv(sk->host); const char *err_msg;
sk->daddr = rpki_hostname_autoresolv(sk->host, &err_msg);
if (ipa_zero(sk->daddr)) if (ipa_zero(sk->daddr))
{ {
CACHE_TRACE(D_EVENTS, cache, "Cannot resolve the hostname '%s'", sk->host); log(L_ERR "%s: Cannot resolve hostname '%s': %s",
cache->p->p.name, sk->host, err_msg);
return RPKI_TR_ERROR; return RPKI_TR_ERROR;
} }
} }