(do_connect): v6-ify

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@6755 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-08-05 12:21:17 +00:00
parent cff9accccb
commit 3b5d22066c

View File

@@ -96,42 +96,51 @@ usage (int ret)
} }
static int static int
do_connect (const char *host, int port, int nodelay) do_connect (const char *hostname, int port, int nodelay)
{ {
struct hostent *h; struct hostent *hostent = NULL;
struct sockaddr_in addr; char **h;
char **p; int error;
int s = -1; int af;
int s;
h = roken_gethostbyname (host); #ifdef HAVE_IPV6
if (h == NULL) if (hostent == NULL)
errx (1, "gethostbyname: %s", hstrerror(h_errno)); hostent = getipnodebyname (hostname, AF_INET6, 0, &error);
memset (&addr, 0, sizeof(addr)); #endif
addr.sin_family = AF_INET; if (hostent == NULL)
addr.sin_port = port; hostent = getipnodebyname (hostname, AF_INET, 0, &error);
for (p = h->h_addr_list; *p; ++p) {
memcpy(&addr.sin_addr, *p, sizeof(addr.sin_addr));
s = socket (AF_INET, SOCK_STREAM, 0); 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"); err (1, "socket");
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { if (connect(s, sa, socket_sockaddr_size(sa)) < 0) {
warn ("connect(%s)", host); warn ("connect(%s)", hostname);
close (s); close (s);
continue; continue;
} else { } else {
break; break;
} }
} }
if (*p == NULL) freehostent (hostent);
if (*h == NULL)
return -1; return -1;
else { 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"); return s;
return s;
}
} }
typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP, typedef enum { INIT = 0, GREET, USER, PASS, STAT, RETR, TOP,