From: Wolfgang Bumiller Date: Tue, 25 Aug 2015 09:00:06 +0000 (+0200) Subject: Add generic parse_host_and_port function X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=b2613777279f5a55b1ce2b460768943d946ccb9d Add generic parse_host_and_port function 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. --- diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index a7bcd35..0c6dde6 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -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;