diff options
| author | beck <> | 2020-01-26 23:47:57 +0000 |
|---|---|---|
| committer | beck <> | 2020-01-26 23:47:57 +0000 |
| commit | 1ea50969cfc23d07b5348c6a9d444203a377a0a7 (patch) | |
| tree | 3f3e41073a1dfc2c39510490a7d4a4a45ec5be88 /src | |
| parent | 85f22c1783d28a9f6940fc25e4f7caf249372ec7 (diff) | |
| download | openbsd-1ea50969cfc23d07b5348c6a9d444203a377a0a7.tar.gz openbsd-1ea50969cfc23d07b5348c6a9d444203a377a0a7.tar.bz2 openbsd-1ea50969cfc23d07b5348c6a9d444203a377a0a7.zip | |
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 'src')
| -rw-r--r-- | src/usr.bin/nc/netcat.c | 100 |
1 files changed, 36 insertions, 64 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index dec23305a7..c4b524431b 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: netcat.c,v 1.215 2020/01/07 17:36:04 bluhm Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.216 2020/01/26 23:47:57 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
| 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. | 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. |
| @@ -1120,14 +1120,13 @@ void | |||
| 1120 | readwrite(int net_fd, struct tls *tls_ctx) | 1120 | readwrite(int net_fd, struct tls *tls_ctx) |
| 1121 | { | 1121 | { |
| 1122 | struct pollfd pfd[4]; | 1122 | struct pollfd pfd[4]; |
| 1123 | int gone[4] = { 0 }; | ||
| 1124 | int stdin_fd = STDIN_FILENO; | 1123 | int stdin_fd = STDIN_FILENO; |
| 1125 | int stdout_fd = STDOUT_FILENO; | 1124 | int stdout_fd = STDOUT_FILENO; |
| 1126 | unsigned char netinbuf[BUFSIZE]; | 1125 | unsigned char netinbuf[BUFSIZE]; |
| 1127 | size_t netinbufpos = 0; | 1126 | size_t netinbufpos = 0; |
| 1128 | unsigned char stdinbuf[BUFSIZE]; | 1127 | unsigned char stdinbuf[BUFSIZE]; |
| 1129 | size_t stdinbufpos = 0; | 1128 | size_t stdinbufpos = 0; |
| 1130 | int n, num_fds, shutdown_netin, shutdown_netout; | 1129 | int n, num_fds; |
| 1131 | ssize_t ret; | 1130 | ssize_t ret; |
| 1132 | 1131 | ||
| 1133 | /* don't read from stdin if requested */ | 1132 | /* don't read from stdin if requested */ |
| @@ -1150,20 +1149,17 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1150 | pfd[POLL_STDOUT].fd = stdout_fd; | 1149 | pfd[POLL_STDOUT].fd = stdout_fd; |
| 1151 | pfd[POLL_STDOUT].events = 0; | 1150 | pfd[POLL_STDOUT].events = 0; |
| 1152 | 1151 | ||
| 1153 | /* used to indicate we wish to shut down the network socket */ | ||
| 1154 | shutdown_netin = shutdown_netout = 0; | ||
| 1155 | |||
| 1156 | while (1) { | 1152 | while (1) { |
| 1157 | /* both inputs are gone, buffers are empty, we are done */ | 1153 | /* both inputs are gone, buffers are empty, we are done */ |
| 1158 | if (gone[POLL_STDIN] && gone[POLL_NETIN] && | 1154 | if (pfd[POLL_STDIN].fd == -1 && pfd[POLL_NETIN].fd == -1 && |
| 1159 | stdinbufpos == 0 && netinbufpos == 0) | 1155 | stdinbufpos == 0 && netinbufpos == 0) |
| 1160 | return; | 1156 | return; |
| 1161 | /* both outputs are gone, we can't continue */ | 1157 | /* both outputs are gone, we can't continue */ |
| 1162 | if (gone[POLL_NETOUT] && gone[POLL_STDOUT]) | 1158 | if (pfd[POLL_NETOUT].fd == -1 && pfd[POLL_STDOUT].fd == -1) |
| 1163 | return; | 1159 | return; |
| 1164 | /* listen and net in gone, queues empty, done */ | 1160 | /* listen and net in gone, queues empty, done */ |
| 1165 | if (lflag && gone[POLL_NETIN] && stdinbufpos == 0 | 1161 | if (lflag && pfd[POLL_NETIN].fd == -1 && |
| 1166 | && netinbufpos == 0) | 1162 | stdinbufpos == 0 && netinbufpos == 0) |
| 1167 | return; | 1163 | return; |
| 1168 | 1164 | ||
| 1169 | /* help says -i is for "wait between lines sent". We read and | 1165 | /* help says -i is for "wait between lines sent". We read and |
| @@ -1172,12 +1168,6 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1172 | if (iflag) | 1168 | if (iflag) |
| 1173 | sleep(iflag); | 1169 | sleep(iflag); |
| 1174 | 1170 | ||
| 1175 | /* If it's gone, take it away from poll */ | ||
| 1176 | for (n = 0; n < 4; n++) { | ||
| 1177 | if (gone[n]) | ||
| 1178 | pfd[n].events = pfd[n].revents = 0; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | /* poll */ | 1171 | /* poll */ |
| 1182 | num_fds = poll(pfd, 4, timeout); | 1172 | num_fds = poll(pfd, 4, timeout); |
| 1183 | 1173 | ||
| @@ -1192,36 +1182,36 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1192 | /* treat socket error conditions */ | 1182 | /* treat socket error conditions */ |
| 1193 | for (n = 0; n < 4; n++) { | 1183 | for (n = 0; n < 4; n++) { |
| 1194 | if (pfd[n].revents & (POLLERR|POLLNVAL)) { | 1184 | if (pfd[n].revents & (POLLERR|POLLNVAL)) { |
| 1195 | gone[n] = 1; | 1185 | pfd[n].fd = -1; |
| 1196 | } | 1186 | } |
| 1197 | } | 1187 | } |
| 1198 | /* reading is possible after HUP */ | 1188 | /* reading is possible after HUP */ |
| 1199 | if (pfd[POLL_STDIN].events & POLLIN && | 1189 | if (pfd[POLL_STDIN].events & POLLIN && |
| 1200 | pfd[POLL_STDIN].revents & POLLHUP && | 1190 | pfd[POLL_STDIN].revents & POLLHUP && |
| 1201 | !(pfd[POLL_STDIN].revents & POLLIN)) | 1191 | !(pfd[POLL_STDIN].revents & POLLIN)) |
| 1202 | gone[POLL_STDIN] = 1; | 1192 | pfd[POLL_STDIN].fd = -1; |
| 1203 | 1193 | ||
| 1204 | if (pfd[POLL_NETIN].events & POLLIN && | 1194 | if (pfd[POLL_NETIN].events & POLLIN && |
| 1205 | pfd[POLL_NETIN].revents & POLLHUP && | 1195 | pfd[POLL_NETIN].revents & POLLHUP && |
| 1206 | !(pfd[POLL_NETIN].revents & POLLIN)) | 1196 | !(pfd[POLL_NETIN].revents & POLLIN)) |
| 1207 | gone[POLL_NETIN] = 1; | 1197 | pfd[POLL_NETIN].fd = -1; |
| 1208 | 1198 | ||
| 1209 | if (pfd[POLL_NETOUT].revents & POLLHUP) { | 1199 | if (pfd[POLL_NETOUT].revents & POLLHUP) { |
| 1210 | if (Nflag) | 1200 | if (Nflag) |
| 1211 | shutdown_netout = 1; | 1201 | shutdown(pfd[POLL_NETOUT].fd, SHUT_WR); |
| 1212 | gone[POLL_NETOUT] = 1; | 1202 | pfd[POLL_NETOUT].fd = -1; |
| 1213 | } | 1203 | } |
| 1214 | /* if no net out, stop watching stdin */ | 1204 | /* if HUP, stop watching stdout */ |
| 1215 | if (gone[POLL_NETOUT]) | ||
| 1216 | gone[POLL_STDIN] = 1; | ||
| 1217 | |||
| 1218 | /* if stdout HUP's, stop watching stdout */ | ||
| 1219 | if (pfd[POLL_STDOUT].revents & POLLHUP) | 1205 | if (pfd[POLL_STDOUT].revents & POLLHUP) |
| 1220 | gone[POLL_STDOUT] = 1; | 1206 | pfd[POLL_STDOUT].fd = -1; |
| 1207 | /* if no net out, stop watching stdin */ | ||
| 1208 | if (pfd[POLL_NETOUT].fd == -1) | ||
| 1209 | pfd[POLL_STDIN].fd = -1; | ||
| 1221 | /* if no stdout, stop watching net in */ | 1210 | /* if no stdout, stop watching net in */ |
| 1222 | if (gone[POLL_STDOUT]) { | 1211 | if (pfd[POLL_STDOUT].fd == -1) { |
| 1223 | shutdown_netin = 1; | 1212 | if (pfd[POLL_NETIN].fd != -1) |
| 1224 | gone[POLL_NETIN] = 1; | 1213 | shutdown(pfd[POLL_NETIN].fd, SHUT_RD); |
| 1214 | pfd[POLL_NETIN].fd = -1; | ||
| 1225 | } | 1215 | } |
| 1226 | 1216 | ||
| 1227 | /* try to read from stdin */ | 1217 | /* try to read from stdin */ |
| @@ -1233,7 +1223,7 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1233 | else if (ret == TLS_WANT_POLLOUT) | 1223 | else if (ret == TLS_WANT_POLLOUT) |
| 1234 | pfd[POLL_STDIN].events = POLLOUT; | 1224 | pfd[POLL_STDIN].events = POLLOUT; |
| 1235 | else if (ret == 0 || ret == -1) | 1225 | else if (ret == 0 || ret == -1) |
| 1236 | gone[POLL_STDIN] = 1; | 1226 | pfd[POLL_STDIN].fd = -1; |
| 1237 | /* read something - poll net out */ | 1227 | /* read something - poll net out */ |
| 1238 | if (stdinbufpos > 0) | 1228 | if (stdinbufpos > 0) |
| 1239 | pfd[POLL_NETOUT].events = POLLOUT; | 1229 | pfd[POLL_NETOUT].events = POLLOUT; |
| @@ -1250,7 +1240,7 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1250 | else if (ret == TLS_WANT_POLLOUT) | 1240 | else if (ret == TLS_WANT_POLLOUT) |
| 1251 | pfd[POLL_NETOUT].events = POLLOUT; | 1241 | pfd[POLL_NETOUT].events = POLLOUT; |
| 1252 | else if (ret == -1) | 1242 | else if (ret == -1) |
| 1253 | gone[POLL_NETOUT] = 1; | 1243 | pfd[POLL_NETOUT].fd = -1; |
| 1254 | /* buffer empty - remove self from polling */ | 1244 | /* buffer empty - remove self from polling */ |
| 1255 | if (stdinbufpos == 0) | 1245 | if (stdinbufpos == 0) |
| 1256 | pfd[POLL_NETOUT].events = 0; | 1246 | pfd[POLL_NETOUT].events = 0; |
| @@ -1267,15 +1257,17 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1267 | else if (ret == TLS_WANT_POLLOUT) | 1257 | else if (ret == TLS_WANT_POLLOUT) |
| 1268 | pfd[POLL_NETIN].events = POLLOUT; | 1258 | pfd[POLL_NETIN].events = POLLOUT; |
| 1269 | else if (ret == -1) | 1259 | else if (ret == -1) |
| 1270 | gone[POLL_NETIN] = 1; | 1260 | pfd[POLL_NETIN].fd = -1; |
| 1271 | /* eof on net in - remove from pfd */ | 1261 | /* eof on net in - remove from pfd */ |
| 1272 | if (ret == 0) { | 1262 | if (ret == 0) { |
| 1273 | gone[POLL_NETIN] = 1; | 1263 | shutdown(pfd[POLL_NETIN].fd, SHUT_RD); |
| 1264 | pfd[POLL_NETIN].fd = -1; | ||
| 1274 | } | 1265 | } |
| 1275 | if (recvlimit > 0 && ++recvcount >= recvlimit) { | 1266 | if (recvlimit > 0 && ++recvcount >= recvlimit) { |
| 1276 | shutdown_netin = 1; | 1267 | if (pfd[POLL_NETIN].fd != -1) |
| 1277 | gone[POLL_NETIN] = 1; | 1268 | shutdown(pfd[POLL_NETIN].fd, SHUT_RD); |
| 1278 | gone[POLL_STDIN] = 1; | 1269 | pfd[POLL_NETIN].fd = -1; |
| 1270 | pfd[POLL_STDIN].fd = -1; | ||
| 1279 | } | 1271 | } |
| 1280 | /* read something - poll stdout */ | 1272 | /* read something - poll stdout */ |
| 1281 | if (netinbufpos > 0) | 1273 | if (netinbufpos > 0) |
| @@ -1297,7 +1289,7 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1297 | else if (ret == TLS_WANT_POLLOUT) | 1289 | else if (ret == TLS_WANT_POLLOUT) |
| 1298 | pfd[POLL_STDOUT].events = POLLOUT; | 1290 | pfd[POLL_STDOUT].events = POLLOUT; |
| 1299 | else if (ret == -1) | 1291 | else if (ret == -1) |
| 1300 | gone[POLL_STDOUT] = 1; | 1292 | pfd[POLL_STDOUT].fd = -1; |
| 1301 | /* buffer empty - remove self from polling */ | 1293 | /* buffer empty - remove self from polling */ |
| 1302 | if (netinbufpos == 0) | 1294 | if (netinbufpos == 0) |
| 1303 | pfd[POLL_STDOUT].events = 0; | 1295 | pfd[POLL_STDOUT].events = 0; |
| @@ -1307,34 +1299,14 @@ readwrite(int net_fd, struct tls *tls_ctx) | |||
| 1307 | } | 1299 | } |
| 1308 | 1300 | ||
| 1309 | /* stdin gone and queue empty? */ | 1301 | /* stdin gone and queue empty? */ |
| 1310 | if (gone[POLL_STDIN] && stdinbufpos == 0) { | 1302 | if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) { |
| 1311 | if (Nflag) { | 1303 | if (pfd[POLL_NETOUT].fd != -1 && Nflag) |
| 1312 | shutdown_netin = 1; | 1304 | shutdown(pfd[POLL_NETOUT].fd, SHUT_WR); |
| 1313 | shutdown_netout = 1; | 1305 | pfd[POLL_NETOUT].fd = -1; |
| 1314 | } | ||
| 1315 | gone[POLL_NETOUT] = 1; | ||
| 1316 | } | 1306 | } |
| 1317 | /* net in gone and queue empty? */ | 1307 | /* net in gone and queue empty? */ |
| 1318 | if (gone[POLL_NETIN] && netinbufpos == 0) { | 1308 | if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) { |
| 1319 | if (Nflag) { | 1309 | pfd[POLL_STDOUT].fd = -1; |
| 1320 | shutdown_netin = 1; | ||
| 1321 | shutdown_netout = 1; | ||
| 1322 | } | ||
| 1323 | gone[POLL_STDOUT] = 1; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | /* call tls_close if any part of the network socket is closing */ | ||
| 1327 | if ((shutdown_netin || shutdown_netout) && usetls) { | ||
| 1328 | timeout_tls(pfd[POLL_NETIN].fd, tls_ctx, tls_close); | ||
| 1329 | shutdown_netout = shutdown_netin = 1; | ||
| 1330 | } | ||
| 1331 | if (shutdown_netin) { | ||
| 1332 | shutdown(pfd[POLL_NETIN].fd, SHUT_RD); | ||
| 1333 | gone[POLL_NETIN] = 1; | ||
| 1334 | } | ||
| 1335 | if (shutdown_netout) { | ||
| 1336 | shutdown(pfd[POLL_NETOUT].fd, SHUT_WR); | ||
| 1337 | gone[POLL_NETOUT] = 1; | ||
| 1338 | } | 1310 | } |
| 1339 | } | 1311 | } |
| 1340 | } | 1312 | } |
