--- embedaddon/mtr/ui/dns.c 2021/03/17 00:07:30 1.1.1.2 +++ embedaddon/mtr/ui/dns.c 2023/09/27 11:18:58 1.1.1.3 @@ -3,7 +3,7 @@ Copyright (C) 1997,1998 Matt Kimball This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -52,13 +52,13 @@ struct dns_results { static struct dns_results *results; char *strlongip( - struct mtr_ctl *ctl, + sa_family_t family, ip_t * ip) { #ifdef ENABLE_IPV6 static char addrstr[INET6_ADDRSTRLEN]; - return (char *) inet_ntop(ctl->af, ip, addrstr, sizeof addrstr); + return (char *) inet_ntop(family, ip, addrstr, sizeof addrstr); #else return inet_ntoa(*ip); #endif @@ -87,18 +87,6 @@ static int longipstr( } -struct hostent *dns_forward( - const char *name) -{ - struct hostent *host; - - if ((host = gethostbyname(name))) - return host; - else - return NULL; -} - - static struct dns_results *findip( struct mtr_ctl *ctl, ip_t * ip) @@ -114,17 +102,17 @@ static struct dns_results *findip( } static void set_sockaddr_ip( - struct mtr_ctl *ctl, + sa_family_t family, struct sockaddr_storage *sa, ip_t * ip) { memset(sa, 0, sizeof(struct sockaddr_storage)); - sa->ss_family = ctl->af; + sa->ss_family = family; memcpy(sockaddr_addr_offset(sa), ip, sockaddr_addr_size(sa)); } void dns_open( - struct mtr_ctl *ctl) + void) { int pid; @@ -173,16 +161,17 @@ void dns_open( buf[strlen(buf) - 1] = 0; /* chomp newline. */ - longipstr(buf, &host, ctl->af); - set_sockaddr_ip(ctl, &sa, &host); - salen = (ctl->af == AF_INET) ? sizeof(struct sockaddr_in) : + sa_family_t family = (buf[0] == '4') ? AF_INET : AF_INET6; + longipstr(buf +1, &host, family); + set_sockaddr_ip(family, &sa, &host); + salen = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); rv = getnameinfo((struct sockaddr *) &sa, salen, hostname, sizeof(hostname), NULL, 0, 0); if (rv == 0) { snprintf(result, sizeof(result), - "%s %s\n", strlongip(ctl, &host), hostname); + "%s %s\n", strlongip(family, &host), hostname); rv = write(fromdns[1], result, strlen(result)); if (rv < 0) @@ -256,7 +245,7 @@ char *dns_lookup2( ip_t * ip) { struct dns_results *r; - char buf[INET6_ADDRSTRLEN + 1]; + char buf[INET6_ADDRSTRLEN + 2]; // af_byte + addr + null int rv; r = findip(ctl, ip); @@ -270,7 +259,8 @@ char *dns_lookup2( r->name = NULL; r->next = results; results = r; - snprintf(buf, sizeof(buf), "%s\n", strlongip(ctl, ip)); + char ip4or6 = (ctl->af == AF_INET) ? '4' : '6'; + snprintf(buf, sizeof(buf), "%c%s\n", ip4or6, strlongip(ctl->af, ip)); rv = write(todns[1], buf, strlen(buf)); if (rv < 0) error(0, errno, "couldn't write to resolver process"); @@ -288,7 +278,7 @@ char *dns_lookup( if (!ctl->dns || !ctl->use_dns) return NULL; t = dns_lookup2(ctl, ip); - return t ? t : strlongip(ctl, ip); + return t ? t : strlongip(ctl->af, ip); } /* XXX check if necessary/exported. */