(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:
@@ -95,45 +95,41 @@ usage (int ret)
|
||||
static int
|
||||
do_connect (const char *hostname, int port, int nodelay)
|
||||
{
|
||||
struct hostent *hostent = NULL;
|
||||
char **h;
|
||||
struct addrinfo *ai, *a;
|
||||
struct addrinfo hints;
|
||||
int error;
|
||||
int af;
|
||||
int s;
|
||||
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) {
|
||||
warn ("connect(%s)", hostname);
|
||||
close (s);
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
|
||||
warn ("connect(%s)", hostname);
|
||||
close (s);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
freehostent (hostent);
|
||||
if (*h == NULL)
|
||||
freeaddrinfo (ai);
|
||||
if (a == NULL) {
|
||||
warnx ("failed to contact %s", hostname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
|
||||
(void *)&nodelay, sizeof(nodelay)) < 0)
|
||||
err (1, "setsockopt TCP_NODELAY");
|
||||
|
Reference in New Issue
Block a user