From df0497c4aaabd119d5d835bce6339be8fed58602 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 24 Apr 2017 09:39:56 +0000 Subject: [PATCH] Add more arguments to SocketAddress::GetAddressInfo Summary: the reason for this is two-fold: - getaddrinfo without the extra arguments will return the same (network-level) address multiple times, once for each supported transport protocol, which is not what is usually intended (it certainly wasn't in D31823) - it enables us to rewrite the getaddrinfo member function in terms of the static GetAddressInfo function. Reviewers: beanz, tberghammer Subscribers: lldb-commits Differential Revision: https://reviews.llvm.org/D32357 llvm-svn: 301168 --- lldb/include/lldb/Host/SocketAddress.h | 5 +++-- lldb/source/Host/common/SocketAddress.cpp | 32 +++++++++++++------------------ lldb/unittests/Host/SocketAddressTest.cpp | 16 ++++++++++------ 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lldb/include/lldb/Host/SocketAddress.h b/lldb/include/lldb/Host/SocketAddress.h index bc66ad9..8e9026b 100644 --- a/lldb/include/lldb/Host/SocketAddress.h +++ b/lldb/include/lldb/Host/SocketAddress.h @@ -41,8 +41,9 @@ public: //---------------------------------------------------------------------------- // 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 diff --git a/lldb/source/Host/common/SocketAddress.cpp b/lldb/source/Host/common/SocketAddress.cpp index 48c3ec1..b41cef6 100644 --- a/lldb/source/Host/common/SocketAddress.cpp +++ b/lldb/source/Host/common/SocketAddress.cpp @@ -227,6 +227,18 @@ bool SocketAddress::getaddrinfo(const char *host, const char *service, 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 @@ bool SocketAddress::getaddrinfo(const char *host, const char *service, 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) { diff --git a/lldb/unittests/Host/SocketAddressTest.cpp b/lldb/unittests/Host/SocketAddressTest.cpp index 3c18137..ddcbf53 100644 --- a/lldb/unittests/Host/SocketAddressTest.cpp +++ b/lldb/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 @@ TEST_F(SocketAddressTest, Set) { 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}; -- 2.7.4