1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +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) { if (o.ipprotscan) {
current = NULL; current = NULL;
while ((current = plist->nextPort(current, &port, IPPROTO_IP, 0)) != NULL) { while ((current = plist->nextPort(current, &port, IPPROTO_IP, 0)) != NULL) {
if (!plist->isIgnoredState(current->state)) { if (!plist->isIgnoredState(current->state, NULL)) {
if (!first) if (!first)
log_write(LOG_MACHINE, ", "); log_write(LOG_MACHINE, ", ");
else else
@@ -729,7 +729,7 @@ void printportoutput(Target *currenths, PortList *plist) {
current = NULL; current = NULL;
while ((current = plist->nextPort(current, &port, TCPANDUDPANDSCTP, 0)) != NULL) { while ((current = plist->nextPort(current, &port, TCPANDUDPANDSCTP, 0)) != NULL) {
if (!plist->isIgnoredState(current->state)) { if (!plist->isIgnoredState(current->state, NULL)) {
if (!first) if (!first)
log_write(LOG_MACHINE, ", "); log_write(LOG_MACHINE, ", ");
else else

View File

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

View File

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

View File

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