|
|
| version 1.1.1.1, 2019/10/21 14:25:31 | version 1.1.1.3, 2023/09/27 11:18:58 |
|---|---|
| Line 3 | Line 3 |
| Copyright (C) 1997,1998 Matt Kimball | Copyright (C) 1997,1998 Matt Kimball |
| This program is free software; you can redistribute it and/or modify | 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. | published by the Free Software Foundation. |
| This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, |
| Line 11 | Line 11 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. | GNU General Public License for more details. |
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License along |
| along with this program; if not, write to the Free Software | with this program; if not, write to the Free Software Foundation, Inc., |
| Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| */ | */ |
| /* | /* |
| Line 41 | Line 41 |
| #include "dns.h" | #include "dns.h" |
| #include "net.h" | #include "net.h" |
| #include "utils.h" | #include "utils.h" |
| #include "packet/sockaddr.h" | |
| struct dns_results { | struct dns_results { |
| ip_t ip; | ip_t ip; |
| Line 51 struct dns_results { | Line 52 struct dns_results { |
| static struct dns_results *results; | static struct dns_results *results; |
| char *strlongip( | char *strlongip( |
| struct mtr_ctl *ctl, | sa_family_t family, |
| ip_t * ip) | ip_t * ip) |
| { | { |
| #ifdef ENABLE_IPV6 | #ifdef ENABLE_IPV6 |
| static char addrstr[INET6_ADDRSTRLEN]; | 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 | #else |
| return inet_ntoa(*ip); | return inet_ntoa(*ip); |
| #endif | #endif |
| Line 86 static int longipstr( | Line 87 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( | static struct dns_results *findip( |
| struct mtr_ctl *ctl, | struct mtr_ctl *ctl, |
| ip_t * ip) | ip_t * ip) |
| Line 105 static struct dns_results *findip( | Line 94 static struct dns_results *findip( |
| struct dns_results *t; | struct dns_results *t; |
| for (t = results; t; t = t->next) { | 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; | return t; |
| } | } |
| Line 113 static struct dns_results *findip( | Line 102 static struct dns_results *findip( |
| } | } |
| static void set_sockaddr_ip( | static void set_sockaddr_ip( |
| struct mtr_ctl *ctl, | sa_family_t family, |
| struct sockaddr_storage *sa, | struct sockaddr_storage *sa, |
| ip_t * ip) | ip_t * ip) |
| { | { |
| struct sockaddr_in *sa_in; | |
| struct sockaddr_in6 *sa_in6; | |
| memset(sa, 0, sizeof(struct sockaddr_storage)); | memset(sa, 0, sizeof(struct sockaddr_storage)); |
| switch (ctl->af) { | sa->ss_family = family; |
| case AF_INET: | memcpy(sockaddr_addr_offset(sa), ip, sockaddr_addr_size(sa)); |
| 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; | |
| } | |
| } | } |
| void dns_open( | void dns_open( |
| struct mtr_ctl *ctl) | void) |
| { | { |
| int pid; | int pid; |
| Line 162 void dns_open( | Line 138 void dns_open( |
| error(EXIT_FAILURE, errno, "signal"); | error(EXIT_FAILURE, errno, "signal"); |
| } | } |
| /* Close all unneccessary FDs. | /* Close all unnecessary FDs. |
| for debugging and error reporting, keep std-in/out/err. */ | for debugging and error reporting, keep std-in/out/err. */ |
| for (i = 3; i < fromdns[1]; i++) { | for (i = 3; i < fromdns[1]; i++) { |
| if (i == todns[0]) | if (i == todns[0]) |
| Line 185 void dns_open( | Line 161 void dns_open( |
| buf[strlen(buf) - 1] = 0; /* chomp newline. */ | buf[strlen(buf) - 1] = 0; /* chomp newline. */ |
| longipstr(buf, &host, ctl->af); | sa_family_t family = (buf[0] == '4') ? AF_INET : AF_INET6; |
| set_sockaddr_ip(ctl, &sa, &host); | longipstr(buf +1, &host, family); |
| salen = (ctl->af == AF_INET) ? sizeof(struct sockaddr_in) : | set_sockaddr_ip(family, &sa, &host); |
| salen = (family == AF_INET) ? sizeof(struct sockaddr_in) : | |
| sizeof(struct sockaddr_in6); | sizeof(struct sockaddr_in6); |
| rv = getnameinfo((struct sockaddr *) &sa, salen, | rv = getnameinfo((struct sockaddr *) &sa, salen, |
| hostname, sizeof(hostname), NULL, 0, 0); | hostname, sizeof(hostname), NULL, 0, 0); |
| if (rv == 0) { | if (rv == 0) { |
| snprintf(result, sizeof(result), | 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)); | rv = write(fromdns[1], result, strlen(result)); |
| if (rv < 0) | if (rv < 0) |
| Line 268 char *dns_lookup2( | Line 245 char *dns_lookup2( |
| ip_t * ip) | ip_t * ip) |
| { | { |
| struct dns_results *r; | struct dns_results *r; |
| char buf[INET6_ADDRSTRLEN + 1]; | char buf[INET6_ADDRSTRLEN + 2]; // af_byte + addr + null |
| int rv; | int rv; |
| r = findip(ctl, ip); | r = findip(ctl, ip); |
| Line 276 char *dns_lookup2( | Line 253 char *dns_lookup2( |
| /* we've got a result. */ | /* we've got a result. */ |
| if (r->name) | if (r->name) |
| return r->name; | return r->name; |
| else | |
| return strlongip(ctl, ip); | |
| } else { | } else { |
| r = xmalloc(sizeof(struct dns_results)); | r = xmalloc(sizeof(struct dns_results)); |
| memcpy(&r->ip, ip, sizeof(r->ip)); | memcpy(&r->ip, ip, sizeof(r->ip)); |
| r->name = NULL; | r->name = NULL; |
| r->next = results; | r->next = results; |
| results = r; | 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)); | rv = write(todns[1], buf, strlen(buf)); |
| if (rv < 0) | if (rv < 0) |
| error(0, errno, "couldn't write to resolver process"); | error(0, errno, "couldn't write to resolver process"); |
| } | } |
| return strlongip(ctl, ip); | return NULL; |
| } | } |
| Line 302 char *dns_lookup( | Line 278 char *dns_lookup( |
| if (!ctl->dns || !ctl->use_dns) | if (!ctl->dns || !ctl->use_dns) |
| return NULL; | return NULL; |
| t = dns_lookup2(ctl, ip); | t = dns_lookup2(ctl, ip); |
| return t; | return t ? t : strlongip(ctl->af, ip); |
| } | } |
| /* XXX check if necessary/exported. */ | /* XXX check if necessary/exported. */ |