2023-05-04 23:19:43 +02:00
|
|
|
import ipaddress
|
2023-05-05 10:25:15 +02:00
|
|
|
import RNS.vendor.ifaddr
|
2023-05-04 17:46:56 +02:00
|
|
|
import socket
|
|
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
AF_INET6 = socket.AF_INET6.value
|
|
|
|
AF_INET = socket.AF_INET.value
|
|
|
|
|
|
|
|
def interfaces() -> List[str]:
|
2023-05-05 10:37:22 +02:00
|
|
|
adapters = RNS.vendor.ifaddr.get_adapters(include_unconfigured=True)
|
2023-05-04 17:46:56 +02:00
|
|
|
return [a.name for a in adapters]
|
|
|
|
|
2024-05-16 18:09:11 +02:00
|
|
|
def interface_names_to_indexes() -> dict:
|
|
|
|
adapters = RNS.vendor.ifaddr.get_adapters(include_unconfigured=True)
|
|
|
|
results = {}
|
|
|
|
for adapter in adapters:
|
|
|
|
results[adapter.name] = adapter.index
|
|
|
|
return results
|
|
|
|
|
2024-05-17 23:54:48 +02:00
|
|
|
def interface_name_to_nice_name(ifname) -> str:
|
|
|
|
try:
|
|
|
|
adapters = RNS.vendor.ifaddr.get_adapters(include_unconfigured=True)
|
|
|
|
for adapter in adapters:
|
|
|
|
if adapter.name == ifname:
|
|
|
|
if hasattr(adapter, "nice_name"):
|
|
|
|
return adapter.nice_name
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2023-05-04 17:46:56 +02:00
|
|
|
def ifaddresses(ifname) -> dict:
|
2023-05-05 10:37:22 +02:00
|
|
|
adapters = RNS.vendor.ifaddr.get_adapters(include_unconfigured=True)
|
2023-05-04 17:46:56 +02:00
|
|
|
ifa = {}
|
|
|
|
for a in adapters:
|
|
|
|
if a.name == ifname:
|
|
|
|
ipv4s = []
|
|
|
|
ipv6s = []
|
|
|
|
for ip in a.ips:
|
|
|
|
t = {}
|
|
|
|
if ip.is_IPv4:
|
2023-05-04 23:19:43 +02:00
|
|
|
net = ipaddress.ip_network(str(ip.ip)+"/"+str(ip.network_prefix), strict=False)
|
2023-05-04 17:46:56 +02:00
|
|
|
t["addr"] = ip.ip
|
2023-05-04 23:19:43 +02:00
|
|
|
t["prefix"] = ip.network_prefix
|
|
|
|
t["broadcast"] = str(net.broadcast_address)
|
2023-05-04 17:46:56 +02:00
|
|
|
ipv4s.append(t)
|
|
|
|
if ip.is_IPv6:
|
|
|
|
t["addr"] = ip.ip[0]
|
|
|
|
ipv6s.append(t)
|
|
|
|
|
|
|
|
if len(ipv4s) > 0:
|
|
|
|
ifa[AF_INET] = ipv4s
|
|
|
|
if len(ipv6s) > 0:
|
|
|
|
ifa[AF_INET6] = ipv6s
|
|
|
|
|
|
|
|
return ifa
|