diff --git a/Target.h b/Target.h index ce871ce7c..bfa34b155 100644 --- a/Target.h +++ b/Target.h @@ -329,9 +329,8 @@ class Target { std::list traceroute_hops; /* If the address for this target came from a DNS lookup, the list of - resultant addresses (sometimes there are more than one). The address - actually used is always the first element in this list. */ - std::list resolved_addrs; + resultant addresses (sometimes there are more than one) that were not scanned. */ + std::list unscanned_addrs; #ifndef NOLUA ScriptResults scriptResults; diff --git a/TargetGroup.cc b/TargetGroup.cc index 8a24a9861..532e5dbf4 100644 --- a/TargetGroup.cc +++ b/TargetGroup.cc @@ -162,6 +162,7 @@ public: virtual ~NetBlock() {} std::string hostname; std::list resolvedaddrs; + std::list unscanned_addrs; /* 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 @@ -380,20 +381,13 @@ bail: return NULL; } -/* Returns the first address which matches the address family af */ -static const struct sockaddr_storage *first_af_address(const std::list *addrs, int af) { - for (std::list::const_iterator it = addrs->begin(), end = addrs->end(); it != end; ++it) { - if (it->ss_family == af) { - return &*it; +bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const { + for (std::list::const_iterator it = this->resolvedaddrs.begin(), end = this->resolvedaddrs.end(); it != end; ++it) { + if (sockaddr_storage_equal(&*it, ss)) { + return true; } } - return NULL; -} - -bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const { - if (this->resolvedaddrs.empty()) - return false; - return sockaddr_storage_equal(first_af_address(&this->resolvedaddrs, ss->ss_family), ss); + return false; } NetBlockIPv4Ranges::NetBlockIPv4Ranges() { @@ -721,6 +715,7 @@ std::string NetBlockIPv6Netmask::str() const { NetBlock *NetBlockHostname::resolve() { struct addrinfo *addrs, *addr; std::list resolvedaddrs; + std::list unscanned_addrs; NetBlock *netblock; const struct sockaddr_storage *sp = NULL; struct sockaddr_storage ss; @@ -730,17 +725,22 @@ NetBlock *NetBlockHostname::resolve() { for (addr = addrs; addr != NULL; addr = addr->ai_next) { if (addr->ai_addrlen < sizeof(ss)) { memcpy(&ss, addr->ai_addr, addr->ai_addrlen); - resolvedaddrs.push_back(ss); + if (sp == NULL && addr->ai_family == this->af) { + resolvedaddrs.push_back(ss); + sp = &resolvedaddrs.back(); + } + else { + unscanned_addrs.push_back(ss); + } } } if (addrs != NULL) freeaddrinfo(addrs); - if (resolvedaddrs.empty()) + if (resolvedaddrs.empty() && unscanned_addrs.empty()) return NULL; - sp = first_af_address(&resolvedaddrs, this->af); - if (sp == NULL || sp->ss_family != this->af) { + if (sp == NULL) { switch (this->af) { case AF_INET: error("Warning: Hostname %s resolves, but not to any IPv4 address. Try scanning with -6", this->hostname.c_str()); @@ -757,9 +757,9 @@ NetBlock *NetBlockHostname::resolve() { ss = *sp; sslen = sizeof(ss); - if (resolvedaddrs.size() > 1 && o.verbose > 1) { + if (!unscanned_addrs.empty() > 1 && o.verbose > 1) { error("Warning: Hostname %s resolves to %lu IPs. Using %s.", this->hostname.c_str(), - (unsigned long) resolvedaddrs.size(), inet_ntop_ez(&ss, sslen)); + (unsigned long) unscanned_addrs.size() + resolvedaddrs.size(), inet_ntop_ez(&ss, sslen)); } netblock = NULL; @@ -782,6 +782,7 @@ NetBlock *NetBlockHostname::resolve() { netblock->hostname = this->hostname; netblock->resolvedaddrs = resolvedaddrs; + netblock->unscanned_addrs = unscanned_addrs; netblock->apply_netmask(this->bits); return netblock; @@ -873,10 +874,10 @@ const char *TargetGroup::get_resolved_name(void) const { 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 &TargetGroup::get_resolved_addrs(void) const { - return this->netblock->resolvedaddrs; +/* Return the list of addresses that the name for this group resolved to, but + which were not scanned, if it came from a name resolution. */ +const std::list &TargetGroup::get_unscanned_addrs(void) const { + return this->netblock->unscanned_addrs; } /* is the current expression a named host */ diff --git a/TargetGroup.h b/TargetGroup.h index acd3235ef..4d7ab98f5 100644 --- a/TargetGroup.h +++ b/TargetGroup.h @@ -164,9 +164,9 @@ public: 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 &get_resolved_addrs(void) const; + /* Return the list of addresses that the name for this group resolved to, but + which were not scanned, if it came from a name resolution. */ + const std::list &get_unscanned_addrs(void) const; /* is the current expression a named host */ int get_namedhost() const; }; diff --git a/output.cc b/output.cc index a689ec83c..70303f3c0 100644 --- a/output.cc +++ b/output.cc @@ -1443,17 +1443,14 @@ void write_host_header(Target *currenths) { } write_host_status(currenths); if (currenths->TargetName() != NULL - && currenths->resolved_addrs.size() > 1) { - const struct sockaddr_storage *hs_ss = currenths->TargetSockAddr(); + && !currenths->unscanned_addrs.empty()) { log_write(LOG_PLAIN, "Other addresses for %s (not scanned):", currenths->TargetName()); - for (std::list::const_iterator it = currenths->resolved_addrs.begin(), end = currenths->resolved_addrs.end(); + for (std::list::const_iterator it = currenths->unscanned_addrs.begin(), end = currenths->unscanned_addrs.end(); it != end; it++) { struct sockaddr_storage ss = *it; - if (!sockaddr_storage_equal(&ss, hs_ss)) { - log_write(LOG_PLAIN, " %s", inet_ntop_ez(&ss, sizeof(ss))); - } + log_write(LOG_PLAIN, " %s", inet_ntop_ez(&ss, sizeof(ss))); } log_write(LOG_PLAIN, "\n"); } diff --git a/targets.cc b/targets.cc index d5541fdac..b59f8bb5c 100644 --- a/targets.cc +++ b/targets.cc @@ -445,7 +445,7 @@ static Target *setup_target(const HostGroupState *hs, if (hs->current_group.is_resolved_address(ss)) { if (hs->current_group.get_namedhost()) t->setTargetName(hs->current_group.get_resolved_name()); - t->resolved_addrs = hs->current_group.get_resolved_addrs(); + t->unscanned_addrs = hs->current_group.get_unscanned_addrs(); } /* We figure out the source IP/device IFF