(connect_host): use getaddrinfo

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7497 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-12-04 18:05:50 +00:00
parent 69c0957e94
commit 2afff852fe

View File

@@ -94,47 +94,51 @@ usr2handler (int sig)
static int static int
connect_host (kx_context *kc) connect_host (kx_context *kc)
{ {
struct addrinfo *ai, *a;
struct addrinfo hints;
int error;
char portstr[NI_MAXSERV];
int addrlen; int addrlen;
struct hostent *hostent;
int s; int s;
char **p; struct sockaddr_storage thisaddr_ss;
struct sockaddr_in thisaddr; struct sockaddr *thisaddr = (struct sockaddr *)&thisaddr_ss;
struct sockaddr_in thataddr;
hostent = gethostbyname (kc->host); memset (&hints, 0, sizeof(hints));
if (hostent == NULL) { hints.ai_socktype = SOCK_STREAM;
warnx ("gethostbyname '%s' failed: %s", kc->host, hints.ai_protocol = IPPROTO_TCP;
hstrerror(h_errno));
snprintf (portstr, sizeof(portstr), "%u", ntohs(kc->port));
error = getaddrinfo (kc->host, portstr, &hints, &ai);
if (error) {
warnx ("%s: %s", kc->host, gai_strerror(error));
return -1; return -1;
} }
memset (&thataddr, 0, sizeof(thataddr)); for (a = ai; a != NULL; a = a->ai_next) {
thataddr.sin_family = AF_INET; s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
thataddr.sin_port = kc->port;
for(p = hostent->h_addr_list; *p; ++p) {
memcpy (&thataddr.sin_addr, *p, sizeof(thataddr.sin_addr));
s = socket (AF_INET, SOCK_STREAM, 0);
if (s < 0) if (s < 0)
err (1, "socket"); continue;
if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
if (connect (s, (struct sockaddr *)&thataddr, sizeof(thataddr)) < 0) {
warn ("connect(%s)", kc->host); warn ("connect(%s)", kc->host);
close (s); close (s);
continue; continue;
} else { }
break; break;
} }
}
if (*p == NULL)
return -1;
addrlen = sizeof(thisaddr); if (a == NULL) {
if (getsockname (s, (struct sockaddr *)&thisaddr, &addrlen) < 0 || freeaddrinfo (ai);
addrlen != sizeof(thisaddr)) return -1;
}
addrlen = a->ai_addrlen;
if (getsockname (s, thisaddr, &addrlen) < 0 ||
addrlen != a->ai_addrlen)
err(1, "getsockname(%s)", kc->host); err(1, "getsockname(%s)", kc->host);
kc->thisaddr = thisaddr; memcpy (&kc->thisaddr, thisaddr, sizeof(kc->thisaddr));
kc->thataddr = thataddr; memcpy (&kc->thataddr, a->ai_addrlen, sizeof(kc->thataddr));
freeaddrinfo (ai);
if ((*kc->authenticate)(kc, s)) if ((*kc->authenticate)(kc, s))
return -1; return -1;
return s; return s;