From 4e867b8a352744eb10aa7032936c96b762f4c144 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 11 Oct 2016 08:19:41 -0500 Subject: Make netcat work with nommu and factor out poll() loop into net/net.c. --- lib/lib.h | 1 + lib/net.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'lib') diff --git a/lib/lib.h b/lib/lib.h index 69692b73..2afe558b 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -281,6 +281,7 @@ 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); int xpoll(struct pollfd *fds, int nfds, int timeout); +int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout); // password.c int get_salt(char *salt, char * algo); diff --git a/lib/net.c b/lib/net.c index 2e72b268..1a46a5a9 100644 --- a/lib/net.c +++ b/lib/net.c @@ -55,3 +55,40 @@ int xpoll(struct pollfd *fds, int nfds, int timeout) } else return i; } } + +// Loop forwarding data from in1 to out1 and in2 to out2, handling +// half-connection shutdown. timeouts return if no data for X miliseconds. +// Returns 0: both closed, 1 shutdown_timeout, 2 timeout +int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout) +{ + struct pollfd pollfds[2]; + int i, pollcount = 2; + + memset(pollfds, 0, 2*sizeof(struct pollfd)); + pollfds[0].events = pollfds[1].events = POLLIN; + pollfds[0].fd = in1; + pollfds[1].fd = in2; + + // Poll loop copying data from each fd to the other one. + for (;;) { + if (!xpoll(pollfds, pollcount, timeout)) return pollcount; + + for (i=0; i