--- embedaddon/mtr/ui/dns.c 2019/10/21 14:25:31 1.1.1.1 +++ 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, @@ -11,9 +11,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* @@ -41,6 +41,7 @@ #include "dns.h" #include "net.h" #include "utils.h" +#include "packet/sockaddr.h" struct dns_results { ip_t ip; @@ -51,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 @@ -86,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) @@ -105,7 +94,7 @@ static struct dns_results *findip( struct dns_results *t; for (t = results; t; t = t->next) { - if (addrcmp((void *) ip, (void *) &t->ip, ctl->af) == 0) + if (addrcmp(ip, &t->ip, ctl->af) == 0) return t; } @@ -113,30 +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) { - struct sockaddr_in *sa_in; - struct sockaddr_in6 *sa_in6; - memset(sa, 0, sizeof(struct sockaddr_storage)); - switch (ctl->af) { - case AF_INET: - sa_in = (struct sockaddr_in *) sa; - sa_in->sin_family = ctl->af; - addrcpy((void *) &sa_in->sin_addr, (void *) ip, ctl->af); - break; - case AF_INET6: - sa_in6 = (struct sockaddr_in6 *) sa; - sa_in6->sin6_family = ctl->af; - addrcpy((void *) &sa_in6->sin6_addr, (void *) ip, ctl->af); - break; - } + sa->ss_family = family; + memcpy(sockaddr_addr_offset(sa), ip, sockaddr_addr_size(sa)); } void dns_open( - struct mtr_ctl *ctl) + void) { int pid; @@ -162,7 +138,7 @@ void dns_open( error(EXIT_FAILURE, errno, "signal"); } - /* Close all unneccessary FDs. + /* Close all unnecessary FDs. for debugging and error reporting, keep std-in/out/err. */ for (i = 3; i < fromdns[1]; i++) { if (i == todns[0]) @@ -185,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) @@ -268,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); @@ -276,20 +253,19 @@ char *dns_lookup2( /* we've got a result. */ if (r->name) return r->name; - else - return strlongip(ctl, ip); } else { r = xmalloc(sizeof(struct dns_results)); memcpy(&r->ip, ip, sizeof(r->ip)); 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"); } - return strlongip(ctl, ip); + return NULL; } @@ -302,7 +278,7 @@ char *dns_lookup( if (!ctl->dns || !ctl->use_dns) return NULL; t = dns_lookup2(ctl, ip); - return t; + return t ? t : strlongip(ctl->af, ip); } /* XXX check if necessary/exported. */