1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-10 06:31:30 +00:00

Formatting in TargetGroup.{cc,h}.

astyle -a -p -H -s2 -m2 TargetGroup.cc TargetGroup.h
This commit is contained in:
david
2012-12-17 22:41:13 +00:00
parent bfead6f4b7
commit 55e210d589
2 changed files with 114 additions and 115 deletions

View File

@@ -118,14 +118,14 @@ void TargetGroup::Initialize() {
ipsleft = 0; ipsleft = 0;
} }
/* Initializes (or reinitializes) the object with a new expression, such /* Initializes (or reinitializes) the object with a new expression, such
as 192.168.0.0/16 , 10.1.0-5.1-254 , or fe80::202:e3ff:fe14:1102 . as 192.168.0.0/16 , 10.1.0-5.1-254 , or fe80::202:e3ff:fe14:1102 .
Returns 0 for success */ Returns 0 for success */
int TargetGroup::parse_expr(const char *target_expr, int af) { int TargetGroup::parse_expr(const char *target_expr, int af) {
int i=0,j=0,k=0; int i = 0, j = 0, k = 0;
int start, end; int start, end;
char *r,*s, *target_net; char *r, *s, *target_net;
char *addy[5]; char *addy[5];
char *hostexp = strdup(target_expr); char *hostexp = strdup(target_expr);
namedhost = 0; namedhost = 0;
@@ -160,13 +160,13 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
netmask_long = parse_long(s, (char**) &tail); netmask_long = parse_long(s, (char**) &tail);
if (*tail != '\0' || tail == s || netmask_long < 0 || netmask_long > 32) { if (*tail != '\0' || tail == s || netmask_long < 0 || netmask_long > 32) {
error("Illegal netmask value, must be /0 - /32 . Assuming /32 (one host)"); error("Illegal netmask value, must be /0 - /32 . Assuming /32 (one host)");
netmask = 32; netmask = 32;
} else } else
netmask = (u32) netmask_long; netmask = (u32) netmask_long;
} else } else
netmask = 32; netmask = 32;
resolvedname = hostexp; resolvedname = hostexp;
for(i=0; hostexp[i] != '\0'; i++) for (i = 0; hostexp[i]; i++)
if (isupper((int) (unsigned char) hostexp[i]) || if (isupper((int) (unsigned char) hostexp[i]) ||
islower((int) (unsigned char) hostexp[i])) { islower((int) (unsigned char) hostexp[i])) {
namedhost = 1; namedhost = 1;
@@ -205,8 +205,8 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
if (netmask) { if (netmask) {
struct sockaddr_in *sin = (struct sockaddr_in *) &ss; struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
unsigned long longtmp = ntohl(sin->sin_addr.s_addr); unsigned long longtmp = ntohl(sin->sin_addr.s_addr);
startaddr.s_addr = longtmp & (unsigned long) (0 - (1<<(32 - netmask))); startaddr.s_addr = longtmp & (unsigned long) (0 - (1 << (32 - netmask)));
endaddr.s_addr = longtmp | (unsigned long) ((1<<(32 - netmask)) - 1); endaddr.s_addr = longtmp | (unsigned long) ((1 << (32 - netmask)) - 1);
} else { } else {
/* The above calculations don't work for a /0 netmask, though at first /* The above calculations don't work for a /0 netmask, though at first
* glance it appears that they would * glance it appears that they would
@@ -223,17 +223,15 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
fprintf(stderr, "Host specification invalid"); fprintf(stderr, "Host specification invalid");
free(hostexp); free(hostexp);
return 1; return 1;
} } else {
else {
targets_type = IPV4_RANGES; targets_type = IPV4_RANGES;
i=0; i = 0;
while(*r) { while (*r) {
if (*r == '.' && ++i < 4) { if (*r == '.' && ++i < 4) {
*r = '\0'; *r = '\0';
addy[i] = r + 1; addy[i] = r + 1;
} } else if (*r != '*' && *r != ',' && *r != '-' && !isdigit((int) (unsigned char) *r)) {
else if (*r != '*' && *r != ',' && *r != '-' && !isdigit((int) (unsigned char) *r)) {
error("Invalid character in host specification: %s. Note in particular that square brackets [] are no longer allowed. They were redundant and can simply be removed.", target_expr); error("Invalid character in host specification: %s. Note in particular that square brackets [] are no longer allowed. They were redundant and can simply be removed.", target_expr);
return 1; return 1;
} }
@@ -244,24 +242,25 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
return 1; return 1;
} }
for(i=0; i < 4; i++) { for (i = 0; i < 4; i++) {
j=0; j = 0;
do { do {
s = strchr(addy[i],','); s = strchr(addy[i], ',');
if (s) *s = '\0'; if (s) *s = '\0';
if (*addy[i] == '*') { start = 0; end = 255; } if (*addy[i] == '*') {
else if (*addy[i] == '-') { start = 0;
end = 255;
} else if (*addy[i] == '-') {
start = 0; start = 0;
if (*(addy[i] + 1) == '\0') end = 255; if (*(addy[i] + 1) == '\0') end = 255;
else end = atoi(addy[i]+ 1); else end = atoi(addy[i] + 1);
} } else {
else {
start = end = atoi(addy[i]); start = end = atoi(addy[i]);
if ((r = strchr(addy[i],'-')) && *(r+1) ) end = atoi(r + 1); if ((r = strchr(addy[i], '-')) && *(r + 1) ) end = atoi(r + 1);
else if (r && !*(r+1)) end = 255; else if (r && !*(r + 1)) end = 255;
} }
/* if (o.debugging > 2) /* if (o.debugging > 2)
* log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end); */ * log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end); */
if (start < 0 || start > end || start > 255 || end > 255) { if (start < 0 || start > end || start > 255 || end > 255) {
error("Your host specifications are illegal!"); error("Your host specifications are illegal!");
return 1; return 1;
@@ -270,9 +269,9 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
error("Your host specifications are illegal!"); error("Your host specifications are illegal!");
return 1; return 1;
} }
for(k=start; k <= end; k++) for (k = start; k <= end; k++)
addresses[i][j++] = k; addresses[i][j++] = k;
last[i] = j-1; last[i] = j - 1;
if (s) addy[i] = s + 1; if (s) addy[i] = s + 1;
} while (s); } while (s);
} }
@@ -282,8 +281,7 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
(unsigned long long) (last[1] + 1) * (unsigned long long) (last[1] + 1) *
(unsigned long long) (last[2] + 1) * (unsigned long long) (last[2] + 1) *
(unsigned long long) (last[3] + 1); (unsigned long long) (last[3] + 1);
} } else {
else {
#if HAVE_IPV6 #if HAVE_IPV6
struct addrinfo *addrs, *addr; struct addrinfo *addrs, *addr;
struct sockaddr_storage ss; struct sockaddr_storage ss;
@@ -342,8 +340,8 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
* returns: number of hosts skipped */ * returns: number of hosts skipped */
int TargetGroup::skip_range(_octet_nums octet) { int TargetGroup::skip_range(_octet_nums octet) {
unsigned long hosts_skipped = 0, /* number of hosts skipped */ unsigned long hosts_skipped = 0, /* number of hosts skipped */
oct = 0; /* octect number */ oct = 0; /* octect number */
int i = 0; /* simple lcv */ int i = 0; /* simple lcv */
/* This function is only supported for RANGES! */ /* This function is only supported for RANGES! */
if (targets_type != IPV4_RANGES) if (targets_type != IPV4_RANGES)
@@ -359,37 +357,36 @@ int TargetGroup::skip_range(_octet_nums octet) {
return_last_host(); return_last_host();
switch (octet) { switch (octet) {
case FIRST_OCTET: case FIRST_OCTET:
oct = 0; oct = 0;
hosts_skipped = (last[1] + 1) * (last[2] + 1) * (last[3] + 1); hosts_skipped = (last[1] + 1) * (last[2] + 1) * (last[3] + 1);
break; break;
case SECOND_OCTET: case SECOND_OCTET:
oct = 1; oct = 1;
hosts_skipped = (last[2] + 1) * (last[3] + 1); hosts_skipped = (last[2] + 1) * (last[3] + 1);
break; break;
case THIRD_OCTET: case THIRD_OCTET:
oct = 2; oct = 2;
hosts_skipped = (last[3] + 1); hosts_skipped = (last[3] + 1);
break; break;
default: /* Hmm, how'd you do that */ default: /* Hmm, how'd you do that */
return -1; return -1;
} }
/* catch if we try to take more than are left */ /* catch if we try to take more than are left */
assert(ipsleft + 1>= hosts_skipped); assert(ipsleft + 1 >= hosts_skipped);
/* increment the next octect that we can above us */ /* increment the next octect that we can above us */
for (i = oct; i >= 0; i--) { for (i = oct; i >= 0; i--) {
if (current[i] < last[i]) { if (current[i] < last[i]) {
current[i]++; current[i]++;
break; break;
} } else
else
current[i] = 0; current[i] = 0;
} }
/* reset all the ones below us to zero */ /* reset all the ones below us to zero */
for (i = oct+1; i <= 3; i++) { for (i = oct + 1; i <= 3; i++) {
current[i] = 0; current[i] = 0;
} }
@@ -413,16 +410,16 @@ static int get_scope_id(const char *devname) {
return 0; return 0;
} }
/* Grab the next host from this expression (if any) and updates its internal /* Grab the next host from this expression (if any) and updates its internal
state to reflect that the IP was given out. Returns 0 and state to reflect that the IP was given out. Returns 0 and
fills in ss if successful. ss must point to a pre-allocated fills in ss if successful. ss must point to a pre-allocated
sockaddr_storage structure */ sockaddr_storage structure */
int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
int octet; int octet;
struct sockaddr_in *sin = (struct sockaddr_in *) ss; struct sockaddr_in *sin = (struct sockaddr_in *) ss;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss;
startover: /* to handle nmap --resume where I have already startover: /* to handle nmap --resume where I have already
* scanned many of the IPs */ * scanned many of the IPs */
assert(ss); assert(ss);
assert(sslen); assert(sslen);
@@ -446,8 +443,7 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
ipsleft = 0; ipsleft = 0;
return -1; return -1;
} }
} } else if (targets_type == IPV4_RANGES) {
else if (targets_type == IPV4_RANGES) {
memset(sin, 0, sizeof(struct sockaddr_in)); memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family = AF_INET; sin->sin_family = AF_INET;
*sslen = sizeof(struct sockaddr_in); *sslen = sizeof(struct sockaddr_in);
@@ -455,7 +451,7 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
sin->sin_len = *sslen; sin->sin_len = *sslen;
#endif #endif
if (o.debugging > 2) { if (o.debugging > 2) {
log_write(LOG_STDOUT, "doing %d.%d.%d.%d = %d.%d.%d.%d\n", current[0], current[1], current[2], current[3], addresses[0][current[0]],addresses[1][current[1]],addresses[2][current[2]],addresses[3][current[3]]); log_write(LOG_STDOUT, "doing %d.%d.%d.%d = %d.%d.%d.%d\n", current[0], current[1], current[2], current[3], addresses[0][current[0]], addresses[1][current[1]], addresses[2][current[2]], addresses[3][current[3]]);
} }
/* Set the IP to the current value of everything */ /* Set the IP to the current value of everything */
sin->sin_addr.s_addr = htonl(addresses[0][current[0]] << 24 | sin->sin_addr.s_addr = htonl(addresses[0][current[0]] << 24 |
@@ -464,7 +460,7 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
addresses[3][current[3]]); addresses[3][current[3]]);
/* Now we nudge up to the next IP */ /* Now we nudge up to the next IP */
for(octet = 3; octet >= 0; octet--) { for (octet = 3; octet >= 0; octet--) {
if (current[octet] < last[octet]) { if (current[octet] < last[octet]) {
/* OK, this is the column I have room to nudge upwards */ /* OK, this is the column I have room to nudge upwards */
current[octet]++; current[octet]++;
@@ -479,8 +475,10 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
assert(ipsleft == 1); assert(ipsleft == 1);
/* So I set current to last with the very final octet up one ... */ /* So I set current to last with the very final octet up one ... */
/* Note that this may make current[3] == 256 */ /* Note that this may make current[3] == 256 */
current[0] = last[0]; current[1] = last[1]; current[0] = last[0];
current[2] = last[2]; current[3] = last[3] + 1; current[1] = last[1];
current[2] = last[2];
current[3] = last[3] + 1;
} else { } else {
assert(ipsleft > 1); /* There must be at least one more IP left */ assert(ipsleft > 1); /* There must be at least one more IP left */
} }
@@ -528,7 +526,7 @@ int TargetGroup::return_last_host() {
assert(currentaddr.s_addr > startaddr.s_addr); assert(currentaddr.s_addr > startaddr.s_addr);
currentaddr.s_addr--; currentaddr.s_addr--;
} else if (targets_type == IPV4_RANGES) { } else if (targets_type == IPV4_RANGES) {
for(octet = 3; octet >= 0; octet--) { for (octet = 3; octet >= 0; octet--) {
if (current[octet] > 0) { if (current[octet] > 0) {
/* OK, this is the column I have room to nudge downwards */ /* OK, this is the column I have room to nudge downwards */
current[octet]--; current[octet]--;
@@ -549,8 +547,7 @@ int TargetGroup::return_last_host() {
/* Returns true iff the given address is the one that was resolved to create /* Returns true iff the given address is the one that was resolved to create
this target group; i.e., not one of the addresses derived from it with a this target group; i.e., not one of the addresses derived from it with a
netmask. */ netmask. */
bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss) bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss) {
{
struct sockaddr_storage resolvedaddr; struct sockaddr_storage resolvedaddr;
if (resolvedaddrs.empty()) if (resolvedaddrs.empty())
@@ -565,15 +562,13 @@ bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss)
} }
/* Return a string of the name or address that was resolved for this group. */ /* Return a string of the name or address that was resolved for this group. */
const char *TargetGroup::get_resolved_name(void) const char *TargetGroup::get_resolved_name(void) {
{
return resolvedname.c_str(); return resolvedname.c_str();
} }
/* Return the list of addresses that the name for this group resolved to, if /* Return the list of addresses that the name for this group resolved to, if
it came from a name resolution. */ it came from a name resolution. */
const std::list<struct sockaddr_storage> &TargetGroup::get_resolved_addrs(void) const std::list<struct sockaddr_storage> &TargetGroup::get_resolved_addrs(void) {
{
return resolvedaddrs; return resolvedaddrs;
} }
@@ -591,7 +586,7 @@ NewTargets::NewTargets (void) {
void NewTargets::Initialize (void) { void NewTargets::Initialize (void) {
history.clear(); history.clear();
while(!queue.empty()) while (!queue.empty())
queue.pop(); queue.pop();
} }
@@ -611,12 +606,10 @@ unsigned long NewTargets::push (const char *target) {
queue.push(tg); queue.push(tg);
if (o.debugging > 2) if (o.debugging > 2)
log_write(LOG_PLAIN, "New Targets: target %s pushed onto the queue.\n", log_write(LOG_PLAIN, "New Targets: target %s pushed onto the queue.\n", tg.c_str());
tg.c_str());
} else { } else {
if (o.debugging > 2) if (o.debugging > 2)
log_write(LOG_PLAIN, "New Targets: target %s is already in the queue.\n", log_write(LOG_PLAIN, "New Targets: target %s is already in the queue.\n", tg.c_str());
tg.c_str());
/* Return 1 when the target is already in the history cache, /* Return 1 when the target is already in the history cache,
* this will prevent returning 0 when the target queue is * this will prevent returning 0 when the target queue is
* empty since no target was added. */ * empty since no target was added. */

View File

@@ -105,17 +105,17 @@
#include "nmap.h" #include "nmap.h"
class TargetGroup { class TargetGroup {
public: public:
/* used by get_target_types */ /* used by get_target_types */
enum _targets_types { TYPE_NONE, IPV4_NETMASK, IPV4_RANGES, IPV6_ADDRESS }; enum _targets_types { TYPE_NONE, IPV4_NETMASK, IPV4_RANGES, IPV6_ADDRESS };
/* used as input to skip range */ /* used as input to skip range */
enum _octet_nums { FIRST_OCTET, SECOND_OCTET, THIRD_OCTET }; enum _octet_nums { FIRST_OCTET, SECOND_OCTET, THIRD_OCTET };
TargetGroup(); TargetGroup();
/* Initializes (or reinitializes) the object with a new expression, /* Initializes (or reinitializes) the object with a new expression,
such as 192.168.0.0/16 , 10.1.0-5.1-254 , or such as 192.168.0.0/16 , 10.1.0-5.1-254 , or
fe80::202:e3ff:fe14:1102 . The af parameter is AF_INET or fe80::202:e3ff:fe14:1102 . The af parameter is AF_INET or
AF_INET6 Returns 0 for success */ AF_INET6 Returns 0 for success */
int parse_expr(const char *target_expr, int af); int parse_expr(const char *target_expr, int af);
/* Grab the next host from this expression (if any). Returns 0 and /* Grab the next host from this expression (if any). Returns 0 and
fills in ss if successful. ss must point to a pre-allocated fills in ss if successful. ss must point to a pre-allocated
@@ -136,14 +136,20 @@ class TargetGroup {
it came from a name resolution. */ it came from a name resolution. */
const std::list<struct sockaddr_storage> &get_resolved_addrs(void); const std::list<struct sockaddr_storage> &get_resolved_addrs(void);
/* return the target type */ /* return the target type */
char get_targets_type() {return targets_type;}; char get_targets_type() {
return targets_type;
};
/* get the netmask */ /* get the netmask */
int get_mask() {return netmask;}; int get_mask() {
return netmask;
};
/* is the current expression a named host */ /* is the current expression a named host */
int get_namedhost() {return namedhost;}; int get_namedhost() {
return namedhost;
};
/* Skip an octet in the range array */ /* Skip an octet in the range array */
int skip_range(_octet_nums octet); int skip_range(_octet_nums octet);
private: private:
enum _targets_types targets_type; enum _targets_types targets_type;
void Initialize(); void Initialize();
@@ -166,17 +172,17 @@ class TargetGroup {
unsigned int current[4]; unsigned int current[4];
u8 last[4]; u8 last[4];
/* Number of IPs left in this structure -- set to 0 if /* Number of IPs left in this structure -- set to 0 if the fields are not
the fields are not valid */ valid */
unsigned long long ipsleft; unsigned long long ipsleft;
// is the current target expression a named host /* is the current target expression a named host? */
int namedhost; int namedhost;
}; };
/* Adding new targets is for NSE scripts */ /* Adding new targets is for NSE scripts */
class NewTargets { class NewTargets {
public: public:
NewTargets(); NewTargets();
/* return a previous inserted target */ /* return a previous inserted target */
@@ -199,7 +205,7 @@ class NewTargets {
/* insert targets to the new_targets_queue */ /* insert targets to the new_targets_queue */
static unsigned long insert (const char *target); static unsigned long insert (const char *target);
private: private:
/* unsigned long mex_new_targets; */ /* unsigned long mex_new_targets; */
/* A queue to push new targets that were discovered by NSE scripts. /* A queue to push new targets that were discovered by NSE scripts.
@@ -214,14 +220,14 @@ class NewTargets {
/* Save new targets onto the queue */ /* Save new targets onto the queue */
unsigned long push (const char *target); unsigned long push (const char *target);
protected: protected:
static NewTargets *new_targets; static NewTargets *new_targets;
}; };
class HostGroupState { class HostGroupState {
public: public:
HostGroupState(int lookahead, int randomize, char *target_expressions[], HostGroupState(int lookahead, int randomize, char *target_expressions[],
int num_expressions); int num_expressions);
~HostGroupState(); ~HostGroupState();
Target **hostbatch; Target **hostbatch;
int max_batch_sz; /* The size of the hostbatch[] array */ int max_batch_sz; /* The size of the hostbatch[] array */