From: Stefan Schantl Date: Sat, 17 Jul 2021 19:30:15 +0000 (+0200) Subject: network-functions.pl: Introduce convert_to_cidr_or_mask() function. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=79148bda2ca83ca4821fd10b28b7fb815aca76b6;p=people%2Fstevee%2Fipfire-2.x.git network-functions.pl: Introduce convert_to_cidr_or_mask() function. This function is used to convert a single host address or network into different formats. If a single host address is given, it will be converted to a plain address without any notation details. When a single host address is given and a format is specified ("cidr" or "mask") the host address with the notation details will be returned. In case a network is given the output will be formated into the desired format ("cidr" or "mask"). Unless a special output format is given for a network it defaults to CIDR. Signed-off-by: Stefan Schantl --- diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl index b7a840559c..2035c970db 100644 --- a/config/cfgroot/network-functions.pl +++ b/config/cfgroot/network-functions.pl @@ -259,6 +259,72 @@ sub convert_prefix2netmask($) { return undef; } +# Checks and returns a single address or network converted +# to CIDR or with subnetmask. +# +# The conversion target can be specified as optional second argument. +# Returns the address only if a single address is provided. +# +# Defaults to "cidr" to convert networks into CIDR format. +# +# Other options are "mask" to provide a network or single host with subnetmask. +# In case "cidr" is forced, single host addresses also will be returned as CIDR notation. +sub convert_to_cidr_or_mask ($$) { + my ($input, $target) = @_; + + # If no target is given default to "none". + $target //= 'none'; + + # Split the input into address and subnet parts. + my ($address, $subnet) = split('/', $input); + + # Check if the address is valid. + unless (&check_ip_address($address)) { + # Invalid IP address. + return undef; + } + + # Check if a single host IP has been given. + if (!$subnet || $subnet eq "32" || $subnet eq "255.255.255.255") { + # Check if the default target is used. + if ($target eq "none") { + # Return the address only. + return "$address"; + } + } + + # A network or a single host IP address and a convert target have been given. + if ($target eq "none") { + # Set default output to cidr. + $target = "cidr"; + } + + # Assign "32" as prefix, if there is no subnet information. + unless ($subnet) { + $subnet = "32"; + } + + # Check if a valid prefix has been given. + if (&check_prefix($subnet)) { + # Convert prefix to subnetmask if requested. + if ($target ne "cidr") { + $subnet = &convert_prefix2netmask($subnet); + } + # Check if a valid subnetmask has been given. + } elsif (&check_netmask($subnet)) { + # Convert subnetmask into prefix if requested. + if ($target eq "cidr") { + $subnet = &convert_netmask2prefix($subnet); + } + } else { + # An invalid subnet has been given. + return undef; + } + + # Return the result. + return "$address/$subnet"; +} + # Takes an IP address and an offset and # will return the offset'th IP address. sub find_next_ip_address($$) {