[Lldb-commits] [lldb] r301168 - Add more arguments to SocketAddress::GetAddressInfo

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 24 02:39:57 PDT 2017


Author: labath
Date: Mon Apr 24 04:39:56 2017
New Revision: 301168

URL: http://llvm.org/viewvc/llvm-project?rev=301168&view=rev
Log:
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

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

Modified: lldb/trunk/include/lldb/Host/SocketAddress.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/SocketAddress.h?rev=301168&r1=301167&r2=301168&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/SocketAddress.h (original)
+++ lldb/trunk/include/lldb/Host/SocketAddress.h Mon Apr 24 04:39:56 2017
@@ -41,8 +41,9 @@ public:
   //----------------------------------------------------------------------------
   // 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

Modified: lldb/trunk/source/Host/common/SocketAddress.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/SocketAddress.cpp?rev=301168&r1=301167&r2=301168&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/SocketAddress.cpp (original)
+++ lldb/trunk/source/Host/common/SocketAddress.cpp Mon Apr 24 04:39:56 2017
@@ -227,6 +227,18 @@ bool SocketAddress::getaddrinfo(const ch
                                 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;
@@ -234,26 +246,8 @@ bool SocketAddress::getaddrinfo(const ch
   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) {

Modified: lldb/trunk/unittests/Host/SocketAddressTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Host/SocketAddressTest.cpp?rev=301168&r1=301167&r2=301168&view=diff
==============================================================================
--- lldb/trunk/unittests/Host/SocketAddressTest.cpp (original)
+++ lldb/trunk/unittests/Host/SocketAddressTest.cpp Mon Apr 24 04:39:56 2017
@@ -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};




More information about the lldb-commits mailing list