Fix running on servers with disabled IPv6, fixes #118

pull/132/head
David Schweikert 7 years ago
parent 537430f57f
commit bb0eeaaeba

@ -4,6 +4,7 @@ fping 4.1 (UNRELEASED)
## Bugfixes and other changes ## Bugfixes and other changes
- Fix problem when socket fd is 0 (#125, thanks Ramón Novoa!) - 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) fping 4.0 (2017-04-23)
====================== ======================

@ -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"); my $cmd = Test::Command->new(cmd => "/tmp/fping.copy 127.0.0.1");
$cmd->exit_is_num(4); $cmd->exit_is_num(4);
$cmd->stdout_is_eq(""); $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\?\)});
} }

@ -231,11 +231,11 @@ HOST_ENTRY* ev_last;
char* prog; char* prog;
int ident; /* our pid */ int ident; /* our pid */
int socket4 = 0; int socket4 = -1;
#ifndef IPV6 #ifndef IPV6
int hints_ai_family = AF_INET; int hints_ai_family = AF_INET;
#else #else
int socket6 = 0; int socket6 = -1;
int hints_ai_family = AF_UNSPEC; int hints_ai_family = AF_UNSPEC;
#endif #endif
@ -435,14 +435,14 @@ int main(int argc, char** argv)
break; break;
case 'M': case 'M':
#ifdef IP_MTU_DISCOVER #ifdef IP_MTU_DISCOVER
if (socket4) { if (socket4 >= 0) {
int val = IP_PMTUDISC_DO; int val = IP_PMTUDISC_DO;
if (setsockopt(socket4, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) { if (setsockopt(socket4, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) {
perror("setsockopt IP_MTU_DISCOVER"); perror("setsockopt IP_MTU_DISCOVER");
} }
} }
#ifdef IPV6 #ifdef IPV6
if (socket6) { if (socket6 >= 0) {
int val = IPV6_PMTUDISC_DO; int val = IPV6_PMTUDISC_DO;
if (setsockopt(socket6, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val))) { if (setsockopt(socket6, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val))) {
perror("setsockopt IPV6_MTU_DISCOVER"); perror("setsockopt IPV6_MTU_DISCOVER");
@ -624,13 +624,13 @@ int main(int argc, char** argv)
case 'I': case 'I':
#ifdef SO_BINDTODEVICE #ifdef SO_BINDTODEVICE
if (socket4) { if (socket4 >= 0) {
if (setsockopt(socket4, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) { if (setsockopt(socket4, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) {
perror("binding to specific interface (SO_BINTODEVICE)"); perror("binding to specific interface (SO_BINTODEVICE)");
} }
} }
#ifdef IPV6 #ifdef IPV6
if (socket6) { if (socket6 >= 0) {
if (setsockopt(socket6, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) { if (setsockopt(socket6, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) {
perror("binding to specific interface (SO_BINTODEVICE), IPV6"); perror("binding to specific interface (SO_BINTODEVICE), IPV6");
} }
@ -649,13 +649,13 @@ int main(int argc, char** argv)
case 'O': case 'O':
if (sscanf(optparse_state.optarg, "%i", &tos)) { if (sscanf(optparse_state.optarg, "%i", &tos)) {
if (socket4) { if (socket4 >= 0) {
if (setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) { if (setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) {
perror("setting type of service octet IP_TOS"); perror("setting type of service octet IP_TOS");
} }
} }
#if defined(IPV6) && defined(IPV6_TCLASS) #if defined(IPV6) && defined(IPV6_TCLASS)
if (socket6) { if (socket6 >= 0) {
if (setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) { if (setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) {
perror("setting type of service octet IPV6_TCLASS"); perror("setting type of service octet IPV6_TCLASS");
} }
@ -681,6 +681,10 @@ int main(int argc, char** argv)
/* validate various option settings */ /* validate various option settings */
if (socket4 < 0 && socket6 < 0) {
crash_and_burn("can't create socket (must run as root?)");
}
if (ttl > 255) { if (ttl > 255) {
fprintf(stderr, "%s: ttl %u out of range\n", prog, ttl); fprintf(stderr, "%s: ttl %u out of range\n", prog, ttl);
exit(1); 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) */ /* set the TTL, if the -H option was set (otherwise ttl will be = 0) */
if (ttl > 0) { if (ttl > 0) {
if (socket4) { if (socket4 >= 0) {
if (setsockopt(socket4, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) { if (setsockopt(socket4, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) {
perror("setting time to live"); perror("setting time to live");
} }
} }
#ifdef IPV6 #ifdef IPV6
if (socket6) { if (socket6 >= 0) {
if (setsockopt(socket6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))) { if (setsockopt(socket6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))) {
perror("setting time to live"); perror("setting time to live");
} }
@ -844,13 +848,13 @@ int main(int argc, char** argv)
#if HAVE_SO_TIMESTAMP #if HAVE_SO_TIMESTAMP
{ {
int opt = 1; int opt = 1;
if (socket4) { if (socket4 >= 0) {
if (setsockopt(socket4, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { if (setsockopt(socket4, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) {
perror("setting SO_TIMESTAMP option"); perror("setting SO_TIMESTAMP option");
} }
} }
#ifdef IPV6 #ifdef IPV6
if (socket6) { if (socket6 >= 0) {
if (setsockopt(socket6, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { if (setsockopt(socket6, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) {
perror("setting SO_TIMESTAMP option (IPv6)"); perror("setting SO_TIMESTAMP option (IPv6)");
} }

@ -62,7 +62,7 @@ int open_ping_socket_ipv4()
/* try non-privileged icmp (works on Mac OSX without privileges, for example) */ /* try non-privileged icmp (works on Mac OSX without privileges, for example) */
s = socket(AF_INET, SOCK_DGRAM, proto->p_proto); s = socket(AF_INET, SOCK_DGRAM, proto->p_proto);
if (s < 0) { if (s < 0) {
errno_crash_and_burn("can't create socket (must run as root?)"); return -1;
} }
} }

@ -61,7 +61,7 @@ int open_ping_socket_ipv6()
/* try non-privileged icmp (works on Mac OSX without privileges, for example) */ /* try non-privileged icmp (works on Mac OSX without privileges, for example) */
s = socket(AF_INET6, SOCK_DGRAM, proto->p_proto); s = socket(AF_INET6, SOCK_DGRAM, proto->p_proto);
if (s < 0) { if (s < 0) {
errno_crash_and_burn("can't create raw socket (must run as root?)"); return -1;
} }
} }

Loading…
Cancel
Save