Skip to content

Commit

Permalink
Replace use of netifaces (compiled, unmaintained) with ifaddr (pure-p…
Browse files Browse the repository at this point in the history
…ython, maintained)
  • Loading branch information
oysstu committed Apr 7, 2022
1 parent 899934b commit bd2f417
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
33 changes: 14 additions & 19 deletions imcpy/network/utils.py
Original file line number Diff line number Diff line change
@@ -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
pass


10 changes: 5 additions & 5 deletions imcpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit bd2f417

Please sign in to comment.