Subject: | Bug handling large outputs from nmap |
Date: | Tue, 12 Mar 2013 15:08:27 -0700 |
To: | bug-Nmap-Scanner [...] rt.cpan.org |
From: | Kelsey Cummings <kgc [...] corp.sonic.net> |
Since the module as written blocks on reading stderr from namp, namp's
stdout output buffer can fill and block causing things to hang.
Included patch updates the code to use select loop on the output to
prevent this from happening and to allow reading of large amounts of
data from nmap.
diff -ru scan_rpc-0.005-BACKUP/lib/Nmap/Scanner/Backend/XML.pm scan_rpc-0.005/lib/Nmap/Scanner/Backend/XML.pm
--- scan_rpc-0.005-BACKUP/lib/Nmap/Scanner/Backend/XML.pm 2013-03-12 13:49:02.100461965 -0700
+++ scan_rpc-0.005/lib/Nmap/Scanner/Backend/XML.pm 2013-03-12 14:41:51.155406282 -0700
@@ -30,11 +30,39 @@
my $cmdline = shift;
my $error = shift;
+ my $readbuf = "";
+ my $errorbuf = "";
+
# Suppress warnings about reading unopened handle
$^W = 0;
- my $err = join('', (<$error>));
+
+ my $fd_set = new IO::Select();
+ $fd_set->add($read);
+ $fd_set->add($error);
+
+ while (1) {
+ my ($rh_set) = IO::Select->select($fd_set, undef, undef, 10);
+
+ for my $fd (@$rh_set) {
+ my $tbuf = "";
+ if ($fd == $read) {
+ my $retval = sysread($read, $tbuf, 1024);
+ $readbuf .= $tbuf;
+ goto DONE if (!defined($retval) || $retval == 0);
+ }
+ if ($fd == $error) {
+ my $retval = sysread($error, $tbuf, 1024);
+ $errorbuf .= $tbuf;
+ goto DONE if (!defined($retval) || $retval == 0);
+ }
+ }
+ }
+ DONE:
+
$^W = 1;
+ my $err = $errorbuf;
+
if ($err ne '') {
close($read);
@@ -53,15 +81,12 @@
my $handler = NmapHandler->new($self);
my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
- eval { $parser->parse_file($read) };
+ eval { $parser->parse_string($readbuf) };
if (defined($@) && ($@ ne '')) {
- my $msg = join('', <$read>);
-
- Nmap::Scanner::debug("bytes in input stream: " . tell($read));
- Nmap::Scanner::debug("bytes in error stream: " . tell($error));
+ my $msg = $readbuf;
warn <<EOF;
<nmap-error>
--
Kelsey Cummings - kgc@corp.sonic.net sonic.net, inc.
System Architect 2260 Apollo Way
707.522.1000 Santa Rosa, CA 95407