bug-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-inetutils] ifconfig -a doesn't show all interfaces


From: Giuseppe Scrivano
Subject: Re: [bug-inetutils] ifconfig -a doesn't show all interfaces
Date: Sun, 01 Aug 2010 22:18:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

if there are no objections, I will push this one:

>From ab21a4a2c5a4b4c382b683a6c45cf554acf74d43 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <address@hidden>
Date: Sun, 1 Aug 2010 22:08:44 +0200
Subject: [PATCH] Under GNU/Linux ifconfig -a includes interfaces without an 
address

* bootstrap.conf (gnulib_modules): Add module `read-file'.

* NEWS: Mention the new change.

* ifconfig/system/linux.h (HAVE_SYSTEM_IF_NAMEINDEX): New macro.
(system_if_nameindex): Add declaration.

* ifconfig/system/linux.c (system_if_nameindex): New function.

* ifconfig/if_index.c (if_nameindex) [HAVE_SYSTEM_IF_NAMEINDEX]: Use
system_if_nameindex.
---
 ChangeLog               |   17 ++++++++++
 NEWS                    |    8 +++++
 bootstrap.conf          |    1 +
 ifconfig/if_index.c     |   18 ++++++++---
 ifconfig/system/linux.c |   79 +++++++++++++++++++++++++++++++++++++++++++++++
 ifconfig/system/linux.h |    6 +++-
 6 files changed, 123 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 052daee..9c8a355 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2010-08-01  Giuseppe Scrivano  <address@hidden>
+
+       Under GNU/Linux ifconfig -a includes interfaces without an address
+
+       * bootstrap.conf (gnulib_modules): Add module `read-file'.
+
+       * NEWS: Mention the new change.
+
+       * ifconfig/system/linux.h (HAVE_SYSTEM_IF_NAMEINDEX): New macro.
+       (system_if_nameindex): Add declaration.
+       Include <net/if.> for "struct if_nameindex".
+
+       * ifconfig/system/linux.c (system_if_nameindex): New function.
+
+       * ifconfig/if_index.c (if_nameindex) [HAVE_SYSTEM_IF_NAMEINDEX]: Use
+       system_if_nameindex.
+
 2010-07-30  Giuseppe Scrivano  <address@hidden>
 
        * ifconfig/if_index.c (if_nameindex): Remove local variable `len'.
diff --git a/NEWS b/NEWS
index 4583586..08f8ac3 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,14 @@ See the end of this file for for license conditions.
 
 Please send inetutils bug reports to <address@hidden>.
 
+XXX YY, ZZ
+Version A.B:
+
+* ifconfig
+
+Under GNU/Linux ifconfig -a includes interfaces without an address.
+
+
 May 15, 2010
 Version 1.8:
 
diff --git a/bootstrap.conf b/bootstrap.conf
index bbb5c2e..facab0a 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -53,6 +53,7 @@ minmax
 obstack
 poll
 progname
+read-file
 readline
 readutmp
 realloc
diff --git a/ifconfig/if_index.c b/ifconfig/if_index.c
index 3062bf7..03072cd 100644
--- a/ifconfig/if_index.c
+++ b/ifconfig/if_index.c
@@ -94,10 +94,17 @@ if_freenameindex (struct if_nameindex *ifn)
   free (ifn);
 }
 
+#ifdef HAVE_SYSTEM_IF_NAMEINDEX
 struct if_nameindex *
 if_nameindex (void)
 {
-#if defined(SIOCGIFCONF)
+  return system_if_nameindex ();
+}
+#else
+struct if_nameindex *
+if_nameindex (void)
+{
+# if defined(SIOCGIFCONF)
   int fd = socket (AF_INET, SOCK_DGRAM, 0);
   struct ifconf ifc;
   unsigned int i = 0;
@@ -165,11 +172,11 @@ if_nameindex (void)
          return NULL;
        }
 
-# if defined(SIOCGIFINDEX)
+#  if defined(SIOCGIFINDEX)
       if (ioctl (fd, SIOCGIFINDEX, cur) >= 0)
        idx[i].if_index = cur->ifr_index;
       else
-# endif
+#  endif
        idx[i].if_index = i + 1;
       i++;
     }
@@ -193,8 +200,9 @@ if_nameindex (void)
   close (fd);
   return idx;
 
-#else
+# else
   errno = ENOSYS;
   return NULL;
-#endif
+# endif
 }
+#endif
diff --git a/ifconfig/system/linux.c b/ifconfig/system/linux.c
index fd5a1b2..58f0bcd 100644
--- a/ifconfig/system/linux.c
+++ b/ifconfig/system/linux.c
@@ -46,6 +46,8 @@
 #include <net/if_arp.h>
 #include <linux/if_ether.h>
 
+#include <read-file.h>
+
 #include "../ifconfig.h"
 
 
@@ -846,3 +848,80 @@ system_configure (int sfd, struct ifreq *ifr, struct 
system_ifconfig *ifs)
     }
   return 0;
 }
+
+struct if_nameindex *
+system_if_nameindex (void)
+{
+  char *content, *it;
+  size_t length, index;
+  struct if_nameindex *idx = NULL;
+  int fd;
+
+  fd = socket (AF_INET, SOCK_DGRAM, 0);
+  if (fd < 0)
+    return NULL;
+
+  content = read_file (PATH_PROCNET_DEV, &length);
+  if (content == NULL)
+    return NULL;
+
+  /* Count how many interfaces we have.  */
+  {
+    size_t n = 0;
+    it = content;
+    do
+      {
+        it = memchr (it + 1, ':', length - (it - content));
+        n++;
+      }
+    while (it);
+
+    idx = malloc (n * sizeof (*idx));
+    if (idx == NULL)
+      {
+        int saved_errno = errno;
+        close (fd);
+        free (content);
+        errno = saved_errno;
+        return NULL;
+      }
+  }
+
+  for (it = memchr (content, ':', length), index = 0; it;
+       it = memchr (it, ':', it - content), index++)
+    {
+      char *start = it - 1;
+      *it = '\0';
+
+      while (*start != ' ' && *start != '\n')
+        start--;
+
+      idx[index].if_name = strdup (start + 1);
+      idx[index].if_index = index + 1;
+
+# if defined(SIOCGIFINDEX)
+      {
+        struct ifreq cur;
+        strcpy (cur.ifr_name, idx[index].if_name);
+        cur.ifr_index = -1;
+        if (ioctl (fd, SIOCGIFINDEX, &cur) >= 0)
+          idx[index].if_index = cur.ifr_index;
+      }
+# endif
+
+      if (idx[index].if_name == NULL)
+        {
+          int saved_errno = errno;
+          close (fd);
+          free (content);
+          errno = saved_errno;
+          return NULL;
+        }
+    }
+
+  idx[index].if_index = 0;
+  idx[index].if_name = NULL;
+
+  free (content);
+  return idx;
+}
diff --git a/ifconfig/system/linux.h b/ifconfig/system/linux.h
index 5b03581..dff1293 100644
--- a/ifconfig/system/linux.h
+++ b/ifconfig/system/linux.h
@@ -24,6 +24,7 @@
 
 # include "../printif.h"
 # include "../options.h"
+#include <net/if.h>
 
 
 /* Option support.  */
@@ -86,7 +87,10 @@ void system_fh_txqlen_query (format_data_t form, int argc, 
char *argv[]);
 void system_fh_txqlen (format_data_t form, int argc, char *argv[]);
 
 void system_fh_ifstat_query (format_data_t form, int argc, char *argv[]);
-                                   
+
+#define HAVE_SYSTEM_IF_NAMEINDEX 1
+struct if_nameindex *system_if_nameindex (void);
+
 _IU_EXTRN (rx_packets)
 _IU_EXTRN (tx_packets)
 _IU_EXTRN (rx_bytes)
-- 
1.7.1



reply via email to

[Prev in Thread] Current Thread [Next in Thread]