(do_connect): use `getaddrinfo'

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7538 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-12-05 13:06:43 +00:00
parent 96ba48cfef
commit ad622a77ad

View File

@@ -95,45 +95,41 @@ usage (int ret)
static int static int
do_connect (const char *hostname, int port, int nodelay) do_connect (const char *hostname, int port, int nodelay)
{ {
struct hostent *hostent = NULL; struct addrinfo *ai, *a;
char **h; struct addrinfo hints;
int error; int error;
int af; int s;
char portstr[NI_MAXSERV];
memset (&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
error = getaddrinfo (hostname, portstr, &hints, &ai);
if (error)
errx (1, "getaddrinfo(%s): %s", hostname, gai_strerror(error));
for (a = ai; a != NULL; a = a->ai_next) {
int s; int s;
#ifdef HAVE_IPV6 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
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;
sa->sa_family = af;
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;
} else { }
break; break;
} }
} freeaddrinfo (ai);
freehostent (hostent); if (a == NULL) {
if (*h == NULL) warnx ("failed to contact %s", hostname);
return -1; return -1;
}
if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY, if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
(void *)&nodelay, sizeof(nodelay)) < 0) (void *)&nodelay, sizeof(nodelay)) < 0)
err (1, "setsockopt TCP_NODELAY"); err (1, "setsockopt TCP_NODELAY");