Skip to content

Commit a509e5a

Browse files
authored
669 windows broadcast addr (#2501)
1 parent d4b37d9 commit a509e5a

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

HISTORY.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ XXXX-XX-XX
77

88
**Enhancements**
99

10+
- 669_, [Windows]: `net_if_addrs()`_ also returns the ``broadcast`` address
11+
instead of ``None``.
1012
- 2480_: Python 2.7 is no longer supported. Latest version supporting Python
1113
2.7 is psutil 6.1.X. Install it with: ``pip2 install psutil==6.1.*``.
1214
- 2490_: removed long deprecated ``Process.memory_info_ex()`` method. It was
@@ -15,7 +17,7 @@ XXXX-XX-XX
1517

1618
**Bug fixes**
1719

18-
- 2496_, [Linux]: Avoid segfault (a cPython bug) on ``Process.memory_maps()``
20+
- 2496_, [Linux]: Avoid segfault (a cPython bug) on ``Process.memory_maps()``
1921
for processes that use hundreds of GBs of memory.
2022

2123
**Compatibility notes**

docs/index.rst

+3
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,9 @@ Network
734734
.. versionchanged:: 4.4.0 added support for *netmask* field on Windows which
735735
is no longer ``None``.
736736

737+
.. versionchanged:: 7.0.0 added support for *broadcast* field on Windows
738+
which is no longer ``None``.
739+
737740
.. function:: net_if_stats()
738741

739742
Return information about each NIC (network interface card) installed on the

psutil/__init__.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -2228,14 +2228,30 @@ def net_if_addrs():
22282228
# We re-set the family here so that repr(family)
22292229
# will show AF_LINK rather than AF_PACKET
22302230
fam = _psplatform.AF_LINK
2231+
22312232
if fam == _psplatform.AF_LINK:
22322233
# The underlying C function may return an incomplete MAC
22332234
# address in which case we fill it with null bytes, see:
22342235
# https://github.com/giampaolo/psutil/issues/786
22352236
separator = ":" if POSIX else "-"
22362237
while addr.count(separator) < 5:
22372238
addr += f"{separator}00"
2238-
ret[name].append(_common.snicaddr(fam, addr, mask, broadcast, ptp))
2239+
2240+
nt = _common.snicaddr(fam, addr, mask, broadcast, ptp)
2241+
2242+
# On Windows broadcast is None, so we determine it via
2243+
# ipaddress module.
2244+
if WINDOWS and fam in {socket.AF_INET, socket.AF_INET6}:
2245+
try:
2246+
broadcast = _common.broadcast_addr(nt)
2247+
except Exception as err: # noqa: BLE001
2248+
debug(err)
2249+
else:
2250+
if broadcast is not None:
2251+
nt._replace(broadcast=broadcast)
2252+
2253+
ret[name].append(nt)
2254+
22392255
return dict(ret)
22402256

22412257

psutil/_common.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5-
"""Common objects shared by __init__.py and _ps*.py modules."""
6-
7-
# Note: this module is imported by setup.py so it should not import
8-
# psutil or third-party modules.
5+
"""Common objects shared by __init__.py and _ps*.py modules.
96
7+
Note: this module is imported by setup.py, so it should not import
8+
psutil or third-party modules.
9+
"""
1010

1111
import collections
1212
import enum
@@ -603,6 +603,28 @@ def conn_to_ntuple(fd, fam, type_, laddr, raddr, status, status_map, pid=None):
603603
return sconn(fd, fam, type_, laddr, raddr, status, pid)
604604

605605

606+
def broadcast_addr(addr):
607+
"""Given the address ntuple returned by ``net_if_addrs()``
608+
calculates the broadcast address.
609+
"""
610+
import ipaddress
611+
612+
if not addr.address or not addr.netmask:
613+
return None
614+
if addr.family == socket.AF_INET:
615+
return str(
616+
ipaddress.IPv4Network(
617+
f"{addr.address}/{addr.netmask}", strict=False
618+
).broadcast_address
619+
)
620+
if addr.family == socket.AF_INET6:
621+
return str(
622+
ipaddress.IPv6Network(
623+
f"{addr.address}/{addr.netmask}", strict=False
624+
).broadcast_address
625+
)
626+
627+
606628
def deprecated_method(replacement):
607629
"""A decorator which can be used to mark a method as deprecated
608630
'replcement' is the method name which will be called instead.

psutil/tests/test_system.py

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from psutil import POSIX
3131
from psutil import SUNOS
3232
from psutil import WINDOWS
33+
from psutil._common import broadcast_addr
3334
from psutil.tests import ASCII_FS
3435
from psutil.tests import CI_TESTING
3536
from psutil.tests import GITHUB_ACTIONS
@@ -858,6 +859,14 @@ def test_net_if_addrs(self):
858859
elif addr.ptp:
859860
assert addr.broadcast is None
860861

862+
# check broadcast address
863+
if (
864+
addr.broadcast
865+
and addr.netmask
866+
and addr.family in {socket.AF_INET, socket.AF_INET6}
867+
):
868+
assert addr.broadcast == broadcast_addr(addr)
869+
861870
if BSD or MACOS or SUNOS:
862871
if hasattr(socket, "AF_LINK"):
863872
assert psutil.AF_LINK == socket.AF_LINK

0 commit comments

Comments
 (0)