tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / os_network.c
index 5e4546d0877ca95083b54cb8a268be4774084af1..9008c5c5ada024443d40234f4783350932cb6f4f 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2004, 2005, 2006, 2015 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 
 */
 /**
 #include "gnunet_util_lib.h"
 
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-os-network", __VA_ARGS__)
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-network", syscall, filename)
 
 
-#if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS)
+#if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS) && !MINGW
 /**
  * Try to enumerate all network interfaces using 'ifconfig'.
  *
@@ -67,15 +67,29 @@ try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
   struct sockaddr *pass_bcaddr;
   struct sockaddr *pass_netmask;
   int prefixlen;
+  static char *pcall;
+
+  if (NULL == pcall)
+  {
+    const char *sbin_ifconfig;
 
-  if (system ("ifconfig -a > /dev/null 2> /dev/null"))
-    if (0 == system ("/sbin/ifconfig -a > /dev/null 2> /dev/null"))
-      f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
+#ifdef IFCONFIG
+    if (0 == access (IFCONFIG, X_OK))
+      sbin_ifconfig = IFCONFIG;
     else
-      f = NULL;
-  else
-    f = popen ("ifconfig -a 2> /dev/null", "r");
-  if (! f)
+#endif
+    if (0 == access ("/sbin/ifconfig", X_OK))
+      sbin_ifconfig = "/sbin/ifconfig";
+    else if (0 == access ("/usr/sbin/ifconfig", X_OK))
+      sbin_ifconfig = "/usr/sbin/ifconfig";
+    else
+      sbin_ifconfig = "ifconfig";
+    GNUNET_asprintf (&pcall,
+                     "%s -a 2> /dev/null",
+                     sbin_ifconfig);
+  }
+  f = popen (pcall, "r");
+  if (NULL == f)
   {
     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
                        "popen",
@@ -238,8 +252,28 @@ try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
   struct sockaddr_in6 netmask6;
   unsigned int i;
   unsigned int prefixlen;
+  static char *pcall;
 
-  f = popen ("ip -o add 2> /dev/null", "r");
+  if (NULL == pcall)
+  {
+    const char *sbin_ip;
+
+#ifdef IFCONFIG
+    if (0 == access (PATH_TO_IP, X_OK))
+      sbin_ip = PATH_TO_IP;
+    else
+#endif
+    if (0 == access ("/sbin/ip", X_OK))
+      sbin_ip = "/sbin/ip";
+    else if (0 == access ("/usr/sbin/ip", X_OK))
+      sbin_ip = "/usr/sbin/ip";
+    else
+      sbin_ip = "if";
+    GNUNET_asprintf (&pcall,
+                     "%s -o add 2> /dev/null",
+                     sbin_ip);
+  }
+  f = popen (pcall, "r");
   if (! f)
   {
     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,