Blame SOURCES/python-ethtool-0.6-return-ipv6-only-interface-names.patch

becf37
--- a/python-ethtool/ethtool.c.orig	2013-08-07 10:37:37.378764077 +0200
becf37
+++ b/python-ethtool/ethtool.c	2013-08-07 10:39:27.111760146 +0200
becf37
@@ -26,6 +26,7 @@
becf37
 #include <sys/socket.h>
becf37
 #include <sys/ioctl.h>
becf37
 #include <sys/types.h>
becf37
+#include <ifaddrs.h>
becf37
 
becf37
 #include "etherinfo_struct.h"
becf37
 #include "etherinfo_obj.h"
becf37
@@ -54,55 +55,24 @@
becf37
 static PyObject *get_active_devices(PyObject *self __unused, PyObject *args __unused)
becf37
 {
becf37
 	PyObject *list;
becf37
-	int numreqs = 30;
becf37
-	struct ifconf ifc;
becf37
-	struct ifreq *ifr;
becf37
-	int n;
becf37
+	struct ifaddrs *ifaddr, *ifa;
becf37
 
becf37
-	/* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
becf37
-	   (as of 2.1.128) */
becf37
-	/* Open control socket. */
becf37
-	int skfd = socket(AF_INET, SOCK_DGRAM, 0);
becf37
-
becf37
-	if (skfd < 0) {
becf37
+	if (getifaddrs(&ifaddr) == -1) {
becf37
 		PyErr_SetString(PyExc_OSError, strerror(errno));
becf37
 		return NULL;
becf37
 	}
becf37
 
becf37
-	ifc.ifc_buf = NULL;
becf37
-	for (;;) {
becf37
-		ifc.ifc_len = sizeof(struct ifreq) * numreqs;
becf37
-		ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
becf37
-
becf37
-		if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
becf37
-			PyErr_SetString(PyExc_OSError, strerror(errno));
becf37
-			free(ifc.ifc_buf);
becf37
-			close(skfd);
becf37
-			return NULL;
becf37
-		}
becf37
-
becf37
-		if (ifc.ifc_len == (int)sizeof(struct ifreq) * numreqs) {
becf37
-			/* assume it overflowed and try again */
becf37
-			numreqs += 10;
becf37
-			continue;
becf37
-		}
becf37
-		break;
becf37
-	}
becf37
-
becf37
 	list = PyList_New(0);
becf37
-	ifr = ifc.ifc_req;
becf37
-	for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
becf37
-		if (!(ioctl(skfd, SIOCGIFFLAGS, ifr) < 0))
becf37
-			if (ifr->ifr_flags & IFF_UP) {
becf37
-				PyObject *str = PyString_FromString(ifr->ifr_name);
becf37
-				PyList_Append(list, str);
becf37
-				Py_DECREF(str);
becf37
-			}
becf37
-			ifr++;
becf37
+	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
becf37
+		PyObject *str = PyString_FromString(ifa->ifa_name);
becf37
+		/* names are not unique (listed for both ipv4 and ipv6) */
becf37
+		if (!PySequence_Contains(list, str) && (ifa->ifa_flags & (IFF_UP))) {
becf37
+			PyList_Append(list, str);
becf37
+		}
becf37
+	Py_DECREF(str);
becf37
 	}
becf37
 
becf37
-	free(ifc.ifc_buf);
becf37
-	close(skfd);
becf37
+	freeifaddrs(ifaddr);
becf37
 
becf37
 	return list;
becf37
 }