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

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