Skip to content
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ AC_ARG_ENABLE([debug],
AS_IF([test "x$enable_debug" = "xyes"], [
AC_DEFINE([DEBUG], [1], [Define if debugging is enabled])])


AC_ARG_ENABLE([getprotobyname],
AS_HELP_STRING([--disable-getprotobyname], [Disable use of getprotobyname]),
Comment thread
gsnw-sebast marked this conversation as resolved.
Outdated
[], [enable_getprotobyname=yes])
AS_IF([test "x$enable_getprotobyname" = "xyes"], [
AC_DEFINE([USE_GETPROTOBYNAME], [1], [Enable use of getprotobyname])
])

AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AM_MAINTAINER_MODE

Expand Down
17 changes: 10 additions & 7 deletions src/socket4.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,23 @@ 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");
#if defined(USE_GETPROTOBYNAME) && !(defined(ANDROID) || defined(__ANDROID__))
/* confirm that ICMP is available on this machine */
if (getprotobyname("icmp") == NULL) {
crash_and_burn("icmp: unknown protocol");
}
Comment thread
gsnw-sebast marked this conversation as resolved.
Outdated
#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
17 changes: 10 additions & 7 deletions src/socket6.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ 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");
#if defined(USE_GETPROTOBYNAME) && !(defined(ANDROID) || defined(__ANDROID__))
/* confirm that ICMP6 is available on this machine */
if (getprotobyname("ipv6-icmp") == NULL ) {
crash_and_burn("ipv6-icmp: unknown protocol");
}
Comment thread
Paliak marked this conversation as resolved.
Outdated
#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