From 1566da56f8b9663dd71e69b945cd269616025326 Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 15 Dec 2020 21:45:56 +0000 Subject: [PATCH] Reduce/cache calls to getStateCounts --- output.cc | 4 ++-- portlist.cc | 31 +++++++++++++++++++++++-------- portlist.h | 2 +- portreasons.cc | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/output.cc b/output.cc index 11dbbbd46..8d1ddf116 100644 --- a/output.cc +++ b/output.cc @@ -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 diff --git a/portlist.cc b/portlist.cc index ef8624574..35a8201e7 100644 --- a/portlist.cc +++ b/portlist.cc @@ -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; diff --git a/portlist.h b/portlist.h index ff942d72d..5539a1222 100644 --- a/portlist.h +++ b/portlist.h @@ -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(); diff --git a/portreasons.cc b/portreasons.cc index c24bc3282..5a58dbdbe 100644 --- a/portreasons.cc +++ b/portreasons.cc @@ -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); }