1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 14:11:29 +00:00

Move TargetGroup class to TargetGroup files; make NetBlock class private

This commit is contained in:
dmiller
2017-08-06 03:20:40 +00:00
parent 675ae460b3
commit 817dd2a944
4 changed files with 125 additions and 124 deletions

View File

@@ -139,6 +139,7 @@
#include "nmap.h" #include "nmap.h"
#include "libnetutil/netutil.h" #include "libnetutil/netutil.h"
#include <string>
#include <sstream> #include <sstream>
#include <errno.h> #include <errno.h>
#include <limits.h> // CHAR_BIT #include <limits.h> // CHAR_BIT
@@ -156,6 +157,29 @@ typedef bitvector_t octet_bitvector[(256 - 1) / (sizeof(unsigned long) * CHAR_BI
extern NmapOps o; 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 { class NetBlockIPv4Ranges : public NetBlock {
public: public:
octet_bitvector octets[4]; octet_bitvector octets[4];
@@ -775,3 +799,75 @@ std::string NetBlockHostname::str() const {
return result.str(); 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;
}

View File

@@ -136,29 +136,39 @@
#define TARGETGROUP_H #define TARGETGROUP_H
#include <list> #include <list>
#include <string>
class NetBlock { class NetBlock;
class TargetGroup {
public: public:
virtual ~NetBlock() {} NetBlock *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 TargetGroup() {
fe80::202:e3ff:fe14:1102/112 and returns a newly allocated NetBlock. The af this->netblock = NULL;
parameter is AF_INET or AF_INET6. Returns NULL in case of error. */ }
static NetBlock *parse_expr(const char *target_expr, int af);
~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; bool is_resolved_address(const struct sockaddr_storage *ss) const;
/* Return a string of the name or address that was resolved for this group. */
/* For NetBlock subclasses that need to "resolve" themselves into a different const char *get_resolved_name(void) const;
* NetBlock subclass, override this method. Otherwise, it's safe to reassign /* Return the list of addresses that the name for this group resolved to, if
* the return value to the pointer that this method was called through. it came from a name resolution. */
* On error, return NULL. */ const std::list<struct sockaddr_storage> &get_resolved_addrs(void) const;
virtual NetBlock *resolve() { return this; } /* is the current expression a named host */
virtual bool next(struct sockaddr_storage *ss, size_t *sslen) = 0; int get_namedhost() const;
virtual void apply_netmask(int bits) = 0;
virtual std::string str() const = 0;
}; };
#endif /* TARGETGROUP_H */ #endif /* TARGETGROUP_H */

View File

@@ -137,7 +137,6 @@
#include "timing.h" #include "timing.h"
#include "tcpip.h" #include "tcpip.h"
#include "NmapOps.h" #include "NmapOps.h"
#include "TargetGroup.h"
#include "NewTargets.h" #include "NewTargets.h"
#include "Target.h" #include "Target.h"
#include "scan_engine.h" #include "scan_engine.h"
@@ -351,78 +350,6 @@ bool target_needs_new_hostgroup(Target **targets, int targets_sz, const Target *
return false; 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 /* Lookahead is the number of hosts that can be
checked (such as ping scanned) in advance. Randomize causes each checked (such as ping scanned) in advance. Randomize causes each
group of up to lookahead hosts to be internally shuffled around. group of up to lookahead hosts to be internally shuffled around.

View File

@@ -134,41 +134,9 @@
#define TARGETS_H #define TARGETS_H
#include <list> #include <list>
class NetBlock; #include "TargetGroup.h"
class Target; 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 { class HostGroupState {
public: public:
/* The maximum number of entries we want to allow storing in defer_buffer. */ /* The maximum number of entries we want to allow storing in defer_buffer. */