[pve-devel] [RFC v3 pve-common] Add generic parse_host_and_port function
Wolfgang Bumiller
w.bumiller at proxmox.com
Tue Aug 25 11:00:06 CEST 2015
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.
---
Changes:
* added missing '?' after port in 3rd variant
src/PVE/Tools.pm | 13 +++++++++++++
1 file changed, 13 insertions(+)
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;
--
2.1.4
More information about the pve-devel
mailing list