pygui: fixed wireless nodes not linking with full netmasks, fixed assigning addresses for nodes connected to other nodes that may have had a full netmask

This commit is contained in:
Blake Harnden 2021-01-14 13:28:06 -08:00
parent 2b171631c7
commit 886b56cf8c
3 changed files with 59 additions and 55 deletions

View file

@ -24,7 +24,6 @@ from core.api.grpc.wrappers import (
ConfigOption, ConfigOption,
ConfigService, ConfigService,
ExceptionEvent, ExceptionEvent,
Interface,
Link, Link,
LinkEvent, LinkEvent,
LinkType, LinkType,
@ -838,50 +837,6 @@ class CoreClient:
links.append(edge.link) links.append(edge.link)
self.ifaces_manager.removed(links) self.ifaces_manager.removed(links)
def create_iface(self, canvas_node: CanvasNode) -> Interface:
node = canvas_node.core_node
ip4, ip6 = self.ifaces_manager.get_ips(node)
ip4_mask = self.ifaces_manager.ip4_mask
ip6_mask = self.ifaces_manager.ip6_mask
iface_id = canvas_node.next_iface_id()
name = f"eth{iface_id}"
iface = Interface(
id=iface_id,
name=name,
ip4=ip4,
ip4_mask=ip4_mask,
ip6=ip6,
ip6_mask=ip6_mask,
)
logging.info("create node(%s) interface(%s)", node.name, iface)
return iface
def create_link(
self, edge: CanvasEdge, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode
) -> Link:
"""
Create core link for a pair of canvas nodes, with token referencing
the canvas edge.
"""
src_node = canvas_src_node.core_node
dst_node = canvas_dst_node.core_node
self.ifaces_manager.determine_subnets(canvas_src_node, canvas_dst_node)
src_iface = None
if NodeUtils.is_container_node(src_node.type):
src_iface = self.create_iface(canvas_src_node)
dst_iface = None
if NodeUtils.is_container_node(dst_node.type):
dst_iface = self.create_iface(canvas_dst_node)
link = Link(
type=LinkType.WIRED,
node1_id=src_node.id,
node2_id=dst_node.id,
iface1=src_iface,
iface2=dst_iface,
)
logging.info("added link between %s and %s", src_node.name, dst_node.name)
return link
def save_edge( def save_edge(
self, edge: CanvasEdge, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode self, edge: CanvasEdge, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode
) -> None: ) -> None:

View file

@ -369,7 +369,7 @@ class CanvasManager:
src = edge.src src = edge.src
edge.complete(dst) edge.complete(dst)
if link is None: if link is None:
link = self.core.create_link(edge, src, dst) link = self.core.ifaces_manager.create_link(edge)
edge.link = link edge.link = link
if link.iface1: if link.iface1:
iface1 = link.iface1 iface1 = link.iface1

View file

@ -4,13 +4,19 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple
import netaddr import netaddr
from netaddr import EUI, IPNetwork from netaddr import EUI, IPNetwork
from core.api.grpc.wrappers import Interface, Link, Node from core.api.grpc.wrappers import Interface, Link, LinkType, Node
from core.gui.graph.edges import CanvasEdge
from core.gui.graph.node import CanvasNode from core.gui.graph.node import CanvasNode
from core.gui.nodeutils import NodeUtils from core.gui.nodeutils import NodeUtils
if TYPE_CHECKING: if TYPE_CHECKING:
from core.gui.app import Application from core.gui.app import Application
IP4_MASK: int = 24
IP6_MASK: int = 64
WIRELESS_IP4_MASK: int = 32
WIRELESS_IP6_MASK: int = 128
def get_index(iface: Interface) -> Optional[int]: def get_index(iface: Interface) -> Optional[int]:
if not iface.ip4: if not iface.ip4:
@ -47,10 +53,8 @@ class InterfaceManager:
self.app: "Application" = app self.app: "Application" = app
ip4 = self.app.guiconfig.ips.ip4 ip4 = self.app.guiconfig.ips.ip4
ip6 = self.app.guiconfig.ips.ip6 ip6 = self.app.guiconfig.ips.ip6
self.ip4_mask: int = 24 self.ip4_subnets: IPNetwork = IPNetwork(f"{ip4}/{IP4_MASK}")
self.ip6_mask: int = 64 self.ip6_subnets: IPNetwork = IPNetwork(f"{ip6}/{IP6_MASK}")
self.ip4_subnets: IPNetwork = IPNetwork(f"{ip4}/{self.ip4_mask}")
self.ip6_subnets: IPNetwork = IPNetwork(f"{ip6}/{self.ip6_mask}")
mac = self.app.guiconfig.mac mac = self.app.guiconfig.mac
self.mac: EUI = EUI(mac, dialect=netaddr.mac_unix_expanded) self.mac: EUI = EUI(mac, dialect=netaddr.mac_unix_expanded)
self.current_mac: Optional[EUI] = None self.current_mac: Optional[EUI] = None
@ -60,8 +64,8 @@ class InterfaceManager:
def update_ips(self, ip4: str, ip6: str) -> None: def update_ips(self, ip4: str, ip6: str) -> None:
self.reset() self.reset()
self.ip4_subnets = IPNetwork(f"{ip4}/{self.ip4_mask}") self.ip4_subnets = IPNetwork(f"{ip4}/{IP4_MASK}")
self.ip6_subnets = IPNetwork(f"{ip6}/{self.ip6_mask}") self.ip6_subnets = IPNetwork(f"{ip6}/{IP6_MASK}")
def next_mac(self) -> str: def next_mac(self) -> str:
while str(self.current_mac) in self.used_macs: while str(self.current_mac) in self.used_macs:
@ -163,10 +167,10 @@ class InterfaceManager:
def get_subnets(self, iface: Interface) -> Subnets: def get_subnets(self, iface: Interface) -> Subnets:
ip4_subnet = self.ip4_subnets ip4_subnet = self.ip4_subnets
if iface.ip4: if iface.ip4:
ip4_subnet = IPNetwork(f"{iface.ip4}/{iface.ip4_mask}").cidr ip4_subnet = IPNetwork(f"{iface.ip4}/{IP4_MASK}").cidr
ip6_subnet = self.ip6_subnets ip6_subnet = self.ip6_subnets
if iface.ip6: if iface.ip6:
ip6_subnet = IPNetwork(f"{iface.ip6}/{iface.ip6_mask}").cidr ip6_subnet = IPNetwork(f"{iface.ip6}/{IP6_MASK}").cidr
subnets = Subnets(ip4_subnet, ip6_subnet) subnets = Subnets(ip4_subnet, ip6_subnet)
return self.used_subnets.get(subnets.key(), subnets) return self.used_subnets.get(subnets.key(), subnets)
@ -219,3 +223,48 @@ class InterfaceManager:
logging.info("found subnets: %s", subnets) logging.info("found subnets: %s", subnets)
break break
return subnets return subnets
def create_link(self, edge: CanvasEdge) -> Link:
"""
Create core link for a given edge based on src/dst nodes.
"""
src_node = edge.src.core_node
dst_node = edge.dst.core_node
self.determine_subnets(edge.src, edge.dst)
src_iface = None
if NodeUtils.is_container_node(src_node.type):
src_iface = self.create_iface(edge.src, edge.linked_wireless)
dst_iface = None
if NodeUtils.is_container_node(dst_node.type):
dst_iface = self.create_iface(edge.dst, edge.linked_wireless)
link = Link(
type=LinkType.WIRED,
node1_id=src_node.id,
node2_id=dst_node.id,
iface1=src_iface,
iface2=dst_iface,
)
logging.info("added link between %s and %s", src_node.name, dst_node.name)
return link
def create_iface(self, canvas_node: CanvasNode, wireless_link: bool) -> Interface:
node = canvas_node.core_node
ip4, ip6 = self.get_ips(node)
if wireless_link:
ip4_mask = WIRELESS_IP4_MASK
ip6_mask = WIRELESS_IP6_MASK
else:
ip4_mask = IP4_MASK
ip6_mask = IP6_MASK
iface_id = canvas_node.next_iface_id()
name = f"eth{iface_id}"
iface = Interface(
id=iface_id,
name=name,
ip4=ip4,
ip4_mask=ip4_mask,
ip6=ip6,
ip6_mask=ip6_mask,
)
logging.info("create node(%s) interface(%s)", node.name, iface)
return iface