1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-15 20:29:03 +00:00

Another round of const

This commit is contained in:
dmiller
2021-04-27 19:22:12 +00:00
parent 68d2c0e960
commit 74e066d356
11 changed files with 71 additions and 72 deletions

View File

@@ -175,7 +175,7 @@ class Target {
/* Give the name from the last setTargetName() call, which is the
name of the target given on the command line if it's a named
host. */
const char *TargetName() { return targetname; }
const char *TargetName() const { return targetname; }
/* You can set to NULL to erase a name. The targetname is blown
away when you setTargetSockAddr(), so make sure you do these in proper
order

View File

@@ -274,7 +274,7 @@ public:
delete[] hosts_storage;
}
u32 hash(sockaddr_storage ip)
u32 hash(const sockaddr_storage &ip) const
{
u32 ret = 0;
@@ -475,7 +475,7 @@ static void write_evt_handler(nsock_pool nsp, nsock_event evt, void *req_v) {
// (calls nsock_write()). Does various other tasks like recording
// the time for the timeout.
static void put_dns_packet_on_wire(request *req) {
const size_t maxlen = 512;
static const size_t maxlen = 512;
u8 packet[maxlen];
size_t plen=0;
@@ -643,7 +643,7 @@ static int process_result(const sockaddr_storage &ip, const std::string &result,
// Nsock read handler. One nsock read for each DNS server exists at each
// time. This function uses various helper functions as defined above.
static void read_evt_handler(nsock_pool nsp, nsock_event evt, void *) {
u8 *buf;
const u8 *buf;
int buflen;
if (total_reqs >= 1)
@@ -793,7 +793,7 @@ static void connect_evt_handler(nsock_pool, nsock_event, void *) {}
// commas or spaces - NOTE this doesn't actually do any connecting!
static void add_dns_server(char *ipaddrs) {
std::list<dns_server>::iterator servI;
char *hostname;
const char *hostname;
struct sockaddr_storage addr;
size_t addr_len = sizeof(addr);
@@ -852,7 +852,7 @@ static void connect_dns_servers() {
#ifdef WIN32
static bool interface_is_known_by_guid(const char *guid) {
struct interface_info *iflist;
const struct interface_info *iflist;
int i, n;
iflist = getinterfaces(&n, NULL, 0);
@@ -861,7 +861,7 @@ static bool interface_is_known_by_guid(const char *guid) {
for (i = 0; i < n; i++) {
char pcap_name[1024];
char *pcap_guid;
const char *pcap_guid;
if (!DnetName2PcapName(iflist[i].devname, pcap_name, sizeof(pcap_name)))
continue;
@@ -1355,7 +1355,7 @@ bool DNS::Factory::ptrToIp(const std::string &ptr, sockaddr_storage &ip)
if (NULL != (p = strcasestr(cptr + ptr.length() + 1 - sizeof(C_IPV4_PTR_DOMAIN), C_IPV4_PTR_DOMAIN)))
{
struct sockaddr_in *ip4 = (struct sockaddr_in *)&ip;
u8 place_value[] = {1, 10, 100};
static const u8 place_value[] = {1, 10, 100};
u8 *v = (u8 *) &(ip4->sin_addr.s_addr);
size_t place = 0;
size_t i = 0;

View File

@@ -489,13 +489,13 @@ static char *formatScriptOutput(const ScriptResult &sr) {
#endif /* NOLUA */
/* Output a list of ports, compressing ranges like 80-85 */
static void output_rangelist_given_ports(int logt, unsigned short *ports, int numports);
static void output_rangelist_given_ports(int logt, const unsigned short *ports, int numports);
/* Prints the familiar Nmap tabular output showing the "interesting"
ports found on the machine. It also handles the Machine/Grepable
output and the XML output. It is pretty ugly -- in particular I
should write helper functions to handle the table creation */
void printportoutput(Target *currenths, PortList *plist) {
void printportoutput(const Target *currenths, const PortList *plist) {
char protocol[MAX_IPPROTOSTRLEN + 1];
char portinfo[64];
char grepvers[256];
@@ -1067,7 +1067,7 @@ void log_flush_all() {
/* Open a log descriptor of the type given to the filename given. If
append is true, the file will be appended instead of clobbered if
it already exists. If the file does not exist, it will be created */
int log_open(int logt, bool append, char *filename) {
int log_open(int logt, bool append, const char *filename) {
int i = 0;
if (logt <= 0 || logt > LOG_FILE_MASK)
return -1;
@@ -1098,7 +1098,7 @@ int log_open(int logt, bool append, char *filename) {
/* The items in ports should be
in sequential order for space savings and easier to read output. Outputs the
rangelist to the log stream given (such as LOG_MACHINE or LOG_XML) */
static void output_rangelist_given_ports(int logt, unsigned short *ports,
static void output_rangelist_given_ports(int logt, const unsigned short *ports,
int numports) {
int start, end;
@@ -1120,7 +1120,7 @@ static void output_rangelist_given_ports(int logt, unsigned short *ports,
/* Output the list of ports scanned to the top of machine parseable
logs (in a comment, unfortunately). The items in ports should be
in sequential order for space savings and easier to read output */
void output_ports_to_machine_parseable_output(struct scan_lists *ports) {
void output_ports_to_machine_parseable_output(const struct scan_lists *ports) {
int tcpportsscanned = ports->tcp_count;
int udpportsscanned = ports->udp_count;
int sctpportsscanned = ports->sctp_count;
@@ -1170,7 +1170,7 @@ static void doscanflags() {
/* Simple helper function for output_xml_scaninfo_records */
static void doscaninfo(const char *type, const char *proto,
unsigned short *ports, int numports) {
const unsigned short *ports, int numports) {
xml_open_start_tag("scaninfo");
xml_attribute("type", "%s", type);
if (strncmp(proto, "tcp", 3) == 0) {
@@ -1223,7 +1223,7 @@ std::string join_quoted(const char * const strings[], unsigned int n) {
/* Similar to output_ports_to_machine_parseable_output, this function
outputs the XML version, which is scaninfo records of each scan
requested and the ports which it will scan for */
void output_xml_scaninfo_records(struct scan_lists *scanlist) {
void output_xml_scaninfo_records(const struct scan_lists *scanlist) {
if (o.synscan)
doscaninfo("syn", "tcp", scanlist->tcp_ports, scanlist->tcp_count);
if (o.ackscan)
@@ -1254,7 +1254,7 @@ void output_xml_scaninfo_records(struct scan_lists *scanlist) {
}
/* Prints the MAC address (if discovered) to XML output */
static void print_MAC_XML_Info(Target *currenths) {
static void print_MAC_XML_Info(const Target *currenths) {
const u8 *mac = currenths->MACAddress();
char macascii[32];
@@ -1274,7 +1274,7 @@ static void print_MAC_XML_Info(Target *currenths) {
/* Helper function to write the status and address/hostname info of a host
into the XML log */
static void write_xml_initial_hostinfo(Target *currenths,
static void write_xml_initial_hostinfo(const Target *currenths,
const char *status) {
xml_open_start_tag("status");
xml_attribute("state", "%s", status);
@@ -1313,7 +1313,7 @@ static void write_xml_initial_hostinfo(Target *currenths,
log_flush_all();
}
void write_xml_hosthint(Target *currenths) {
void write_xml_hosthint(const Target *currenths) {
xml_start_tag("hosthint");
write_xml_initial_hostinfo(currenths, (currenths->flags & HOST_UP) ? "up" : "down");
xml_end_tag();
@@ -1391,7 +1391,7 @@ static char *num_to_string_sigdigits(double d, int digits) {
/* Writes a heading for a full scan report ("Nmap scan report for..."),
including host status and DNS records. */
void write_host_header(Target *currenths) {
void write_host_header(const Target *currenths) {
if ((currenths->flags & HOST_UP) || o.verbose || o.always_resolve) {
if (currenths->flags & HOST_UP) {
log_write(LOG_PLAIN, "Nmap scan report for %s\n", currenths->NameIP());
@@ -1427,7 +1427,7 @@ void write_host_header(Target *currenths) {
/* Writes host status info to the log streams (including STDOUT). An
example is "Host: 10.11.12.13 (foo.bar.example.com)\tStatus: Up\n" to
machine log. */
void write_host_status(Target *currenths) {
void write_host_status(const Target *currenths) {
if (o.listscan) {
/* write "unknown" to machine and xml */
log_write(LOG_MACHINE, "Host: %s (%s)\tStatus: Unknown\n",
@@ -1630,7 +1630,7 @@ static void printosclassificationoutput(const struct
network. This only prints to human output -- XML is handled by a
separate call ( print_MAC_XML_Info ) because it needs to be printed
in a certain place to conform to DTD. */
void printmacinfo(Target *currenths) {
void printmacinfo(const Target *currenths) {
const u8 *mac = currenths->MACAddress();
char macascii[32];
@@ -1833,7 +1833,7 @@ static void write_merged_fpr(const FingerPrintResults *FPR,
/* Prints the formatted OS Scan output to stdout, logfiles, etc (but only
if an OS Scan was performed).*/
void printosscanoutput(Target *currenths) {
void printosscanoutput(const Target *currenths) {
int i;
char numlst[512]; /* For creating lists of numbers */
char *p; /* Used in manipulating numlst above */
@@ -2079,7 +2079,7 @@ static int hostcmp(const char *a, const char *b) {
/* Prints the alternate hostname/OS/device information we got from the service
scan (if it was performed) */
void printserviceinfooutput(Target *currenths) {
void printserviceinfooutput(const Target *currenths) {
Port *p = NULL;
Port port;
struct serviceDeductions sd;
@@ -2210,7 +2210,7 @@ void printserviceinfooutput(Target *currenths) {
}
#ifndef NOLUA
void printscriptresults(ScriptResults *scriptResults, stype scantype) {
void printscriptresults(const ScriptResults *scriptResults, stype scantype) {
ScriptResults::const_iterator iter;
char *script_output;
@@ -2234,7 +2234,7 @@ void printscriptresults(ScriptResults *scriptResults, stype scantype) {
}
}
void printhostscriptresults(Target *currenths) {
void printhostscriptresults(const Target *currenths) {
ScriptResults::const_iterator iter;
char *script_output;
@@ -2258,7 +2258,7 @@ void printhostscriptresults(Target *currenths) {
#endif
/* Print a table with traceroute hops. */
static void printtraceroute_normal(Target *currenths) {
static void printtraceroute_normal(const Target *currenths) {
static const int HOP_COL = 0, RTT_COL = 1, HOST_COL = 2;
NmapOutputTable Tbl(currenths->traceroute_hops.size() + 1, 3);
struct probespec probe;
@@ -2367,7 +2367,7 @@ static void printtraceroute_normal(Target *currenths) {
log_flush(LOG_PLAIN);
}
static void printtraceroute_xml(Target *currenths) {
static void printtraceroute_xml(const Target *currenths) {
struct probespec probe;
std::list<TracerouteHop>::const_iterator it;
@@ -2422,12 +2422,12 @@ static void printtraceroute_xml(Target *currenths) {
log_flush(LOG_XML);
}
void printtraceroute(Target *currenths) {
void printtraceroute(const Target *currenths) {
printtraceroute_normal(currenths);
printtraceroute_xml(currenths);
}
void printtimes(Target *currenths) {
void printtimes(const Target *currenths) {
if (currenths->to.srtt != -1 || currenths->to.rttvar != -1) {
if (o.debugging) {
log_write(LOG_STDOUT, "Final times for host: srtt: %d rttvar: %d to: %d\n",

View File

@@ -134,14 +134,14 @@ void win32_fatal_raw_sockets(const char *devname);
ports found on the machine. It also handles the Machine/Grepable
output and the XML output. It is pretty ugly -- in particular I
should write helper functions to handle the table creation */
void printportoutput(Target *currenths, PortList *plist);
void printportoutput(const Target *currenths, const PortList *plist);
/* Prints the MAC address if one was found for the target (generally
this means that the target is directly connected on an ethernet
network. This only prints to human output -- XML is handled by a
separate call ( print_MAC_XML_Info ) because it needs to be printed
in a certain place to conform to DTD. */
void printmacinfo(Target *currenths);
void printmacinfo(const Target *currenths);
char *logfilename(const char *str, struct tm *tm);
@@ -172,12 +172,12 @@ void log_flush_all();
/* Open a log descriptor of the type given to the filename given. If
append is nonzero, the file will be appended instead of clobbered if
it already exists. If the file does not exist, it will be created */
int log_open(int logt, bool append, char *filename);
int log_open(int logt, bool append, const char *filename);
/* Output the list of ports scanned to the top of machine parseable
logs (in a comment, unfortunately). The items in ports should be
in sequential order for space savings and easier to read output */
void output_ports_to_machine_parseable_output(struct scan_lists *ports);
void output_ports_to_machine_parseable_output(const struct scan_lists *ports);
/* Return a std::string containing all n strings separated by whitespace, and
individually quoted if needed. */
@@ -186,42 +186,42 @@ std::string join_quoted(const char * const strings[], unsigned int n);
/* Similar to output_ports_to_machine_parseable_output, this function
outputs the XML version, which is scaninfo records of each scan
requested and the ports which it will scan for */
void output_xml_scaninfo_records(struct scan_lists *ports);
void output_xml_scaninfo_records(const struct scan_lists *ports);
/* Writes a heading for a full scan report ("Nmap scan report for..."),
including host status and DNS records. */
void write_host_header(Target *currenths);
void write_host_header(const Target *currenths);
/* Writes host status info to the log streams (including STDOUT). An
example is "Host: 10.11.12.13 (foo.bar.example.com)\tStatus: Up\n" to
machine log. */
void write_host_status(Target *currenths);
void write_host_status(const Target *currenths);
/* Writes host status info to the XML stream wrapped in a <hosthint> tag */
void write_xml_hosthint(Target *currenths);
void write_xml_hosthint(const Target *currenths);
/* Prints the formatted OS Scan output to stdout, logfiles, etc (but only
if an OS Scan was performed */
void printosscanoutput(Target *currenths);
void printosscanoutput(const Target *currenths);
/* Prints the alternate hostname/OS/device information we got from the
service scan (if it was performed) */
void printserviceinfooutput(Target *currenths);
void printserviceinfooutput(const Target *currenths);
#ifndef NOLUA
std::string protect_xml(const std::string s);
/* Use this function to report NSE_PRE_SCAN and NSE_POST_SCAN results */
void printscriptresults(ScriptResults *scriptResults, stype scantype);
void printscriptresults(const ScriptResults *scriptResults, stype scantype);
void printhostscriptresults(Target *currenths);
void printhostscriptresults(const Target *currenths);
#endif
/* Print a table with traceroute hops. */
void printtraceroute(Target *currenths);
void printtraceroute(const Target *currenths);
/* Print "times for host" output with latency. */
void printtimes(Target *currenths);
void printtimes(const Target *currenths);
/* Print a detailed list of Nmap interfaces and routes to
normal/skiddy/stdout output */

View File

@@ -142,7 +142,7 @@ void Port::getNmapServiceName(char *namebuf, int buflen) const {
if (service != NULL && service->name != NULL) {
service_name = service->name;
} else {
struct servent *service;
const struct servent *service;
service = nmap_getservbyport(portno, IPPROTO2STR(proto));
if (service != NULL)
@@ -260,7 +260,7 @@ void PortList::getServiceDeductions(u16 portno, int protocol, struct serviceDedu
port = lookupPort(portno, protocol);
if (port == NULL || port->service == NULL) {
struct servent *service;
const struct servent *service;
/* Look up the service name. */
*sd = serviceDeductions();
@@ -330,7 +330,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
/* PROBESTATE_FINISHED_NOMATCH, PROBESTATE_EXCLUDED, PROBESTATE_INCOMPLETE.
Just look up the service name if none is provided. */
if (sname == NULL) {
struct servent *service;
const struct servent *service;
service = nmap_getservbyport(portno, IPPROTO2STR(protocol));
if (service != NULL)
sname = service->s_name;
@@ -550,7 +550,7 @@ int PortList::getStateCounts(int state) const {
except that if you ask for both TCP, UDP & SCTP, every TCP port
will be returned before we start returning UDP and SCTP ports */
Port *PortList::nextPort(const Port *cur, Port *next,
int allowed_protocol, int allowed_state) {
int allowed_protocol, int allowed_state) const {
int proto;
int mapped_pno;
Port *port;
@@ -734,7 +734,7 @@ void PortList::initializePortMap(int protocol, u16 *ports, int portcount) {
most popular one. Returns the state if there is one, but returns
PORT_UNKNOWN if there are no (more) states which qualify for
consolidation */
int PortList::nextIgnoredState(int prevstate) {
int PortList::nextIgnoredState(int prevstate) const {
int beststate = PORT_UNKNOWN;
int count = 0;
@@ -775,7 +775,7 @@ int PortList::nextIgnoredState(int prevstate) {
/* Returns true if a state should be ignored (consolidated), false otherwise.
* 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) {
bool PortList::isIgnoredState(int state, int *count) const {
int tmp_count = 0;
if (o.debugging > 2)
@@ -813,7 +813,7 @@ bool PortList::isIgnoredState(int state, int *count) {
return false;
}
int PortList::numIgnoredStates() {
int PortList::numIgnoredStates() const {
int numstates = 0;
for(int state=0; state < PORT_HIGHEST_STATE; state++) {
if (isIgnoredState(state, NULL))
@@ -822,7 +822,7 @@ int PortList::numIgnoredStates() {
return numstates;
}
int PortList::numIgnoredPorts() {
int PortList::numIgnoredPorts() const {
int numports = 0;
int tmp = 0;

View File

@@ -203,7 +203,7 @@ class PortList {
except that if you ask for TCP, UDP & SCTP, all TCP ports will be
returned before we start returning UDP and finally SCTP ports */
Port *nextPort(const Port *cur, Port *next,
int allowed_protocol, int allowed_state);
int allowed_protocol, int allowed_state) const;
int setStateReason(u16 portno, u8 proto, reason_t reason, u8 ttl, const struct sockaddr_storage *ip_addr);
@@ -251,13 +251,13 @@ class PortList {
most popular one. Returns the state if there is one, but returns
PORT_UNKNOWN if there are no (more) states which qualify for
consolidation */
int nextIgnoredState(int prevstate);
int nextIgnoredState(int prevstate) const;
/* Returns true if a state should be ignored (consolidated), false otherwise */
bool isIgnoredState(int state, int *count);
bool isIgnoredState(int state, int *count) const;
int numIgnoredStates();
int numIgnoredPorts();
int numIgnoredStates() const;
int numIgnoredPorts() const;
int numPorts() const;
bool hasOpenPorts() const;

View File

@@ -346,7 +346,7 @@ static int update_state_summary(state_reason_summary_t *head, Port *port) {
/* Converts Port objects and their corresponding state_reason structures into
* state_reason_summary structures using update_state_summary */
static unsigned int get_state_summary(state_reason_summary_t *head, PortList *Ports, int state) {
static unsigned int get_state_summary(state_reason_summary_t *head, const PortList *Ports, int state) {
Port *current = NULL;
Port port;
state_reason_summary_t *reason;
@@ -367,7 +367,7 @@ static unsigned int get_state_summary(state_reason_summary_t *head, PortList *Po
}
/* parse and sort reason summary for main print_* functions */
state_reason_summary_t *get_state_reason_summary(PortList *Ports, int state) {
state_reason_summary_t *get_state_reason_summary(const PortList *Ports, int state) {
state_reason_summary_t *reason_head;
reason_head = (state_reason_summary_t *)safe_malloc(sizeof(state_reason_summary_t));
@@ -404,7 +404,7 @@ void state_reason_init(state_reason_t *reason) {
/* converts target into reason message for ping scans. Uses a static
* buffer so new values overwrite old values */
const char *target_reason_str(Target *t) {
const char *target_reason_str(const Target *t) {
static char reason[128];
memset(reason,'\0', 128);
Snprintf(reason, 128, "received %s", reason_str(t->reason.reason_id, SINGULAR));

View File

@@ -161,7 +161,7 @@ void state_reason_init(state_reason_t *reason);
const char *reason_str(reason_t reason_id, unsigned int number);
/* Returns a linked list of reasons why ports are in a given state */
state_reason_summary_t *get_state_reason_summary(PortList *Ports, int state);
state_reason_summary_t *get_state_reason_summary(const PortList *Ports, int state);
/* Frees the linked list from get_state_reason_summary */
void state_reason_summary_dinit(state_reason_summary_t *r);
@@ -169,7 +169,7 @@ void state_reason_summary_dinit(state_reason_summary_t *r);
* Uses static return value so previous values will be over
* written by subsequent calls */
const char *port_reason_str(state_reason_t r);
const char *target_reason_str(Target *t);
const char *target_reason_str(const Target *t);
#endif

View File

@@ -117,7 +117,7 @@ static int nmap_services_init() {
FILE *fp;
char servicename[128], proto[16];
u16 portno;
char *p;
const char *p;
char line[1024];
int lineno = 0;
int res;
@@ -300,8 +300,8 @@ int addportsfromservmask(const char *mask, u8 *porttbl, int range_type) {
struct servent *nmap_getservbyport(int port, const char *proto) {
std::map<port_spec, service_node>::iterator i;
const struct servent *nmap_getservbyport(int port, const char *proto) {
std::map<port_spec, service_node>::const_iterator i;
port_spec ps;
if (nmap_services_init() == -1)
@@ -373,7 +373,7 @@ void gettoppts(double level, const char *portlist, struct scan_lists * ports, co
struct scan_lists ptsdata = { 0 };
bool ptsdata_initialized = false;
const struct service_node *current;
std::list<service_node>::iterator i;
std::list<service_node>::const_iterator i;
if (!services_initialized && nmap_services_init() == -1)
fatal("%s: Couldn't get port numbers", __func__);

View File

@@ -75,7 +75,7 @@
#define SERVICE_TABLE_SIZE 1024
int addportsfromservmask(const char *mask, u8 *porttbl, int range_type);
struct servent *nmap_getservbyport(int port, const char *proto);
const struct servent *nmap_getservbyport(int port, const char *proto);
void gettoppts(double level, const char *portlist, struct scan_lists * ports, const char *exclude_list = NULL);
void free_services();

View File

@@ -240,7 +240,7 @@ public:
double completion_fraction() const;
private:
void child_parent_ttl(u8 ttl, Hop **child, Hop **parent);
void child_parent_ttl(u8 ttl, Hop **child, Hop **parent) const;
static u8 distance_guess(const Target *target);
static struct probespec get_probe(const Target *target);
};
@@ -492,7 +492,6 @@ void HostState::link_to(Hop *hop) {
}
double HostState::completion_fraction() const {
std::vector<bool>::iterator it;
unsigned int i, n;
if (this->is_finished())
@@ -507,7 +506,7 @@ double HostState::completion_fraction() const {
return (double) n / sent_ttls.size();
}
void HostState::child_parent_ttl(u8 ttl, Hop **child, Hop **parent) {
void HostState::child_parent_ttl(u8 ttl, Hop **child, Hop **parent) const {
*child = NULL;
*parent = this->hops;
while (*parent != NULL && (*parent)->ttl > ttl) {
@@ -832,7 +831,7 @@ Probe *Probe::make(HostState *host, struct probespec pspec, u8 ttl)
}
TracerouteState::TracerouteState(std::vector<Target *> &targets) {
std::vector<Target *>::iterator it;
std::vector<Target *>::const_iterator it;
struct sockaddr_storage srcaddr;
size_t sslen;
char pcap_filter[128];
@@ -898,7 +897,7 @@ void TracerouteState::next_active_host() {
}
void TracerouteState::send_new_probes() {
std::list<HostState *>::iterator failed_host;
std::list<HostState *>::const_iterator failed_host;
struct timeval now;
now = get_now();
@@ -1439,7 +1438,7 @@ void TracerouteState::transfer_hops() {
Probe *TracerouteState::lookup_probe(
const struct sockaddr_storage *target_addr, u16 token) {
std::list<HostState *>::iterator host_iter;
std::list<HostState *>::const_iterator host_iter;
std::list<Probe *>::iterator probe_iter;
for (host_iter = active_hosts.begin(); host_iter != active_hosts.end(); host_iter++) {