summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Houghton <alastair@alastairs-place.net>2021-05-16 12:56:42 +0100
committerGitHub <noreply@github.com>2021-05-16 12:56:42 +0100
commit5228014d21b8bd51e425e7055f2bddd9a0a3036e (patch)
treedd5d6dbb745cf8c97c6bb230c1d0498941a19e0f
parent8b0d75750543453cdc8680bc52d4240eb4de2636 (diff)
parent65f8f26ed892f16011970ff19f075b7a288299dc (diff)
downloadnetifaces-git-5228014d21b8bd51e425e7055f2bddd9a0a3036e.tar.gz
Merge pull request #71 from yuri-sevatz/develop
[NETLINK] Add support for empty destination, and empty metric
-rw-r--r--netifaces.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/netifaces.c b/netifaces.c
index bf18c80..839c42c 100644
--- a/netifaces.c
+++ b/netifaces.c
@@ -1894,8 +1894,14 @@ gateways (PyObject *self)
attr = RTA_NEXT(attr, len);
}
+ static const unsigned char ipv4_default[4] = {};
+ static const unsigned char ipv6_default[16] = {};
+
/* We're looking for gateways with no destination */
- if (!dst && gw && ifndx >= 0) {
+ if ((!dst
+ || (pmsg->rt.rtm_family == AF_INET && !memcmp(dst, ipv4_default, sizeof(ipv4_default)))
+ || (pmsg->rt.rtm_family == AF_INET6 && !memcmp(dst, ipv6_default, sizeof(ipv6_default)))
+ ) && gw && ifndx >= 0) {
char buffer[256];
char ifnamebuf[IF_NAMESIZE];
char *ifname;
@@ -1921,16 +1927,22 @@ gateways (PyObject *self)
isdefault = pmsg->rt.rtm_table == RT_TABLE_MAIN ? Py_True : Py_False;
+ /* Priority starts at 0, having none means we use kernel default (0) */
+ if (priority < 0) {
+ priority = 0;
+ }
+
/* 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)
+ /* If no active default route found, or metric is lower */
+ if (def_priorities[pmsg->rt.rtm_family] == -1
+ || priority < def_priorities[pmsg->rt.rtm_family])
+ /* Set new default */
def_priorities[pmsg->rt.rtm_family] = priority;
- else {
- if (priority == -1
- || priority > def_priorities[pmsg->rt.rtm_family])
- isdefault = Py_False;
- }
+ else
+ /* Leave default, but unset isdefault for iface tuple */
+ isdefault = Py_False;
}
pyifname = PyUnicode_FromString (ifname);