mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 12:41:29 +00:00
Report IPv4 and IPv6 addresses resolved for each host
Previously, we would only request IPv4 addresses by default, or only IPv6 when scanning with -6. Now, we'll request both (by not passing an address family in the hints to getaddrinfo) and report them all in the "Other addresses" line. This should encourage more users to scan with -6. Additionally, it allows us to catch and report attempts to scan IPv6-only hosts via IPv4, where previously we would just say "Failed to resolve" Closes #76
This commit is contained in:
@@ -294,10 +294,20 @@ bail:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Returns the first address which matches the address family af */
|
||||
static const struct sockaddr_storage *first_af_address(const std::list<struct sockaddr_storage> *addrs, int af) {
|
||||
for (std::list<struct sockaddr_storage>::const_iterator it = addrs->begin(), end = addrs->end(); it != end; ++it) {
|
||||
if (it->ss_family == af) {
|
||||
return &*it;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
|
||||
if (this->resolvedaddrs.empty())
|
||||
return false;
|
||||
return sockaddr_storage_equal(&*this->resolvedaddrs.begin(), ss);
|
||||
return sockaddr_storage_equal(first_af_address(&this->resolvedaddrs, ss->ss_family), ss);
|
||||
}
|
||||
|
||||
NetBlockIPv4Ranges::NetBlockIPv4Ranges() {
|
||||
@@ -610,10 +620,11 @@ NetBlock *NetBlockHostname::resolve() const {
|
||||
struct addrinfo *addrs, *addr;
|
||||
std::list<struct sockaddr_storage> resolvedaddrs;
|
||||
NetBlock *netblock;
|
||||
const struct sockaddr_storage *sp = NULL;
|
||||
struct sockaddr_storage ss;
|
||||
size_t sslen;
|
||||
|
||||
addrs = resolve_all(this->hostname.c_str(), this->af);
|
||||
addrs = resolve_all(this->hostname.c_str(), AF_UNSPEC);
|
||||
for (addr = addrs; addr != NULL; addr = addr->ai_next) {
|
||||
if (addr->ai_addrlen < sizeof(ss)) {
|
||||
memcpy(&ss, addr->ai_addr, addr->ai_addrlen);
|
||||
@@ -626,7 +637,22 @@ NetBlock *NetBlockHostname::resolve() const {
|
||||
if (resolvedaddrs.empty())
|
||||
return NULL;
|
||||
|
||||
ss = *resolvedaddrs.begin();
|
||||
sp = first_af_address(&resolvedaddrs, this->af);
|
||||
if (sp == NULL or sp->ss_family != this->af) {
|
||||
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());
|
||||
break;
|
||||
case AF_INET6:
|
||||
error("Warning: Hostname %s resolves, but not to any IPv6 address. Try scanning without -6", this->hostname.c_str());
|
||||
break;
|
||||
default:
|
||||
error("Warning: Unknown address family: %d", this->af);
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
ss = *sp;
|
||||
sslen = sizeof(ss);
|
||||
|
||||
if (resolvedaddrs.size() > 1 && o.verbose > 1) {
|
||||
|
||||
13
output.cc
13
output.cc
@@ -1426,14 +1426,17 @@ void write_host_header(Target *currenths) {
|
||||
write_host_status(currenths);
|
||||
if (currenths->TargetName() != NULL
|
||||
&& currenths->resolved_addrs.size() > 1) {
|
||||
std::list<struct sockaddr_storage>::iterator it;
|
||||
const struct sockaddr_storage *hs_ss = currenths->TargetSockAddr();
|
||||
|
||||
log_write(LOG_PLAIN, "Other addresses for %s (not scanned):",
|
||||
currenths->TargetName());
|
||||
it = currenths->resolved_addrs.begin();
|
||||
it++;
|
||||
for (; it != currenths->resolved_addrs.end(); it++)
|
||||
log_write(LOG_PLAIN, " %s", inet_ntop_ez(&*it, sizeof(*it)));
|
||||
for (std::list<struct sockaddr_storage>::const_iterator it = currenths->resolved_addrs.begin(), end = currenths->resolved_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, "\n");
|
||||
}
|
||||
/* Print reverse DNS if it differs. */
|
||||
|
||||
Reference in New Issue
Block a user