From bb0eeaaeba6db5a2fe6b63ae6dac11fd4650e056 Mon Sep 17 00:00:00 2001 From: David Schweikert Date: Wed, 21 Feb 2018 17:28:09 +0100 Subject: [PATCH] Fix running on servers with disabled IPv6, fixes #118 --- CHANGELOG.md | 1 + ci/test-11-nopriv.pl | 2 +- src/fping.c | 28 ++++++++++++++++------------ src/socket4.c | 2 +- src/socket6.c | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ead8e3..e164a0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ fping 4.1 (UNRELEASED) ## Bugfixes and other changes - Fix problem when socket fd is 0 (#125, thanks Ramón Novoa!) +- Fix running on servers with disabled IPv6 (#118, thanks Simon Matter) fping 4.0 (2017-04-23) ====================== diff --git a/ci/test-11-nopriv.pl b/ci/test-11-nopriv.pl index 904b382..a6bcfd2 100755 --- a/ci/test-11-nopriv.pl +++ b/ci/test-11-nopriv.pl @@ -18,5 +18,5 @@ system("cp $fping_bin /tmp/fping.copy; chmod +x /tmp/fping.copy"); my $cmd = Test::Command->new(cmd => "/tmp/fping.copy 127.0.0.1"); $cmd->exit_is_num(4); $cmd->stdout_is_eq(""); -$cmd->stderr_like(qr{: can't create socket \(must run as root\?\) : .*\n}); +$cmd->stderr_like(qr{: can't create socket \(must run as root\?\)}); } diff --git a/src/fping.c b/src/fping.c index d4d112f..82d785b 100644 --- a/src/fping.c +++ b/src/fping.c @@ -231,11 +231,11 @@ HOST_ENTRY* ev_last; char* prog; int ident; /* our pid */ -int socket4 = 0; +int socket4 = -1; #ifndef IPV6 int hints_ai_family = AF_INET; #else -int socket6 = 0; +int socket6 = -1; int hints_ai_family = AF_UNSPEC; #endif @@ -435,14 +435,14 @@ int main(int argc, char** argv) break; case 'M': #ifdef IP_MTU_DISCOVER - if (socket4) { + if (socket4 >= 0) { int val = IP_PMTUDISC_DO; if (setsockopt(socket4, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) { perror("setsockopt IP_MTU_DISCOVER"); } } #ifdef IPV6 - if (socket6) { + if (socket6 >= 0) { int val = IPV6_PMTUDISC_DO; if (setsockopt(socket6, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val))) { perror("setsockopt IPV6_MTU_DISCOVER"); @@ -624,13 +624,13 @@ int main(int argc, char** argv) case 'I': #ifdef SO_BINDTODEVICE - if (socket4) { + if (socket4 >= 0) { if (setsockopt(socket4, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) { perror("binding to specific interface (SO_BINTODEVICE)"); } } #ifdef IPV6 - if (socket6) { + if (socket6 >= 0) { if (setsockopt(socket6, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) { perror("binding to specific interface (SO_BINTODEVICE), IPV6"); } @@ -649,13 +649,13 @@ int main(int argc, char** argv) case 'O': if (sscanf(optparse_state.optarg, "%i", &tos)) { - if (socket4) { + if (socket4 >= 0) { if (setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) { perror("setting type of service octet IP_TOS"); } } #if defined(IPV6) && defined(IPV6_TCLASS) - if (socket6) { + if (socket6 >= 0) { if (setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) { perror("setting type of service octet IPV6_TCLASS"); } @@ -681,6 +681,10 @@ int main(int argc, char** argv) /* validate various option settings */ + if (socket4 < 0 && socket6 < 0) { + crash_and_burn("can't create socket (must run as root?)"); + } + if (ttl > 255) { fprintf(stderr, "%s: ttl %u out of range\n", prog, ttl); exit(1); @@ -827,13 +831,13 @@ int main(int argc, char** argv) /* set the TTL, if the -H option was set (otherwise ttl will be = 0) */ if (ttl > 0) { - if (socket4) { + if (socket4 >= 0) { if (setsockopt(socket4, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) { perror("setting time to live"); } } #ifdef IPV6 - if (socket6) { + if (socket6 >= 0) { if (setsockopt(socket6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))) { perror("setting time to live"); } @@ -844,13 +848,13 @@ int main(int argc, char** argv) #if HAVE_SO_TIMESTAMP { int opt = 1; - if (socket4) { + if (socket4 >= 0) { if (setsockopt(socket4, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { perror("setting SO_TIMESTAMP option"); } } #ifdef IPV6 - if (socket6) { + if (socket6 >= 0) { if (setsockopt(socket6, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { perror("setting SO_TIMESTAMP option (IPv6)"); } diff --git a/src/socket4.c b/src/socket4.c index d613059..e65440a 100644 --- a/src/socket4.c +++ b/src/socket4.c @@ -62,7 +62,7 @@ int open_ping_socket_ipv4() /* try non-privileged icmp (works on Mac OSX without privileges, for example) */ s = socket(AF_INET, SOCK_DGRAM, proto->p_proto); if (s < 0) { - errno_crash_and_burn("can't create socket (must run as root?)"); + return -1; } } diff --git a/src/socket6.c b/src/socket6.c index 03f2ade..c576b0e 100644 --- a/src/socket6.c +++ b/src/socket6.c @@ -61,7 +61,7 @@ int open_ping_socket_ipv6() /* try non-privileged icmp (works on Mac OSX without privileges, for example) */ s = socket(AF_INET6, SOCK_DGRAM, proto->p_proto); if (s < 0) { - errno_crash_and_burn("can't create raw socket (must run as root?)"); + return -1; } } -- 2.43.0