about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/if_index.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2004-06-30 07:43:11 +0000
committerUlrich Drepper <drepper@redhat.com>2004-06-30 07:43:11 +0000
commitf5164429135806c41da679afb662d8482eec0a41 (patch)
tree3cd260d001c1fdb72635223a258d066e989337dd /sysdeps/unix/sysv/linux/if_index.c
parent606135cf62624432a4b6fac724cad0e23b01202a (diff)
downloadglibc-f5164429135806c41da679afb662d8482eec0a41.tar.gz
glibc-f5164429135806c41da679afb662d8482eec0a41.tar.xz
glibc-f5164429135806c41da679afb662d8482eec0a41.zip
Update.
2004-06-30  Ulrich Drepper  <drepper@redhat.com>

	* include/net/if.h: Handle if_nameindex and if_freenameindex with
	libc_proto_hidden.
	* sysdeps/unix/sysv/linux/netlinkaccess.h: New file.
	* sysdeps/unix/sysv/linux/ifaddrs.c: Export netlink handling functions.
	* sysdeps/unix/sysv/linux/if_index.c (if_nameindex): Implement using
	netlink if possible.  Fall back on ioctl method if necessary.

	* include/unistd.h: Declare __truncate.
	* sysdeps/generic/truncate.c: Also define __truncate.
	* sysdeps/mach/hurd/truncate.c: Likewise.
	* sysdeps/unix/common/syscalls.list: Likewise.
	* sysdeps/unix/sysv/linux/truncate64.c: Use __truncate, not truncate.
Diffstat (limited to 'sysdeps/unix/sysv/linux/if_index.c')
-rw-r--r--sysdeps/unix/sysv/linux/if_index.c159
1 files changed, 149 insertions, 10 deletions
diff --git a/sysdeps/unix/sysv/linux/if_index.c b/sysdeps/unix/sysv/linux/if_index.c
index 92fb11b180..377ccf5199 100644
--- a/sysdeps/unix/sysv/linux/if_index.c
+++ b/sysdeps/unix/sysv/linux/if_index.c
@@ -28,10 +28,11 @@
 #include <bits/libc-lock.h>
 #include <not-cancel.h>
 
-#include "kernel-features.h"
+#include "netlinkaccess.h"
+
 
 /* Variable to signal whether SIOCGIFCONF is not available.  */
-#if __ASSUME_SIOCGIFNAME == 0
+# if __ASSUME_SIOCGIFNAME == 0
 static int old_siocgifconf;
 #else
 # define old_siocgifconf 0
@@ -73,20 +74,18 @@ if_freenameindex (struct if_nameindex *ifn)
   struct if_nameindex *ptr = ifn;
   while (ptr->if_name || ptr->if_index)
     {
-      if (ptr->if_name)
-	free (ptr->if_name);
+      free (ptr->if_name);
       ++ptr;
     }
   free (ifn);
 }
+libc_hidden_def (if_freenameindex)
 
-struct if_nameindex *
-if_nameindex (void)
+
+#if __ASSUME_NETLINK_SUPPORT == 0
+static struct if_nameindex *
+if_nameindex_ioctl (void)
 {
-#ifndef SIOCGIFINDEX
-  __set_errno (ENOSYS);
-  return NULL;
-#else
   int fd = __opensock ();
   struct ifconf ifc;
   unsigned int nifs, i;
@@ -174,8 +173,147 @@ if_nameindex (void)
 
   close_not_cancel_no_status (fd);
   return idx;
+}
+#endif
+
+
+static struct if_nameindex *
+if_nameindex_netlink (void)
+{
+  struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
+  struct if_nameindex *idx = NULL;
+
+  if (__no_netlink_support || __netlink_open (&nh) < 0)
+    return NULL;
+
+
+  /* Tell the kernel that we wish to get a list of all
+     active interfaces.  */
+  if (__netlink_sendreq (&nh, RTM_GETLINK) < 0)
+    goto exit_close;
+
+  /* Collect all data for every interface.  */
+  if (__netlink_receive (&nh) < 0)
+    goto exit_free;
+
+  /* Count the interfaces.  */
+  unsigned int nifs = 0;
+  for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
+    {
+      struct nlmsghdr *nlh;
+      size_t size = nlp->size;
+
+      if (nlp->nlh == NULL)
+	continue;
+
+      /* Walk through all entries we got from the kernel and look, which
+         message type they contain.  */
+      for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
+	{
+	  /* Check if the message is what we want.  */
+	  if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
+	    continue;
+
+	  if (nlh->nlmsg_type == NLMSG_DONE)
+	    break;		/* ok */
+
+	  if (nlh->nlmsg_type == RTM_NEWLINK)
+	    ++nifs;
+	}
+    }
+
+  idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
+  if (idx == NULL)
+    {
+    nomem:
+      __set_errno (ENOBUFS);
+      goto exit_free;
+    }
+
+  /* Add the interfaces.  */
+  nifs = 0;
+  for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
+    {
+      struct nlmsghdr *nlh;
+      size_t size = nlp->size;
+
+      if (nlp->nlh == NULL)
+	continue;
+
+      /* Walk through all entries we got from the kernel and look, which
+         message type they contain.  */
+      for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
+	{
+	  /* Check if the message is what we want.  */
+	  if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
+	    continue;
+
+	  if (nlh->nlmsg_type == NLMSG_DONE)
+	    break;		/* ok */
+
+	  if (nlh->nlmsg_type == RTM_NEWLINK)
+	    {
+	      struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
+	      struct rtattr *rta = IFLA_RTA (ifim);
+	      size_t rtasize = IFLA_PAYLOAD (nlh);
+
+	      idx[nifs].if_index = ifim->ifi_index;
+
+	      while (RTA_OK (rta, rtasize))
+		{
+		  char *rta_data = RTA_DATA (rta);
+		  size_t rta_payload = RTA_PAYLOAD (rta);
+
+		  if (rta->rta_type == IFLA_IFNAME)
+		    {
+		      idx[nifs].if_name = __strndup (rta_data, rta_payload);
+		      if (idx[nifs].if_name == NULL)
+			{
+			  idx[nifs].if_index = 0;
+			  if_freenameindex (idx);
+			  idx = NULL;
+			  goto nomem;
+			}
+		      break;
+		    }
+
+		  rta = RTA_NEXT (rta, rtasize);
+		}
+
+	      ++nifs;
+	    }
+	}
+    }
+
+  idx[nifs].if_index = 0;
+  idx[nifs].if_name = NULL;
+
+ exit_free:
+  __netlink_free_handle (&nh);
+ exit_close:
+  __netlink_close (&nh);
+
+  return idx;
+}
+
+
+struct if_nameindex *
+if_nameindex (void)
+{
+#ifndef SIOCGIFINDEX
+  __set_errno (ENOSYS);
+  return NULL;
+#else
+  struct if_nameindex *result = if_nameindex_netlink ();
+# if __ASSUME_NETLINK_SUPPORT == 0
+  if (__no_netlink_support)
+    result = if_nameindex_ioctl ();
+# endif
+  return result;
 #endif
 }
+libc_hidden_def (if_nameindex)
+
 
 char *
 if_indextoname (unsigned int ifindex, char *ifname)
@@ -263,6 +401,7 @@ if_indextoname (unsigned int ifindex, char *ifname)
 }
 libc_hidden_def (if_indextoname)
 
+
 #if 0
 void
 internal_function