aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.h5
-rw-r--r--lib/net.c28
-rw-r--r--toys/net/ftpget.c3
-rw-r--r--toys/other/nbd_client.c2
-rw-r--r--toys/pending/telnet.c3
5 files changed, 26 insertions, 15 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 0b93bde9..bb8dfd7f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -284,8 +284,9 @@ void tty_sigreset(int i);
// net.c
int xsocket(int domain, int type, int protocol);
void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
-int xconnect(char *host, char *port, int family, int socktype, int protocol,
- int flags);
+struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
+ int protocol, int flags);
+int xconnect(struct addrinfo *ai_arg);
int xpoll(struct pollfd *fds, int nfds, int timeout);
int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
diff --git a/lib/net.c b/lib/net.c
index df2551f7..5c6d4f7c 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -15,11 +15,11 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt");
}
-int xconnect(char *host, char *port, int family, int socktype, int protocol,
- int flags)
+struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
+ int protocol, int flags)
{
- struct addrinfo info, *ai, *ai2;
- int fd;
+ struct addrinfo info, *ai;
+ int rc;
memset(&info, 0, sizeof(struct addrinfo));
info.ai_family = family;
@@ -27,20 +27,28 @@ int xconnect(char *host, char *port, int family, int socktype, int protocol,
info.ai_protocol = protocol;
info.ai_flags = flags;
- fd = getaddrinfo(host, port, &info, &ai);
- if (fd || !ai)
- error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
- fd ? gai_strerror(fd) : "not found");
+ rc = getaddrinfo(host, port, &info, &ai);
+ if (rc || !ai)
+ error_exit("%s%s%s: %s", host, port ? ":" : "", port ? port : "",
+ rc ? gai_strerror(rc) : "not found");
+
+ return ai;
+}
+
+int xconnect(struct addrinfo *ai_arg)
+{
+ struct addrinfo *ai;
+ int fd = -1;
// Try all the returned addresses. Report errors if last entry can't connect.
- for (ai2 = ai; ai; ai = ai->ai_next) {
+ for (ai = ai_arg; ai; ai = ai->ai_next) {
fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
ai->ai_protocol);
if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
else if (!ai->ai_next) perror_exit("connect");
close(fd);
}
- freeaddrinfo(ai2);
+ freeaddrinfo(ai_arg);
return fd;
}
diff --git a/toys/net/ftpget.c b/toys/net/ftpget.c
index 94e5caed..3c63905e 100644
--- a/toys/net/ftpget.c
+++ b/toys/net/ftpget.c
@@ -107,7 +107,8 @@ void ftpget_main(void)
if (!remote) remote = toys.optargs[1];
// connect
- TT.fd = xconnect(*toys.optargs, TT.port, 0, SOCK_STREAM, 0, AI_ADDRCONFIG);
+ TT.fd = xconnect(xgetaddrifo(*toys.optargs, TT.port, 0, SOCK_STREAM, 0,
+ AI_ADDRCONFIG));
if (getpeername(TT.fd, (void *)&si6, &sl)) perror_exit("getpeername");
// Login
diff --git a/toys/other/nbd_client.c b/toys/other/nbd_client.c
index 3ad366f3..fcd0fca8 100644
--- a/toys/other/nbd_client.c
+++ b/toys/other/nbd_client.c
@@ -52,7 +52,7 @@ void nbd_client_main(void)
// Find and connect to server
- sock = xconnect(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0);
+ sock = xconnect(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
temp = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
diff --git a/toys/pending/telnet.c b/toys/pending/telnet.c
index dc3487a5..e37b982c 100644
--- a/toys/pending/telnet.c
+++ b/toys/pending/telnet.c
@@ -306,7 +306,8 @@ void telnet_main(void)
}
terminal_size(&TT.win_width, &TT.win_height);
- TT.sfd = xconnect(*toys.optargs, port, 0, SOCK_STREAM, IPPROTO_TCP, 0);
+ TT.sfd = xconnect(xgetaddrinfo(*toys.optargs, port, 0, SOCK_STREAM,
+ IPPROTO_TCP, 0));
setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
setsockopt(TT.sfd, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));