Merge pull request #397 from rpaaron/consistent_resolution_strategy

Consistent resolution strategy
This commit is contained in:
Roger Wolff 2021-04-09 08:55:46 +02:00 committed by GitHub
commit a7342019a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 86 deletions

View File

@ -268,9 +268,10 @@ class TestProbeICMPv6(mtrpacket.MtrPacketTest):
'''Test sending probes using IP version 6'''
def __init__(self, *args):
google_addr = resolve_ipv6_address(mtrpacket.IPV6_TEST_HOST)
if mtrpacket.HAVE_IPV6:
google_addr = resolve_ipv6_address(mtrpacket.IPV6_TEST_HOST)
self.google_addr = google_addr # type: str
self.google_addr = google_addr # type: str
super(TestProbeICMPv6, self).__init__(*args)

View File

@ -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)
@ -124,7 +112,7 @@ static void set_sockaddr_ip(
}
void dns_open(
sa_family_t family)
void)
{
int pid;
@ -173,7 +161,8 @@ void dns_open(
buf[strlen(buf) - 1] = 0; /* chomp newline. */
longipstr(buf, &host, family);
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);
@ -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->af, 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");

View File

@ -23,7 +23,7 @@
/* Prototypes for dns.c */
extern void dns_open(
sa_family_t family);
void);
extern int dns_waitfd(
void);
extern void dns_ack(
@ -41,8 +41,6 @@ extern char *dns_lookup(
extern char *dns_lookup2(
struct mtr_ctl *ctl,
ip_t * address);
extern struct hostent *dns_forward(
const char *name);
extern char *strlongip(
sa_family_t family,
ip_t * ip);

View File

@ -244,11 +244,13 @@ static gint Host_activate(
gpointer data)
{
struct mtr_ctl *ctl = (struct mtr_ctl *) data;
struct hostent *addr;
struct addrinfo *res = NULL;
addr = dns_forward(gtk_entry_get_text(GTK_ENTRY(entry)));
if (addr) {
net_reopen(ctl, addr);
ctl->af = DEFAULT_AF; // should this obey the cmd line option?
ctl->Hostname = gtk_entry_get_text(GTK_ENTRY(entry));
if (get_addrinfo_from_name(ctl, &res, ctl->Hostname) == 0) {
net_reopen(ctl, res);
freeaddrinfo(res);
net_send_batch(ctl);
/* If we are "Paused" at this point it is usually because someone
entered a non-existing host. Therefore do the go-ahead... */

View File

@ -38,7 +38,6 @@
#include <sys/limits.h>
#endif
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <ctype.h>
@ -63,13 +62,6 @@
#include "portability/getopt.h"
#endif
#ifdef ENABLE_IPV6
#define DEFAULT_AF AF_UNSPEC
#else
#define DEFAULT_AF AF_INET
#endif
char *myname;
const struct fields data_fields[MAXFLD] = {
@ -696,31 +688,25 @@ static void init_rand(
srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
}
/*
For historical reasons, we need a hostent structure to represent
our remote target for probing. The obsolete way of doing this
would be to use gethostbyname(). We'll use getaddrinfo() instead
to generate the hostent.
*/
static int get_hostent_from_name(
int get_addrinfo_from_name(
struct mtr_ctl *ctl,
struct hostent *host,
const char *name,
char **alptr)
struct addrinfo **res,
const char *name)
{
int gai_error;
struct addrinfo hints, *res;
struct sockaddr_in *sa4;
#ifdef ENABLE_IPV6
struct sockaddr_in6 *sa6;
#endif
struct addrinfo hints;
/* gethostbyname2() is deprecated so we'll use getaddrinfo() instead. */
memset(&hints, 0, sizeof hints);
hints.ai_family = ctl->af;
hints.ai_socktype = SOCK_DGRAM;
gai_error = getaddrinfo(name, NULL, &hints, &res);
gai_error = getaddrinfo(name, NULL, &hints, res);
if (gai_error) {
if (gai_error == EAI_SYSTEM)
error(0, 0, "Failed to resolve host: %s", name);
@ -731,33 +717,7 @@ static int get_hostent_from_name(
return -1;
}
/* Convert the first addrinfo into a hostent. */
memset(host, 0, sizeof(struct hostent));
host->h_name = res->ai_canonname;
host->h_aliases = NULL;
host->h_addrtype = res->ai_family;
ctl->af = res->ai_family;
host->h_length = res->ai_addrlen;
host->h_addr_list = alptr;
switch (ctl->af) {
case AF_INET:
sa4 = (struct sockaddr_in *) res->ai_addr;
alptr[0] = (void *) &(sa4->sin_addr);
break;
#ifdef ENABLE_IPV6
case AF_INET6:
sa6 = (struct sockaddr_in6 *) res->ai_addr;
alptr[0] = (void *) &(sa6->sin6_addr);
break;
#endif
default:
error(0, 0, "unknown address type");
errno = EINVAL;
return -1;
}
alptr[1] = NULL;
ctl->af = (*res)->ai_family;
return 0;
}
@ -766,9 +726,6 @@ int main(
int argc,
char **argv)
{
struct hostent *host = NULL;
struct hostent trhost;
char *alptr[2];
names_t *names_head = NULL;
names_t *names_walk;
@ -837,8 +794,8 @@ int main(
sizeof(ctl.LocalHostname));
}
host = &trhost;
if (get_hostent_from_name(&ctl, host, ctl.Hostname, alptr) != 0) {
struct addrinfo *res = NULL;
if (get_addrinfo_from_name(&ctl, &res, ctl.Hostname) != 0) {
if (ctl.Interactive)
exit(EXIT_FAILURE);
else {
@ -847,7 +804,7 @@ int main(
}
}
if (net_open(&ctl, host) != 0) {
if (net_open(&ctl, res) != 0) {
error(0, 0, "Unable to start net module");
if (ctl.Interactive)
exit(EXIT_FAILURE);
@ -857,8 +814,10 @@ int main(
}
}
freeaddrinfo(res);
lock(stdout);
dns_open(ctl.af);
dns_open();
display_open(&ctl);
display_loop(&ctl);

View File

@ -23,6 +23,7 @@
#include "config.h"
#include <stdint.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
@ -32,8 +33,10 @@
/* Typedefs */
#ifdef ENABLE_IPV6
#define DEFAULT_AF AF_UNSPEC
typedef struct in6_addr ip_t;
#else
#define DEFAULT_AF AF_INET
typedef struct in_addr ip_t;
#endif
@ -82,7 +85,7 @@ struct mtr_ctl {
int MaxPing;
float WaitTime;
float GraceTime;
char *Hostname;
const char *Hostname;
char *InterfaceName;
char *InterfaceAddress;
char LocalHostname[128];
@ -146,4 +149,9 @@ struct mplslen {
#define running_as_root() (getuid() == 0)
#endif
int get_addrinfo_from_name(
struct mtr_ctl *ctl,
struct addrinfo **res,
const char *name);
#endif /* MTR_MTR_H */

View File

@ -736,7 +736,7 @@ static void net_find_local_address(
int net_open(
struct mtr_ctl *ctl,
struct hostent *hostent)
struct addrinfo *res)
{
int err;
@ -746,7 +746,7 @@ int net_open(
return err;
}
net_reopen(ctl, hostent);
net_reopen(ctl, res);
return 0;
}
@ -754,7 +754,7 @@ int net_open(
void net_reopen(
struct mtr_ctl *ctl,
struct hostent *hostent)
struct addrinfo *res)
{
int at;
@ -764,9 +764,9 @@ void net_reopen(
net_reset(ctl);
ctl->af = remotesockaddr->sa_family = sourcesockaddr->sa_family = hostent->h_addrtype;
ctl->af = remotesockaddr->sa_family = sourcesockaddr->sa_family = res->ai_family;
remoteaddress = sockaddr_addr_offset(remotesockaddr);
memcpy(remoteaddress, hostent->h_addr, sockaddr_addr_size(remotesockaddr));
memcpy(remoteaddress, sockaddr_addr_offset(res->ai_addr), sockaddr_addr_size(remotesockaddr));
inet_ntop(remotesockaddr->sa_family, remoteaddress, remoteaddr, sizeof(remoteaddr));
sourceaddress = sockaddr_addr_offset(sourcesockaddr);

View File

@ -33,10 +33,10 @@
extern int net_open(
struct mtr_ctl *ctl,
struct hostent *host);
struct addrinfo *res);
extern void net_reopen(
struct mtr_ctl *ctl,
struct hostent *address);
struct addrinfo *res);
extern void net_reset(
struct mtr_ctl *ctl);
extern void net_close(