2019-11-22 20:51:58 +00:00
|
|
|
import logging
|
|
|
|
import random
|
2020-01-13 20:03:13 +00:00
|
|
|
from typing import Optional, Set
|
2019-10-11 01:02:28 +01:00
|
|
|
|
2019-11-22 20:51:58 +00:00
|
|
|
from netaddr import IPNetwork
|
2019-10-11 01:02:28 +01:00
|
|
|
|
2020-01-13 20:03:13 +00:00
|
|
|
from core.gui.graph.node import CanvasNode
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.nodeutils import NodeUtils
|
2019-10-22 00:33:18 +01:00
|
|
|
|
2019-11-22 20:51:58 +00:00
|
|
|
|
|
|
|
def random_mac():
|
|
|
|
return ("{:02x}" * 6).format(*[random.randrange(256) for _ in range(6)])
|
2019-10-22 00:33:18 +01:00
|
|
|
|
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
class InterfaceManager:
|
2020-01-13 20:03:13 +00:00
|
|
|
def __init__(
|
|
|
|
self, app, address: Optional[str] = "10.0.0.0", mask: Optional[int] = 24
|
|
|
|
):
|
2019-11-22 20:51:58 +00:00
|
|
|
self.app = app
|
2019-11-25 19:31:40 +00:00
|
|
|
self.mask = mask
|
|
|
|
self.base_prefix = max(self.mask - 8, 0)
|
|
|
|
self.subnets = IPNetwork(f"{address}/{self.base_prefix}")
|
|
|
|
self.current_subnet = None
|
|
|
|
|
|
|
|
def next_subnet(self):
|
|
|
|
# define currently used subnets
|
|
|
|
used_subnets = set()
|
2019-12-19 19:10:08 +00:00
|
|
|
for edge in self.app.core.links.values():
|
|
|
|
link = edge.link
|
2019-11-25 19:31:40 +00:00
|
|
|
if link.HasField("interface_one"):
|
|
|
|
subnet = self.get_subnet(link.interface_one)
|
|
|
|
used_subnets.add(subnet)
|
|
|
|
if link.HasField("interface_two"):
|
|
|
|
subnet = self.get_subnet(link.interface_two)
|
|
|
|
used_subnets.add(subnet)
|
|
|
|
|
|
|
|
# find next available subnet
|
|
|
|
for i in self.subnets.subnet(self.mask):
|
|
|
|
if i not in used_subnets:
|
|
|
|
return i
|
2019-11-22 20:51:58 +00:00
|
|
|
|
2019-11-22 22:58:41 +00:00
|
|
|
def reset(self):
|
2019-11-25 19:31:40 +00:00
|
|
|
self.current_subnet = None
|
2019-11-22 22:58:41 +00:00
|
|
|
|
2020-01-13 20:03:13 +00:00
|
|
|
def get_ips(self, node_id: int):
|
2019-11-25 19:31:40 +00:00
|
|
|
ip4 = self.current_subnet[node_id]
|
2019-11-22 20:51:58 +00:00
|
|
|
ip6 = ip4.ipv6()
|
2019-11-25 19:31:40 +00:00
|
|
|
prefix = self.current_subnet.prefixlen
|
|
|
|
return str(ip4), str(ip6), prefix
|
2019-11-22 20:51:58 +00:00
|
|
|
|
2019-11-23 07:48:10 +00:00
|
|
|
@classmethod
|
2019-11-25 19:31:40 +00:00
|
|
|
def get_subnet(cls, interface):
|
2019-11-23 07:48:10 +00:00
|
|
|
return IPNetwork(f"{interface.ip4}/{interface.ip4mask}").cidr
|
2019-11-23 00:30:25 +00:00
|
|
|
|
2020-01-13 20:03:13 +00:00
|
|
|
def determine_subnet(
|
|
|
|
self, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode
|
|
|
|
):
|
2019-11-22 20:51:58 +00:00
|
|
|
src_node = canvas_src_node.core_node
|
|
|
|
dst_node = canvas_dst_node.core_node
|
|
|
|
is_src_container = NodeUtils.is_container_node(src_node.type)
|
|
|
|
is_dst_container = NodeUtils.is_container_node(dst_node.type)
|
|
|
|
if is_src_container and is_dst_container:
|
2019-11-25 19:31:40 +00:00
|
|
|
self.current_subnet = self.next_subnet()
|
2019-11-22 20:51:58 +00:00
|
|
|
elif is_src_container and not is_dst_container:
|
2019-11-25 19:31:40 +00:00
|
|
|
subnet = self.find_subnet(canvas_dst_node, visited={src_node.id})
|
|
|
|
if subnet:
|
|
|
|
self.current_subnet = subnet
|
2019-11-22 20:51:58 +00:00
|
|
|
else:
|
2019-11-25 19:31:40 +00:00
|
|
|
self.current_subnet = self.next_subnet()
|
2019-11-22 20:51:58 +00:00
|
|
|
elif not is_src_container and is_dst_container:
|
2019-11-25 19:31:40 +00:00
|
|
|
subnet = self.find_subnet(canvas_src_node, visited={dst_node.id})
|
|
|
|
if subnet:
|
|
|
|
self.current_subnet = subnet
|
2019-11-22 20:51:58 +00:00
|
|
|
else:
|
2019-11-25 19:31:40 +00:00
|
|
|
self.current_subnet = self.next_subnet()
|
2019-11-22 20:51:58 +00:00
|
|
|
else:
|
|
|
|
logging.info("ignoring subnet change for link between network nodes")
|
|
|
|
|
2020-01-13 20:03:13 +00:00
|
|
|
def find_subnet(self, canvas_node: CanvasNode, visited: Optional[Set[int]] = None):
|
2019-11-22 20:51:58 +00:00
|
|
|
logging.info("finding subnet for node: %s", canvas_node.core_node.name)
|
|
|
|
canvas = self.app.canvas
|
|
|
|
cidr = None
|
2019-11-23 07:48:10 +00:00
|
|
|
if not visited:
|
|
|
|
visited = set()
|
2019-11-22 20:51:58 +00:00
|
|
|
visited.add(canvas_node.core_node.id)
|
|
|
|
for edge in canvas_node.edges:
|
|
|
|
src_node = canvas.nodes[edge.src]
|
|
|
|
dst_node = canvas.nodes[edge.dst]
|
|
|
|
interface = edge.src_interface
|
|
|
|
check_node = src_node
|
|
|
|
if src_node == canvas_node:
|
|
|
|
interface = edge.dst_interface
|
|
|
|
check_node = dst_node
|
|
|
|
if check_node.core_node.id in visited:
|
|
|
|
continue
|
|
|
|
visited.add(check_node.core_node.id)
|
|
|
|
if interface:
|
2019-11-25 19:31:40 +00:00
|
|
|
cidr = self.get_subnet(interface)
|
2019-11-22 20:51:58 +00:00
|
|
|
else:
|
|
|
|
cidr = self.find_subnet(check_node, visited)
|
2019-11-23 07:48:10 +00:00
|
|
|
if cidr:
|
|
|
|
logging.info("found subnet: %s", cidr)
|
|
|
|
break
|
2019-11-22 20:51:58 +00:00
|
|
|
return cidr
|