Skip to content

Commit

Permalink
Move TargetGroup class to TargetGroup files; make NetBlock class private
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Aug 6, 2017
1 parent 675ae46 commit 817dd2a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 124 deletions.
96 changes: 96 additions & 0 deletions TargetGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
#include "nmap.h"
#include "libnetutil/netutil.h"

#include <string>
#include <sstream>
#include <errno.h>
#include <limits.h> // CHAR_BIT
Expand All @@ -156,6 +157,29 @@ typedef bitvector_t octet_bitvector[(256 - 1) / (sizeof(unsigned long) * CHAR_BI

extern NmapOps o;

class NetBlock {
public:
virtual ~NetBlock() {}
std::string hostname;
std::list<struct sockaddr_storage> resolvedaddrs;

/* Parses an expression such as 192.168.0.0/16, 10.1.0-5.1-254, or
fe80::202:e3ff:fe14:1102/112 and returns a newly allocated NetBlock. The af
parameter is AF_INET or AF_INET6. Returns NULL in case of error. */
static NetBlock *parse_expr(const char *target_expr, int af);

bool is_resolved_address(const struct sockaddr_storage *ss) const;

/* For NetBlock subclasses that need to "resolve" themselves into a different
* NetBlock subclass, override this method. Otherwise, it's safe to reassign
* the return value to the pointer that this method was called through.
* On error, return NULL. */
virtual NetBlock *resolve() { return this; }
virtual bool next(struct sockaddr_storage *ss, size_t *sslen) = 0;
virtual void apply_netmask(int bits) = 0;
virtual std::string str() const = 0;
};

class NetBlockIPv4Ranges : public NetBlock {
public:
octet_bitvector octets[4];
Expand Down Expand Up @@ -775,3 +799,75 @@ std::string NetBlockHostname::str() const {

return result.str();
}

TargetGroup::~TargetGroup() {
if (this->netblock != NULL)
delete this->netblock;
}

/* 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 .
Returns 0 for success */
int TargetGroup::parse_expr(const char *target_expr, int af) {
if (this->netblock != NULL)
delete this->netblock;
this->netblock = NetBlock::parse_expr(target_expr, af);
if (this->netblock != NULL)
return 0;
else
return 1;
}

/* 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
fills in ss if successful. ss must point to a pre-allocated
sockaddr_storage structure */
int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
if (this->netblock == NULL)
return -1;

/* If all we have at this point is a hostname and netmask, resolve into
something where we know the address. If we ever have to use strictly the
hostname, without doing local DNS resolution (like with a proxy scan), this
has to be made conditional (and perhaps an error if the netmask doesn't
limit it to exactly one address). */
NetBlock *netblock_resolved = this->netblock->resolve();
if (netblock_resolved != NULL) {
this->netblock = netblock_resolved;
}
else {
error("Failed to resolve \"%s\".", this->netblock->hostname.c_str());
return -1;
}

if (this->netblock->next(ss, sslen))
return 0;
else
return -1;
}

/* 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
netmask. */
bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss) const {
return this->netblock->is_resolved_address(ss);
}

/* Return a string of the name or address that was resolved for this group. */
const char *TargetGroup::get_resolved_name(void) const {
if (this->netblock->hostname.empty())
return NULL;
else
return this->netblock->hostname.c_str();
}

/* Return the list of addresses that the name for this group resolved to, if
it came from a name resolution. */
const std::list<struct sockaddr_storage> &TargetGroup::get_resolved_addrs(void) const {
return this->netblock->resolvedaddrs;
}

/* is the current expression a named host */
int TargetGroup::get_namedhost() const {
return this->get_resolved_name() != NULL;
}
46 changes: 28 additions & 18 deletions TargetGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,39 @@
#define TARGETGROUP_H

#include <list>
#include <string>

class NetBlock {
class NetBlock;

class TargetGroup {
public:
virtual ~NetBlock() {}
std::string hostname;
std::list<struct sockaddr_storage> resolvedaddrs;
NetBlock *netblock;

/* Parses an expression such as 192.168.0.0/16, 10.1.0-5.1-254, or
fe80::202:e3ff:fe14:1102/112 and returns a newly allocated NetBlock. The af
parameter is AF_INET or AF_INET6. Returns NULL in case of error. */
static NetBlock *parse_expr(const char *target_expr, int af);
TargetGroup() {
this->netblock = NULL;
}

bool is_resolved_address(const struct sockaddr_storage *ss) const;
~TargetGroup();

/* For NetBlock subclasses that need to "resolve" themselves into a different
* NetBlock subclass, override this method. Otherwise, it's safe to reassign
* the return value to the pointer that this method was called through.
* On error, return NULL. */
virtual NetBlock *resolve() { return this; }
virtual bool next(struct sockaddr_storage *ss, size_t *sslen) = 0;
virtual void apply_netmask(int bits) = 0;
virtual std::string str() const = 0;
/* 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 . The af parameter is AF_INET or
AF_INET6 Returns 0 for success */
int parse_expr(const char *target_expr, int af);
/* Grab the next host from this expression (if any). Returns 0 and
fills in ss if successful. ss must point to a pre-allocated
sockaddr_storage structure */
int get_next_host(struct sockaddr_storage *ss, size_t *sslen);
/* 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
netmask. */
bool is_resolved_address(const struct sockaddr_storage *ss) const;
/* Return a string of the name or address that was resolved for this group. */
const char *get_resolved_name(void) const;
/* Return the list of addresses that the name for this group resolved to, if
it came from a name resolution. */
const std::list<struct sockaddr_storage> &get_resolved_addrs(void) const;
/* is the current expression a named host */
int get_namedhost() const;
};

#endif /* TARGETGROUP_H */
Expand Down
73 changes: 0 additions & 73 deletions targets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
#include "timing.h"
#include "tcpip.h"
#include "NmapOps.h"
#include "TargetGroup.h"
#include "NewTargets.h"
#include "Target.h"
#include "scan_engine.h"
Expand Down Expand Up @@ -351,78 +350,6 @@ bool target_needs_new_hostgroup(Target **targets, int targets_sz, const Target *
return false;
}

TargetGroup::~TargetGroup() {
if (this->netblock != NULL)
delete this->netblock;
}

/* 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 .
Returns 0 for success */
int TargetGroup::parse_expr(const char *target_expr, int af) {
if (this->netblock != NULL)
delete this->netblock;
this->netblock = NetBlock::parse_expr(target_expr, af);
if (this->netblock != NULL)
return 0;
else
return 1;
}

/* 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
fills in ss if successful. ss must point to a pre-allocated
sockaddr_storage structure */
int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
if (this->netblock == NULL)
return -1;

/* If all we have at this point is a hostname and netmask, resolve into
something where we know the address. If we ever have to use strictly the
hostname, without doing local DNS resolution (like with a proxy scan), this
has to be made conditional (and perhaps an error if the netmask doesn't
limit it to exactly one address). */
NetBlock *netblock_resolved = this->netblock->resolve();
if (netblock_resolved != NULL) {
this->netblock = netblock_resolved;
}
else {
error("Failed to resolve \"%s\".", this->netblock->hostname.c_str());
return -1;
}

if (this->netblock->next(ss, sslen))
return 0;
else
return -1;
}

/* 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
netmask. */
bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss) const {
return this->netblock->is_resolved_address(ss);
}

/* Return a string of the name or address that was resolved for this group. */
const char *TargetGroup::get_resolved_name(void) const {
if (this->netblock->hostname.empty())
return NULL;
else
return this->netblock->hostname.c_str();
}

/* Return the list of addresses that the name for this group resolved to, if
it came from a name resolution. */
const std::list<struct sockaddr_storage> &TargetGroup::get_resolved_addrs(void) const {
return this->netblock->resolvedaddrs;
}

/* is the current expression a named host */
int TargetGroup::get_namedhost() const {
return this->get_resolved_name() != NULL;
}

/* Lookahead is the number of hosts that can be
checked (such as ping scanned) in advance. Randomize causes each
group of up to lookahead hosts to be internally shuffled around.
Expand Down
34 changes: 1 addition & 33 deletions targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,41 +134,9 @@
#define TARGETS_H

#include <list>
class NetBlock;
#include "TargetGroup.h"
class Target;

class TargetGroup {
public:
NetBlock *netblock;

TargetGroup() {
this->netblock = NULL;
}

~TargetGroup();

/* 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 . The af parameter is AF_INET or
AF_INET6 Returns 0 for success */
int parse_expr(const char *target_expr, int af);
/* Grab the next host from this expression (if any). Returns 0 and
fills in ss if successful. ss must point to a pre-allocated
sockaddr_storage structure */
int get_next_host(struct sockaddr_storage *ss, size_t *sslen);
/* 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
netmask. */
bool is_resolved_address(const struct sockaddr_storage *ss) const;
/* Return a string of the name or address that was resolved for this group. */
const char *get_resolved_name(void) const;
/* Return the list of addresses that the name for this group resolved to, if
it came from a name resolution. */
const std::list<struct sockaddr_storage> &get_resolved_addrs(void) const;
/* is the current expression a named host */
int get_namedhost() const;
};

class HostGroupState {
public:
/* The maximum number of entries we want to allow storing in defer_buffer. */
Expand Down

0 comments on commit 817dd2a

Please sign in to comment.