labath created this revision.

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 https://reviews.llvm.org/D31823)

- it enables us to rewrite the getaddrinfo member function in terms of

the static GetAddressInfo function.


https://reviews.llvm.org/D32357

Files:
  include/lldb/Host/SocketAddress.h
  source/Host/common/SocketAddress.cpp
  unittests/Host/SocketAddressTest.cpp

Index: unittests/Host/SocketAddressTest.cpp
===================================================================
--- unittests/Host/SocketAddressTest.cpp
+++ 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};
Index: source/Host/common/SocketAddress.cpp
===================================================================
--- source/Host/common/SocketAddress.cpp
+++ source/Host/common/SocketAddress.cpp
@@ -227,33 +227,27 @@
                                 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>
+SocketAddress::GetAddressInfo(const char *hostname, const char *servname,
+                              int ai_family, int ai_socktype, int ai_protocol,
+                              int ai_flags) {
+  std::vector<SocketAddress> addr_list;
+
   struct addrinfo hints;
   memset(&hints, 0, sizeof(hints));
   hints.ai_family = ai_family;
   hints.ai_socktype = ai_socktype;
   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> SocketAddress::GetAddressInfo(const char *hostname,
-                                                       const char *servname) {
-  std::vector<SocketAddress> 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: include/lldb/Host/SocketAddress.h
===================================================================
--- include/lldb/Host/SocketAddress.h
+++ 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<SocketAddress> GetAddressInfo(const char *hostname,
-                                                 const char *servname);
+  static std::vector<SocketAddress>
+  GetAddressInfo(const char *hostname, const char *servname, int ai_family,
+                 int ai_socktype, int ai_protocol, int ai_flags = 0);
 
   //------------------------------------------------------------------
   // Constructors and Destructors
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to