From 0120abea1e352fc1ebe1941c7a70e886068e161e Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 23 Jan 2026 19:41:35 -0300 Subject: [PATCH] add network utilities for CIDR normalization and validation --- wireguard_tools/networks.py | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 wireguard_tools/networks.py diff --git a/wireguard_tools/networks.py b/wireguard_tools/networks.py new file mode 100644 index 0000000..d43fe45 --- /dev/null +++ b/wireguard_tools/networks.py @@ -0,0 +1,42 @@ +import ipaddress + + +def normalize_cidr_list(cidr_list): + """ + Receives a list of strings like ['10.0.0.1/24', '10.0.0.0/24']. + Returns a sorted unique list of canonical CIDRs (strict=False). + Invalid items are ignored. + """ + normalized = set() + for item in cidr_list or []: + if not item: + continue + try: + normalized.add(str(ipaddress.ip_network(item, strict=False))) + except Exception: + continue + return sorted(normalized) + + +def normalize_cidr_pairs(ip_netmask_pairs): + """ + Receives an iterable of (ip, netmask) pairs and returns a sorted unique list + of canonical CIDRs (strict=False). Invalid items are ignored. + """ + cidrs = [] + for ip, netmask in ip_netmask_pairs or []: + if ip is None or netmask is None: + continue + cidrs.append(f"{ip}/{netmask}") + return normalize_cidr_list(cidrs) + + +def safe_network_cidr(address, netmask): + """ + Returns the network CIDR for an address/netmask, or None if invalid. + """ + try: + network = ipaddress.ip_network(f"{address}/{netmask}", strict=False) + return str(network) + except Exception: + return None