1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-02 12:59:02 +00:00

Make resolve return a getaddrinfo error code.

The only error we can have apart from a getaddrinfo error is a list of
zero addresses; return EAI_NONAME in that case.

This unfortunately inverts the truth value of the return code of
resolve; 0 now means success.
This commit is contained in:
david
2012-09-15 17:56:08 +00:00
parent 8803578976
commit 0e738370ee
10 changed files with 34 additions and 30 deletions

View File

@@ -339,7 +339,7 @@ static void initialize_idleproxy(struct idle_proxy_info *proxy, char *proxyName,
}
proxy->host.setHostName(name);
if (resolve(name, 0, 0, &ss, &sslen, o.pf()) == 0) {
if (resolve(name, 0, 0, &ss, &sslen, o.pf()) != 0) {
fatal("Could not resolve idle scan zombie host: %s", name);
}
proxy->host.setTargetSockAddr(&ss, sslen);

View File

@@ -381,7 +381,7 @@ after:
- Parameter "nodns": If set, it means that the supplied hostname is actually a
numeric IP address. The flag prevents any type of name resolution service
from being called. In 99% of the cases this should be 0.
Returns 1 on success or 0 if hostname could not be resolved. */
Returns 0 on success, or a getaddrinfo return code on failure. */
int resolve(const char *hostname, u16 port, int nodns, struct sockaddr_storage *ss, size_t *sslen, int af){
struct addrinfo hints;
struct addrinfo *result;
@@ -396,20 +396,22 @@ int resolve(const char *hostname, u16 port, int nodns, struct sockaddr_storage *
hints.ai_family = af;
hints.ai_socktype = SOCK_DGRAM;
if (nodns)
hints.ai_flags |= AI_NUMERICHOST;
hints.ai_flags |= AI_NUMERICHOST;
/* Make the port number a string to give to getaddrinfo. */
rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port);
assert(rc >= 0 && rc < sizeof(portbuf));
rc = getaddrinfo(hostname, portbuf, &hints, &result);
if (rc != 0 || result == NULL)
return 0;
if (rc != 0)
return rc;
if (result == NULL)
return EAI_NONAME;
assert(result->ai_addrlen > 0 && result->ai_addrlen <= (int) sizeof(struct sockaddr_storage));
*sslen = result->ai_addrlen;
memcpy(ss, result->ai_addr, *sslen);
freeaddrinfo(result);
return 1;
return 0;
}
/*

View File

@@ -164,7 +164,7 @@ int parse_ip_options(const char *txt, u8 *data, int datalen, int* firsthopoff, i
- Parameter "nodns": If set, it means that the supplied hostname is actually a
numeric IP address. The flag prevents any type of name resolution service
from being called. In 99% of the cases this should be 0.
Returns 1 on success or 0 if hostname could not be resolved. */
Returns 0 on success, or a getaddrinfo return code on failure. */
int resolve(const char *hostname, u16 port, int nodns, struct sockaddr_storage *ss, size_t *sslen, int af);
/*

View File

@@ -181,8 +181,8 @@ void options_init(void)
}
/* Tries to resolve the given name (or literal IP) into a sockaddr structure.
Pass 0 for the port if you don't care. Returns 0 if hostname cannot be
resolved. */
Pass 0 for the port if you don't care. Returns 0 on success, or a getaddrinfo
return code on error. */
int resolve(char *hostname, unsigned short port,
struct sockaddr_storage *ss, size_t *sslen, int af)
{
@@ -205,13 +205,15 @@ int resolve(char *hostname, unsigned short port,
assert(rc >= 0 && rc < sizeof(portbuf));
rc = getaddrinfo(hostname, portbuf, &hints, &result);
if (rc != 0 || result == NULL)
return 0;
if (rc != 0)
return rc;
if (result == NULL)
return EAI_NONAME;
assert(result->ai_addrlen > 0 && result->ai_addrlen <= (int) sizeof(struct sockaddr_storage));
*sslen = result->ai_addrlen;
memcpy(ss, result->ai_addr, *sslen);
freeaddrinfo(result);
return 1;
return 0;
}
int fdinfo_close(struct fdinfo *fdn)

View File

@@ -173,8 +173,8 @@ extern struct timeval start_time;
void options_init(void);
/* Tries to resolve the given name (or literal IP) into a sockaddr structure.
Pass 0 for the port if you don't care. Returns 0 if hostname cannot be
resolved. */
Pass 0 for the port if you don't care. Returns 0 on success, or a getaddrinfo
return code on error. */
int resolve(char *hostname, unsigned short port,
struct sockaddr_storage *ss, size_t *sslen, int af);

View File

@@ -136,7 +136,7 @@ static void parseproxy(char *str, struct sockaddr_storage *ss, unsigned short de
else
portno = defport;
if (!resolve(ptr, portno, ss, &sslen, o.af)) {
if (resolve(ptr, portno, ss, &sslen, o.af) != 0) {
loguser("Could not resolve proxy \"%s\".\n", ptr);
if (o.af == AF_INET6 && httpproxy)
loguser("Did you specify the port number? It's required for IPv6.\n");
@@ -317,7 +317,7 @@ int main(int argc, char *argv[])
do {
union sockaddr_u addr;
size_t sslen;
if (!resolve(a, 0, &addr.storage, &sslen, AF_INET))
if (resolve(a, 0, &addr.storage, &sslen, AF_INET) != 0)
bye("Sorry, could not resolve source route hop %s.", a);
o.srcrtes[o.numsrcrtes] = addr.in.sin_addr;
} while (o.numsrcrtes++ <= 8 && (a = strtok(NULL, ",")));
@@ -578,7 +578,7 @@ int main(int argc, char *argv[])
if (o.listen)
bye("-l and -s are incompatible. Specify the address and port to bind to like you would a host to connect to.");
if (!resolve(source, 0, &srcaddr.storage, &srcaddrlen, o.af))
if (resolve(source, 0, &srcaddr.storage, &srcaddrlen, o.af) != 0)
bye("Could not resolve source address %s.", source);
}
@@ -596,7 +596,7 @@ int main(int argc, char *argv[])
if (strspn(argv[optind], "0123456789") != strlen(argv[optind])) {
o.target = argv[optind];
/* resolve hostname */
if (!resolve(o.target, 0, &targetss.storage, &targetsslen, o.af))
if (resolve(o.target, 0, &targetss.storage, &targetsslen, o.af) != 0)
bye("Could not resolve hostname %s.", o.target);
optind++;
} else {
@@ -758,7 +758,7 @@ static int ncat_listen_mode(void)
if (o.af == AF_INET6 || o.af == AF_UNSPEC) {
ss_len = sizeof(listenaddrs[num_listenaddrs]);
rc = resolve("::", o.portno, &listenaddrs[num_listenaddrs].storage, &ss_len, AF_INET6);
if (!rc)
if (rc != 0)
bye("Failed to resolve default IPv6 address.");
num_listenaddrs++;
}
@@ -766,7 +766,7 @@ static int ncat_listen_mode(void)
if (o.af == AF_INET || o.af == AF_UNSPEC) {
ss_len = sizeof(listenaddrs[num_listenaddrs]);
rc = resolve("0.0.0.0", o.portno, &listenaddrs[num_listenaddrs].storage, &ss_len, AF_INET);
if (!rc)
if (rc != 0)
bye("Failed to resolve default IPv4 address.");
num_listenaddrs++;
}

View File

@@ -438,7 +438,7 @@ static int handle_connect(struct socket_buffer *client_sock,
if (o.debug > 1)
logdebug("CONNECT to %s:%hu.\n", request->uri.host, request->uri.port);
if (!resolve(request->uri.host, request->uri.port, &su.storage, &sslen, o.af)) {
if (resolve(request->uri.host, request->uri.port, &su.storage, &sslen, o.af) != 0) {
if (o.debug)
logdebug("Can't resolve name %s.\n", request->uri.host);
return 504;
@@ -543,7 +543,7 @@ static int handle_method(struct socket_buffer *client_sock,
return 400;
}
if (!resolve(request->uri.host, request->uri.port, &su.storage, &sslen, o.af)) {
if (resolve(request->uri.host, request->uri.port, &su.storage, &sslen, o.af) != 0) {
if (o.debug)
logdebug("Can't resolve name %s:%d.\n", request->uri.host, request->uri.port);
return 504;

View File

@@ -996,7 +996,7 @@ void parse_options(int argc, char **argv) {
/* Try to resolve it */
struct sockaddr_in decoytemp;
size_t decoytemplen = sizeof(struct sockaddr_in);
if ( resolve(p, 0, 0, (sockaddr_storage*)&decoytemp, &decoytemplen, AF_INET) == 1) {
if (resolve(p, 0, 0, (sockaddr_storage*)&decoytemp, &decoytemplen, AF_INET) == 0) {
o.decoys[o.numdecoys] = decoytemp.sin_addr;
o.numdecoys++;
} else {
@@ -1338,7 +1338,7 @@ void apply_delayed_options() {
size_t sslen;
if (o.spoofsource) {
if (resolve(delayed_options.spoofSource, 0, 0, &ss, &sslen, o.af()) == 0)
if (resolve(delayed_options.spoofSource, 0, 0, &ss, &sslen, o.af()) != 0)
fatal("Failed to resolve/decode supposed %s source address %s.", (o.af() == AF_INET) ? "IPv4" : "IPv6", delayed_options.spoofSource);
o.setSourceSockAddr(&ss, sslen);
}
@@ -1633,7 +1633,7 @@ int nmap_main(int argc, char *argv[]) {
size_t sslen;
dst = route_dst_hosts[i].c_str();
if (!resolve(dst, 0, 0, &ss, &sslen, o.af()))
if (resolve(dst, 0, 0, &ss, &sslen, o.af()) != 0)
fatal("Can't resolve %s.", dst);
printf("%s\n", inet_ntop_ez(&ss, sslen));
@@ -1936,7 +1936,7 @@ int nmap_main(int argc, char *argv[]) {
currenths->setSourceSockAddr(&ss, sslen);
} else {
if (gethostname(myname, MAXHOSTNAMELEN) ||
resolve(myname, 0, 0, &ss, &sslen, o.af()) == 0)
resolve(myname, 0, 0, &ss, &sslen, o.af()) != 0)
fatal("Cannot get hostname! Try using -S <my_IP_address> or -e <interface to scan through>\n");
o.setSourceSockAddr(&ss, sslen);

View File

@@ -820,7 +820,7 @@ static void add_dns_server(char *ipaddrs) {
for (hostname = strtok(ipaddrs, " ,"); hostname != NULL; hostname = strtok(NULL, " ,")) {
if (!resolve(hostname, 0, 0, (struct sockaddr_storage *) &addr, &addr_len, PF_UNSPEC)) continue;
if (resolve(hostname, 0, 0, (struct sockaddr_storage *) &addr, &addr_len, PF_UNSPEC) != 0) continue;
for(servI = servs.begin(); servI != servs.end(); servI++) {
tpserv = *servI;

View File

@@ -106,7 +106,7 @@ extern NpingOps o;
int atoIP(const char *hostname, struct in_addr *dst){
struct sockaddr_in i;
unsigned int stlen=0;
if ( resolve(hostname, 0, 0, (sockaddr_storage*)&i, (size_t *)&stlen , PF_INET) == 0 )
if ( resolve(hostname, 0, 0, (sockaddr_storage*)&i, (size_t *)&stlen , PF_INET) != 0 )
return OP_FAILURE;
*dst=i.sin_addr;
return OP_SUCCESS;
@@ -118,7 +118,7 @@ int atoIP(const char *hostname, struct sockaddr_storage *ss, int family){
return OP_FAILURE;
if(family!=AF_INET && family!=AF_INET6)
return OP_FAILURE;
if ( resolve(hostname, 0, 0, ss, &stlen , family) == 0 )
if ( resolve(hostname, 0, 0, ss, &stlen , family) != 0 )
return OP_FAILURE;
return OP_SUCCESS;
} /* End of atoIP */
@@ -556,7 +556,7 @@ int resolveCached(char *host, struct sockaddr_storage *ss, size_t *sslen, int pf
misses++;
outPrint(DBG_4, "resolveCached(): Cache miss %d for %s\n", misses, host);
if( (result=resolve(host, 0, 0, ss, sslen, pf)) == 1 ){
if( (result=resolve(host, 0, 0, ss, sslen, pf)) == 0 ){
/* Increment count */
if( cached_count < MAX_CACHED_HOSTS )