1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 06:01:28 +00:00

Refactor HSS::nextTimeout for efficiency

Avoid function call in macro expansion. Reduce number of struct timeval
assignments.
This commit is contained in:
dmiller
2024-06-26 21:54:29 +00:00
parent ba249b2d65
commit eb21ac9ea0
2 changed files with 19 additions and 27 deletions

View File

@@ -633,15 +633,11 @@ bool HostScanStats::sendOK(struct timeval *when) const {
return false; return false;
} }
/* If there are pending probe timeouts, fills in when with the time of /* If there are pending probe timeouts, compares the earliest one with `when`;
the earliest one and returns true. Otherwise returns false and if it is earlier than `when`, replaces `when` with the time of
puts now in when. */ the earliest one and returns true. Otherwise returns false. */
bool HostScanStats::nextTimeout(struct timeval *when) const { bool HostScanStats::soonerTimeout(struct timeval *when) const {
struct timeval earliest_to = USI->now;
std::list<UltraProbe *>::const_iterator probeI, endI; std::list<UltraProbe *>::const_iterator probeI, endI;
bool pending_probes = false;
assert(when);
/* For any given invocation, the probe timeout is the same for all probes, so /* For any given invocation, the probe timeout is the same for all probes, so
* we can get the earliest-sent probe and then add the timeout to that. * we can get the earliest-sent probe and then add the timeout to that.
@@ -650,22 +646,21 @@ bool HostScanStats::nextTimeout(struct timeval *when) const {
probeI != endI; probeI++) { probeI != endI; probeI++) {
UltraProbe *probe = *probeI; UltraProbe *probe = *probeI;
if (!probe->timedout) { if (!probe->timedout) {
pending_probes = true; unsigned long usec_to = probeTimeout();
if (TIMEVAL_BEFORE(probe->sent, earliest_to)) { struct timeval our_when;
earliest_to = probe->sent; TIMEVAL_ADD(our_when, probe->sent, usec_to);
}
// probes_outstanding is in order by time sent, so // probes_outstanding is in order by time sent, so
// the first one we find is the earliest. // the first one we find is the earliest.
if (TIMEVAL_BEFORE(our_when, *when)) {
// If ours is earlier, replace when.
*when = our_when;
return true;
}
// regardless, there are no earlier probes, so stop looking.
break; break;
} }
} }
if (pending_probes) { return false;
TIMEVAL_ADD(*when, earliest_to, probeTimeout());
}
else {
*when = USI->now;
}
return pending_probes;
} }
/* gives the maximum try number (try numbers start at zero and /* gives the maximum try number (try numbers start at zero and
@@ -1060,10 +1055,7 @@ bool UltraScanInfo::sendOK(struct timeval *when) const {
// or probe timeout. // or probe timeout.
for (host = incompleteHosts.begin(); host != incompleteHosts.end(); for (host = incompleteHosts.begin(); host != incompleteHosts.end();
host++) { host++) {
if ((*host)->nextTimeout(&tmptv)) { (*host)->soonerTimeout(&lowhtime);
if (TIMEVAL_BEFORE(tmptv, lowhtime))
lowhtime = tmptv;
}
} }
*when = lowhtime; *when = lowhtime;
} }

View File

@@ -391,10 +391,10 @@ public:
true. */ true. */
bool sendOK(struct timeval *when) const; bool sendOK(struct timeval *when) const;
/* If there are pending probe timeouts, fills in when with the time of /* If there are pending probe timeouts, compares the earliest one with `when`;
the earliest one and returns true. Otherwise returns false and if it is earlier than `when`, replaces `when` with the time of
puts now in when. */ the earliest one and returns true. Otherwise returns false. */
bool nextTimeout(struct timeval *when) const; bool soonerTimeout(struct timeval *when) const;
UltraScanInfo *USI; /* The USI which contains this HSS */ UltraScanInfo *USI; /* The USI which contains this HSS */
/* Removes a probe from probes_outstanding, adjusts HSS and USS /* Removes a probe from probes_outstanding, adjusts HSS and USS