From 7453ec34d015eb3eb0d2b4ac1f794a8dfa5585a6 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 28 Mar 2011 20:34:32 +0000 Subject: [PATCH] Change o.TimeSinceStartMS returning milliseconds to o.TimeSinceStart returning floating-point seconds. Everywhere o.TimeSinceStartMS was called, the return value was being divided by 1000.0, which had the same effect but would overflow when the difference exceeded about 25 days (2^31 milliseconds). This patch is by Daniel Miller. --- CHANGELOG | 3 +++ NmapOps.cc | 6 +++--- NmapOps.h | 4 ++-- osscan2.cc | 6 +++--- output.cc | 10 +++++----- scan_engine.cc | 2 +- tcpip.cc | 6 +++--- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b65bdbb10..a4820a420 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ # Nmap Changelog ($Id$); -*-text-*- +o Fixed an overflow in scan elapsed time display that caused negative + times to be printed after about 25 days. [Daniel Miller] + o [NSE] ssh-hostkey now additionally has a postrule that prints hosts that have the same hostkey. [Henri Doreau] diff --git a/NmapOps.cc b/NmapOps.cc index 5f1f08e11..62464942a 100644 --- a/NmapOps.cc +++ b/NmapOps.cc @@ -155,15 +155,15 @@ const struct in_addr *NmapOps::v4sourceip() { return NULL; } -// Number of milliseconds since getStartTime(). The current time is an +// Number of seconds since getStartTime(). The current time is an // optional argument to avoid an extra gettimeofday() call. -int NmapOps::TimeSinceStartMS(const struct timeval *now) { +float NmapOps::TimeSinceStart(const struct timeval *now) { struct timeval tv; if (!now) gettimeofday(&tv, NULL); else tv = *now; - return TIMEVAL_MSEC_SUBTRACT(tv, start_time); + return TIMEVAL_FSEC_SUBTRACT(tv, start_time); } // Convert a filename to a file:// URL. The return value must be freed. diff --git a/NmapOps.h b/NmapOps.h index 76a255de2..62b476e81 100644 --- a/NmapOps.h +++ b/NmapOps.h @@ -113,9 +113,9 @@ class NmapOps { // The time this obj. was instantiated or last ReInit()ed. const struct timeval *getStartTime() { return &start_time; } - // Number of milliseconds since getStartTime(). The current time is an + // Number of seconds since getStartTime(). The current time is an // optional argument to avoid an extra gettimeofday() call. - int TimeSinceStartMS(const struct timeval *now=NULL); + float TimeSinceStart(const struct timeval *now=NULL); struct in_addr v4source(); const struct in_addr *v4sourceip(); diff --git a/osscan2.cc b/osscan2.cc index 562db1c93..245157d2a 100644 --- a/osscan2.cc +++ b/osscan2.cc @@ -523,7 +523,7 @@ public: resetHostIterator() afterward). Don't let this list get empty, then add to it again, or you may mess up nextI (I'm not sure) */ list incompleteHosts; - unsigned int starttimems; + float starttime; unsigned int numIncompleteHosts() {return incompleteHosts.size();} HostOsScanInfo *findIncompleteHost(struct sockaddr_storage *ss); @@ -2886,7 +2886,7 @@ int OsScanInfo::removeCompletedHosts() { if (remain && !timedout) log_write(LOG_STDOUT, "Completed os scan against %s in %.3fs (%d %s)\n", hsi->target->targetipstr(), - (o.TimeSinceStartMS() - this->starttimems) / 1000.0, remain, + o.TimeSinceStart() - this->starttime, remain, (remain == 1)? "host left" : "hosts left"); else if (timedout) log_write(LOG_STDOUT, "%s timed out during os scan (%d %s)\n", @@ -3747,7 +3747,7 @@ static int os_scan_2(vector &Targets) { delete OSI; return 1; } - OSI->starttimems = o.TimeSinceStartMS(); + OSI->starttime = o.TimeSinceStart(); HOS = new HostOsScan(Targets[0]); startTimeOutClocks(OSI); diff --git a/output.cc b/output.cc index d0b325f9b..bba5c2ddc 100644 --- a/output.cc +++ b/output.cc @@ -2193,7 +2193,7 @@ void printStatusMessage() { // Pre-computations struct timeval tv; gettimeofday(&tv, NULL); - int time = (int) (o.TimeSinceStartMS(&tv) / 1000.0); + int time = (int) (o.TimeSinceStart(&tv)); log_write(LOG_STDOUT, "Stats: %d:%02d:%02d elapsed; %d hosts completed (%d up), %d undergoing %s\n", time / 60 / 60, time / 60 % 60, time % 60, o.numhosts_scanned, @@ -2213,13 +2213,13 @@ void print_xml_finished_open(time_t timep, const struct timeval *tv) { xml_open_start_tag("finished"); xml_attribute("time", "%lu", (unsigned long) timep); xml_attribute("timestr", "%s", mytime); - xml_attribute("elapsed", "%.2f", o.TimeSinceStartMS(tv) / 1000.0); + xml_attribute("elapsed", "%.2f", o.TimeSinceStart(tv)); xml_attribute("summary", "Nmap done at %s; %d %s (%d %s up) scanned in %.2f seconds", mytime, o.numhosts_scanned, (o.numhosts_scanned == 1) ? "IP address" : "IP addresses", o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts", - o.TimeSinceStartMS(tv) / 1000.0); + o.TimeSinceStart(tv)); } void print_xml_hosts() { @@ -2264,7 +2264,7 @@ void printfinaloutput() { o.numhosts_scanned, (o.numhosts_scanned == 1) ? "IP address" : "IP addresses", o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts", - o.TimeSinceStartMS(&tv) / 1000.0); + o.TimeSinceStart(&tv)); if (o.verbose && o.isr00t && o.RawScan()) log_write(LOG_STDOUT | LOG_SKID, " %s\n", getFinalPacketStats(statbuf, sizeof(statbuf))); @@ -2286,7 +2286,7 @@ void printfinaloutput() { mytime, o.numhosts_scanned, (o.numhosts_scanned == 1) ? "IP address" : "IP addresses", o.numhosts_up, (o.numhosts_up == 1) ? "host" : "hosts", - o.TimeSinceStartMS(&tv) / 1000.0); + o.TimeSinceStart(&tv)); xml_end_tag(); /* nmaprun */ xml_newline(); diff --git a/scan_engine.cc b/scan_engine.cc index e1e155e44..76ac44681 100644 --- a/scan_engine.cc +++ b/scan_engine.cc @@ -3662,7 +3662,7 @@ static void printAnyStats(UltraScanInfo *USI) { /* Print debugging states for each host being scanned */ if (o.debugging > 2) { - log_write(LOG_PLAIN, "**TIMING STATS** (%.4fs): IP, probes active/freshportsleft/retry_stack/outstanding/retranwait/onbench, cwnd/ssthresh/delay, timeout/srtt/rttvar/\n", o.TimeSinceStartMS() / 1000.0); + log_write(LOG_PLAIN, "**TIMING STATS** (%.4fs): IP, probes active/freshportsleft/retry_stack/outstanding/retranwait/onbench, cwnd/ssthresh/delay, timeout/srtt/rttvar/\n", o.TimeSinceStart()); log_write(LOG_PLAIN, " Groupstats (%d/%d incomplete): %d/*/*/*/*/* %.2f/%d/* %d/%d/%d\n", USI->numIncompleteHosts(), USI->numInitialHosts(), USI->gstats->num_probes_active, USI->gstats->timing.cwnd, diff --git a/tcpip.cc b/tcpip.cc index d82171210..c46625052 100644 --- a/tcpip.cc +++ b/tcpip.cc @@ -204,7 +204,7 @@ void PacketTrace::traceArp(pdirection pdir, const u8 *frame, u32 len, log_write(LOG_STDOUT | LOG_NORMAL, "%s (%.4fs) ARP %s\n", (pdir == SENT) ? "SENT" : "RCVD", - o.TimeSinceStartMS(&tv) / 1000.0, arpdesc); + o.TimeSinceStart(&tv), arpdesc); return; } @@ -260,7 +260,7 @@ void PacketTrace::trace(pdirection pdir, const u8 *packet, u32 len, log_write(LOG_STDOUT | LOG_NORMAL, "%s (%.4fs) %s\n", (pdir == SENT) ? "SENT" : "RCVD", - o.TimeSinceStartMS(&tv) / 1000.0, nmap_format_ippacket(packet, len)); + o.TimeSinceStart(&tv), nmap_format_ippacket(packet, len)); return; } @@ -326,7 +326,7 @@ void PacketTrace::traceConnect(u8 proto, const struct sockaddr *sock, log_write(LOG_STDOUT | LOG_NORMAL, "CONN (%.4fs) %s localhost > %s:%d => %s\n", - o.TimeSinceStartMS(&tv) / 1000.0, + o.TimeSinceStart(&tv), (proto == IPPROTO_TCP) ? "TCP" : "UDP", targetipstr, targetport, errbuf); }