From 3cf9a8a3cd29c1110070fd90fe293f2705e14b62 Mon Sep 17 00:00:00 2001 From: David Schweikert Date: Fri, 4 Apr 2014 22:23:32 +0200 Subject: [PATCH] Fix confusing error message with -g and IPv6 addresses (fixes #58) --- ChangeLog | 4 ++++ ci/test-issue-58.pl | 10 ++++++++++ src/fping.c | 24 +++++++++++++----------- 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100755 ci/test-issue-58.pl diff --git a/ChangeLog b/ChangeLog index 131e9d7..a79c611 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +UNRELEASED + * More tests added + * Fix confusing error message with -g and IPv6 addresses (#58, reported by Axel Beckert) + 2014-03-08 David Schweikert * Version 3.9 * Fix random output on socket error (reported by Aleksandrs Saveljevs, #56) diff --git a/ci/test-issue-58.pl b/ci/test-issue-58.pl new file mode 100755 index 0000000..df3e601 --- /dev/null +++ b/ci/test-issue-58.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +# regression testing for github issue #58 + +use Test::Command tests => 3; + +my $cmd1 = Test::Command->new(cmd => "src/fping -a -g 2001:db8:120:4161::4/64"); +$cmd1->exit_is_num(1); +$cmd1->stdout_is_eq(""); +$cmd1->stderr_is_eq("Error: -g works only with IPv4 addresses\n"); diff --git a/src/fping.c b/src/fping.c index 1ac9b7a..03e98b0 100644 --- a/src/fping.c +++ b/src/fping.c @@ -794,7 +794,7 @@ int main( int argc, char **argv ) } if( !num_hosts ) - exit( 2 ); + exit(1); if(src_addr_present) { socket_set_src_addr(s, src_addr); @@ -880,10 +880,6 @@ void add_cidr(char *addr) *addr_end = '\0'; mask_str = addr_end + 1; mask = atoi(mask_str); - if(mask < 1 || mask > 30) { - fprintf(stderr, "Error: netmask must be between 1 and 30 (is: %s)\n", mask_str); - exit(2); - } /* parse address (IPv4 only) */ memset(&addr_hints, 0, sizeof(struct addrinfo)); @@ -892,14 +888,20 @@ void add_cidr(char *addr) ret = getaddrinfo(addr, NULL, &addr_hints, &addr_res); if(ret) { fprintf(stderr, "Error: can't parse address %s: %s\n", addr, gai_strerror(ret)); - exit(2); + exit(1); } if(addr_res->ai_family != AF_INET) { fprintf(stderr, "Error: -g works only with IPv4 addresses\n"); - exit(2); + exit(1); } net_addr = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr); + /* check mask */ + if(mask < 1 || mask > 30) { + fprintf(stderr, "Error: netmask must be between 1 and 30 (is: %s)\n", mask_str); + exit(1); + } + /* convert mask integer from 1 to 32 to a bitmask */ bitmask = ((unsigned long) 0xFFFFFFFF) << (32-mask); @@ -934,11 +936,11 @@ void add_range(char *start, char *end) ret = getaddrinfo(start, NULL, &addr_hints, &addr_res); if(ret) { fprintf(stderr, "Error: can't parse address %s: %s\n", start, gai_strerror(ret)); - exit(2); + exit(1); } if(addr_res->ai_family != AF_INET) { fprintf(stderr, "Error: -g works only with IPv4 addresses\n"); - exit(2); + exit(1); } start_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr); @@ -949,11 +951,11 @@ void add_range(char *start, char *end) ret = getaddrinfo(end, NULL, &addr_hints, &addr_res); if(ret) { fprintf(stderr, "Error: can't parse address %s: %s\n", end, gai_strerror(ret)); - exit(2); + exit(1); } if(addr_res->ai_family != AF_INET) { fprintf(stderr, "Error: -g works only with IPv4 addresses\n"); - exit(2); + exit(1); } end_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr);