(get_socket): use getaddrinfo

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7632 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-12-20 05:41:24 +00:00
parent d11446db24
commit c8a35e1432

View File

@@ -66,44 +66,32 @@ loop(int s)
static int static int
get_socket (const char *hostname, int port) get_socket (const char *hostname, int port)
{ {
struct hostent *hostent = NULL; int ret;
char **h; struct addrinfo *ai, *a;
int error; struct addrinfo hints;
int af; 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 for (a = ai; a != NULL; a = a->ai_next) {
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;
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);
close (s); close (s);
continue; continue;
} }
freehostent (hostent); freeaddrinfo (ai);
return s; return s;
} }
freehostent (hostent); err (1, "failed to connect to %s", hostname);
exit (1);
} }
#ifdef KRB4 #ifdef KRB4