Permalink
Browse files

make `getaddrinfo()` and `getnameinfo()` usage consistent

Use modern name resolution functions and use them in a consistent
manner.

 * Avoid obsolete `gethostbyname()`, `gethostbyname2()` and `gethostbyaddr()`
 * Maintain consistency in variable names and error messages
 * Leave IDN handling up to name resolution functions

Note: This patch provides a primitive translation of calls to obsolete
functions to calls to modern functions but more changes are needed to
process the whole result lists instead of using just the first result.

Note: This patch skips functions that use jumps to avoid making more
damage than benefits.
  • Loading branch information...
1 parent 8d6b07f commit 37953bfbe4bd1b4c2be26d837dcfa2934d5a4e16 Pavel Šimerda committed with okias Jun 12, 2015
Showing with 142 additions and 204 deletions.
  1. +2 −1 Makefile
  2. +12 −18 arping.c
  3. +15 −13 clockdiff.c
  4. +3 −3 ninfod/ninfod.c
  5. +18 −49 ping.c
  6. +23 −36 ping6_common.c
  7. +4 −0 ping_common.h
  8. +4 −5 rdisc.c
  9. +23 −35 tracepath.c
  10. +15 −14 tracepath6.c
  11. +23 −30 traceroute6.c
View
@@ -148,8 +148,9 @@ LIB_clockdiff = $(LIB_CAP)
# ping / ping6
DEF_ping_common = $(DEF_CAP) $(DEF_IDN)
+DEF_ping6_common = $(DEF_CAP) $(DEF_IDN)
DEF_ping = $(DEF_CAP) $(DEF_IDN) $(DEF_WITHOUT_IFADDRS)
-LIB_ping = $(LIB_CAP) $(LIB_IDN)
+LIB_ping = $(LIB_CAP) $(LIB_IDN) $(LIB_RESOLV)
DEF_ping6 = $(DEF_CAP) $(DEF_IDN) $(DEF_WITHOUT_IFADDRS) $(DEF_ENABLE_PING6_RTHDR) $(DEF_CRYPTO)
LIB_ping6 = $(LIB_CAP) $(LIB_IDN) $(LIB_RESOLV) $(LIB_CRYPTO)
View
@@ -1091,30 +1091,24 @@ main(int argc, char **argv)
}
if (inet_aton(target, &dst) != 1) {
- struct hostent *hp;
- char *idn = target;
+ struct addrinfo hints = {
+ .ai_family = AF_INET,
+ .ai_socktype = SOCK_RAW,
#ifdef USE_IDN
- int rc;
-
- rc = idna_to_ascii_lz(target, &idn, 0);
-
- if (rc != IDNA_SUCCESS) {
- fprintf(stderr, "arping: IDN encoding failed: %s\n", idna_strerror(rc));
- exit(2);
- }
+ .ai_flags = AI_IDN | AI_CANONIDN
#endif
+ };
+ struct addrinfo *result;
+ int status;
- hp = gethostbyname2(idn, AF_INET);
- if (!hp) {
- fprintf(stderr, "arping: unknown host %s\n", target);
+ status = getaddrinfo(target, NULL, &hints, &result);
+ if (status) {
+ fprintf(stderr, "arping: %s: %s\n", target, gai_strerror(status));
exit(2);
}
-#ifdef USE_IDN
- free(idn);
-#endif
-
- memcpy(&dst, hp->h_addr, 4);
+ memcpy(&dst, &((struct sockaddr_in *) result->ai_addr)->sin_addr, sizeof dst);
+ freeaddrinfo(result);
}
if (source && inet_aton(source, &src) != 1) {
View
@@ -550,7 +550,9 @@ int
main(int argc, char *argv[])
{
int measure_status;
- struct hostent * hp;
+ struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_RAW, .ai_flags = AI_CANONNAME };
+ struct addrinfo *result;
+ int status;
char hostname[MAX_HOSTNAMELEN];
int s_errno = 0;
int n_errno = 0;
@@ -598,23 +600,23 @@ main(int argc, char *argv[])
id = getpid();
(void)gethostname(hostname,sizeof(hostname));
- hp = gethostbyname(hostname);
- if (hp == NULL) {
- fprintf(stderr, "clockdiff: %s: my host not found\n", hostname);
- exit(1);
+ status = getaddrinfo(hostname, NULL, &hints, &result);
+ if (status) {
+ fprintf(stderr, "clockdiff: %s: %s\n", hostname, gai_strerror(status));
+ exit(2);
}
- myname = strdup(hp->h_name);
+ myname = strdup(result->ai_canonname);
+ freeaddrinfo(result);
- hp = gethostbyname(argv[1]);
- if (hp == NULL) {
- fprintf(stderr, "clockdiff: %s: host not found\n", argv[1]);
+ status = getaddrinfo(argv[1], NULL, &hints, &result);
+ if (status) {
+ fprintf(stderr, "clockdiff: %s: %s\n", argv[1], gai_strerror(status));
exit(1);
}
- hisname = strdup(hp->h_name);
+ hisname = strdup(result->ai_canonname);
- memset(&server, 0, sizeof(server));
- server.sin_family = hp->h_addrtype;
- memcpy(&(server.sin_addr.s_addr), hp->h_addr, 4);
+ memcpy(&server, result->ai_addr, sizeof server);
+ freeaddrinfo(result);
if (connect(sock_raw, (struct sockaddr*)&server, sizeof(server)) == -1) {
perror("connect");
View
@@ -705,7 +705,7 @@ int main (int argc, char **argv)
struct icmp6_hdr *icmph;
#if ENABLE_DEBUG
char saddrbuf[NI_MAXHOST];
- int gni;
+ int status;
#endif
init_core(0);
@@ -734,12 +734,12 @@ int main (int argc, char **argv)
}
#if ENABLE_DEBUG
- gni = getnameinfo((struct sockaddr *)&p->addr,
+ status = getnameinfo((struct sockaddr *)&p->addr,
p->addrlen,
saddrbuf, sizeof(saddrbuf),
NULL, 0,
NI_NUMERICHOST);
- if (gni)
+ if (status)
sprintf(saddrbuf, "???");
#endif
init_core(0);
View
67 ping.c
@@ -81,7 +81,6 @@ ping_func_set_st ping4_func_set = {
#define MAXICMPLEN 76
#define NROUTES 9 /* number of record route slots */
#define TOS_MAX 255 /* 8-bit TOS field */
-#define MAX_HOSTNAMELEN NI_MAXHOST
static const int max_ping4_packet = 0x10000;
@@ -125,7 +124,9 @@ static void set_socket(socket_st *sock, int fd, int e)
int
main(int argc, char **argv)
{
- struct hostent *hp;
+ struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_protocol = IPPROTO_UDP, .ai_flags = getaddrinfo_flags };
+ struct addrinfo *result;
+ int status;
int ch, hold, packlen;
unsigned char *packet;
char *target;
@@ -136,11 +137,7 @@ main(int argc, char **argv)
char ipbuf[64];
int orig_argc = argc;
char **orig_argv = argv;
-#ifdef USE_IDN
- char *hnamebuf = NULL;
-#else
- char hnamebuf[MAX_HOSTNAMELEN];
-#endif
+ char hnamebuf[NI_MAXHOST];
char rspace[3 + 4 * NROUTES + 1]; /* record route space */
memset(&sock, 0, sizeof(sock));
@@ -289,53 +286,25 @@ main(int argc, char **argv)
if (argc == 1)
options |= F_NUMERIC;
} else {
- char *idn;
-#ifdef USE_IDN
- int rc;
-
- if (hnamebuf) {
- free(hnamebuf);
- hnamebuf = NULL;
- }
-
- rc = idna_to_ascii_lz(target, &idn, 0);
- if (rc != IDNA_SUCCESS) {
- fprintf(stderr, "ping: IDN encoding failed: %s\n", idna_strerror(rc));
- exit(2);
- }
-#else
- idn = target;
-#endif
- hp = gethostbyname2(idn, AF_INET);
- if (!hp) {
- if (force_ipv4 == 0) {
- hp = gethostbyname2(idn, AF_INET6);
- }
-
- if (hp) {
+ hints.ai_family = AF_INET;
+ status = getaddrinfo(target, NULL, &hints, &result);
+ if (status && !force_ipv4) {
+ hints.ai_family = AF_INET6;
+ status = getaddrinfo(target, NULL, &hints, &result);
+ if (!status) {
set_socket(&sock, sock6, sock6_errno);
return ping6_main(orig_argc, orig_argv, &sock);
- } else {
- fprintf(stderr, "ping: unknown host %s\n", target);
- exit(2);
}
}
-#ifdef USE_IDN
- free(idn);
-#endif
- memcpy(&whereto.sin_addr, hp->h_addr, 4);
-#ifdef USE_IDN
- if (idna_to_unicode_lzlz(hp->h_name, &hnamebuf, 0) != IDNA_SUCCESS) {
- hnamebuf = strdup(hp->h_name);
- if (!hnamebuf) {
- perror("ping: strdup");
- exit(-1);
- }
+ if (status) {
+ fprintf(stderr, "ping: %s: %s\n", target, gai_strerror(status));
+ exit(2);
}
-#else
- strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
- hnamebuf[sizeof(hnamebuf) - 1] = 0;
-#endif
+ memcpy(&whereto, result->ai_addr, sizeof whereto);
+ memset(hnamebuf, 0, sizeof hnamebuf);
+ if (result->ai_canonname)
+ strncpy(hnamebuf, result->ai_canonname, sizeof hnamebuf - 1);
+ freeaddrinfo(result);
hostname = hnamebuf;
}
if (argc > 1)
View
@@ -71,10 +71,6 @@
#include <ifaddrs.h>
#endif
-#ifdef USE_IDN
-#include <stringprep.h>
-#endif
-
#include "ping6_common.h"
#include "ping6_niquery.h"
#include "in6_flowlabel.h"
@@ -449,17 +445,16 @@ static int niquery_set_subject_type(int type)
static int niquery_option_subject_addr_handler(int index, const char *arg)
{
- struct addrinfo hints, *ai0, *ai;
+ struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = getaddrinfo_flags };
+ struct addrinfo *result, *ai;
+ int status;
int offset;
- int gai;
if (niquery_set_subject_type(niquery_options[index].data) < 0)
return -1;
ni_subject_type = niquery_options[index].data;
- memset(&hints, 0, sizeof(hints));
-
switch (niquery_options[index].data) {
case NI_SUBJ_IPV6:
ni_subject_len = sizeof(struct in6_addr);
@@ -476,18 +471,13 @@ static int niquery_option_subject_addr_handler(int index, const char *arg)
offset = -1;
}
- hints.ai_socktype = SOCK_DGRAM;
-#ifdef USE_IDN
- hints.ai_flags = AI_IDN;
-#endif
-
- gai = getaddrinfo(arg, 0, &hints, &ai0);
- if (gai) {
- fprintf(stderr, "Unknown host: %s\n", arg);
+ status = getaddrinfo(arg, 0, &hints, &result);
+ if (status) {
+ fprintf(stderr, "ping6: %s: %s\n", arg, gai_strerror(status));
return -1;
}
- for (ai = ai0; ai; ai = ai->ai_next) {
+ for (ai = result; ai; ai = ai->ai_next) {
void *p = malloc(ni_subject_len);
if (!p)
continue;
@@ -496,7 +486,7 @@ static int niquery_option_subject_addr_handler(int index, const char *arg)
ni_subject = p;
break;
}
- freeaddrinfo(ai0);
+ freeaddrinfo(result);
return 0;
}
@@ -703,8 +693,8 @@ int ping6_main(int argc, char *argv[], socket_st *sock)
int ch, hold, packlen;
unsigned char *packet;
char *target;
- struct addrinfo hints, *ai;
- int gai;
+ struct addrinfo hints, *result;
+ int status;
struct sockaddr_in6 firsthop;
struct icmp6_filter filter;
int err;
@@ -850,15 +840,13 @@ int ping6_main(int argc, char *argv[], socket_st *sock)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
-#ifdef USE_IDN
- hints.ai_flags = AI_IDN;
-#endif
- gai = getaddrinfo(target, NULL, &hints, &ai);
- if (gai) {
- fprintf(stderr, "unknown host\n");
+ hints.ai_flags = getaddrinfo_flags
+ status = getaddrinfo(target, NULL, &hints, &result);
+ if (status) {
+ fprintf(stderr, "ping6: %s: %s\n", target, gai_strerror(status));
exit(2);
}
- addr = &((struct sockaddr_in6 *)(ai->ai_addr))->sin6_addr;
+ addr = &((struct sockaddr_in6 *)(result->ai_addr))->sin6_addr;
#ifdef ENABLE_PING6_RTHDR_RFC3542
inet6_rth_add(CMSG_DATA(srcrt), addr);
#else
@@ -867,7 +855,7 @@ int ping6_main(int argc, char *argv[], socket_st *sock)
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, addr, 16);
#ifdef HAVE_SIN6_SCOPEID
- firsthop.sin6_scope_id = ((struct sockaddr_in6 *)(ai->ai_addr))->sin6_scope_id;
+ firsthop.sin6_scope_id = ((struct sockaddr_in6 *)(result->ai_addr))->sin6_scope_id;
/* Verify scope_id is the same as previous nodes */
if (firsthop.sin6_scope_id && scope_id && firsthop.sin6_scope_id != scope_id) {
fprintf(stderr, "scope discrepancy among the nodes\n");
@@ -877,7 +865,7 @@ int ping6_main(int argc, char *argv[], socket_st *sock)
}
#endif
}
- freeaddrinfo(ai);
+ freeaddrinfo(result);
argv++;
argc--;
@@ -910,22 +898,21 @@ int ping6_main(int argc, char *argv[], socket_st *sock)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
#ifdef USE_IDN
- hints.ai_flags = AI_IDN;
+ hints.ai_flags = AI_IDN | AI_CANONIDN;
#endif
- gai = getaddrinfo(target, NULL, &hints, &ai);
- if (gai) {
- fprintf(stderr, "unknown host\n");
+ status = getaddrinfo(target, NULL, &hints, &result);
+ if (status) {
+ fprintf(stderr, "ping6: %s: %s\n", target, gai_strerror(status));
exit(2);
}
- memcpy(&whereto, ai->ai_addr, sizeof(whereto));
+ memcpy(&whereto, result->ai_addr, sizeof(whereto));
whereto.sin6_port = htons(IPPROTO_ICMPV6);
+ freeaddrinfo(result);
if (memchr(target, ':', strlen(target)))
options |= F_NUMERIC;
- freeaddrinfo(ai);
-
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, &whereto.sin6_addr, 16);
#ifdef HAVE_SIN6_SCOPEID
View
@@ -27,6 +27,10 @@
#ifdef USE_IDN
#include <locale.h>
#include <idna.h>
+#include <stringprep.h>
+#define getaddrinfo_flags (AI_CANONNAME | AI_IDN | AI_CANONIDN)
+#else
+#define getaddrinfo_flags (AI_CANONNAME)
#endif
#include <netinet/in.h>
View
@@ -699,13 +699,12 @@ pr_type(int t)
*/
char *pr_name(struct in_addr addr)
{
- struct hostent *phe;
+ struct sockaddr_in sin = { .sin_family = AF_INET, .sin_addr = addr };
+ char hnamebuf[NI_MAXHOST] = "";
static char buf[80];
- phe = gethostbyaddr((char *)&addr.s_addr, 4, AF_INET);
- if (phe == NULL)
- return( inet_ntoa(addr));
- snprintf(buf, sizeof(buf), "%s (%s)", phe->h_name, inet_ntoa(addr));
+ getnameinfo((struct sockaddr *) &sin, sizeof sin, hnamebuf, sizeof hnamebuf, NULL, 0, 0);
+ snprintf(buf, sizeof buf, "%s (%s)", hnamebuf, inet_ntoa(addr));
return(buf);
}
Oops, something went wrong.

0 comments on commit 37953bf

Please sign in to comment.