Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/unittests/Host/SocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ TEST_P(SocketTest, TCPListen0GetListeningConnectionURI) {
}

TEST_F(SocketTest, TCPListen0MultiListenerGetListeningConnectionURI) {
if (!HostSupportsIPv6() || !HostSupportsIPv4())
if (!HostSupportsLocalhostToIPv4() || !HostSupportsLocalhostToIPv6())
return;

llvm::Expected<std::unique_ptr<TCPSocket>> sock =
Expand Down
18 changes: 18 additions & 0 deletions lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ bool lldb_private::HostSupportsIPv6() {
return CheckIPSupport("IPv6", "[::1]:0");
}

bool lldb_private::HostSupportsLocalhostToIPv4() {
if (!HostSupportsIPv4())
return false;

auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET,
SOCK_STREAM, IPPROTO_TCP);
return !addresses.empty();
}

bool lldb_private::HostSupportsLocalhostToIPv6() {
if (!HostSupportsIPv6())
return false;

auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET6,
SOCK_STREAM, IPPROTO_TCP);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got

llvm-project git:(becab1c1b0a3) cat /etc/hosts     
127.0.0.1 localhost
127.0.1.1 ubuntu-linux-22-04-desktop

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

For some reason, getaddrinfo("localhost", AF_UNSPEC, ...) returns only one address (IPv4 127.0.0.1), but getaddrinfo("localhost", AF_INET6, ...) returns an address as well (IPv6 ::1).

I think it's probably an oddity in getaddrinfo implementation (my gai.conf is empty). This check seems to work the way expected:

auto addresses = SocketAddress::GetAddressInfo(
    "localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
return std::find_if(addresses.begin(), addresses.end(),
                    [](SocketAddress &addr) {
                      return addr.GetFamily() == AF_INET6;
                    }) != addresses.end();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!

I tried this on my mac by commenting out the ::1 localhost line in my hosts file, but I wonder if I had some caching or something happening.

Applied this suggestion.

return !addresses.empty();
}

llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
if (HostSupportsIPv4())
return "127.0.0.1";
Expand Down
5 changes: 5 additions & 0 deletions lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
bool HostSupportsIPv6();
bool HostSupportsIPv4();

/// Returns true if the name `localhost` maps to a loopback IPv4 address.
bool HostSupportsLocalhostToIPv4();
/// Returns true if the name `localhost` maps to a loopback IPv6 address.
bool HostSupportsLocalhostToIPv6();

/// Return an IP for localhost based on host support.
///
/// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6
Expand Down
Loading