From e5dd640096925637a0dd19991130c58a8a39dcea Mon Sep 17 00:00:00 2001 From: hugh <> Date: Thu, 30 May 2002 09:37:38 +0000 Subject: Avoid spinning poll, and while we're at it more closely reproduce the original netcat's timeout behaviour. Theo says go. --- src/usr.bin/nc/netcat.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 3a1e9c1193..67e33de490 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.48 2002/05/29 09:23:25 deraadt Exp $ */ +/* $OpenBSD: netcat.c,v 1.49 2002/05/30 09:37:38 hugh Exp $ */ /* * Copyright (c) 2001 Eric Jackson * @@ -66,7 +66,7 @@ int vflag; /* Verbosity */ int xflag; /* Socks proxy */ int zflag; /* Port Scan Flag */ -int timeout; +int timeout = -1; int family = AF_UNSPEC; char *portlist[PORT_MAX]; @@ -160,6 +160,9 @@ main(int argc, char *argv[]) timeout = (int)strtoul(optarg, &endp, 10); if (timeout < 0 || *endp != '\0') errx(1, "timeout cannot be negative"); + if (timeout >= (INT_MAX / 1000)) + errx(1, "timeout too large"); + timeout *= 1000; break; case 'x': xflag = 1; @@ -548,13 +551,16 @@ readwrite(int nfd) if (iflag) sleep(iflag); - if (poll(pfd, 2, timeout) < 0) { + if ((n = poll(pfd, 2, timeout)) < 0) { close(nfd); close(wfd); free(pfd); errx(1, "Polling Error"); } + if (n == 0) + return; + if (pfd[0].revents & POLLIN) { if ((n = read(nfd, buf, sizeof(buf))) <= 0) { return; -- cgit v1.2.3-55-g6feb