[pve-devel] [PATCH common 2/2] tools: add addr_to_ip helper
Thomas Lamprecht
t.lamprecht at proxmox.com
Thu May 4 09:14:17 CEST 2017
This helper was picked up by me and is now available in
PVE::Network::addr_to_ip
Also another helper was pushed, get_ip_from_hostname,
while it currently rejects 127.* addresses this could be easily made
optional
and so you could reuse it for your qemu-server patch (fix #1229: more
explicit
spice port allocation) in this series, if wanted :)
cheers,
Thomas
On 04/24/2017 03:52 PM, Wolfgang Bumiller wrote:
> Converts a packed address into a numerical string.
> (Optionally includes the port).
> ---
> src/PVE/Tools.pm | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
> index 36a9f5a..dd63e65 100644
> --- a/src/PVE/Tools.pm
> +++ b/src/PVE/Tools.pm
> @@ -4,7 +4,8 @@ use strict;
> use warnings;
> use POSIX qw(EINTR EEXIST EOPNOTSUPP);
> use IO::Socket::IP;
> -use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME SOCK_DGRAM);
> +use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME SOCK_DGRAM
> + NI_NUMERICHOST NI_NUMERICSERV);
> use IO::Select;
> use File::Basename;
> use File::Path qw(make_path);
> @@ -1200,6 +1201,14 @@ sub get_host_address_family {
> return $res[0]->{family};
> }
>
> +sub addr_to_ip {
> + my ($addr) = @_;
> + my ($err, $host, $port) = Socket::getnameinfo($addr, NI_NUMERICHOST | NI_NUMERICSERV);
> + die "failed to get numerical host address: $err\n" if $err;
> + return ($host, $port) if wantarray;
> + return $host;
> +}
> +
> # get the fully qualified domain name of a host
> # same logic as hostname(1): The FQDN is the name getaddrinfo(3) returns,
> # given a nodename as a parameter
More information about the pve-devel
mailing list