1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-29 01:29:22 +00:00

Move random address generation to TargetGroup/NetBlock

This commit is contained in:
dmiller
2024-05-01 22:19:29 +00:00
parent 019557382b
commit b940270375
8 changed files with 53 additions and 18 deletions

View File

@@ -116,6 +116,18 @@ public:
virtual std::string str() const = 0;
};
class NetBlockRandomIPv4 : public NetBlock {
public:
NetBlockRandomIPv4();
bool next(struct sockaddr_storage *ss, size_t *sslen);
void apply_netmask(int bits) {}
std::string str() const {return "Random IPv4 addresses";}
private:
struct sockaddr_in base;
};
class NetBlockIPv4Ranges : public NetBlock {
public:
octet_bitvector octets[4];
@@ -325,6 +337,20 @@ bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
return false;
}
NetBlockRandomIPv4::NetBlockRandomIPv4() {
memset(&base, 0, sizeof(base));
base.sin_family = AF_INET;
}
bool NetBlockRandomIPv4::next(struct sockaddr_storage *ss, size_t *sslen) {
do {
base.sin_addr.s_addr = get_random_unique_u32();
} while (ip_is_reserved(&base.sin_addr));
memcpy(ss, &base, sizeof(base));
*sslen = sizeof(base);
return true;
}
NetBlockIPv4Ranges::NetBlockIPv4Ranges() {
unsigned int i;
@@ -777,6 +803,11 @@ int TargetGroup::parse_expr(const char *target_expr, int af) {
return 1;
}
void TargetGroup::generate_random_ips() {
assert(this->netblock == NULL);
this->netblock = new NetBlockRandomIPv4();
}
/* 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