diff options
author | Yuri Sevatz <yuri_sevatz@hotmail.com> | 2020-12-17 06:56:08 -0500 |
---|---|---|
committer | Yuri Sevatz <yuri_sevatz@hotmail.com> | 2020-12-17 06:56:08 -0500 |
commit | 65f8f26ed892f16011970ff19f075b7a288299dc (patch) | |
tree | dd5d6dbb745cf8c97c6bb230c1d0498941a19e0f | |
parent | 4bd4d2b2927c87b65de915d42ebd7c082ae13ff8 (diff) | |
download | netifaces-git-65f8f26ed892f16011970ff19f075b7a288299dc.tar.gz |
Interpret missing gateway priority, handle some out of order metrics
-rw-r--r-- | netifaces.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/netifaces.c b/netifaces.c index ffaf8c8..839c42c 100644 --- a/netifaces.c +++ b/netifaces.c @@ -1927,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); |