Skip to content

Commit

Permalink
chore: improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ntamas committed Jul 3, 2024
1 parent 61166c3 commit f282188
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/flockwave/networking/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""Generic networking-related utility functions."""

from ipaddress import ip_address, ip_network, IPv6Address, IPv6Network
from ipaddress import (
ip_address,
ip_network,
IPv4Network,
IPv6Address,
IPv6Network,
)
from netifaces import AF_INET, AF_INET6, AF_LINK, ifaddresses, interfaces
from typing import Sequence
from typing import Optional, Sequence, Union

from .addressing import canonicalize_mac_address

Expand All @@ -17,7 +23,9 @@
)


def find_interfaces_with_address(address: str) -> Sequence[tuple[str, str]]:
def find_interfaces_with_address(
address: str,
) -> Sequence[tuple[str, Union[IPv4Network, IPv6Network]]]:
"""Finds the network interfaces of the current machine that contain the given
address in their network.
Expand All @@ -29,27 +37,29 @@ def find_interfaces_with_address(address: str) -> Sequence[tuple[str, str]]:
belongs to the given network, the name of the network interface itself and
the network of the interface, in a tuple
"""
address = ip_address(address)
address_obj = ip_address(address)
if isinstance(address, IPv6Address):
family = AF_INET6
else:
family = AF_INET

candidates = []
candidates: list[tuple[str, Union[IPv4Network, IPv6Network]]] = []
for interface in interfaces():
specs = ifaddresses(interface).get(family) or []
ip_addresses_in_network = (
(spec.get("addr"), spec.get("netmask")) for spec in specs
)
for if_address, netmask in ip_addresses_in_network:
network = ip_network(f"{if_address}/{netmask}", strict=False)
if address in network:
if address_obj in network:
candidates.append((interface, network))

return candidates


def find_interfaces_in_network(network: str) -> Sequence[tuple[str, str, str]]:
def find_interfaces_in_network(
network: str,
) -> Sequence[tuple[str, str, Optional[str]]]:
"""Finds the network interfaces of the current machine that have at
least one address that belongs to the given network.
Expand All @@ -62,19 +72,19 @@ def find_interfaces_in_network(network: str) -> Sequence[tuple[str, str, str]]:
itself, the matched address and the network of the interface, in
a tuple
"""
network = ip_network(network)
if isinstance(network, IPv6Network):
network_obj = ip_network(network)
if isinstance(network_obj, IPv6Network):
family = AF_INET6
else:
family = AF_INET

candidates = []
candidates: list[tuple[str, str, Optional[str]]] = []
for interface in interfaces():
specs = ifaddresses(interface).get(family) or []
ip_addresses_in_network = (
(spec.get("addr"), spec.get("netmask"))
(spec.get("addr") or "", spec.get("netmask"))
for spec in specs
if ip_address(str(spec.get("addr"))) in network
if ip_address(str(spec.get("addr"))) in network_obj
)
for address, netmask in ip_addresses_in_network:
candidates.append(
Expand Down

0 comments on commit f282188

Please sign in to comment.