mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 12:41:29 +00:00
Store port numbers in host byte order in number-to-service mapping.
I don't know why these were in network byte order. Every single interaction had a htons on entrance and ntohs on exit.
This commit is contained in:
6
nmap.cc
6
nmap.cc
@@ -2661,15 +2661,15 @@ static void getpts_aux(const char *origexpr, int nested, u8 *porttbl, int range_
|
|||||||
} else {
|
} else {
|
||||||
if (nested) {
|
if (nested) {
|
||||||
if ((range_type & SCAN_TCP_PORT) &&
|
if ((range_type & SCAN_TCP_PORT) &&
|
||||||
nmap_getservbyport(htons(rangestart), "tcp")) {
|
nmap_getservbyport(rangestart, "tcp")) {
|
||||||
porttbl[rangestart] |= SCAN_TCP_PORT;
|
porttbl[rangestart] |= SCAN_TCP_PORT;
|
||||||
}
|
}
|
||||||
if ((range_type & SCAN_UDP_PORT) &&
|
if ((range_type & SCAN_UDP_PORT) &&
|
||||||
nmap_getservbyport(htons(rangestart), "udp")) {
|
nmap_getservbyport(rangestart, "udp")) {
|
||||||
porttbl[rangestart] |= SCAN_UDP_PORT;
|
porttbl[rangestart] |= SCAN_UDP_PORT;
|
||||||
}
|
}
|
||||||
if ((range_type & SCAN_SCTP_PORT) &&
|
if ((range_type & SCAN_SCTP_PORT) &&
|
||||||
nmap_getservbyport(htons(rangestart), "sctp")) {
|
nmap_getservbyport(rangestart, "sctp")) {
|
||||||
porttbl[rangestart] |= SCAN_SCTP_PORT;
|
porttbl[rangestart] |= SCAN_SCTP_PORT;
|
||||||
}
|
}
|
||||||
if ((range_type & SCAN_PROTOCOLS) &&
|
if ((range_type & SCAN_PROTOCOLS) &&
|
||||||
|
|||||||
@@ -707,15 +707,15 @@ void getpts_aux(const char *origexpr, int nested, u8 *porttbl, int *portwarning)
|
|||||||
} else {
|
} else {
|
||||||
//if (nested) {
|
//if (nested) {
|
||||||
//if ((range_type & SCAN_TCP_PORT) &&
|
//if ((range_type & SCAN_TCP_PORT) &&
|
||||||
//nmap_getservbyport(htons(rangestart), "tcp")) {
|
//nmap_getservbyport(rangestart, "tcp")) {
|
||||||
//porttbl[rangestart] |= SCAN_TCP_PORT;
|
//porttbl[rangestart] |= SCAN_TCP_PORT;
|
||||||
//}
|
//}
|
||||||
//if ((range_type & SCAN_UDP_PORT) &&
|
//if ((range_type & SCAN_UDP_PORT) &&
|
||||||
//nmap_getservbyport(htons(rangestart), "udp")) {
|
//nmap_getservbyport(rangestart, "udp")) {
|
||||||
//porttbl[rangestart] |= SCAN_UDP_PORT;
|
//porttbl[rangestart] |= SCAN_UDP_PORT;
|
||||||
//}
|
//}
|
||||||
//if ((range_type & SCAN_SCTP_PORT) &&
|
//if ((range_type & SCAN_SCTP_PORT) &&
|
||||||
//nmap_getservbyport(htons(rangestart), "sctp")) {
|
//nmap_getservbyport(rangestart, "sctp")) {
|
||||||
//porttbl[rangestart] |= SCAN_SCTP_PORT;
|
//porttbl[rangestart] |= SCAN_SCTP_PORT;
|
||||||
//}
|
//}
|
||||||
//if ((range_type & SCAN_PROTOCOLS) &&
|
//if ((range_type & SCAN_PROTOCOLS) &&
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ void Port::getNmapServiceName(char *namebuf, int buflen) const {
|
|||||||
} else {
|
} else {
|
||||||
struct servent *service;
|
struct servent *service;
|
||||||
|
|
||||||
service = nmap_getservbyport(htons(portno), IPPROTO2STR(proto));
|
service = nmap_getservbyport(portno, IPPROTO2STR(proto));
|
||||||
if (service != NULL)
|
if (service != NULL)
|
||||||
service_name = service->s_name;
|
service_name = service->s_name;
|
||||||
else
|
else
|
||||||
@@ -293,7 +293,7 @@ void PortList::getServiceDeductions(u16 portno, int protocol, struct serviceDedu
|
|||||||
|
|
||||||
/* Look up the service name. */
|
/* Look up the service name. */
|
||||||
*sd = serviceDeductions();
|
*sd = serviceDeductions();
|
||||||
service = nmap_getservbyport(htons(portno), IPPROTO2STR(protocol));
|
service = nmap_getservbyport(portno, IPPROTO2STR(protocol));
|
||||||
if (service != NULL)
|
if (service != NULL)
|
||||||
sd->name = service->s_name;
|
sd->name = service->s_name;
|
||||||
else
|
else
|
||||||
@@ -360,7 +360,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
|
|||||||
Just look up the service name if none is provided. */
|
Just look up the service name if none is provided. */
|
||||||
if (sname == NULL) {
|
if (sname == NULL) {
|
||||||
struct servent *service;
|
struct servent *service;
|
||||||
service = nmap_getservbyport(htons(portno), IPPROTO2STR(protocol));
|
service = nmap_getservbyport(portno, IPPROTO2STR(protocol));
|
||||||
if (service != NULL)
|
if (service != NULL)
|
||||||
sname = service->s_name;
|
sname = service->s_name;
|
||||||
}
|
}
|
||||||
|
|||||||
31
services.cc
31
services.cc
@@ -106,7 +106,7 @@
|
|||||||
/* This structure is the key for looking up services in the
|
/* This structure is the key for looking up services in the
|
||||||
port/proto -> service map. */
|
port/proto -> service map. */
|
||||||
struct port_spec {
|
struct port_spec {
|
||||||
int portno; /* Network byte order */
|
int portno;
|
||||||
std::string proto;
|
std::string proto;
|
||||||
|
|
||||||
/* Sort in the usual nmap-services order. */
|
/* Sort in the usual nmap-services order. */
|
||||||
@@ -234,8 +234,6 @@ static int nmap_services_init() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
portno = htons(portno);
|
|
||||||
|
|
||||||
port_spec ps;
|
port_spec ps;
|
||||||
ps.portno = portno;
|
ps.portno = portno;
|
||||||
ps.proto = proto;
|
ps.proto = proto;
|
||||||
@@ -245,7 +243,7 @@ static int nmap_services_init() {
|
|||||||
i = service_table.find(ps);
|
i = service_table.find(ps);
|
||||||
if (i != service_table.end()) {
|
if (i != service_table.end()) {
|
||||||
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", portno, proto, filename);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,15 +312,15 @@ int addportsfromservmask(char *mask, u8 *porttbl, int range_type) {
|
|||||||
service_node& current = i->second;
|
service_node& current = i->second;
|
||||||
if (wildtest(mask, current.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.s_proto, "tcp") == 0) {
|
||||||
porttbl[ntohs(current.s_port)] |= SCAN_TCP_PORT;
|
porttbl[current.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.s_proto, "udp") == 0) {
|
||||||
porttbl[ntohs(current.s_port)] |= SCAN_UDP_PORT;
|
porttbl[current.s_port] |= SCAN_UDP_PORT;
|
||||||
t++;
|
t++;
|
||||||
}
|
}
|
||||||
if ((range_type & SCAN_SCTP_PORT) && strcmp(current.s_proto, "sctp") == 0) {
|
if ((range_type & SCAN_SCTP_PORT) && strcmp(current.s_proto, "sctp") == 0) {
|
||||||
porttbl[ntohs(current.s_port)] |= SCAN_SCTP_PORT;
|
porttbl[current.s_port] |= SCAN_SCTP_PORT;
|
||||||
t++;
|
t++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -333,7 +331,6 @@ int addportsfromservmask(char *mask, u8 *porttbl, int range_type) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* 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) {
|
||||||
std::map<port_spec, service_node>::iterator i;
|
std::map<port_spec, service_node>::iterator i;
|
||||||
port_spec ps;
|
port_spec ps;
|
||||||
@@ -371,15 +368,15 @@ static bool is_port_member(const struct scan_lists *ptsdata, const struct servic
|
|||||||
|
|
||||||
if (strcmp(serv->s_proto, "tcp") == 0) {
|
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->s_port) == ptsdata->tcp_ports[i])
|
if (serv->s_port == ptsdata->tcp_ports[i])
|
||||||
return true;
|
return true;
|
||||||
} else if (strcmp(serv->s_proto, "udp") == 0) {
|
} 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->s_port) == ptsdata->udp_ports[i])
|
if (serv->s_port == ptsdata->udp_ports[i])
|
||||||
return true;
|
return true;
|
||||||
} else if (strcmp(serv->s_proto, "sctp") == 0) {
|
} else if (strcmp(serv->s_proto, "sctp") == 0) {
|
||||||
for (i=0; i<ptsdata->sctp_count; i++)
|
for (i=0; i<ptsdata->sctp_count; i++)
|
||||||
if (ntohs(serv->s_port) == ptsdata->sctp_ports[i])
|
if (serv->s_port == ptsdata->sctp_ports[i])
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,11 +470,11 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
continue;
|
continue;
|
||||||
if (current->ratio >= level) {
|
if (current->ratio >= level) {
|
||||||
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0)
|
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0)
|
||||||
ports->tcp_ports[ti++] = ntohs(current->s_port);
|
ports->tcp_ports[ti++] = current->s_port;
|
||||||
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0)
|
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0)
|
||||||
ports->udp_ports[ui++] = ntohs(current->s_port);
|
ports->udp_ports[ui++] = current->s_port;
|
||||||
else if (o.SCTPScan() && strcmp(current->s_proto, "sctp") == 0)
|
else if (o.SCTPScan() && strcmp(current->s_proto, "sctp") == 0)
|
||||||
ports->sctp_ports[si++] = ntohs(current->s_port);
|
ports->sctp_ports[si++] = current->s_port;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -506,11 +503,11 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) {
|
|||||||
if (ptsdata_initialized && !is_port_member(&ptsdata, current))
|
if (ptsdata_initialized && !is_port_member(&ptsdata, current))
|
||||||
continue;
|
continue;
|
||||||
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0 && ti < ports->tcp_count)
|
if (o.TCPScan() && strcmp(current->s_proto, "tcp") == 0 && ti < ports->tcp_count)
|
||||||
ports->tcp_ports[ti++] = ntohs(current->s_port);
|
ports->tcp_ports[ti++] = current->s_port;
|
||||||
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0 && ui < ports->udp_count)
|
else if (o.UDPScan() && strcmp(current->s_proto, "udp") == 0 && ui < ports->udp_count)
|
||||||
ports->udp_ports[ui++] = ntohs(current->s_port);
|
ports->udp_ports[ui++] = current->s_port;
|
||||||
else if (o.SCTPScan() && strcmp(current->s_proto, "sctp") == 0 && si < ports->sctp_count)
|
else if (o.SCTPScan() && strcmp(current->s_proto, "sctp") == 0 && si < ports->sctp_count)
|
||||||
ports->sctp_ports[si++] = ntohs(current->s_port);
|
ports->sctp_ports[si++] = current->s_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ti < ports->tcp_count) ports->tcp_count = ti;
|
if (ti < ports->tcp_count) ports->tcp_count = ti;
|
||||||
|
|||||||
Reference in New Issue
Block a user