From c33ef3747d6a4b31f548b42a5c2544dc036e6e7c Mon Sep 17 00:00:00 2001 From: Assar Westerlund Date: Sat, 4 Dec 1999 18:04:09 +0000 Subject: [PATCH] (doit): rewrite to use getaddrinfo git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7495 ec53bebd-3082-4978-b11e-865c3cabbd6b --- appl/kf/kf.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/appl/kf/kf.c b/appl/kf/kf.c index 3b9b04b05..f4f1bcae7 100644 --- a/appl/kf/kf.c +++ b/appl/kf/kf.c @@ -309,43 +309,38 @@ proto (int sock, const char *hostname, const char *service) static int doit (const char *hostname, int port, const char *service) { - struct hostent *hostent = NULL; - char **h; + struct addrinfo *ai, *a; + struct addrinfo hints; int error; - int af; + char portstr[NI_MAXSERV]; -#ifdef HAVE_IPV6 - if (hostent == NULL) - hostent = getipnodebyname (hostname, AF_INET6, 0, &error); -#endif - if (hostent == NULL) - hostent = getipnodebyname (hostname, AF_INET, 0, &error); + memset (&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; - if (hostent == NULL) - errx(1, "gethostbyname '%s' failed: %s", hostname, hstrerror(error)); + snprintf (portstr, sizeof(portstr), "%u", ntohs(port)); - 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) { - struct sockaddr_storage sa_ss; - struct sockaddr *sa = (struct sockaddr *)&sa_ss; + for (a = ai; a != NULL; a = a->ai_next) { int s; - sa->sa_family = af; - socket_set_address_and_port (sa, *h, port); - - s = socket (af, SOCK_STREAM, 0); + s = socket (a->ai_family, a->ai_socktype, a->ai_protocol); if (s < 0) - err (1, "socket"); - if (connect (s, sa, socket_sockaddr_size(sa)) < 0) { + continue; + if (connect (s, a->ai_addr, a->ai_addrlen) < 0) { warn ("connect(%s)", hostname); close (s); continue; } - freehostent (hostent); + freeaddrinfo (ai); return proto (s, hostname, service); } - freehostent (hostent); + warnx ("failed to contact %s", hostname); + freeaddrinfo (ai); return 1; }