Commit 69c97017 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾 Committed by GitHub

Config.py: Use some interfaces even if other fail (#12)

Allow to run PIM, IGMP or MLD on working interfaces even if it failed for other
interfaces listed in the configuration file.
parent 939e35ba
import yaml
import sys, yaml
from functools import partial
from pimdm.tree import pim_globals
from igmp.igmp2 import igmp_globals
from mld.mld1 import mld_globals
......@@ -34,18 +35,22 @@ def parse_config_file(file_path):
pim_globals.T_LIMIT = pim_config["DefaultTimers"].get("T_LIMIT", pim_globals.T_LIMIT)
if "Interfaces" in pim_config:
interfaces = pim_config["Interfaces"] # type: dict
for if_name, if_value in interfaces.items():
if if_value.get("ipv4", False):
if_ipv4 = if_value["ipv4"]
if if_ipv4.get("enabled", False):
Main.add_pim_interface(interface_name=if_name, state_refresh_capable=if_ipv4.get("state_refresh", False), ipv4=True, ipv6=False)
if if_value.get("ipv6", False):
if_ipv6 = if_value["ipv6"]
if if_ipv6.get("enabled", False):
Main.add_pim_interface(interface_name=if_name, state_refresh_capable=if_ipv6.get("state_refresh", False), ipv4=False, ipv6=True)
interface_dict = pim_config["Interfaces"]
add_pim_interface_dict = {
'ipv4': partial(Main.add_pim_interface, ipv4=True, ipv6=False),
'ipv6': partial(Main.add_pim_interface, ipv4=False, ipv6=True),
}
for if_name, ip_family_dict in interface_dict.items():
for ip_family, if_dict in ip_family_dict.items():
if if_dict.get("enabled", False):
try:
add_pim_interface_dict[ip_family](
interface_name=if_name,
state_refresh_capable=if_dict.get("state_refresh", False),
)
except Exception as e:
print(e, file=sys.stderr)
##### IGMP config #######
......@@ -65,11 +70,14 @@ def parse_config_file(file_path):
igmp_globals.VERSION_1_ROUTER_PRESENT_TIMEOUT = igmp_config["Settings"].get("VERSION_1_ROUTER_PRESENT_TIMEOUT", igmp_globals.VERSION_1_ROUTER_PRESENT_TIMEOUT)
if "Interfaces" in igmp_config:
interfaces = igmp_config["Interfaces"] # type: dict
interface_dict = igmp_config["Interfaces"]
for if_name, if_value in interfaces.items():
if if_value.get("enabled", False):
Main.add_membership_interface(interface_name=if_name, ipv4=True, ipv6=False)
for if_name, if_value in interface_dict.items():
try:
if if_value.get("enabled", False):
Main.add_membership_interface(interface_name=if_name, ipv4=True, ipv6=False)
except Exception as e:
print(e, file=sys.stderr)
##### MLD config #######
if "Settings" in mld_config:
......@@ -85,11 +93,14 @@ def parse_config_file(file_path):
mld_globals.UNSOLICITED_REPORT_INTERVAL = mld_config["Settings"].get("UNSOLICITED_REPORT_INTERVAL", mld_globals.UNSOLICITED_REPORT_INTERVAL)
if "Interfaces" in mld_config:
interfaces = mld_config["Interfaces"] # type: dict
for if_name, if_value in interfaces.items():
if if_value.get("enabled", False):
Main.add_membership_interface(interface_name=if_name, ipv4=False, ipv6=True)
interface_dict = mld_config["Interfaces"]
for if_name, if_value in interface_dict.items():
try:
if if_value.get("enabled", False):
Main.add_membership_interface(interface_name=if_name, ipv4=False, ipv6=True)
except Exception as e:
print(e, file=sys.stderr)
def get_yaml_file():
......
......@@ -54,7 +54,11 @@ class InterfacePim(Interface):
self.interface_logger = logging.LoggerAdapter(InterfacePim.LOGGER, {'vif': vif_index, 'interfacename': interface_name})
# SOCKET
ip_interface = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
if_addr_dict = netifaces.ifaddresses(interface_name)
if not netifaces.AF_INET in if_addr_dict:
raise Exception("Adding PIM interface failed because %s does not "
"have any ipv4 address" % interface_name)
ip_interface = if_addr_dict[netifaces.AF_INET][0]['addr']
self.ip_interface = ip_interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_PIM)
......
......@@ -46,12 +46,16 @@ class InterfacePim6(InterfacePim):
s = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_PIM)
ip_interface = ""
for if_addr in netifaces.ifaddresses(interface_name)[netifaces.AF_INET6]:
ip_interface = if_addr["addr"]
if ipaddress.IPv6Address(if_addr['addr'].split("%")[0]).is_link_local:
ip_interface = if_addr['addr'].split("%")[0]
if_addr_dict = netifaces.ifaddresses(interface_name)
if not netifaces.AF_INET6 in if_addr_dict:
raise Exception("Adding PIM interface failed because %s does not "
"have any ipv6 address" % interface_name)
for network_dict in if_addr_dict[netifaces.AF_INET6]:
full_ip_interface = network_dict["addr"]
ip_interface = full_ip_interface.split("%")[0]
if ipaddress.IPv6Address(ip_interface).is_link_local:
# bind to interface
s.bind(socket.getaddrinfo(if_addr['addr'], None, 0, socket.SOCK_RAW, 0, socket.AI_PASSIVE)[0][4])
s.bind(socket.getaddrinfo(full_ip_interface, None, 0, socket.SOCK_RAW, 0, socket.AI_PASSIVE)[0][4])
break
self.ip_interface = ip_interface
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment