(doit): rewrite to use getaddrinfo
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7495 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
41
appl/kf/kf.c
41
appl/kf/kf.c
@@ -309,43 +309,38 @@ proto (int sock, const char *hostname, const char *service)
|
|||||||
static int
|
static int
|
||||||
doit (const char *hostname, int port, const char *service)
|
doit (const char *hostname, int port, const char *service)
|
||||||
{
|
{
|
||||||
struct hostent *hostent = NULL;
|
struct addrinfo *ai, *a;
|
||||||
char **h;
|
struct addrinfo hints;
|
||||||
int error;
|
int error;
|
||||||
int af;
|
char portstr[NI_MAXSERV];
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
memset (&hints, 0, sizeof(hints));
|
||||||
if (hostent == NULL)
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hostent = getipnodebyname (hostname, AF_INET6, 0, &error);
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
#endif
|
|
||||||
if (hostent == NULL)
|
|
||||||
hostent = getipnodebyname (hostname, AF_INET, 0, &error);
|
|
||||||
|
|
||||||
if (hostent == NULL)
|
snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
|
||||||
errx(1, "gethostbyname '%s' failed: %s", hostname, hstrerror(error));
|
|
||||||
|
|
||||||
af = hostent->h_addrtype;
|
error = getaddrinfo (hostname, portstr, &hints, &ai);
|
||||||
|
if (error) {
|
||||||
|
errx (1, "getaddrinfo(%s): %s", hostname, gai_strerror(error));
|
||||||
|
}
|
||||||
|
|
||||||
for (h = hostent->h_addr_list; *h != NULL; ++h) {
|
for (a = ai; a != NULL; a = a->ai_next) {
|
||||||
struct sockaddr_storage sa_ss;
|
|
||||||
struct sockaddr *sa = (struct sockaddr *)&sa_ss;
|
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
sa->sa_family = af;
|
s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
|
||||||
socket_set_address_and_port (sa, *h, port);
|
|
||||||
|
|
||||||
s = socket (af, SOCK_STREAM, 0);
|
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
err (1, "socket");
|
continue;
|
||||||
if (connect (s, sa, socket_sockaddr_size(sa)) < 0) {
|
if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
|
||||||
warn ("connect(%s)", hostname);
|
warn ("connect(%s)", hostname);
|
||||||
close (s);
|
close (s);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
freehostent (hostent);
|
freeaddrinfo (ai);
|
||||||
return proto (s, hostname, service);
|
return proto (s, hostname, service);
|
||||||
}
|
}
|
||||||
freehostent (hostent);
|
warnx ("failed to contact %s", hostname);
|
||||||
|
freeaddrinfo (ai);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user