summaryrefslogtreecommitdiff
path: root/usr.bin/nc
diff options
context:
space:
mode:
authorBob Beck <beck@cvs.openbsd.org>2020-01-26 23:47:58 +0000
committerBob Beck <beck@cvs.openbsd.org>2020-01-26 23:47:58 +0000
commit3c32a73e48328afc1813ef682abf768a4b17d44c (patch)
treeec2e06efba82b136abebb4845b4bff06049c9ce7 /usr.bin/nc
parent482a039355a781c7a14e35dd7ce3f8414a567f1e (diff)
revert previous nc loop refactor from 1.211, breaks bluhm's stuff
will attempt again later, now that there is new regress
Diffstat (limited to 'usr.bin/nc')
-rw-r--r--usr.bin/nc/netcat.c100
1 files changed, 36 insertions, 64 deletions
diff --git a/usr.bin/nc/netcat.c b/usr.bin/nc/netcat.c
index dec23305a78..c4b524431bf 100644
--- a/usr.bin/nc/netcat.c
+++ b/usr.bin/nc/netcat.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: netcat.c,v 1.215 2020/01/07 17:36:04 bluhm Exp $ */
+/* $OpenBSD: netcat.c,v 1.216 2020/01/26 23:47:57 beck Exp $ */
/*
* Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
* Copyright (c) 2015 Bob Beck. All rights reserved.
@@ -1120,14 +1120,13 @@ void
readwrite(int net_fd, struct tls *tls_ctx)
{
struct pollfd pfd[4];
- int gone[4] = { 0 };
int stdin_fd = STDIN_FILENO;
int stdout_fd = STDOUT_FILENO;
unsigned char netinbuf[BUFSIZE];
size_t netinbufpos = 0;
unsigned char stdinbuf[BUFSIZE];
size_t stdinbufpos = 0;
- int n, num_fds, shutdown_netin, shutdown_netout;
+ int n, num_fds;
ssize_t ret;
/* don't read from stdin if requested */
@@ -1150,20 +1149,17 @@ readwrite(int net_fd, struct tls *tls_ctx)
pfd[POLL_STDOUT].fd = stdout_fd;
pfd[POLL_STDOUT].events = 0;
- /* used to indicate we wish to shut down the network socket */
- shutdown_netin = shutdown_netout = 0;
-
while (1) {
/* both inputs are gone, buffers are empty, we are done */
- if (gone[POLL_STDIN] && gone[POLL_NETIN] &&
+ if (pfd[POLL_STDIN].fd == -1 && pfd[POLL_NETIN].fd == -1 &&
stdinbufpos == 0 && netinbufpos == 0)
return;
/* both outputs are gone, we can't continue */
- if (gone[POLL_NETOUT] && gone[POLL_STDOUT])
+ if (pfd[POLL_NETOUT].fd == -1 && pfd[POLL_STDOUT].fd == -1)
return;
/* listen and net in gone, queues empty, done */
- if (lflag && gone[POLL_NETIN] && stdinbufpos == 0
- && netinbufpos == 0)
+ if (lflag && pfd[POLL_NETIN].fd == -1 &&
+ stdinbufpos == 0 && netinbufpos == 0)
return;
/* help says -i is for "wait between lines sent". We read and
@@ -1172,12 +1168,6 @@ readwrite(int net_fd, struct tls *tls_ctx)
if (iflag)
sleep(iflag);
- /* If it's gone, take it away from poll */
- for (n = 0; n < 4; n++) {
- if (gone[n])
- pfd[n].events = pfd[n].revents = 0;
- }
-
/* poll */
num_fds = poll(pfd, 4, timeout);
@@ -1192,36 +1182,36 @@ readwrite(int net_fd, struct tls *tls_ctx)
/* treat socket error conditions */
for (n = 0; n < 4; n++) {
if (pfd[n].revents & (POLLERR|POLLNVAL)) {
- gone[n] = 1;
+ pfd[n].fd = -1;
}
}
/* reading is possible after HUP */
if (pfd[POLL_STDIN].events & POLLIN &&
pfd[POLL_STDIN].revents & POLLHUP &&
!(pfd[POLL_STDIN].revents & POLLIN))
- gone[POLL_STDIN] = 1;
+ pfd[POLL_STDIN].fd = -1;
if (pfd[POLL_NETIN].events & POLLIN &&
pfd[POLL_NETIN].revents & POLLHUP &&
!(pfd[POLL_NETIN].revents & POLLIN))
- gone[POLL_NETIN] = 1;
+ pfd[POLL_NETIN].fd = -1;
if (pfd[POLL_NETOUT].revents & POLLHUP) {
if (Nflag)
- shutdown_netout = 1;
- gone[POLL_NETOUT] = 1;
+ shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
+ pfd[POLL_NETOUT].fd = -1;
}
- /* if no net out, stop watching stdin */
- if (gone[POLL_NETOUT])
- gone[POLL_STDIN] = 1;
-
- /* if stdout HUP's, stop watching stdout */
+ /* if HUP, stop watching stdout */
if (pfd[POLL_STDOUT].revents & POLLHUP)
- gone[POLL_STDOUT] = 1;
+ pfd[POLL_STDOUT].fd = -1;
+ /* if no net out, stop watching stdin */
+ if (pfd[POLL_NETOUT].fd == -1)
+ pfd[POLL_STDIN].fd = -1;
/* if no stdout, stop watching net in */
- if (gone[POLL_STDOUT]) {
- shutdown_netin = 1;
- gone[POLL_NETIN] = 1;
+ if (pfd[POLL_STDOUT].fd == -1) {
+ if (pfd[POLL_NETIN].fd != -1)
+ shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
+ pfd[POLL_NETIN].fd = -1;
}
/* try to read from stdin */
@@ -1233,7 +1223,7 @@ readwrite(int net_fd, struct tls *tls_ctx)
else if (ret == TLS_WANT_POLLOUT)
pfd[POLL_STDIN].events = POLLOUT;
else if (ret == 0 || ret == -1)
- gone[POLL_STDIN] = 1;
+ pfd[POLL_STDIN].fd = -1;
/* read something - poll net out */
if (stdinbufpos > 0)
pfd[POLL_NETOUT].events = POLLOUT;
@@ -1250,7 +1240,7 @@ readwrite(int net_fd, struct tls *tls_ctx)
else if (ret == TLS_WANT_POLLOUT)
pfd[POLL_NETOUT].events = POLLOUT;
else if (ret == -1)
- gone[POLL_NETOUT] = 1;
+ pfd[POLL_NETOUT].fd = -1;
/* buffer empty - remove self from polling */
if (stdinbufpos == 0)
pfd[POLL_NETOUT].events = 0;
@@ -1267,15 +1257,17 @@ readwrite(int net_fd, struct tls *tls_ctx)
else if (ret == TLS_WANT_POLLOUT)
pfd[POLL_NETIN].events = POLLOUT;
else if (ret == -1)
- gone[POLL_NETIN] = 1;
+ pfd[POLL_NETIN].fd = -1;
/* eof on net in - remove from pfd */
if (ret == 0) {
- gone[POLL_NETIN] = 1;
+ shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
+ pfd[POLL_NETIN].fd = -1;
}
if (recvlimit > 0 && ++recvcount >= recvlimit) {
- shutdown_netin = 1;
- gone[POLL_NETIN] = 1;
- gone[POLL_STDIN] = 1;
+ if (pfd[POLL_NETIN].fd != -1)
+ shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
+ pfd[POLL_NETIN].fd = -1;
+ pfd[POLL_STDIN].fd = -1;
}
/* read something - poll stdout */
if (netinbufpos > 0)
@@ -1297,7 +1289,7 @@ readwrite(int net_fd, struct tls *tls_ctx)
else if (ret == TLS_WANT_POLLOUT)
pfd[POLL_STDOUT].events = POLLOUT;
else if (ret == -1)
- gone[POLL_STDOUT] = 1;
+ pfd[POLL_STDOUT].fd = -1;
/* buffer empty - remove self from polling */
if (netinbufpos == 0)
pfd[POLL_STDOUT].events = 0;
@@ -1307,34 +1299,14 @@ readwrite(int net_fd, struct tls *tls_ctx)
}
/* stdin gone and queue empty? */
- if (gone[POLL_STDIN] && stdinbufpos == 0) {
- if (Nflag) {
- shutdown_netin = 1;
- shutdown_netout = 1;
- }
- gone[POLL_NETOUT] = 1;
+ if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) {
+ if (pfd[POLL_NETOUT].fd != -1 && Nflag)
+ shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
+ pfd[POLL_NETOUT].fd = -1;
}
/* net in gone and queue empty? */
- if (gone[POLL_NETIN] && netinbufpos == 0) {
- if (Nflag) {
- shutdown_netin = 1;
- shutdown_netout = 1;
- }
- gone[POLL_STDOUT] = 1;
- }
-
- /* call tls_close if any part of the network socket is closing */
- if ((shutdown_netin || shutdown_netout) && usetls) {
- timeout_tls(pfd[POLL_NETIN].fd, tls_ctx, tls_close);
- shutdown_netout = shutdown_netin = 1;
- }
- if (shutdown_netin) {
- shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
- gone[POLL_NETIN] = 1;
- }
- if (shutdown_netout) {
- shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
- gone[POLL_NETOUT] = 1;
+ if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) {
+ pfd[POLL_STDOUT].fd = -1;
}
}
}