From af2f0960ab48de220895b934bbdb0ae25dbca6b3 Mon Sep 17 00:00:00 2001 From: henri Date: Sat, 18 Aug 2012 16:25:50 +0000 Subject: [PATCH] Automatically enforce the use of the nsock select engine if we detect that stdin is a regular file. Under linux, that provides epoll(7), this allows proper behavior when redirecting a regular file to stdin (e.g.: 'ncat localhost < file.txt'). Bug was reported by Michal Hlavinka. See http://seclists.org/nmap-dev/2012/q3/490 for more details. --- ncat/ncat_connect.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ncat/ncat_connect.c b/ncat/ncat_connect.c index f811137d5..969246f2a 100644 --- a/ncat/ncat_connect.c +++ b/ncat/ncat_connect.c @@ -458,11 +458,31 @@ bail: return -1; } +#if defined(LINUX) +static int stdin_is_reg(void) { + struct stat buf; + + if (fstat(STDIN_FILENO, &buf) < 0) + fatal("fstat(): %s", strerror(errno)); + + return S_ISREG(buf.st_mode); +} +#endif + int ncat_connect(void) { nsock_pool mypool; int rc; +#if defined(LINUX) + /* -- Hack!! + * epoll(7) doesn't support regular files (e.g.: ncat < file.c) + * If we detect that STDIN is a regular file, then we enforce + * the use of the select-based engine. */ + if (stdin_is_reg()) + nsock_set_default_engine("select"); +#endif + /* Create an nsock pool */ if ((mypool = nsp_new(NULL)) == NULL) bye("Failed to create nsock_pool.");