]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
Add generic parse_host_and_port function
[pve-common.git] / src / PVE / Tools.pm
index a7bcd355f95c690348c4822f6ee56bf9b7435519..0c6dde6570bffad93003d95d47fc3a4c64f7b8d4 100644 (file)
@@ -1102,4 +1102,17 @@ sub get_host_address_family {
     return $res[0]->{family};
 }
 
+# Parses any sane kind of host, or host+port pair:
+# The port is always optional and thus may be undef.
+sub parse_host_and_port {
+    my ($address) = @_;
+    if ($address =~ /^($IPV4RE|[[:alnum:]\-.]+)(?::(\d+))?$/ ||             # ipv4 or host with optional ':port'
+        $address =~ /^\[($IPV6RE|$IPV4RE|[[:alnum:]\-.]+)\](?::(\d+))?$/ || # anything in brackets with optional ':port'
+        $address =~ /^($IPV6RE)(?:\.(\d+))?$/)                              # ipv6 with optional port separated by dot
+    {
+       return ($1, $2, 1); # end with 1 to support simple if(parse...) tests
+    }
+    return; # nothing
+}
+
 1;