(hookup): re-write to use getaddrinfo
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7493 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -55,62 +55,56 @@ typedef void (*sighand) (int);
|
|||||||
char *
|
char *
|
||||||
hookup (const char *host, int port)
|
hookup (const char *host, int port)
|
||||||
{
|
{
|
||||||
struct hostent *hp = NULL;
|
|
||||||
int s, len;
|
|
||||||
static char hostnamebuf[MaxHostNameLen];
|
static char hostnamebuf[MaxHostNameLen];
|
||||||
|
struct addrinfo *ai, *a;
|
||||||
|
struct addrinfo hints;
|
||||||
int error;
|
int error;
|
||||||
int af;
|
char portstr[NI_MAXSERV];
|
||||||
char **h;
|
int len;
|
||||||
int ret;
|
int s;
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
memset (&hints, 0, sizeof(hints));
|
||||||
if (hp == NULL)
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hp = getipnodebyname (host, AF_INET6, 0, &error);
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
#endif
|
hints.ai_flags = AI_CANONNAME;
|
||||||
if (hp == NULL)
|
|
||||||
hp = getipnodebyname (host, AF_INET, 0, &error);
|
|
||||||
|
|
||||||
if (hp == NULL) {
|
snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
|
||||||
warnx ("%s: %s", host, hstrerror(error));
|
|
||||||
|
error = getaddrinfo (host, portstr, &hints, &ai);
|
||||||
|
if (error) {
|
||||||
|
warnx ("%s: %s", host, gai_strerror(error));
|
||||||
code = -1;
|
code = -1;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
strlcpy (hostnamebuf, hp->h_name, sizeof(hostnamebuf));
|
strlcpy (hostnamebuf, ai->ai_canonname, sizeof(hostnamebuf));
|
||||||
hostname = hostnamebuf;
|
hostname = hostnamebuf;
|
||||||
af = hisctladdr->sa_family = hp->h_addrtype;
|
|
||||||
|
|
||||||
for (h = hp->h_addr_list;
|
for (a = ai; a != NULL; a = a->ai_next) {
|
||||||
*h != NULL;
|
s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
|
||||||
++h) {
|
if (s < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
memcpy (hisctladdr, a->ai_addr, a->ai_addrlen);
|
||||||
|
|
||||||
s = socket (af, SOCK_STREAM, 0);
|
error = connect (s, a->ai_addr, a->ai_addrlen);
|
||||||
if (s < 0) {
|
if (error < 0) {
|
||||||
warn ("socket");
|
char addrstr[256];
|
||||||
code = -1;
|
|
||||||
freehostent (hp);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
socket_set_address_and_port (hisctladdr, *h, port);
|
if (getnameinfo (a->ai_addr, a->ai_addrlen,
|
||||||
|
addrstr, sizeof(addrstr),
|
||||||
ret = connect (s, hisctladdr, socket_sockaddr_size(hisctladdr));
|
NULL, 0, NI_NUMERICHOST) != 0)
|
||||||
if (ret < 0) {
|
strlcpy (addrstr, "unknown address", sizeof(addrstr));
|
||||||
char addr[256];
|
|
||||||
|
warn ("connect %s", addrstr);
|
||||||
if (inet_ntop (af, socket_get_address(hisctladdr),
|
|
||||||
addr, sizeof(addr)) == NULL)
|
|
||||||
strlcpy (addr, "unknown address",
|
|
||||||
sizeof(addr));
|
|
||||||
warn ("connect %s", addr);
|
|
||||||
close (s);
|
close (s);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
freehostent (hp);
|
freeaddrinfo (ai);
|
||||||
if (ret < 0) {
|
if (error < 0) {
|
||||||
|
warnx ("failed to contact %s", host);
|
||||||
code = -1;
|
code = -1;
|
||||||
close (s);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user