Nmap Development mailing list archives

[PATCH] Flushing console input buffer in nmap_tty.cc (pt2)


From: Kris Katterjohn <katterjohn () gmail com>
Date: Fri, 12 Jan 2007 18:45:00 -0600

I was talking on the list here a while back about flushing the input
buffer differently in nmap_tty.cc with tcflush() for UNIX, and possibly
FlushConsoleInputBuffer() for Win32 (which kx found for me).

Here's the original post: http://seclists.org/nmap-dev/2006/q3/0257.html


I have applied a hopefully working patch to SVN (/nmap-exp/kris r4349),
which I also attached.


Here's the SVN log, which elaborates a little more:

------------------------------------------------------------------------
r4349 | kris | 2007-01-12 18:21:19 -0600 (Fri, 12 Jan 2007) | 1 line

I'm trying to find a good way to flush the input queue in nmap_tty.cc
for Windows (which I can't test). I'm using tcflush() for UNIX of course
(which works very well and seems a good bit faster when I test it, like
pressing a lot of buttons in a row), and I'm putting
FlushConsoleInputBuffer() in for Win32 for now. Reading some docs and
other code, I think the way I put it in now *should* work. Hopefully
somebody on nmap-dev can test it out and we can put this in instead of
the loop-and-read used now to flush it. It's not a big bottleneck or
anything, but it should help out if we can get this Win32 thing working.
------------------------------------------------------------------------

So if any users can test this on Win32, that'd be great. And UNIX people
please test too, and let me know if you have any problems (which you
shouldn't :)).

Thanks a lot,
Kris Katterjohn
Index: nmap_tty.cc
===================================================================
--- nmap_tty.cc (revision 4348)
+++ nmap_tty.cc (revision 4349)
@@ -129,6 +129,13 @@
 static int tty_getchar() { return _kbhit() ? _getch() : -1; }
 static void tty_done() { return; }
 
+static void tty_flush(void)
+{
+       static HANDLE stdinput = GetStdHandle(STD_INPUT_HANDLE);
+
+       FlushConsoleInputBuffer(stdinput);
+}
+
 #else
 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
 #define O_NONBLOCK                     O_NDELAY
@@ -183,6 +190,15 @@
        tty_fd = 0;
 }
 
+static void tty_flush(void)
+{
+       /* we don't need to test for tty_fd==0 here because
+        * this isn't called unless we succeeded
+        */
+
+       tcflush(tty_fd, TCIFLUSH);
+}
+
 /*
  * Initializes the terminal for unbuffered non-blocking input. Also
  * registers tty_done() via atexit().  You need to call this before
@@ -228,8 +244,7 @@
     return false;
 
   if ((c = tty_getchar()) >= 0) {
-    // Eat any extra keys (so they can't queue up and print forever)
-    while (tty_getchar() >= 0); 
+    tty_flush(); /* flush input queue */
 
     // printf("You pressed key '%c'!\n", c);
     if (c == 'v') {


_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: