1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Reduce/cache calls to getStateCounts

This commit is contained in:
dmiller
2020-12-15 21:45:56 +00:00
parent 27b9a90f3e
commit 1566da56f8
4 changed files with 27 additions and 12 deletions

View File

@@ -676,7 +676,7 @@ void printportoutput(Target *currenths, PortList *plist) {
if (o.ipprotscan) {
current = NULL;
while ((current = plist->nextPort(current, &port, IPPROTO_IP, 0)) != NULL) {
if (!plist->isIgnoredState(current->state)) {
if (!plist->isIgnoredState(current->state, NULL)) {
if (!first)
log_write(LOG_MACHINE, ", ");
else
@@ -729,7 +729,7 @@ void printportoutput(Target *currenths, PortList *plist) {
current = NULL;
while ((current = plist->nextPort(current, &port, TCPANDUDPANDSCTP, 0)) != NULL) {
if (!plist->isIgnoredState(current->state)) {
if (!plist->isIgnoredState(current->state, NULL)) {
if (!first)
log_write(LOG_MACHINE, ", ");
else

View File

@@ -735,10 +735,17 @@ void PortList::initializePortMap(int protocol, u16 *ports, int portcount) {
int PortList::nextIgnoredState(int prevstate) {
int beststate = PORT_UNKNOWN;
int count = 0;
int prevcount = 0;
int bestcount = 0;
if (prevstate != PORT_UNKNOWN) {
prevcount = getStateCounts(prevstate);
}
for(int state=0; state < PORT_HIGHEST_STATE; state++) {
/* The state must be ignored */
if (!isIgnoredState(state))
if (!isIgnoredState(state, &count))
continue;
/* We can't give the same state again ... */
@@ -747,24 +754,28 @@ int PortList::nextIgnoredState(int prevstate) {
/* If a previous state was given, we must have fewer ports than
that one, or be tied but be a larger state number */
if (prevstate != PORT_UNKNOWN &&
(getStateCounts(state) > getStateCounts(prevstate) ||
(getStateCounts(state) == getStateCounts(prevstate) && state <= prevstate)))
(count > prevcount ||
(count == prevcount && state <= prevstate)))
continue;
/* We only qualify if we have more ports than the current best */
if (beststate != PORT_UNKNOWN && getStateCounts(beststate) >= getStateCounts(state))
if (beststate != PORT_UNKNOWN && bestcount >= count)
continue;
/* Yay! We found the best state so far ... */
beststate = state;
bestcount = count;
}
return beststate;
}
/* Returns true if a state should be ignored (consolidated), false otherwise */
bool PortList::isIgnoredState(int state) {
/* Returns true if a state should be ignored (consolidated), false otherwise.
* If result is true and count is provided, it will be filled with the count of
* ports in that state. */
bool PortList::isIgnoredState(int state, int *count) {
int tmp_count = 0;
if (o.debugging > 2)
return false;
@@ -775,10 +786,14 @@ bool PortList::isIgnoredState(int state) {
if (state == PORT_OPENFILTERED && (o.verbose > 2 || o.debugging > 2))
return false;
tmp_count = getStateCounts(state);
if (count != NULL) {
*count = tmp_count;
}
/* If openonly, we always ignore states that don't at least have open
as a possibility. */
if (o.openOnly() && state != PORT_OPENFILTERED && state != PORT_UNFILTERED
&& getStateCounts(state) > 0)
&& tmp_count > 0)
return true;
int max_per_state = 25; // Ignore states with more ports than this
@@ -790,7 +805,7 @@ bool PortList::isIgnoredState(int state) {
max_per_state *= ((o.verbose + 1) + 20 * o.debugging);
}
if (getStateCounts(state) > max_per_state)
if (tmp_count > max_per_state)
return true;
return false;

View File

@@ -254,7 +254,7 @@ class PortList {
int nextIgnoredState(int prevstate);
/* Returns true if a state should be ignored (consolidated), false otherwise */
bool isIgnoredState(int state);
bool isIgnoredState(int state, int *count);
int numIgnoredStates();
int numIgnoredPorts();

View File

@@ -372,7 +372,7 @@ static unsigned int get_state_summary(state_reason_summary_t *head, PortList *Po
reason = head;
while((current = Ports->nextPort(current, &port, proto, state)) != NULL) {
if(Ports->isIgnoredState(current->state)) {
if(Ports->isIgnoredState(current->state, NULL)) {
total++;
update_state_summary(reason, current->reason.reason_id);
}