new option --rdns

pull/89/merge
David Schweikert 8 years ago
parent 2a86182f95
commit bab585e9fa

@ -15,9 +15,14 @@ Unreleased
* Version 4.0 * Version 4.0
* (feature) Unified 'fping' and 'fping6' into one binary (#80) * (feature) Unified 'fping' and 'fping6' into one binary (#80)
* (feature) Long option names for all options
* (feature) --enable-ipv6 is now default * (feature) --enable-ipv6 is now default
* (feature) New option '-4' to force IPv4 * (feature) New option -4 to force IPv4
* (feature) New option '-6' to force IPv6 * (feature) New option -6 to force IPv6
* (feature) Keep original name if a hostname is given with -n/--name
* (feature) New option -X/--rdns, similar to -n/--name, but which always
does a rdns-lookup, even for names (name->IP->name), as '-n'
was doing until now
* (bugfix) Fix compatibility issue with GNU Hurd * (bugfix) Fix compatibility issue with GNU Hurd
* (other) A C99 compiler is now required * (other) A C99 compiler is now required
* (other) Option parsing with optparse (https://github.com/skeeto/optparse) * (other) Option parsing with optparse (https://github.com/skeeto/optparse)

@ -9,7 +9,7 @@ if(!gethostbyname("www.google.com")) {
exit 0; exit 0;
} }
plan tests => 21; plan tests => 27;
my $re_num = qr{\d+(?:\.\d+)?}; my $re_num = qr{\d+(?:\.\d+)?};
@ -41,6 +41,22 @@ $cmd->stdout_is_eq("google-public-dns-a.google.com (8.8.8.8) is alive\n");
$cmd->stderr_is_eq(""); $cmd->stderr_is_eq("");
} }
# fping -4 --addr --rdns
{
my $cmd = Test::Command->new(cmd => "fping -4 --addr --rdns www.google.com");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{^\S+\.1e100\.net \(\d+\.\d+\.\d+\.\d+\) is alive\n$});
$cmd->stderr_is_eq("");
}
# fping -4 --addr --name
{
my $cmd = Test::Command->new(cmd => "fping -4 --addr --name www.google.com");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{^www\.google\.com \(\d+\.\d+\.\d+\.\d+\) is alive\n$});
$cmd->stderr_is_eq("");
}
# fping -A -n (IPv6) # fping -A -n (IPv6)
SKIP: { SKIP: {
if(system("/sbin/ifconfig | grep inet6.*Scope:Global") != 0) { if(system("/sbin/ifconfig | grep inet6.*Scope:Global") != 0) {

@ -281,7 +281,7 @@ struct timezone tz;
int generate_flag = 0; /* flag for IP list generation */ int generate_flag = 0; /* flag for IP list generation */
int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag; int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag;
int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag; int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag;
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag; int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdns_flag;
int multif_flag; int multif_flag;
int outage_flag = 0; int outage_flag = 0;
int timestamp_flag = 0; int timestamp_flag = 0;
@ -368,42 +368,42 @@ int main(int argc, char** argv)
/* get command line options */ /* get command line options */
struct optparse_long longopts[] = { struct optparse_long longopts[] = {
{ NULL, '4', OPTPARSE_NONE }, { NULL, '4', OPTPARSE_NONE },
{ NULL, '6', OPTPARSE_NONE }, { NULL, '6', OPTPARSE_NONE },
{ "alive", 'a', OPTPARSE_NONE }, { "alive", 'a', OPTPARSE_NONE },
{ "addr", 'A', OPTPARSE_NONE }, { "addr", 'A', OPTPARSE_NONE },
{ "size", 'b', OPTPARSE_REQUIRED }, { "size", 'b', OPTPARSE_REQUIRED },
{ "backoff", 'B', OPTPARSE_REQUIRED }, { "backoff", 'B', OPTPARSE_REQUIRED },
{ "count", 'c', OPTPARSE_REQUIRED }, { "count", 'c', OPTPARSE_REQUIRED },
{ "vcount", 'C', OPTPARSE_REQUIRED }, { "vcount", 'C', OPTPARSE_REQUIRED },
{ NULL, 'd', OPTPARSE_NONE }, // same as '--names' { NULL, 'd', OPTPARSE_NONE }, // same as '--name'
{ "timestamp", 'D', OPTPARSE_NONE }, { "timestamp", 'D', OPTPARSE_NONE },
{ "elapsed", 'e', OPTPARSE_NONE }, { "elapsed", 'e', OPTPARSE_NONE },
{ "file", 'f', OPTPARSE_REQUIRED }, { "file", 'f', OPTPARSE_REQUIRED },
{ "generate", 'g', OPTPARSE_NONE }, { "generate", 'g', OPTPARSE_NONE },
{ "help", 'h', OPTPARSE_NONE }, { "help", 'h', OPTPARSE_NONE },
{ "ttl", 'H', OPTPARSE_REQUIRED }, { "ttl", 'H', OPTPARSE_REQUIRED },
{ "interval", 'i', OPTPARSE_REQUIRED }, { "interval", 'i', OPTPARSE_REQUIRED },
{ "iface", 'I', OPTPARSE_REQUIRED }, { "iface", 'I', OPTPARSE_REQUIRED },
{ "loop", 'l', OPTPARSE_NONE }, { "loop", 'l', OPTPARSE_NONE },
{ "all", 'm', OPTPARSE_NONE }, { "all", 'm', OPTPARSE_NONE },
{ "dontfrag", 'M', OPTPARSE_NONE }, { "dontfrag", 'M', OPTPARSE_NONE },
{ "names", 'n', OPTPARSE_NONE }, { "name", 'n', OPTPARSE_NONE },
{ "netdata", 'N', OPTPARSE_NONE }, { "netdata", 'N', OPTPARSE_NONE },
{ "outage", 'o', OPTPARSE_NONE }, { "outage", 'o', OPTPARSE_NONE },
{ "tos", 'O', OPTPARSE_REQUIRED }, { "tos", 'O', OPTPARSE_REQUIRED },
{ "period", 'p', OPTPARSE_REQUIRED }, { "period", 'p', OPTPARSE_REQUIRED },
{ "quiet", 'q', OPTPARSE_NONE }, { "quiet", 'q', OPTPARSE_NONE },
{ "squiet", 'Q', OPTPARSE_REQUIRED }, { "squiet", 'Q', OPTPARSE_REQUIRED },
{ "retry", 'r', OPTPARSE_REQUIRED }, { "retry", 'r', OPTPARSE_REQUIRED },
{ "random", 'R', OPTPARSE_NONE }, { "random", 'R', OPTPARSE_NONE },
{ "stats", 's', OPTPARSE_NONE }, { "stats", 's', OPTPARSE_NONE },
{ "src", 'S', OPTPARSE_REQUIRED }, { "src", 'S', OPTPARSE_REQUIRED },
{ "timeout", 't', OPTPARSE_REQUIRED }, { "timeout", 't', OPTPARSE_REQUIRED },
{ NULL, 'T', OPTPARSE_REQUIRED }, { NULL, 'T', OPTPARSE_REQUIRED },
{ "unreach", 'u', OPTPARSE_NONE }, { "unreach", 'u', OPTPARSE_NONE },
{ "version", 'v', OPTPARSE_NONE }, { "version", 'v', OPTPARSE_NONE },
{ "rdns", 'X', OPTPARSE_NONE }, // FIXME: similar to --names, but do name->IP->name if a name is provided { "rdns", 'X', OPTPARSE_NONE }, // FIXME: similar to --name, but do name->IP->name if a name is provided
{ 0, 0, 0 } { 0, 0, 0 }
}; };
@ -529,6 +529,10 @@ int main(int argc, char** argv)
name_flag = 1; name_flag = 1;
break; break;
case 'X':
rdns_flag = 1;
break;
case 'A': case 'A':
addr_flag = 1; addr_flag = 1;
break; break;
@ -2215,22 +2219,27 @@ void add_name(char* name)
*/ */
for (res = res0; res; res = res->ai_next) { for (res = res0; res; res = res->ai_next) {
/* name_flag: addr -> name lookup requested) */ /* name_flag: addr -> name lookup requested) */
if (!name_flag) { if (name_flag || rdns_flag) {
printname = name; int do_rdns = rdns_flag ? 1 : 0;
} if (name_flag) {
else { /* Was it a numerical address? Only then do a rdns-query */
int ret; struct addrinfo* nres;
ret = getnameinfo(res->ai_addr, res->ai_addrlen, namebuf, hints.ai_flags = AI_NUMERICHOST;
sizeof(namebuf) / sizeof(char), NULL, 0, 0); if (getaddrinfo(name, NULL, &hints, &nres) == 0) {
if (ret) { do_rdns = 1;
if (!quiet_flag) { freeaddrinfo(nres);
print_warning("%s: can't reverse-lookup (%s)\n", name, gai_strerror(ret));
} }
printname = name;
} }
else {
if (do_rdns && getnameinfo(res->ai_addr, res->ai_addrlen, namebuf, sizeof(namebuf) / sizeof(char), NULL, 0, 0) == 0) {
printname = namebuf; printname = namebuf;
} }
else {
printname = name;
}
}
else {
printname = name;
} }
/* addr_flag: name -> addr lookup requested */ /* addr_flag: name -> addr lookup requested */
@ -2245,7 +2254,7 @@ void add_name(char* name)
continue; continue;
} }
if (name_flag) { if (name_flag || rdns_flag) {
char nameaddrbuf[512]; char nameaddrbuf[512];
snprintf(nameaddrbuf, sizeof(nameaddrbuf) / sizeof(char), "%s (%s)", printname, addrbuf); snprintf(nameaddrbuf, sizeof(nameaddrbuf) / sizeof(char), "%s (%s)", printname, addrbuf);
add_addr(name, nameaddrbuf, res->ai_addr, res->ai_addrlen); add_addr(name, nameaddrbuf, res->ai_addr, res->ai_addrlen);

Loading…
Cancel
Save