Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]),

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.

Is the --disable-getprotobyname flag really necessary? If not, then an attempt should be made to perform an automatic check.
For example, using AC_CHECK_FUNCS()

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.

This was the most portable way i could think of implementing this. AC_CHECK_FUNCS will return true on Android as getprotobyname does exist. It just always returns NULL. The only automatic way to check this that i know of would be to use AC_RUN_IFELSE but i decided against that as i feel it would be quite messy (and maybe cause issues with cross compilation). I guess the flag could just be removed altogether as #if !(defined(ANDROID) || defined(__ANDROID__)) would suffice for what this PR is trying to accomplish.

Just thought that it could be a nice to have.

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.

In that case, it would be better to move the Android detection to configure.ac. We already have the case "${target}" in function, and if other systems use the same approach, it can be extended quite easily.
This keeps the exception in the code itself more flexible.

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.

Implemented in be8cc59 let me know what you think.

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 would first check whether getprotobyname is present, and then check whether it needs to be disabled again.

Here's my example:

# Detect Operatingsystem
AC_CANONICAL_TARGET
only_clock_realtime=no
disable_getprotobyname=no

case "${target}" in
  *darwin*)
    only_clock_realtime=yes
    ;;
  *freebsd*)
    only_clock_realtime=yes
    ;;
  *openbsd*)
    only_clock_realtime=yes
    ;;
  *android*)
    disable_getprotobyname=yes
    ;;
esac

dnl Checks for libraries.

AC_CHECK_FUNCS([getprotobyname])

AS_IF([test "x$disable_getprotobyname" = "xyes"], [
  AC_MSG_NOTICE([Android detected: Disabling getprotobyname])
  ac_cv_func_getprotobyname=no
])

if test $ac_cv_func_getprotobyname = yes; then
  AC_DEFINE([USE_GETPROTOBYNAME], [1], [Define if getprotobyname is available and should be used.])
fi

[], [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;

/* 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