Skip to content

Commit

Permalink
Simplify get_next_host and name resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Aug 4, 2017
1 parent 0bc76de commit cde6853
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion TargetGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<struct sockaddr_storage> resolvedaddrs;
NetBlock *netblock;
Expand Down
7 changes: 6 additions & 1 deletion TargetGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ class NetBlock {

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;
Expand Down Expand Up @@ -203,7 +208,7 @@ class NetBlockHostname : public NetBlock {
int af;
int bits;

NetBlock *resolve() const;
NetBlock *resolve();

bool next(struct sockaddr_storage *ss, size_t *sslen);
void apply_netmask(int bits);
Expand Down
17 changes: 7 additions & 10 deletions targets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetBlockHostname *>(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))
Expand Down

0 comments on commit cde6853

Please sign in to comment.