From a0601febebfa073096e5c3c266c40cb09d5f15e4 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 25 Sep 2012 05:24:16 +0000 Subject: [PATCH] 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. --- nmap.cc | 6 +++--- nping/common_modified.cc | 6 +++--- portlist.cc | 6 +++--- services.cc | 31 ++++++++++++++----------------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/nmap.cc b/nmap.cc index 94c496829..9fcf03232 100644 --- a/nmap.cc +++ b/nmap.cc @@ -2661,15 +2661,15 @@ static void getpts_aux(const char *origexpr, int nested, u8 *porttbl, int range_ } else { if (nested) { if ((range_type & SCAN_TCP_PORT) && - nmap_getservbyport(htons(rangestart), "tcp")) { + nmap_getservbyport(rangestart, "tcp")) { porttbl[rangestart] |= SCAN_TCP_PORT; } if ((range_type & SCAN_UDP_PORT) && - nmap_getservbyport(htons(rangestart), "udp")) { + nmap_getservbyport(rangestart, "udp")) { porttbl[rangestart] |= SCAN_UDP_PORT; } if ((range_type & SCAN_SCTP_PORT) && - nmap_getservbyport(htons(rangestart), "sctp")) { + nmap_getservbyport(rangestart, "sctp")) { porttbl[rangestart] |= SCAN_SCTP_PORT; } if ((range_type & SCAN_PROTOCOLS) && diff --git a/nping/common_modified.cc b/nping/common_modified.cc index 8fa2b3f76..3ee43583b 100644 --- a/nping/common_modified.cc +++ b/nping/common_modified.cc @@ -707,15 +707,15 @@ void getpts_aux(const char *origexpr, int nested, u8 *porttbl, int *portwarning) } else { //if (nested) { //if ((range_type & SCAN_TCP_PORT) && - //nmap_getservbyport(htons(rangestart), "tcp")) { + //nmap_getservbyport(rangestart, "tcp")) { //porttbl[rangestart] |= SCAN_TCP_PORT; //} //if ((range_type & SCAN_UDP_PORT) && - //nmap_getservbyport(htons(rangestart), "udp")) { + //nmap_getservbyport(rangestart, "udp")) { //porttbl[rangestart] |= SCAN_UDP_PORT; //} //if ((range_type & SCAN_SCTP_PORT) && - //nmap_getservbyport(htons(rangestart), "sctp")) { + //nmap_getservbyport(rangestart, "sctp")) { //porttbl[rangestart] |= SCAN_SCTP_PORT; //} //if ((range_type & SCAN_PROTOCOLS) && diff --git a/portlist.cc b/portlist.cc index b8e759dcc..e69c6568c 100644 --- a/portlist.cc +++ b/portlist.cc @@ -173,7 +173,7 @@ void Port::getNmapServiceName(char *namebuf, int buflen) const { } else { struct servent *service; - service = nmap_getservbyport(htons(portno), IPPROTO2STR(proto)); + service = nmap_getservbyport(portno, IPPROTO2STR(proto)); if (service != NULL) service_name = service->s_name; else @@ -293,7 +293,7 @@ void PortList::getServiceDeductions(u16 portno, int protocol, struct serviceDedu /* Look up the service name. */ *sd = serviceDeductions(); - service = nmap_getservbyport(htons(portno), IPPROTO2STR(protocol)); + service = nmap_getservbyport(portno, IPPROTO2STR(protocol)); if (service != NULL) sd->name = service->s_name; else @@ -360,7 +360,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol, Just look up the service name if none is provided. */ if (sname == NULL) { struct servent *service; - service = nmap_getservbyport(htons(portno), IPPROTO2STR(protocol)); + service = nmap_getservbyport(portno, IPPROTO2STR(protocol)); if (service != NULL) sname = service->s_name; } diff --git a/services.cc b/services.cc index 1b07fdcef..4c512740c 100644 --- a/services.cc +++ b/services.cc @@ -106,7 +106,7 @@ /* This structure is the key for looking up services in the port/proto -> service map. */ struct port_spec { - int portno; /* Network byte order */ + int portno; std::string proto; /* Sort in the usual nmap-services order. */ @@ -234,8 +234,6 @@ static int nmap_services_init() { continue; } - portno = htons(portno); - port_spec ps; ps.portno = portno; ps.proto = proto; @@ -245,7 +243,7 @@ static int nmap_services_init() { i = service_table.find(ps); if (i != service_table.end()) { 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; } @@ -314,15 +312,15 @@ int addportsfromservmask(char *mask, u8 *porttbl, int range_type) { service_node& current = i->second; if (wildtest(mask, current.s_name)) { 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++; } 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++; } 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++; } } @@ -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) { std::map::iterator i; 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) { for (i=0; itcp_count; i++) - if (ntohs(serv->s_port) == ptsdata->tcp_ports[i]) + if (serv->s_port == ptsdata->tcp_ports[i]) return true; } else if (strcmp(serv->s_proto, "udp") == 0) { for (i=0; iudp_count; i++) - if (ntohs(serv->s_port) == ptsdata->udp_ports[i]) + if (serv->s_port == ptsdata->udp_ports[i]) return true; } else if (strcmp(serv->s_proto, "sctp") == 0) { for (i=0; isctp_count; i++) - if (ntohs(serv->s_port) == ptsdata->sctp_ports[i]) + if (serv->s_port == ptsdata->sctp_ports[i]) return true; } @@ -473,11 +470,11 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) { continue; if (current->ratio >= level) { 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) - 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) - ports->sctp_ports[si++] = ntohs(current->s_port); + ports->sctp_ports[si++] = current->s_port; } else { break; } @@ -506,11 +503,11 @@ void gettoppts(double level, char *portlist, struct scan_lists * ports) { if (ptsdata_initialized && !is_port_member(&ptsdata, current)) continue; 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) - 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) - ports->sctp_ports[si++] = ntohs(current->s_port); + ports->sctp_ports[si++] = current->s_port; } if (ti < ports->tcp_count) ports->tcp_count = ti;