(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;
}
if (connect (s, (struct sockaddr *)&thataddr, sizeof(thataddr)) < 0) { for (a = ai; a != NULL; a = a->ai_next) {
warn ("connect(%s)", kc->host); s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
close (s); if (s < 0)
continue; continue;
} else { if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
break; warn ("connect(%s)", kc->host);
} close (s);
} continue;
if (*p == NULL) }
return -1; break;
}
addrlen = sizeof(thisaddr); if (a == NULL) {
if (getsockname (s, (struct sockaddr *)&thisaddr, &addrlen) < 0 || freeaddrinfo (ai);
addrlen != sizeof(thisaddr)) return -1;
err(1, "getsockname(%s)", kc->host); }
kc->thisaddr = thisaddr;
kc->thataddr = thataddr; addrlen = a->ai_addrlen;
if ((*kc->authenticate)(kc, s)) if (getsockname (s, thisaddr, &addrlen) < 0 ||
return -1; addrlen != a->ai_addrlen)
return s; err(1, "getsockname(%s)", kc->host);
memcpy (&kc->thisaddr, thisaddr, sizeof(kc->thisaddr));
memcpy (&kc->thataddr, a->ai_addrlen, sizeof(kc->thataddr));
freeaddrinfo (ai);
if ((*kc->authenticate)(kc, s))
return -1;
return s;
} }
/* /*