]> git.proxmox.com Git - pve-common.git/commitdiff
Add generic parse_host_and_port function
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 25 Aug 2015 09:00:06 +0000 (11:00 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 25 Aug 2015 10:58:09 +0000 (12:58 +0200)
Added a generic function to split a host+port string to the
host and port part supporting the two most common ipv6
notations beside domains and ipv4: with brackets for the
address or a dot as port separator.

src/PVE/Tools.pm

index a7bcd355f95c690348c4822f6ee56bf9b7435519..0c6dde6570bffad93003d95d47fc3a4c64f7b8d4 100644 (file)
@@ -1102,4 +1102,17 @@ sub get_host_address_family {
     return $res[0]->{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;
 1;