From c8a35e1432ff8c50899b2f8293a3503c46bbd5ef Mon Sep 17 00:00:00 2001 From: Assar Westerlund Date: Mon, 20 Dec 1999 05:41:24 +0000 Subject: [PATCH] (get_socket): use getaddrinfo git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7632 ec53bebd-3082-4978-b11e-865c3cabbd6b --- appl/popper/pop_debug.c | 46 +++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/appl/popper/pop_debug.c b/appl/popper/pop_debug.c index 77a85e150..de27b7e81 100644 --- a/appl/popper/pop_debug.c +++ b/appl/popper/pop_debug.c @@ -66,44 +66,32 @@ loop(int s) static int get_socket (const char *hostname, int port) { - struct hostent *hostent = NULL; - char **h; - int error; - int af; + int ret; + struct addrinfo *ai, *a; + struct addrinfo hints; + char portstr[NI_MAXSERV]; + + memset (&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + snprintf (portstr, sizeof(portstr), "%d", ntohs(port)); + ret = getaddrinfo (hostname, portstr, &hints, &ai); + if (ret) + errx (1, "getaddrinfo %s: %s", hostname, gai_strerror (ret)); -#ifdef HAVE_IPV6 - if (hostent == NULL) - hostent = getipnodebyname (hostname, AF_INET6, 0, &error); -#endif - if (hostent == NULL) - hostent = getipnodebyname (hostname, AF_INET, 0, &error); - - if (hostent == NULL) - errx(1, "gethostbyname '%s' failed: %s", hostname, hstrerror(error)); - - af = hostent->h_addrtype; - - 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) { - warn ("connect(%s)", hostname); + continue; + if (connect (s, a->ai_addr, a->ai_addrlen) < 0) { close (s); continue; } - freehostent (hostent); + freeaddrinfo (ai); return s; } - freehostent (hostent); - exit (1); + err (1, "failed to connect to %s", hostname); } #ifdef KRB4