diff --git a/TargetGroup.cc b/TargetGroup.cc index 175025f31..52c62c92a 100644 --- a/TargetGroup.cc +++ b/TargetGroup.cc @@ -628,7 +628,7 @@ std::string NetBlockIPv6Netmask::str() const { return result.str(); } -NetBlock *NetBlockHostname::resolve() const { +NetBlock *NetBlockHostname::resolve() { struct addrinfo *addrs, *addr; std::list resolvedaddrs; NetBlock *netblock; diff --git a/TargetGroup.h b/TargetGroup.h index 098bd8e63..9200bab48 100644 --- a/TargetGroup.h +++ b/TargetGroup.h @@ -162,6 +162,11 @@ public: 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; @@ -203,7 +208,7 @@ public: int af; int bits; - NetBlock *resolve() const; + NetBlock *resolve(); bool next(struct sockaddr_storage *ss, size_t *sslen); void apply_netmask(int bits); diff --git a/targets.cc b/targets.cc index 4a3926d44..0dd9db2a7 100644 --- a/targets.cc +++ b/targets.cc @@ -383,16 +383,13 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { 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). */ - NetBlockHostname *netblock_hostname; - netblock_hostname = dynamic_cast(this->netblock); - if (netblock_hostname != NULL) { - this->netblock = netblock_hostname->resolve(); - if (this->netblock == NULL) { - error("Failed to resolve \"%s\".", netblock_hostname->hostname.c_str()); - delete netblock_hostname; - return -1; - } - delete netblock_hostname; + 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))