From 7962d8e4e82f7be8447e101ecc16817eabb2b7ac Mon Sep 17 00:00:00 2001 From: Victor Gama Date: Mon, 15 May 2023 20:05:19 -0300 Subject: [PATCH] feat(ipv6): Update test-port-forwarding to obtain system's IPv6 address --- hack/test-port-forwarding.pl | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/hack/test-port-forwarding.pl b/hack/test-port-forwarding.pl index 8def0472c14c..5368ea4c4e97 100755 --- a/hack/test-port-forwarding.pl +++ b/hack/test-port-forwarding.pl @@ -17,20 +17,39 @@ use Config qw(%Config); use IO::Handle qw(); -use Socket qw(inet_ntoa); +use Socket qw(inet_ntop sockaddr_in sockaddr_in6 getaddrinfo AI_CANONNAME SOCK_STREAM AF_INET AF_INET6); use Sys::Hostname qw(hostname); my $instance = shift; -my $addr = scalar gethostbyname(hostname()); +my $hints = {}; +$hints->{flags} = AI_CANONNAME; +$hints->{socktype} = SOCK_STREAM; + # If hostname address cannot be determines, use localhost to trigger fallback to system_profiler lookup -my $ipv4 = length $addr ? inet_ntoa($addr) : "127.0.0.1"; -my $ipv6 = ""; # todo +my $ipv4 = ""; +my $ipv6 = ""; +my ($err, @addrs) = getaddrinfo(hostname(), undef, $hints); + +if (addrs) { + for (@addrs) { + if ($_->{family} == AF_INET && $ipv4 eq "") { + my $addr4 = (sockaddr_in($_->{addr}))[1]; + $ipv4 = inet_ntop(AF_INET, $addr4); + } elsif ($_->{family} == AF_INET6 && $ipv6 eq "") { + my $addr6 = (sockaddr_in6($_->{addr}))[1]; + $ipv6 = inet_ntop(AF_INET6, $addr6); + } + } +} + # macOS Github runners seem to use "localhost" as the hostname if ($ipv4 eq "127.0.0.1" && $Config{osname} eq "darwin") { - $ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first'); + $ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv4.Addresses)) | .IPv4.Addresses | first'); chomp $ipv4; + $ipv6 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv6.Addresses)) | .IPv6.Addresses | first'); + chomp $ipv6; } # If $instance is a filename, add our portForwards to it to enable testing