dirmngr: Refactor 'map_host'.

* dirmngr/ks-engine-hkp.c (add_host): New function.
(map_host): Use the new function.

Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
Justus Winter 2015-11-19 13:00:59 +01:00
parent 23ea641ba2
commit 3f52f6bcac
1 changed files with 110 additions and 99 deletions

View File

@ -256,6 +256,115 @@ arecords_is_pool (dns_addrinfo_t aibuf)
}
/* Add the host AI under the NAME into the HOSTTABLE. Updates the
given reference table. */
static void
add_host (const char *name, const dns_addrinfo_t ai, int is_pool,
int *reftbl, size_t reftblsize, int *refidx)
{
gpg_error_t tmperr;
char *tmphost;
int idx, tmpidx;
int is_numeric = 0;
int i;
idx = find_hostinfo (name);
if (!is_pool && !is_ip_address (name))
{
/* This is a hostname but not a pool. Use the name
as given without going through resolve_dns_addr. */
tmphost = xtrystrdup (name);
if (!tmphost)
tmperr = gpg_error_from_syserror ();
else
tmperr = 0;
}
else
{
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
DNS_WITHBRACKET, &tmphost);
if (tmphost && is_ip_address (tmphost))
is_numeric = 1;
}
if (tmperr)
{
log_info ("resolve_dns_addr failed while checking '%s': %s\n",
name, gpg_strerror (tmperr));
}
else if ((*refidx) + 1 >= reftblsize)
{
log_error ("resolve_dns_addr for '%s': '%s'"
" [index table full - ignored]\n", name, tmphost);
}
else
{
if (!is_pool && is_ip_address (name))
/* Update the original entry. */
tmpidx = idx;
else
tmpidx = find_hostinfo (tmphost);
log_info ("resolve_dns_addr for '%s': '%s'%s\n",
name, tmphost,
tmpidx == -1? "" : " [already known]");
if (tmpidx == -1) /* Create a new entry. */
tmpidx = create_new_hostinfo (tmphost);
if (tmpidx == -1)
{
log_error ("map_host for '%s' problem: %s - '%s'"
" [ignored]\n",
name, strerror (errno), tmphost);
}
else /* Set or update the entry. */
{
char *ipaddr = NULL;
if (!is_numeric)
{
xfree (tmphost);
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
(DNS_NUMERICHOST
| DNS_WITHBRACKET),
&tmphost);
if (tmperr)
log_info ("resolve_dns_addr failed: %s\n",
gpg_strerror (tmperr));
else
{
ipaddr = tmphost;
tmphost = NULL;
}
}
if (ai->family == AF_INET6)
{
hosttable[tmpidx]->v6 = 1;
xfree (hosttable[tmpidx]->v6addr);
hosttable[tmpidx]->v6addr = ipaddr;
}
else if (ai->family == AF_INET)
{
hosttable[tmpidx]->v4 = 1;
xfree (hosttable[tmpidx]->v4addr);
hosttable[tmpidx]->v4addr = ipaddr;
}
else
BUG ();
for (i=0; i < *refidx; i++)
if (reftbl[i] == tmpidx)
break;
if (!(i < *refidx) && tmpidx != idx)
reftbl[(*refidx)++] = tmpidx;
}
}
xfree (tmphost);
}
/* Map the host name NAME to the actual to be used host name. This
allows us to manage round robin DNS names. We use our own strategy
to choose one of the hosts. For example we skip those hosts which
@ -346,109 +455,11 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
for (ai = aibuf; ai; ai = ai->next)
{
gpg_error_t tmperr;
char *tmphost;
int tmpidx;
int is_numeric = 0;
int i;
if (ai->family != AF_INET && ai->family != AF_INET6)
continue;
dirmngr_tick (ctrl);
if (!is_pool && !is_ip_address (name))
{
/* This is a hostname but not a pool. Use the name
as given without going through resolve_dns_addr. */
tmphost = xtrystrdup (name);
if (!tmphost)
tmperr = gpg_error_from_syserror ();
else
tmperr = 0;
}
else
{
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
DNS_WITHBRACKET, &tmphost);
if (tmphost && is_ip_address (tmphost))
is_numeric = 1;
}
if (tmperr)
{
log_info ("resolve_dns_addr failed while checking '%s': %s\n",
name, gpg_strerror (tmperr));
}
else if (refidx+1 >= reftblsize)
{
log_error ("resolve_dns_addr for '%s': '%s'"
" [index table full - ignored]\n", name, tmphost);
}
else
{
if (!is_pool && is_ip_address (name))
/* Update the original entry. */
tmpidx = idx;
else
tmpidx = find_hostinfo (tmphost);
log_info ("resolve_dns_addr for '%s': '%s'%s\n",
name, tmphost,
tmpidx == -1? "" : " [already known]");
if (tmpidx == -1) /* Create a new entry. */
tmpidx = create_new_hostinfo (tmphost);
if (tmpidx == -1)
{
log_error ("map_host for '%s' problem: %s - '%s'"
" [ignored]\n",
name, strerror (errno), tmphost);
}
else /* Set or update the entry. */
{
char *ipaddr = NULL;
if (!is_numeric)
{
xfree (tmphost);
tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
(DNS_NUMERICHOST
| DNS_WITHBRACKET),
&tmphost);
if (tmperr)
log_info ("resolve_dns_addr failed: %s\n",
gpg_strerror (tmperr));
else
{
ipaddr = tmphost;
tmphost = NULL;
}
}
if (ai->family == AF_INET6)
{
hosttable[tmpidx]->v6 = 1;
xfree (hosttable[tmpidx]->v6addr);
hosttable[tmpidx]->v6addr = ipaddr;
}
else if (ai->family == AF_INET)
{
hosttable[tmpidx]->v4 = 1;
xfree (hosttable[tmpidx]->v4addr);
hosttable[tmpidx]->v4addr = ipaddr;
}
else
BUG ();
for (i=0; i < refidx; i++)
if (reftbl[i] == tmpidx)
break;
if (!(i < refidx) && tmpidx != idx)
reftbl[refidx++] = tmpidx;
}
}
xfree (tmphost);
add_host (name, ai, is_pool, reftbl, reftblsize, &refidx);
}
}
reftbl[refidx] = -1;