summaryrefslogtreecommitdiff
path: root/psutil
diff options
context:
space:
mode:
Diffstat (limited to 'psutil')
-rw-r--r--psutil/__init__.py15
-rw-r--r--psutil/_common.py2
-rw-r--r--psutil/_psutil_posix.c33
3 files changed, 40 insertions, 10 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 79b369c9..385b80a4 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1749,17 +1749,22 @@ def net_if_addrs():
"""Return the addresses associated to each NIC (network interface
card) installed on the system as a dictionary whose keys are the
NIC names and value is a list of namedtuples for each address
- assigned to the NIC. Each namedtuple includes 4 fields:
+ assigned to the NIC. Each namedtuple includes 5 fields:
- family
- address
- netmask
- broadcast
+ - ptp
'family' can be either socket.AF_INET, socket.AF_INET6 or
psutil.AF_LINK, which refers to a MAC address.
- 'address' is the primary address, 'netmask' and 'broadcast'
- may be None.
+ 'address' is the primary address and it is always set.
+ 'netmask' and 'broadcast' and 'ptp' may be None.
+ 'ptp' stands for "point to point" and references the destination
+ address on a point to point interface (tipically a VPN).
+ 'broadcast' and 'ptp' are mutually exclusive.
+
Note: you can have more than one address of the same family
associated with each interface.
"""
@@ -1769,7 +1774,7 @@ def net_if_addrs():
rawlist = _psplatform.net_if_addrs()
rawlist.sort(key=lambda x: x[1]) # sort by family
ret = collections.defaultdict(list)
- for name, fam, addr, mask, broadcast in rawlist:
+ for name, fam, addr, mask, broadcast, ptp in rawlist:
if has_enums:
try:
fam = socket.AddressFamily(fam)
@@ -1782,7 +1787,7 @@ def net_if_addrs():
# We re-set the family here so that repr(family)
# will show AF_LINK rather than AF_PACKET
fam = _psplatform.AF_LINK
- ret[name].append(_common.snic(fam, addr, mask, broadcast))
+ ret[name].append(_common.snic(fam, addr, mask, broadcast, ptp))
return dict(ret)
diff --git a/psutil/_common.py b/psutil/_common.py
index e9acf595..9f5c06f2 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -215,7 +215,7 @@ suser = namedtuple('suser', ['name', 'terminal', 'host', 'started'])
sconn = namedtuple('sconn', ['fd', 'family', 'type', 'laddr', 'raddr',
'status', 'pid'])
# psutil.net_if_addrs()
-snic = namedtuple('snic', ['family', 'address', 'netmask', 'broadcast'])
+snic = namedtuple('snic', ['family', 'address', 'netmask', 'broadcast', 'ptp'])
# psutil.net_if_stats()
snicstats = namedtuple('snicstats', ['isup', 'duplex', 'speed', 'mtu'])
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c
index 183dab0e..5967df0c 100644
--- a/psutil/_psutil_posix.c
+++ b/psutil/_psutil_posix.c
@@ -17,6 +17,7 @@
#ifdef __linux
#include <netdb.h>
#include <linux/if_packet.h>
+#include <linux/if.h>
#endif // end linux
#if defined(__FreeBSD__) || defined(__APPLE__)
@@ -163,6 +164,7 @@ psutil_net_if_addrs(PyObject* self, PyObject* args)
PyObject *py_address = NULL;
PyObject *py_netmask = NULL;
PyObject *py_broadcast = NULL;
+ PyObject *py_ptp = NULL;
if (py_retlist == NULL)
return NULL;
@@ -185,20 +187,41 @@ psutil_net_if_addrs(PyObject* self, PyObject* args)
py_netmask = psutil_convert_ipaddr(ifa->ifa_netmask, family);
if (py_netmask == NULL)
goto error;
+
#ifdef __linux
- py_broadcast = psutil_convert_ipaddr(ifa->ifa_ifu.ifu_broadaddr, family);
+ if (ifa->ifa_flags & IFF_BROADCAST) {
+ py_broadcast = psutil_convert_ipaddr(ifa->ifa_broadaddr, family);
+ Py_INCREF(Py_None);
+ py_ptp = Py_None;
+ }
+ else if (ifa->ifa_flags & IFF_POINTOPOINT) {
+ py_ptp = psutil_convert_ipaddr(ifa->ifa_dstaddr, family);
+ Py_INCREF(Py_None);
+ py_broadcast = Py_None;
+ }
+ else {
+ Py_INCREF(Py_None);
+ Py_INCREF(Py_None);
+ py_broadcast = Py_None;
+ py_ptp = Py_None;
+ }
#else
+ // TODO
py_broadcast = psutil_convert_ipaddr(ifa->ifa_broadaddr, family);
+ Py_INCREF(Py_None);
+ py_ptp = Py_None;
#endif
- if (py_broadcast == NULL)
+
+ if ((py_broadcast == NULL) || (py_ptp == NULL))
goto error;
py_tuple = Py_BuildValue(
- "(siOOO)",
+ "(siOOOO)",
ifa->ifa_name,
family,
py_address,
py_netmask,
- py_broadcast
+ py_broadcast,
+ py_ptp
);
if (! py_tuple)
@@ -209,6 +232,7 @@ psutil_net_if_addrs(PyObject* self, PyObject* args)
Py_DECREF(py_address);
Py_DECREF(py_netmask);
Py_DECREF(py_broadcast);
+ Py_DECREF(py_ptp);
}
freeifaddrs(ifaddr);
@@ -222,6 +246,7 @@ error:
Py_XDECREF(py_address);
Py_XDECREF(py_netmask);
Py_XDECREF(py_broadcast);
+ Py_XDECREF(py_ptp);
return NULL;
}