Index: lldb/trunk/include/lldb/Host/SocketAddress.h =================================================================== --- lldb/trunk/include/lldb/Host/SocketAddress.h +++ lldb/trunk/include/lldb/Host/SocketAddress.h @@ -41,8 +41,9 @@ //---------------------------------------------------------------------------- // Static method to get all address information for a host and/or service //---------------------------------------------------------------------------- - static std::vector GetAddressInfo(const char *hostname, - const char *servname); + static std::vector + GetAddressInfo(const char *hostname, const char *servname, int ai_family, + int ai_socktype, int ai_protocol, int ai_flags = 0); //------------------------------------------------------------------ // Constructors and Destructors Index: lldb/trunk/source/Host/common/SocketAddress.cpp =================================================================== --- lldb/trunk/source/Host/common/SocketAddress.cpp +++ lldb/trunk/source/Host/common/SocketAddress.cpp @@ -227,6 +227,18 @@ int ai_flags) { Clear(); + auto addresses = GetAddressInfo(host, service, ai_family, ai_socktype, ai_protocol, ai_flags); + if (!addresses.empty()) + *this = addresses[0]; + return IsValid(); +} + +std::vector +SocketAddress::GetAddressInfo(const char *hostname, const char *servname, + int ai_family, int ai_socktype, int ai_protocol, + int ai_flags) { + std::vector addr_list; + struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = ai_family; @@ -234,26 +246,8 @@ hints.ai_protocol = ai_protocol; hints.ai_flags = ai_flags; - bool result = false; - struct addrinfo *service_info_list = NULL; - int err = ::getaddrinfo(host, service, &hints, &service_info_list); - if (err == 0 && service_info_list) { - *this = service_info_list; - result = IsValid(); - } - - if (service_info_list) - ::freeaddrinfo(service_info_list); - - return result; -} - -std::vector SocketAddress::GetAddressInfo(const char *hostname, - const char *servname) { - std::vector addr_list; - struct addrinfo *service_info_list = NULL; - int err = ::getaddrinfo(hostname, servname, NULL, &service_info_list); + int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list); if (err == 0 && service_info_list) { for (struct addrinfo *service_ptr = service_info_list; service_ptr != NULL; service_ptr = service_ptr->ai_next) { Index: lldb/trunk/unittests/Host/SocketAddressTest.cpp =================================================================== --- lldb/trunk/unittests/Host/SocketAddressTest.cpp +++ lldb/trunk/unittests/Host/SocketAddressTest.cpp @@ -11,13 +11,9 @@ #include "lldb/Host/SocketAddress.h" -namespace { -class SocketAddressTest : public ::testing::Test {}; -} - using namespace lldb_private; -TEST_F(SocketAddressTest, Set) { +TEST(SocketAddressTest, Set) { SocketAddress sa; ASSERT_TRUE(sa.SetToLocalhost(AF_INET, 1138)); ASSERT_STREQ("127.0.0.1", sa.GetIPAddress().c_str()); @@ -34,12 +30,20 @@ ASSERT_EQ(1139, sa.GetPort()); } +TEST(SocketAddressTest, GetAddressInfo) { + auto addr = SocketAddress::GetAddressInfo("127.0.0.1", nullptr, AF_UNSPEC, + SOCK_STREAM, IPPROTO_TCP); + ASSERT_EQ(1u, addr.size()); + EXPECT_EQ(AF_INET, addr[0].GetFamily()); + EXPECT_EQ("127.0.0.1", addr[0].GetIPAddress()); +} + #ifdef _WIN32 // we need to test our inet_ntop implementation for Windows XP const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); -TEST_F(SocketAddressTest, inet_ntop) { +TEST(SocketAddressTest, inet_ntop) { const uint8_t address4[4] = {255, 0, 1, 100}; const uint8_t address6[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 255, 0};