From 4cab2f8598891f800b160281b403602c5c9237fe Mon Sep 17 00:00:00 2001 From: David Schweikert Date: Thu, 16 Aug 2012 13:59:42 +0200 Subject: [PATCH] Fix -n flag in fping6 (#28) --- ChangeLog | 1 + src/fping.c | 53 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37308ab..aa266eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ UNRELEAESD David Schweikert * Added contrib/fping.spec (Stephen Schaefer, #24) * Convert man-page source to POD for easier maintenance * Fix error message on DNS error for IPv6 hosts (#27) + * Fix -n flag in fping6 (#28) * Man-page fix: TOS option typo (Thomas Liske, #23) * Man-page fix: inconsistency in regards to numeric arguments (Robert Henney) * Man-page fix: better description of option -q (#15) diff --git a/src/fping.c b/src/fping.c index 38b587d..f6c618b 100644 --- a/src/fping.c +++ b/src/fping.c @@ -2192,19 +2192,20 @@ void add_name( char *name ) }/* WHILE */ }/* ELSE */ #else - FPING_SOCKADDR dst; - struct addrinfo *res, hints; - int ret_ga; - char *hostname; - size_t len; + FPING_SOCKADDR dst; + struct addrinfo *res, hints; + int ret_ga; + size_t len; + char *printname; + char namebuf[256]; + char addrbuf[256]; /* getaddrinfo */ bzero(&hints, sizeof(struct addrinfo)); - hints.ai_flags = name_flag ? AI_CANONNAME : 0; + hints.ai_flags = 0; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_RAW; hints.ai_protocol = IPPROTO_ICMPV6; - ret_ga = getaddrinfo(name, NULL, &hints, &res); if (ret_ga) { if(!quiet_flag) @@ -2212,24 +2213,33 @@ void add_name( char *name ) num_noaddress++; return; } - if (res->ai_canonname) hostname = res->ai_canonname; - else hostname = name; - if (!res->ai_addr) { - if(!quiet_flag) - print_warning("%s: getaddrinfo failed\n", name); - num_noaddress++; - return; - } len = res->ai_addrlen; if (len > sizeof(FPING_SOCKADDR)) len = sizeof(FPING_SOCKADDR); (void)memcpy(&dst, res->ai_addr, len); - /* numerical addresses requested */ + /* name_flag: addr -> name lookup requested) */ + if(!name_flag) { + printname = name; + } + else { + int ret; + ret = getnameinfo(res->ai_addr, res->ai_addrlen, namebuf, + sizeof(namebuf)/sizeof(char), NULL, 0, 0); + if (ret) { + if(!quiet_flag) { + print_warning("%s: %s\n", name, gai_strerror(ret_ga)); + } + num_noaddress++; + return; + } + printname = namebuf; + } + + /* addr_flag: name -> addr lookup requested */ if(addr_flag) { - char addrbuf[64]; int ret; ret = getnameinfo(res->ai_addr, res->ai_addrlen, addrbuf, - 64, NULL, 0, NI_NUMERICHOST); + sizeof(addrbuf)/sizeof(char), NULL, 0, NI_NUMERICHOST); if (ret) { if(!quiet_flag) { print_warning("%s: %s\n", name, gai_strerror(ret_ga)); @@ -2237,9 +2247,10 @@ void add_name( char *name ) num_noaddress++; return; } + if(name_flag) { - char nameaddrbuf[256]; - snprintf(nameaddrbuf, 256, "%s (%s)", name, addrbuf); + char nameaddrbuf[512]; + snprintf(nameaddrbuf, sizeof(nameaddrbuf)/sizeof(char), "%s (%s)", printname, addrbuf); add_addr(name, nameaddrbuf, &dst); } else { @@ -2247,7 +2258,7 @@ void add_name( char *name ) } } else { - add_addr(name, name, &dst); + add_addr(name, printname, &dst); } #endif } /* add_name() */ -- 2.43.0