Skip to content
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ m4_ifdef([AC_AUTOCONF_VERSION],[AC_USE_SYSTEM_EXTENSIONS], [AC_GNU_SOURCE])
# Detect Operatingsystem
AC_CANONICAL_TARGET
only_clock_realtime=no
use_getprotobyname=yes
Comment thread
Paliak marked this conversation as resolved.
Outdated

case "${target}" in
*darwin*)
Expand All @@ -21,6 +22,9 @@ case "${target}" in
*openbsd*)
only_clock_realtime=yes
;;
*android*)
use_getprotobyname=no
;;
Comment thread
Paliak marked this conversation as resolved.
Outdated
esac

dnl Detect IBM i PASE environment
Expand Down Expand Up @@ -104,6 +108,8 @@ AC_ARG_ENABLE([debug],
AS_IF([test "x$enable_debug" = "xyes"], [
AC_DEFINE([DEBUG], [1], [Define if debugging is enabled])])

AS_IF([test "x$use_getprotobyname" = "xyes"], [AC_DEFINE([USE_GETPROTOBYNAME], [1], [Enable use of getprotobyname])])
Comment thread
Paliak marked this conversation as resolved.
Outdated

AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AM_MAINTAINER_MODE

Expand Down
21 changes: 14 additions & 7 deletions src/socket4.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@ static int outgoing_src_addr_set_ipv4 = 0;

int open_ping_socket_ipv4(int *socktype)
{
struct protoent* proto;
int s;
int s = -1;
int p_proto = IPPROTO_ICMP;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just use this on all platforms? It seems to me that maybe we don't need getprotobyname at all.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my thinking too. @gsnw-sebast suggested there may be situations where IPPROTO_ICMP differs from the return value of getprotobyname, so I added it back per that suggestion.

If we really want to check whether ICMP is supported on a given platform maybe we could just check for the define?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it, and you're actually right. The value could be set statically without the check via getprotobyname(). The numbers are assigned by the IANA [1], and even if fping were to support TCP or UDP in the future, these numbers would remain unchanged with TCP = 6 and UDP = 17.

It would even keep fping running if an incorrect protocol file were stored on the system

[1] https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed getprotobyname altogether in 4cd0304.


/* confirm that ICMP is available on this machine */
if ((proto = getprotobyname("icmp")) == NULL)
crash_and_burn("icmp: unknown protocol");
#ifdef USE_GETPROTOBYNAME
Comment thread
Paliak marked this conversation as resolved.
Outdated
{
/* confirm that ICMP is available on this machine */
struct protoent* proto = getprotobyname("icmp");
if (proto == NULL) {
crash_and_burn("icmp: unknown protocol");
}
p_proto = proto->p_proto;
}
#endif

/* create raw socket for ICMP calls (ping) */
*socktype = SOCK_RAW;
s = socket(AF_INET, *socktype, proto->p_proto);
s = socket(AF_INET, *socktype, p_proto);
if (s < 0) {
/* try non-privileged icmp (works on Mac OSX without privileges, for example) */
*socktype = SOCK_DGRAM;
s = socket(AF_INET, *socktype, proto->p_proto);
s = socket(AF_INET, *socktype, p_proto);
if (s < 0) {
return -1;
}
Expand Down
21 changes: 14 additions & 7 deletions src/socket6.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,27 @@ static int outgoing_src_addr_set_ipv6 = 0;

int open_ping_socket_ipv6(int *socktype)
{
struct protoent* proto;
int s;
int s = -1;
int p_proto = IPPROTO_ICMPV6;

/* confirm that ICMP6 is available on this machine */
if ((proto = getprotobyname("ipv6-icmp")) == NULL)
crash_and_burn("ipv6-icmp: unknown protocol");
#ifdef USE_GETPROTOBYNAME
Comment thread
Paliak marked this conversation as resolved.
Outdated
{
/* confirm that ICMP6 is available on this machine */
struct protoent* proto = getprotobyname("ipv6-icmp");
if (proto == NULL) {
crash_and_burn("ipv6-icmp: unknown protocol");
}
p_proto = proto->p_proto;
}
#endif

/* create raw socket for ICMP6 calls (ping) */
*socktype = SOCK_RAW;
s = socket(AF_INET6, *socktype, proto->p_proto);
s = socket(AF_INET6, *socktype, p_proto);
if (s < 0) {
/* try non-privileged icmp6 (works on Mac OSX without privileges, for example) */
*socktype = SOCK_DGRAM;
s = socket(AF_INET6, *socktype, proto->p_proto);
s = socket(AF_INET6, *socktype, p_proto);
if (s < 0) {
return -1;
}
Expand Down
Loading