Skip to content

Commit 71ac023

Browse files
committed
feat(ipv6): Update test-port-forwarding to obtain system's IPv6 address
1 parent 17585c2 commit 71ac023

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

hack/test-port-forwarding.pl

+23-5
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,38 @@
1717

1818
use Config qw(%Config);
1919
use IO::Handle qw();
20-
use Socket qw(inet_ntoa);
20+
use Socket qw(inet_ntop sockaddr_in sockaddr_in6 getaddrinfo AI_CANONNAME SOCK_STREAM AF_INET AF_INET6);
2121
use Sys::Hostname qw(hostname);
2222

2323
my $instance = shift;
2424

25-
my $addr = scalar gethostbyname(hostname());
25+
my $hints = {};
26+
$hints->{flags} = AI_CANONNAME;
27+
$hints->{socktype} = SOCK_STREAM;
28+
2629
# If hostname address cannot be determines, use localhost to trigger fallback to system_profiler lookup
27-
my $ipv4 = length $addr ? inet_ntoa($addr) : "127.0.0.1";
28-
my $ipv6 = ""; # todo
30+
my $ipv4 = "";
31+
my $ipv6 = "";
32+
my ($err, @addrs) = getaddrinfo(hostname(), undef, $hints);
33+
34+
if (@addrs) {
35+
for (@addrs) {
36+
if ($_->{family} == AF_INET && $ipv4 eq "") {
37+
my $addr4 = (sockaddr_in($_->{addr}))[1];
38+
$ipv4 = inet_ntop(AF_INET, $addr4);
39+
} elsif ($_->{family} == AF_INET6 && $ipv6 eq "") {
40+
my $addr6 = (sockaddr_in6($_->{addr}))[1];
41+
$ipv6 = inet_ntop(AF_INET6, $addr6);
42+
}
43+
}
44+
}
2945

3046
# macOS Github runners seem to use "localhost" as the hostname
3147
if ($ipv4 eq "127.0.0.1" && $Config{osname} eq "darwin") {
32-
$ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first');
48+
$ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv4.Addresses)) | .IPv4.Addresses | first');
3349
chomp $ipv4;
50+
$ipv6 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv6.Addresses)) | .IPv6.Addresses | first');
51+
chomp $ipv6;
3452
}
3553

3654
# If $instance is a filename, add our portForwards to it to enable testing

0 commit comments

Comments
 (0)