From 6c6d4e33b530ff4543db92033be67050cf302a12 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 16 Sep 2022 01:10:18 +0000 Subject: [PATCH] Use const. New function nmap_getprotbyname() --- output.cc | 4 ++-- protocols.cc | 14 +++++++++++++- protocols.h | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/output.cc b/output.cc index 39f4a09a4..b53fe7228 100644 --- a/output.cc +++ b/output.cc @@ -504,7 +504,7 @@ void printportoutput(const Target *currenths, const PortList *plist) { char serviceinfo[64]; int i; int first = 1; - struct nprotoent *proto; + const struct nprotoent *proto; Port *current; Port port; char hostname[1200]; @@ -2282,7 +2282,7 @@ static void printtraceroute_normal(const Target *currenths) { log_write(LOG_PLAIN, "TRACEROUTE (using port %d/%s)\n", probe.pd.sctp.dport, proto2ascii_lowercase(probe.proto)); } else if (probe.type == PS_ICMP || probe.type == PS_ICMPV6 || probe.type == PS_PROTO) { - struct nprotoent *proto = nmap_getprotbynum(probe.proto); + const struct nprotoent *proto = nmap_getprotbynum(probe.proto); log_write(LOG_PLAIN, "TRACEROUTE (using proto %d/%s)\n", probe.proto, proto ? proto->p_name : "unknown"); } else if (probe.type == PS_NONE) { diff --git a/protocols.cc b/protocols.cc index 216b13342..247353988 100644 --- a/protocols.cc +++ b/protocols.cc @@ -181,7 +181,7 @@ int addprotocolsfromservmask(char *mask, u8 *porttbl) { } -struct nprotoent *nmap_getprotbynum(int num) { +const struct nprotoent *nmap_getprotbynum(int num) { if (nmap_protocols_init() == -1) return NULL; @@ -189,3 +189,15 @@ struct nprotoent *nmap_getprotbynum(int num) { assert(num >= 0 && num < UCHAR_MAX); return protocol_table[num]; } + +const struct nprotoent *nmap_getprotbyname(const char *name) { + + if (nmap_protocols_init() == -1) + return NULL; + + ProtoMap::const_iterator it = proto_map.find(name); + if (it != proto_map.end()) { + return &it->second; + } + return NULL; +} diff --git a/protocols.h b/protocols.h index 3f504456c..104ef59cc 100644 --- a/protocols.h +++ b/protocols.h @@ -77,7 +77,8 @@ struct nprotoent { }; int addprotocolsfromservmask(char *mask, u8 *porttbl); -struct nprotoent *nmap_getprotbynum(int num); +const struct nprotoent *nmap_getprotbynum(int num); +const struct nprotoent *nmap_getprotbyname(const char *name); #define MAX_IPPROTOSTRLEN 4 #define IPPROTO2STR(p) \