diff --git a/imcpy/network/utils.py b/imcpy/network/utils.py index fcea310..3cdba07 100644 --- a/imcpy/network/utils.py +++ b/imcpy/network/utils.py @@ -1,23 +1,18 @@ -import netifaces +import ifaddr +from typing import List, Tuple - -def get_interfaces(ignore_local=True): - """ - Retrieves the address of all external interfaces (lo 127.0.0.1 ignored) - :return: List of tuples (interface, addr) - """ - interfaces = netifaces.interfaces() - if_ext = [] - for i in interfaces: - if i == 'lo' and ignore_local: - continue - iface = netifaces.ifaddresses(i).get(netifaces.AF_INET) - if iface: - for j in iface: - if_ext.append((i, j['addr'], j['netmask'])) - - return if_ext +def get_interfaces(ignore_local=True, only_ipv4=True) -> List[Tuple[str, str, int]]: + ifaces = [] + for adapter in ifaddr.get_adapters(): + if adapter.name != 'lo' or not ignore_local: + for ip in adapter.ips: + if type(ip.ip) is str or not only_ipv4: + ifaces.append((adapter.name, ip.ip, ip.network_prefix)) + + return ifaces if __name__ == '__main__': - pass \ No newline at end of file + pass + + \ No newline at end of file diff --git a/imcpy/node.py b/imcpy/node.py index 1ef1d19..7c8569c 100644 --- a/imcpy/node.py +++ b/imcpy/node.py @@ -2,7 +2,7 @@ import ipaddress as ip from urllib.parse import urlparse import time -from typing import Dict +from typing import Dict, List, Optional from imcpy.network.udp import IMCSenderUDP from imcpy.network.utils import get_interfaces @@ -69,7 +69,7 @@ def __init__(self, src, sys_name, service_filter=None, is_fixed=False): # Unparsed services string self.services_string = '' # type: str # Parsed services - self.services = {} # type: Dict[str, IMCService] + self.services = {} # type: Dict[str, List[IMCService]] # Parsed entities self.entities = {} # type: Dict[str, int] # Node arguments @@ -82,9 +82,9 @@ def __init__(self, src, sys_name, service_filter=None, is_fixed=False): # # Time of last heartbeat - self.t_last_heartbeat = None # type: float + self.t_last_heartbeat = None # type: Optional[float] # Time of last announce - self.t_last_announce = None # type: float + self.t_last_announce = None # type: Optional[float] @property def name(self): @@ -153,7 +153,7 @@ def send(self, msg, log_fh=None): # Determine which service to send to based on ip/netmask # Note: this might not account for funky ip routing - networks = [ip.ip_interface(x[1] + '/' + x[2]).network for x in get_interfaces(ignore_local=True)] + networks = [ip.IPv4Network((addr, mask), strict=False) for name, addr, mask in get_interfaces(ignore_local=True, only_ipv4=True)] for svc in imcudp_services: svc_ip = ip.ip_address(svc.ip) diff --git a/setup.py b/setup.py index fc51cde..98504fc 100644 --- a/setup.py +++ b/setup.py @@ -153,7 +153,7 @@ def build_extension(self, ext): 'imcpy.network', 'utils'], python_requires='>=3.6', - install_requires=['netifaces'], + install_requires=['ifaddr'], extras_require={'LSFExporter': ['pandas']}, package_data={'': ['_imcpy.pyi'], 'imcpy.coordinates': ['*.pyi'],