1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Use less memory for ip_addr in port_reason.

This was a sockaddr_storage, which is 128 bytes. This is a lot for a
structure that is part of Port. It is now a union of sockaddr_in and
sockaddr_in6, which is 28 bytes. A new set_ip_addr method sets the union
from a sockaddr_storage, where plain assignment was used before.

The sockaddr_storage was introduced in r23778, the first big IPv6 merge.
This commit is contained in:
david
2012-06-26 03:23:14 +00:00
parent cb38bebdce
commit 3e50107f23
6 changed files with 48 additions and 12 deletions

View File

@@ -1,5 +1,9 @@
# Nmap Changelog ($Id$); -*-text-*-
o Reduced the size of Port structures by about a third (from 176 to 64
bytes on x86_64). They had accidentally been made bigger than they
should during the IPv6 code merge. [David Fifield]
o [NSE] Added default values for Expires, Call-ID, Allow and Content-Length
headers in SIP requests and removed redundant code sip library.
[Hani Benhabiles]

View File

@@ -662,8 +662,11 @@ void printportoutput(Target *currenths, PortList *plist) {
xml_attribute("state", "%s", state);
xml_attribute("reason", "%s", reason_str(current->reason.reason_id, SINGULAR));
xml_attribute("reason_ttl", "%d", current->reason.ttl);
if (current->reason.ip_addr.ss_family != AF_UNSPEC)
xml_attribute("reason_ip", "%s", inet_ntop_ez(&current->reason.ip_addr, sizeof(current->reason.ip_addr)));
if (current->reason.ip_addr.sockaddr.sa_family != AF_UNSPEC) {
struct sockaddr_storage ss;
memcpy(&ss, &current->reason.ip_addr, sizeof(current->reason.ip_addr));
xml_attribute("reason_ip", "%s", inet_ntop_ez(&ss, sizeof(ss)));
}
xml_close_empty_tag();
if (proto && proto->p_name && *proto->p_name) {
@@ -774,8 +777,11 @@ void printportoutput(Target *currenths, PortList *plist) {
xml_attribute("state", "%s", state);
xml_attribute("reason", "%s", reason_str(current->reason.reason_id, SINGULAR));
xml_attribute("reason_ttl", "%d", current->reason.ttl);
if (current->reason.ip_addr.ss_family != AF_UNSPEC)
xml_attribute("reason_ip", "%s", inet_ntop_ez(&current->reason.ip_addr, sizeof(current->reason.ip_addr)));
if (current->reason.ip_addr.sockaddr.sa_family != AF_UNSPEC) {
struct sockaddr_storage ss;
memcpy(&ss, &current->reason.ip_addr, sizeof(current->reason.ip_addr));
xml_attribute("reason_ip", "%s", inet_ntop_ez(&ss, sizeof(ss)));
}
xml_close_empty_tag();
if (sd.name || sd.service_fp || sd.service_tunnel != SERVICE_TUNNEL_NONE)

View File

@@ -928,9 +928,9 @@ int PortList::setStateReason(u16 portno, u8 proto, reason_t reason, u8 ttl,
/* set new reason and increment its count */
answer->reason.reason_id = reason;
if (ip_addr == NULL)
answer->reason.ip_addr.ss_family = AF_UNSPEC;
answer->reason.ip_addr.sockaddr.sa_family = AF_UNSPEC;
else
answer->reason.ip_addr = *ip_addr;
answer->reason.set_ip_addr(ip_addr);
answer->reason.ttl = ttl;
return 0;
}

View File

@@ -108,6 +108,21 @@
extern NmapOps o;
class PortList;
/* Set the ip_addr union to the AF_INET or AF_INET6 value stored in *ss as
appropriate. Returns 0 on success or -1 if the address family of *ss is not
known. */
int port_reason::set_ip_addr(const struct sockaddr_storage *ss) {
if (ss->ss_family == AF_INET) {
this->ip_addr.in = *(struct sockaddr_in *) ss;
return 0;
} else if (ss->ss_family == AF_INET6) {
this->ip_addr.in6 = *(struct sockaddr_in6 *) ss;
return 0;
} else {
return -1;
}
}
/* reason_string initializer */
reason_string::reason_string(){
this->plural = "unknown";
@@ -430,7 +445,7 @@ const char *reason_str(reason_t reason_code, unsigned int number) {
void state_reason_init(state_reason_t *reason) {
reason->reason_id = ER_UNKNOWN;
reason->ip_addr.ss_family = AF_UNSPEC;
reason->ip_addr.sockaddr.sa_family = AF_UNSPEC;
reason->ttl = 0;
}
@@ -506,8 +521,13 @@ char *target_reason_str(Target *t) {
char *port_reason_str(state_reason_t r) {
static char reason[128];
memset(reason,'\0', 128);
Snprintf(reason, 128, "%s%s%s", reason_str(r.reason_id, SINGULAR),
(r.ip_addr.ss_family==AF_UNSPEC)?"":" from ",
(r.ip_addr.ss_family==AF_UNSPEC)?"":inet_ntop_ez(&r.ip_addr, sizeof(r.ip_addr)));
if (r.ip_addr.sockaddr.sa_family == AF_UNSPEC) {
Snprintf(reason, sizeof(reason), "%s", reason_str(r.reason_id, SINGULAR));
} else {
struct sockaddr_storage ss;
memcpy(&ss, &r.ip_addr, sizeof(r.ip_addr));
Snprintf(reason, sizeof(reason), "%s from %s", reason_str(r.reason_id, SINGULAR),
inet_ntop_ez(&ss, sizeof(ss)));
}
return reason;
}

View File

@@ -128,8 +128,14 @@ public:
* why a port is in a specific state */
typedef struct port_reason {
reason_t reason_id;
struct sockaddr_storage ip_addr;
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
struct sockaddr sockaddr;
} ip_addr;
unsigned short ttl;
int set_ip_addr(const struct sockaddr_storage *ss);
} state_reason_t;
/* used to calculate state reason summaries.

View File

@@ -5381,7 +5381,7 @@ static int get_ping_pcap_result(UltraScanInfo *USI, struct timeval *stime) {
hss->target->reason.reason_id = current_reason;
hss->target->reason.ttl = hdr.ttl;
if (sockaddr_storage_cmp(&hdr.src, &target_dst) != 0) {
hss->target->reason.ip_addr = hdr.src;
hss->target->reason.set_ip_addr(&hdr.src);
}
}
}