1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-02 22:38:02 +02:00

dirmngr: Replace use of getnameinfo by resolve_dns_addr.

* dirmngr/ks-engine-hkp.c (my_getnameinfo): Remove.
(map_host): Use resolve_dns_addr.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-10-24 12:25:17 +02:00
parent 816505958a
commit 927f34603d
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 37 additions and 78 deletions

View File

@ -234,52 +234,6 @@ select_random_host (int *table)
} }
/* Simplified version of getnameinfo which also returns a numeric
hostname inside of brackets. The caller should provide a buffer
for HOST which is 2 bytes larger than the largest hostname. If
NUMERIC is true the returned value is numeric IP address. Returns
0 on success or an EAI error code. True is stored at R_ISNUMERIC
if HOST has a numeric IP address. */
static int
my_getnameinfo (dns_addrinfo_t ai, char *host, size_t hostlen,
int numeric, int *r_isnumeric)
{
int ec;
char *p;
*r_isnumeric = 0;
if (hostlen < 5)
return EAI_OVERFLOW;
if (numeric)
ec = EAI_NONAME;
else
ec = getnameinfo (ai->addr, ai->addrlen,
host, hostlen, NULL, 0, NI_NAMEREQD);
if (!ec && *host == '[')
ec = EAI_FAIL; /* A name may never start with a bracket. */
else if (ec == EAI_NONAME)
{
p = host;
if (ai->family == AF_INET6)
{
*p++ = '[';
hostlen -= 2;
}
ec = getnameinfo (ai->addr, ai->addrlen,
p, hostlen, NULL, 0, NI_NUMERICHOST);
if (!ec && ai->family == AF_INET6)
strcat (host, "]");
*r_isnumeric = 1;
}
return ec;
}
/* Map the host name NAME to the actual to be used host name. This /* 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 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 to choose one of the hosts. For example we skip those hosts which
@ -373,10 +327,10 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
for (ai = aibuf; ai; ai = ai->next) for (ai = aibuf; ai; ai = ai->next)
{ {
char tmphost[NI_MAXHOST + 2]; gpg_error_t tmperr;
char *tmphost;
int tmpidx; int tmpidx;
int is_numeric; int is_numeric = 0;
int ec;
int i; int i;
if (ai->family != AF_INET && ai->family != AF_INET6) if (ai->family != AF_INET && ai->family != AF_INET6)
@ -387,37 +341,35 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
if (!is_pool && !is_ip_address (name)) if (!is_pool && !is_ip_address (name))
{ {
/* This is a hostname but not a pool. Use the name /* This is a hostname but not a pool. Use the name
as given without going through getnameinfo. */ as given without going through resolve_dns_addr. */
if (strlen (name)+1 > sizeof tmphost) tmphost = xtrystrdup (name);
{ if (!tmphost)
ec = EAI_SYSTEM; tmperr = gpg_error_from_syserror ();
gpg_err_set_errno (EINVAL);
}
else else
{ tmperr = 0;
ec = 0;
strcpy (tmphost, name);
}
is_numeric = 0;
} }
else else
ec = my_getnameinfo (ai, tmphost, sizeof tmphost,
0, &is_numeric);
if (ec)
{ {
log_info ("getnameinfo failed while checking '%s': %s\n", tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
name, gai_strerror (ec)); 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) else if (refidx+1 >= reftblsize)
{ {
log_error ("getnameinfo returned for '%s': '%s'" log_error ("resolve_dns_addr for '%s': '%s'"
" [index table full - ignored]\n", name, tmphost); " [index table full - ignored]\n", name, tmphost);
} }
else else
{ {
tmpidx = find_hostinfo (tmphost); tmpidx = find_hostinfo (tmphost);
log_info ("getnameinfo returned for '%s': '%s'%s\n", log_info ("resolve_dns_addr for '%s': '%s'%s\n",
name, tmphost, name, tmphost,
tmpidx == -1? "" : " [already known]"); tmpidx == -1? "" : " [already known]");
@ -436,13 +388,19 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
if (!is_numeric) if (!is_numeric)
{ {
ec = my_getnameinfo (ai, tmphost, sizeof tmphost, xfree (tmphost);
1, &is_numeric); tmperr = resolve_dns_addr (ai->addr, ai->addrlen,
if (!ec && !(ipaddr = xtrystrdup (tmphost))) (DNS_NUMERICHOST
ec = EAI_SYSTEM; | DNS_WITHBRACKET),
if (ec) &tmphost);
log_info ("getnameinfo failed: %s\n", if (tmperr)
gai_strerror (ec)); log_info ("resolve_dns_addr failed: %s\n",
gpg_strerror (tmperr));
else
{
ipaddr = tmphost;
tmphost = NULL;
}
} }
if (ai->family == AF_INET6) if (ai->family == AF_INET6)
@ -467,6 +425,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
reftbl[refidx++] = tmpidx; reftbl[refidx++] = tmpidx;
} }
} }
xfree (tmphost);
} }
} }
reftbl[refidx] = -1; reftbl[refidx] = -1;

View File

@ -225,7 +225,7 @@ main (int argc, char **argv)
| (opt_bracket? DNS_WITHBRACKET:0)), | (opt_bracket? DNS_WITHBRACKET:0)),
&host); &host);
if (err) if (err)
printf ("[getnameinfo failed: %s]", gpg_strerror (err)); printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
else else
{ {
printf ("%s", host); printf ("%s", host);
@ -236,7 +236,7 @@ main (int argc, char **argv)
(opt_bracket? DNS_WITHBRACKET:0), (opt_bracket? DNS_WITHBRACKET:0),
&host); &host);
if (err) if (err)
printf ("[getnameinfo failed (2): %s]", gpg_strerror (err)); printf ("[resolve_dns_addr failed (2): %s]", gpg_strerror (err));
else else
{ {
if (!is_ip_address (host)) if (!is_ip_address (host))