]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
network-functions.pl: Introduce convert_to_cidr_or_mask() function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Sat, 17 Jul 2021 19:30:15 +0000 (21:30 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 18 Jul 2021 09:29:30 +0000 (11:29 +0200)
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 <stefan.schantl@ipfire.org>
config/cfgroot/network-functions.pl

index b7a840559ccd5ec52a4cf918065538690a2c32ed..2035c970dbaa87b7f287fad3f9868660c081866b 100644 (file)
@@ -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($$) {