diff options
author | Alastair Houghton <alastair@coriolis-systems.com> | 2016-08-23 10:46:54 +0100 |
---|---|---|
committer | Alastair Houghton <alastair@coriolis-systems.com> | 2016-08-23 10:46:54 +0100 |
commit | dd4f1c4975a2e6af2e4778e79d1aaa22168d7f0c (patch) | |
tree | b87c3e6c2e79665b2ff40da07bdb9b2b40c1bbef | |
parent | d1ee1361a0630240d211405dadeab163c2d8c049 (diff) | |
download | netifaces-dd4f1c4975a2e6af2e4778e79d1aaa22168d7f0c.tar.gz |
Fixed the build on Linux. Also changed the Linux code to respect the interface priority when determining the default gateway.
-rw-r--r-- | netifaces.c | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/netifaces.c b/netifaces.c index f935599..308083f 100644 --- a/netifaces.c +++ b/netifaces.c @@ -837,6 +837,7 @@ ifaddrs (PyObject *self, PyObject *args) if (!addr->ifa_addr) continue; +#if HAVE_IPV6_SOCKET_IOCTLS /* For IPv6 addresses we try to get the flags. */ if (addr->ifa_addr->sa_family == AF_INET6) { struct sockaddr_in6 *sin; @@ -861,6 +862,7 @@ ifaddrs (PyObject *self, PyObject *args) close (sock6); } +#endif /* HAVE_IPV6_SOCKET_IOCTLS */ if (string_from_sockaddr (addr->ifa_addr, buffer, sizeof (buffer)) == 0) pyaddr = PyString_FromString (buffer); @@ -1512,6 +1514,9 @@ gateways (PyObject *self) int bufsize = pagesize < 8192 ? pagesize : 8192; int is_multi = 0; int interrupted = 0; + int def_priorities[RTNL_FAMILY_MAX]; + + memset(def_priorities, 0xff, sizeof(def_priorities)); result = PyDict_New(); defaults = PyDict_New(); @@ -1621,6 +1626,7 @@ gateways (PyObject *self) int ifndx = -1; struct rtattr *attrs, *attr; int len; + int priority; /* Ignore messages not for us */ if (pmsg->hdr.nlmsg_seq != seq || pmsg->hdr.nlmsg_pid != sanl.nl_pid) @@ -1655,7 +1661,7 @@ gateways (PyObject *self) attr = attrs = RTM_RTA(&pmsg->rt); len = RTM_PAYLOAD(&pmsg->hdr); - + priority = -1; while (RTA_OK(attr, len)) { switch (attr->rta_type) { case RTA_GATEWAY: @@ -1667,6 +1673,9 @@ gateways (PyObject *self) case RTA_OIF: ifndx = *(int *)RTA_DATA(attr); break; + case RTA_PRIORITY: + priority = *(int *)RTA_DATA(attr); + break; default: break; } @@ -1700,6 +1709,19 @@ gateways (PyObject *self) routing tables on Linux. */ isdefault = pmsg->rt.rtm_table == RT_TABLE_MAIN ? Py_True : Py_False; + + /* Try to pick the active default route based on priority (which + is displayed in the UI as "metric", confusingly) */ + if (pmsg->rt.rtm_family < RTNL_FAMILY_MAX) { + if (def_priorities[pmsg->rt.rtm_family] == -1) + def_priorities[pmsg->rt.rtm_family] = priority; + else { + if (priority == -1 + || priority > def_priorities[pmsg->rt.rtm_family]) + isdefault = Py_False; + } + } + pyifname = PyString_FromString (ifname); pyaddr = PyString_FromString (buffer); |