mirror of
https://github.com/nmap/nmap.git
synced 2025-12-17 13:09:02 +00:00
Improve the efficiency of loading the nmap-services file. We now use an STL map
instead of a custom hash table and an STL list instead of a custom linked list. The biggest gain comes from using the list.sort method rather than inserting ports in sorted order (equivalent to insertion sort). The new code passes Doug's p-switch-tests. Here are time comparisons, using the old and new services code, and using the standard nmap-services file and the 65535-port nmap-services-huge. The times are the duration of the call to nmap_services_init. Three trials were done for each case, except for the old-code/nmap-services-huge case. nmap-services: old code: 0.215 0.201 0.227 (average 0.214 s) new code: 0.025 0.022 0.023 (average 0.023 s) nmap-services-huge: old code: 441.014 (average 441.014 s) new code: 0.984 0.975 0.978 (average 0.979 s)
This commit is contained in:
210
services.cc
210
services.cc
@@ -99,17 +99,49 @@
|
|||||||
|
|
||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include "services.h"
|
#include "services.h"
|
||||||
#include "NmapOps.h"
|
#include "NmapOps.h"
|
||||||
#include "charpool.h"
|
#include "charpool.h"
|
||||||
#include "nmap_error.h"
|
#include "nmap_error.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
/* This structure is the key for looking up services in the
|
||||||
|
port/proto -> service map. */
|
||||||
|
struct port_spec {
|
||||||
|
int portno; /* Network byte order */
|
||||||
|
std::string proto;
|
||||||
|
|
||||||
|
/* Sort in the usual nmap-services order. */
|
||||||
|
bool operator<(const port_spec& other) const {
|
||||||
|
if (this->portno < other.portno)
|
||||||
|
return true;
|
||||||
|
else if (this->portno > other.portno)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return this->proto < other.proto;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* This is a servent augmented by a frequency ratio. */
|
||||||
|
struct service_node : public servent {
|
||||||
|
public:
|
||||||
|
double ratio;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Compare the ratios of two service nodes for top-ports purposes. Larger ratios
|
||||||
|
come before smaller. */
|
||||||
|
bool service_node_ratio_compare(const service_node& a, const service_node& b) {
|
||||||
|
return a.ratio > b.ratio;
|
||||||
|
}
|
||||||
|
|
||||||
extern NmapOps o;
|
extern NmapOps o;
|
||||||
static int numtcpports = 0;
|
static int numtcpports = 0;
|
||||||
static int numudpports = 0;
|
static int numudpports = 0;
|
||||||
static struct service_list *service_table[SERVICE_TABLE_SIZE];
|
static std::map<port_spec, service_node> service_table;
|
||||||
static struct service_list *sorted_services = NULL;
|
static std::list<service_node> services_by_ratio;
|
||||||
static int services_initialized = 0;
|
static int services_initialized = 0;
|
||||||
static int ratio_format = 0; // 0 = /etc/services no-ratio format. 1 = new nmap format
|
static int ratio_format = 0; // 0 = /etc/services no-ratio format. 1 = new nmap format
|
||||||
|
|
||||||
@@ -123,7 +155,6 @@ static int nmap_services_init() {
|
|||||||
char *p;
|
char *p;
|
||||||
char line[1024];
|
char line[1024];
|
||||||
int lineno = 0;
|
int lineno = 0;
|
||||||
struct service_list *current, *previous, *sp;
|
|
||||||
int res;
|
int res;
|
||||||
double ratio;
|
double ratio;
|
||||||
int ratio_n, ratio_d;
|
int ratio_n, ratio_d;
|
||||||
@@ -159,8 +190,6 @@ static int nmap_services_init() {
|
|||||||
/* Record where this data file was found. */
|
/* Record where this data file was found. */
|
||||||
o.loaded_data_files["nmap-services"] = filename;
|
o.loaded_data_files["nmap-services"] = filename;
|
||||||
|
|
||||||
memset(service_table, 0, sizeof(service_table));
|
|
||||||
|
|
||||||
while(fgets(line, sizeof(line), fp)) {
|
while(fgets(line, sizeof(line), fp)) {
|
||||||
lineno++;
|
lineno++;
|
||||||
p = line;
|
p = line;
|
||||||
@@ -191,21 +220,18 @@ static int nmap_services_init() {
|
|||||||
|
|
||||||
portno = htons(portno);
|
portno = htons(portno);
|
||||||
|
|
||||||
|
port_spec ps;
|
||||||
|
ps.portno = portno;
|
||||||
|
ps.proto = proto;
|
||||||
|
|
||||||
/* Now we make sure our service table doesn't have duplicates */
|
/* Now we make sure our service table doesn't have duplicates */
|
||||||
for(current = service_table[portno % SERVICE_TABLE_SIZE], previous = NULL;
|
std::map<port_spec, service_node>::iterator i;
|
||||||
current; current = current->next) {
|
i = service_table.find(ps);
|
||||||
if (portno == (u16) current->servent->s_port &&
|
if (i != service_table.end()) {
|
||||||
strcasecmp(proto, current->servent->s_proto) == 0) {
|
if (o.debugging)
|
||||||
if (o.debugging) {
|
|
||||||
error("Port %d proto %s is duplicated in services file %s", ntohs(portno), proto, filename);
|
error("Port %d proto %s is duplicated in services file %s", ntohs(portno), proto, filename);
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
previous = current;
|
|
||||||
}
|
|
||||||
/* Current service in the file was a duplicate, get another one */
|
|
||||||
if (current)
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (strncasecmp(proto, "tcp", 3) == 0) {
|
if (strncasecmp(proto, "tcp", 3) == 0) {
|
||||||
numtcpports++;
|
numtcpports++;
|
||||||
@@ -223,41 +249,21 @@ static int nmap_services_init() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
current = (struct service_list *) cp_alloc(sizeof(struct service_list));
|
struct service_node sn;
|
||||||
current->servent = (struct servent *) cp_alloc(sizeof(struct servent));
|
|
||||||
current->ratio = ratio;
|
|
||||||
current->next = NULL;
|
|
||||||
if (previous == NULL) {
|
|
||||||
service_table[portno % SERVICE_TABLE_SIZE] = current;
|
|
||||||
} else {
|
|
||||||
previous->next = current;
|
|
||||||
}
|
|
||||||
current->servent->s_name = cp_strdup(servicename);
|
|
||||||
current->servent->s_port = portno;
|
|
||||||
current->servent->s_proto = cp_strdup(proto);
|
|
||||||
current->servent->s_aliases = NULL;
|
|
||||||
|
|
||||||
sp = (struct service_list *) cp_alloc(sizeof(struct service_list));
|
sn.s_name = cp_strdup(servicename);
|
||||||
sp->servent = current->servent;
|
sn.s_port = portno;
|
||||||
sp->ratio = current->ratio;
|
sn.s_proto = cp_strdup(proto);
|
||||||
sp->next = NULL;
|
sn.s_aliases = NULL;
|
||||||
|
sn.ratio = ratio;
|
||||||
|
|
||||||
if (sorted_services == NULL || sorted_services->ratio < sp->ratio) {
|
service_table[ps] = sn;
|
||||||
sp->next = sorted_services;
|
|
||||||
sorted_services = sp;
|
services_by_ratio.push_back(sn);
|
||||||
} else
|
|
||||||
for (current=sorted_services;;current=current->next) {
|
|
||||||
if (current->next == NULL) {
|
|
||||||
current->next = sp;
|
|
||||||
break;
|
|
||||||
} else if (current->next->ratio < sp->ratio) {
|
|
||||||
sp->next = current->next;
|
|
||||||
current->next = sp;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/* Sort the list of ports sorted by frequency for top-ports purposes. */
|
||||||
|
services_by_ratio.sort(service_node_ratio_compare);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
services_initialized = 1;
|
services_initialized = 1;
|
||||||
@@ -273,52 +279,47 @@ static int nmap_services_init() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
int addportsfromservmask(char *mask, u8 *porttbl, int range_type) {
|
int addportsfromservmask(char *mask, u8 *porttbl, int range_type) {
|
||||||
struct service_list *current;
|
std::map<port_spec, service_node>::iterator i;
|
||||||
int bucket,t=0;
|
int t = 0;
|
||||||
|
|
||||||
if (!services_initialized && nmap_services_init() == -1)
|
if (!services_initialized && nmap_services_init() == -1)
|
||||||
fatal("%s: Couldn't get port numbers", __func__);
|
fatal("%s: Couldn't get port numbers", __func__);
|
||||||
|
|
||||||
for(bucket = 0; bucket < SERVICE_TABLE_SIZE; bucket++) {
|
for (i = service_table.begin(); i != service_table.end(); i++) {
|
||||||
for(current = service_table[bucket % SERVICE_TABLE_SIZE]; current; current = current->next) {
|
service_node& current = i->second;
|
||||||
if (wildtest(mask, current->servent->s_name)) {
|
if (wildtest(mask, current.s_name)) {
|
||||||
|
if ((range_type & SCAN_TCP_PORT) && strcmp(current.s_proto, "tcp") == 0) {
|
||||||
if ((range_type & SCAN_TCP_PORT) && strcmp(current->servent->s_proto, "tcp") == 0) {
|
porttbl[ntohs(current.s_port)] |= SCAN_TCP_PORT;
|
||||||
porttbl[ntohs(current->servent->s_port)] |= SCAN_TCP_PORT;
|
|
||||||
t++;
|
t++;
|
||||||
}
|
}
|
||||||
|
if ((range_type & SCAN_UDP_PORT) && strcmp(current.s_proto, "udp") == 0) {
|
||||||
if ((range_type & SCAN_UDP_PORT) && strcmp(current->servent->s_proto, "udp") == 0) {
|
porttbl[ntohs(current.s_port)] |= SCAN_UDP_PORT;
|
||||||
porttbl[ntohs(current->servent->s_port)] |= SCAN_UDP_PORT;
|
|
||||||
t++;
|
t++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Port must be in network byte order. */
|
||||||
struct servent *nmap_getservbyport(int port, const char *proto) {
|
struct servent *nmap_getservbyport(int port, const char *proto) {
|
||||||
struct service_list *current;
|
std::map<port_spec, service_node>::iterator i;
|
||||||
|
port_spec ps;
|
||||||
|
|
||||||
if (nmap_services_init() == -1)
|
if (nmap_services_init() == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for(current = service_table[port % SERVICE_TABLE_SIZE];
|
ps.portno = port;
|
||||||
current; current = current->next) {
|
ps.proto = proto;
|
||||||
if (((u16) port == (u16) current->servent->s_port) &&
|
i = service_table.find(ps);
|
||||||
strcmp(proto, current->servent->s_proto) == 0)
|
if (i != service_table.end())
|
||||||
return current->servent;
|
return &i->second;
|
||||||
}
|
|
||||||
|
|
||||||
/* Couldn't find it ... oh well. */
|
/* Couldn't find it ... oh well. */
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -336,18 +337,20 @@ static int port_compare(const void *a, const void *b) {
|
|||||||
// called when you use a non-default top-ports or port-ratio value TOGETHER WITH
|
// called when you use a non-default top-ports or port-ratio value TOGETHER WITH
|
||||||
// a -p portlist.
|
// a -p portlist.
|
||||||
|
|
||||||
static int is_port_member(struct scan_lists *ptsdata, struct service_list *serv) {
|
static bool is_port_member(const struct scan_lists *ptsdata, const struct service_node *serv) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (serv->servent->s_proto[0] == 't') {
|
if (strcmp(serv->s_proto, "tcp") == 0) {
|
||||||
for (i=0; i<ptsdata->tcp_count; i++)
|
for (i=0; i<ptsdata->tcp_count; i++)
|
||||||
if (ntohs(serv->servent->s_port) == ptsdata->tcp_ports[i]) return 1;
|
if (ntohs(serv->s_port) == ptsdata->tcp_ports[i])
|
||||||
} else {
|
return true;
|
||||||
|
} else if (strcmp(serv->s_proto, "udp") == 0) {
|
||||||
for (i=0; i<ptsdata->udp_count; i++)
|
for (i=0; i<ptsdata->udp_count; i++)
|
||||||
if (ntohs(serv->servent->s_port) == ptsdata->udp_ports[i]) return 1;
|
if (ntohs(serv->s_port) == ptsdata->udp_ports[i])
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// gettoppts() sets its third parameter, a scan_list, with the most
|
// gettoppts() sets its third parameter, a scan_list, with the most
|
||||||
@@ -367,7 +370,8 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
int ti=0, ui=0;
|
int ti=0, ui=0;
|
||||||
struct scan_lists ptsdata = { 0 };
|
struct scan_lists ptsdata = { 0 };
|
||||||
bool ptsdata_initialized = false;
|
bool ptsdata_initialized = false;
|
||||||
struct service_list *current;
|
const struct service_node *current;
|
||||||
|
std::list<service_node>::iterator i;
|
||||||
|
|
||||||
if (!services_initialized && nmap_services_init() == -1)
|
if (!services_initialized && nmap_services_init() == -1)
|
||||||
fatal("%s: Couldn't get port numbers", __func__);
|
fatal("%s: Couldn't get port numbers", __func__);
|
||||||
@@ -403,13 +407,18 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
ptsdata_initialized = true;
|
ptsdata_initialized = true;
|
||||||
}
|
}
|
||||||
if (level < 1) {
|
if (level < 1) {
|
||||||
for (current=sorted_services; current; current=current->next) {
|
for (i = services_by_ratio.begin(); i != services_by_ratio.end(); i++) {
|
||||||
if (ptsdata_initialized && !is_port_member(&ptsdata, current)) continue;
|
current = &(*i);
|
||||||
|
if (ptsdata_initialized && !is_port_member(&ptsdata, current))
|
||||||
|
continue;
|
||||||
if (current->ratio >= level) {
|
if (current->ratio >= level) {
|
||||||
if (o.TCPScan() && current->servent->s_proto[0] == 't') ports->tcp_count++;
|
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0)
|
||||||
else if (o.UDPScan() && current->servent->s_proto[0] == 'u') ports->udp_count++;
|
ports->tcp_count++;
|
||||||
} else break;
|
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0)
|
||||||
|
ports->udp_count++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ports->tcp_count)
|
if (ports->tcp_count)
|
||||||
@@ -420,15 +429,18 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
|
|
||||||
ports->prots = NULL;
|
ports->prots = NULL;
|
||||||
|
|
||||||
for (current=sorted_services;current;current=current->next) {
|
for (i = services_by_ratio.begin(); i != services_by_ratio.end(); i++) {
|
||||||
if (ptsdata_initialized && !is_port_member(&ptsdata, current)) continue;
|
current = &(*i);
|
||||||
|
if (ptsdata_initialized && !is_port_member(&ptsdata, current))
|
||||||
|
continue;
|
||||||
if (current->ratio >= level) {
|
if (current->ratio >= level) {
|
||||||
if (o.TCPScan() && current->servent->s_proto[0] == 't')
|
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0)
|
||||||
ports->tcp_ports[ti++] = ntohs(current->servent->s_port);
|
ports->tcp_ports[ti++] = ntohs(current->s_port);
|
||||||
else if (o.UDPScan() && current->servent->s_proto[0] == 'u')
|
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0)
|
||||||
ports->udp_ports[ui++] = ntohs(current->servent->s_port);
|
ports->udp_ports[ui++] = ntohs(current->s_port);
|
||||||
} else break;
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (level >= 1) {
|
} else if (level >= 1) {
|
||||||
if (level > 65536)
|
if (level > 65536)
|
||||||
@@ -438,7 +450,6 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
ports->tcp_count = MIN((int) level, numtcpports);
|
ports->tcp_count = MIN((int) level, numtcpports);
|
||||||
ports->tcp_ports = (unsigned short *)safe_zalloc(ports->tcp_count * sizeof(unsigned short));
|
ports->tcp_ports = (unsigned short *)safe_zalloc(ports->tcp_count * sizeof(unsigned short));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.UDPScan()) {
|
if (o.UDPScan()) {
|
||||||
ports->udp_count = MIN((int) level, numudpports);
|
ports->udp_count = MIN((int) level, numudpports);
|
||||||
ports->udp_ports = (unsigned short *)safe_zalloc(ports->udp_count * sizeof(unsigned short));
|
ports->udp_ports = (unsigned short *)safe_zalloc(ports->udp_count * sizeof(unsigned short));
|
||||||
@@ -446,13 +457,14 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
|
|
||||||
ports->prots = NULL;
|
ports->prots = NULL;
|
||||||
|
|
||||||
for (current=sorted_services;current && (ti < ports->tcp_count || ui < ports->udp_count);current=current->next) {
|
for (i = services_by_ratio.begin(); i != services_by_ratio.end(); i++) {
|
||||||
if (ptsdata_initialized && !is_port_member(&ptsdata, current)) continue;
|
current = &(*i);
|
||||||
|
if (ptsdata_initialized && !is_port_member(&ptsdata, current))
|
||||||
if (o.TCPScan() && current->servent->s_proto[0] == 't' && ti < ports->tcp_count)
|
continue;
|
||||||
ports->tcp_ports[ti++] = ntohs(current->servent->s_port);
|
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0 && ti < ports->tcp_count)
|
||||||
else if (o.UDPScan() && current->servent->s_proto[0] == 'u' && ui < ports->udp_count)
|
ports->tcp_ports[ti++] = ntohs(current->s_port);
|
||||||
ports->udp_ports[ui++] = ntohs(current->servent->s_port);
|
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0 && ui < ports->udp_count)
|
||||||
|
ports->udp_ports[ui++] = ntohs(current->s_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ti < ports->tcp_count) ports->tcp_count = ti;
|
if (ti < ports->tcp_count) ports->tcp_count = ti;
|
||||||
|
|||||||
@@ -122,12 +122,6 @@
|
|||||||
#define SCAN_UDP_PORT (1 << 1)
|
#define SCAN_UDP_PORT (1 << 1)
|
||||||
#define SCAN_PROTOCOLS (1 << 2)
|
#define SCAN_PROTOCOLS (1 << 2)
|
||||||
|
|
||||||
struct service_list {
|
|
||||||
struct servent *servent;
|
|
||||||
double ratio;
|
|
||||||
struct service_list *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
int addportsfromservmask(char *mask, u8 *porttbl, int range_type);
|
int addportsfromservmask(char *mask, u8 *porttbl, int range_type);
|
||||||
struct servent *nmap_getservbyport(int port, const char *proto);
|
struct servent *nmap_getservbyport(int port, const char *proto);
|
||||||
void gettoppts(double level, char *portlist, struct scan_lists * ports);
|
void gettoppts(double level, char *portlist, struct scan_lists * ports);
|
||||||
|
|||||||
Reference in New Issue
Block a user