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($$) {