Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 1 | #include "toys.h" |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 2 | |
| 3 | int xsocket(int domain, int type, int protocol) |
| 4 | { |
| 5 | int fd = socket(domain, type, protocol); |
| 6 | |
| 7 | if (fd < 0) perror_exit("socket %x %x", type, protocol); |
Rob Landley | a1a559e | 2017-01-04 01:32:44 -0600 | [diff] [blame] | 8 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 9 | |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 10 | return fd; |
| 11 | } |
Rob Landley | 2fd8624 | 2015-04-27 11:13:19 -0500 | [diff] [blame] | 12 | |
| 13 | void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len) |
| 14 | { |
| 15 | if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt"); |
| 16 | } |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 17 | |
Rob Landley | 35dafc7 | 2015-08-05 20:32:49 -0500 | [diff] [blame] | 18 | int xconnect(char *host, char *port, int family, int socktype, int protocol, |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 19 | int flags) |
| 20 | { |
Rob Landley | 31ff1f2 | 2015-08-05 21:20:27 -0500 | [diff] [blame] | 21 | struct addrinfo info, *ai, *ai2; |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 22 | int fd; |
| 23 | |
| 24 | memset(&info, 0, sizeof(struct addrinfo)); |
| 25 | info.ai_family = family; |
| 26 | info.ai_socktype = socktype; |
| 27 | info.ai_protocol = protocol; |
| 28 | info.ai_flags = flags; |
| 29 | |
Rob Landley | 35dafc7 | 2015-08-05 20:32:49 -0500 | [diff] [blame] | 30 | fd = getaddrinfo(host, port, &info, &ai); |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 31 | if (fd || !ai) |
Rob Landley | 35dafc7 | 2015-08-05 20:32:49 -0500 | [diff] [blame] | 32 | error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "", |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 33 | fd ? gai_strerror(fd) : "not found"); |
| 34 | |
Rob Landley | 31ff1f2 | 2015-08-05 21:20:27 -0500 | [diff] [blame] | 35 | // Try all the returned addresses. Report errors if last entry can't connect. |
| 36 | for (ai2 = ai; ai; ai = ai->ai_next) { |
| 37 | fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype, |
| 38 | ai->ai_protocol); |
| 39 | if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break; |
Rob Landley | a1a559e | 2017-01-04 01:32:44 -0600 | [diff] [blame] | 40 | else if (!ai->ai_next) perror_exit("connect"); |
Rob Landley | 31ff1f2 | 2015-08-05 21:20:27 -0500 | [diff] [blame] | 41 | close(fd); |
| 42 | } |
| 43 | freeaddrinfo(ai2); |
Rob Landley | ea75e75 | 2015-08-03 14:34:01 -0500 | [diff] [blame] | 44 | |
| 45 | return fd; |
| 46 | } |
Rob Landley | 5493916 | 2016-01-16 16:59:47 -0600 | [diff] [blame] | 47 | |
| 48 | int xpoll(struct pollfd *fds, int nfds, int timeout) |
| 49 | { |
| 50 | int i; |
| 51 | |
| 52 | for (;;) { |
| 53 | if (0>(i = poll(fds, nfds, timeout))) { |
Rob Landley | 4a13ca9 | 2016-01-28 22:10:06 -0600 | [diff] [blame] | 54 | if (toys.signal) return i; |
Rob Landley | 5493916 | 2016-01-16 16:59:47 -0600 | [diff] [blame] | 55 | if (errno != EINTR && errno != ENOMEM) perror_exit("xpoll"); |
| 56 | else if (timeout>0) timeout--; |
| 57 | } else return i; |
| 58 | } |
| 59 | } |
Rob Landley | 4e867b8 | 2016-10-11 08:19:41 -0500 | [diff] [blame] | 60 | |
| 61 | // Loop forwarding data from in1 to out1 and in2 to out2, handling |
| 62 | // half-connection shutdown. timeouts return if no data for X miliseconds. |
| 63 | // Returns 0: both closed, 1 shutdown_timeout, 2 timeout |
| 64 | int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout) |
| 65 | { |
| 66 | struct pollfd pollfds[2]; |
| 67 | int i, pollcount = 2; |
| 68 | |
| 69 | memset(pollfds, 0, 2*sizeof(struct pollfd)); |
| 70 | pollfds[0].events = pollfds[1].events = POLLIN; |
| 71 | pollfds[0].fd = in1; |
| 72 | pollfds[1].fd = in2; |
| 73 | |
| 74 | // Poll loop copying data from each fd to the other one. |
| 75 | for (;;) { |
| 76 | if (!xpoll(pollfds, pollcount, timeout)) return pollcount; |
| 77 | |
| 78 | for (i=0; i<pollcount; i++) { |
| 79 | if (pollfds[i].revents & POLLIN) { |
| 80 | int len = read(pollfds[i].fd, libbuf, sizeof(libbuf)); |
| 81 | if (len<1) pollfds[i].revents = POLLHUP; |
| 82 | else xwrite(i ? out2 : out1, libbuf, len); |
| 83 | } |
| 84 | if (pollfds[i].revents & POLLHUP) { |
| 85 | // Close half-connection. This is needed for things like |
| 86 | // "echo GET / | netcat landley.net 80" |
| 87 | // Note that in1 closing triggers timeout, in2 returns now. |
| 88 | if (i) { |
| 89 | shutdown(pollfds[0].fd, SHUT_WR); |
| 90 | pollcount--; |
| 91 | timeout = shutdown_timeout; |
| 92 | } else return 0; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |