From 67d1b8a4e947870165b37b22d3e7e504a9cd3a65 Mon Sep 17 00:00:00 2001 From: Vincent Li Date: Fri, 4 Jul 2025 21:40:08 +0000 Subject: [PATCH] network-functions.pl: Add function to extract prefix wireguard-functions.pl requires get_prefix commit 0e55d277370a0196caf0b374144ef40b43fe6819 Author: Michael Tremer Date: Wed Mar 20 12:09:58 2024 +0100 network-functions.pl: Add function to extract prefix Signed-off-by: Michael Tremer Signed-off-by: Vincent Li --- config/cfgroot/network-functions.pl | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl index d80bf8fae..492cb1110 100644 --- a/config/cfgroot/network-functions.pl +++ b/config/cfgroot/network-functions.pl @@ -291,6 +291,43 @@ sub get_broadcast($) { return &bin2ip($network_bin ^ ~$netmask_bin); } +sub get_prefix($) { + my $network = shift; + + # Convert to binary + my ($network_bin, $netmask_bin) = &network2bin($network); + + if (defined $netmask_bin) { + my $prefix = 0; + + while (1) { + # End the loop if we have consumed all ones + last if ($netmask_bin == 0); + + # Increment prefix + $prefix++; + + # Remove the most-significant one + $netmask_bin <<= 1; + $netmask_bin &= 0xffffffff; + } + + return $prefix; + } + + return undef; +} + +sub get_netmask($) { + my $network = shift; + + # Fetch the prefix + my $prefix = &get_prefix($network); + + # Convert to netmask + return &convert_prefix2netmask($prefix); +} + sub normalize_network($) { my $network = shift; my $address = &get_netaddress($network);