(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; int s;
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; int s;
struct sockaddr *sa = (struct sockaddr *)&sa_ss;
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");
if (connect(s, sa, socket_sockaddr_size(sa)) < 0) {
warn ("connect(%s)", hostname);
close (s);
continue; continue;
} else { if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
break; warn ("connect(%s)", hostname);
close (s);
continue;
} }
break;
} }
freehostent (hostent); freeaddrinfo (ai);
if (*h == NULL) if (a == 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");