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