From 625d88293ea33299ec38cdaa09042224a40e07c0 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 23 Jul 2008 22:21:37 +0000 Subject: [PATCH] Avoid showing the sending rate in bytes per second if no sent bytes have been recorded. This applies during a TCP connect scan, where we have a count of packets sent but not of bytes (which we cannot measure, and will vary from platform to platform). --- scan_engine.cc | 34 ++++++++++++++++++++-------------- timing.cc | 8 ++++++++ timing.h | 2 ++ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/scan_engine.cc b/scan_engine.cc index 73bc9029d..0d13dcfe4 100644 --- a/scan_engine.cc +++ b/scan_engine.cc @@ -611,6 +611,21 @@ public: bool numIncompleteHostsLessThan(unsigned int n); unsigned int numInitialHosts() { return numInitialTargets; } + + void log_overall_rates(int logt) { + log_write(logt, "Overall sending rates: %.2f packets / s", send_rate_meter.getOverallPacketRate(&now)); + if (send_rate_meter.getNumBytes()> 0) + log_write(logt, ", %.2f bytes / s", send_rate_meter.getOverallByteRate(&now)); + log_write(logt, ".\n"); + } + + void log_current_rates(int logt, bool update = true) { + log_write(logt, "Current sending rates: %.2f packets / s", send_rate_meter.getCurrentPacketRate(&now, update)); + if (send_rate_meter.getNumBytes()> 0) + log_write(logt, ", %.2f bytes / s", send_rate_meter.getCurrentByteRate(&now)); + log_write(logt, ".\n"); + } + /* Any function which messes with (removes elements from) incompleteHosts may have to manipulate nextI */ list incompleteHosts; @@ -3208,12 +3223,8 @@ static void printAnyStats(UltraScanInfo *USI) { } } - log_write(LOG_PLAIN, "Current sending rates: %.2f packets / s, %.2f bytes / s.\n", - USI->send_rate_meter.getCurrentPacketRate(), - USI->send_rate_meter.getCurrentByteRate()); - log_write(LOG_PLAIN, "Overall sending rates: %.2f packets / s, %.2f bytes / s.\n", - USI->send_rate_meter.getOverallPacketRate(), - USI->send_rate_meter.getOverallByteRate()); + USI->log_current_rates(LOG_PLAIN); + USI->log_overall_rates(LOG_PLAIN); } /* Now time to figure out how close we are to completion ... */ @@ -4838,9 +4849,7 @@ void ultra_scan(vector &Targets, struct scan_lists *ports, /* Don't update when getting the current rates, otherwise we can get anomalies (rates are too low) from having just done a potentially long waitForResponses without sending any packets. */ - log_write(LOG_STDOUT, "Current sending rates: %.2f packets / s, %.2f bytes / s.\n", - USI->send_rate_meter.getCurrentPacketRate(&USI->now, false), - USI->send_rate_meter.getCurrentByteRate(&USI->now, false)); + USI->log_current_rates(LOG_STDOUT, false); } log_flush(LOG_STDOUT); @@ -4869,11 +4878,8 @@ void ultra_scan(vector &Targets, struct scan_lists *ports, (USI->gstats->num_hosts_timedout == 1)? "host" : "hosts"); USI->SPM->endTask(NULL, additional_info); } - if (o.debugging) { - log_write(LOG_STDOUT, "Overall sending rates: %.2f packets / s, %.2f bytes / s.\n", - USI->send_rate_meter.getOverallPacketRate(), - USI->send_rate_meter.getOverallByteRate()); - } + if (o.debugging) + USI->log_overall_rates(LOG_STDOUT); if (o.debugging > 2 && USI->pd != NULL) pcap_print_stats(LOG_PLAIN, USI->pd); diff --git a/timing.cc b/timing.cc index fd769d4bb..52b8069bc 100644 --- a/timing.cc +++ b/timing.cc @@ -307,6 +307,14 @@ double RateMeter::getCurrentByteRate(const struct timeval *now, bool update) { return current_byte_rate; } +unsigned long long RateMeter::getNumPackets(void) const { + return num_packets; +} + +unsigned long long RateMeter::getNumBytes(void) const { + return num_bytes; +} + /* Update the rates to include packets additional packets and bytes additional bytes. If now is not NULL, use it as the time the packets and bytes were received rather than calling gettimeofday. */ diff --git a/timing.h b/timing.h index a1698d106..f79cd2716 100644 --- a/timing.h +++ b/timing.h @@ -142,6 +142,8 @@ class RateMeter { double getCurrentPacketRate(const struct timeval *now = NULL, bool update =true); double getOverallByteRate(const struct timeval *now = NULL) const; double getCurrentByteRate(const struct timeval *now = NULL, bool update =true); + unsigned long long getNumPackets(void) const; + unsigned long long getNumBytes(void) const; private: /* How many seconds to look back when calculating the "current" rates. */