beanz created this revision.

This patch adds a new wrapper for getaddrinfo which returns a std::vector of 
SocketAddresses. While this patch doesn't add any uses of the new function, I 
have two separable patches that are dependent on this, so I put it in its own 
patch.


https://reviews.llvm.org/D31822

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

Index: source/Host/common/SocketAddress.cpp
===================================================================
--- source/Host/common/SocketAddress.cpp
+++ source/Host/common/SocketAddress.cpp
@@ -89,6 +89,10 @@
   m_socket_addr.sa_storage = s;
 }
 
+SocketAddress::SocketAddress(const struct addrinfo *addr_info) {
+  *this = addr_info;
+}
+
 //----------------------------------------------------------------------
 // SocketAddress copy constructor
 //----------------------------------------------------------------------
@@ -244,6 +248,24 @@
   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);
+  if (err == 0 && service_info_list) {
+    for (struct addrinfo *service_ptr = service_info_list; service_ptr != NULL;
+         service_ptr = service_ptr->ai_next) {
+      addr_list.push_back(SocketAddress(service_ptr));
+    }
+  }
+
+  if (service_info_list)
+    ::freeaddrinfo(service_info_list);
+  return addr_list;
+}
+
 bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) {
   switch (family) {
   case AF_INET:
@@ -287,3 +309,29 @@
   Clear();
   return false;
 }
+
+bool SocketAddress::IsAnyAddr() const {
+  return (GetFamily() == AF_INET)
+             ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY)
+             : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16);
+}
+
+bool SocketAddress::operator==(const SocketAddress &rhs) const {
+  if (GetFamily() != rhs.GetFamily())
+    return false;
+  if (GetLength() != rhs.GetLength())
+    return false;
+  switch (GetFamily()) {
+  case AF_INET:
+    return m_socket_addr.sa_ipv4.sin_addr.s_addr ==
+           rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr;
+  case AF_INET6:
+    return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr,
+                       &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16);
+  }
+  return false;
+}
+
+bool SocketAddress::operator!=(const SocketAddress &rhs) const {
+  return !(*this == rhs);
+}
Index: include/lldb/Host/SocketAddress.h
===================================================================
--- include/lldb/Host/SocketAddress.h
+++ include/lldb/Host/SocketAddress.h
@@ -32,15 +32,23 @@
 // Other libraries and framework includes
 // Project includes
 #include <string>
+#include <vector>
 
 namespace lldb_private {
 
 class SocketAddress {
 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);
+
   //------------------------------------------------------------------
   // Constructors and Destructors
   //------------------------------------------------------------------
   SocketAddress();
+  SocketAddress(const struct addrinfo *addr_info);
   SocketAddress(const struct sockaddr &s);
   SocketAddress(const struct sockaddr_in &s);
   SocketAddress(const struct sockaddr_in6 &s);
@@ -63,6 +71,9 @@
 
   const SocketAddress &operator=(const struct sockaddr_storage &s);
 
+  bool operator==(const SocketAddress &rhs) const;
+  bool operator!=(const SocketAddress &rhs) const;
+
   //------------------------------------------------------------------
   // Clear the contents of this socket address
   //------------------------------------------------------------------
@@ -135,6 +146,11 @@
   bool IsValid() const;
 
   //------------------------------------------------------------------
+  // Returns true if the socket is INADDR_ANY
+  //------------------------------------------------------------------
+  bool IsAnyAddr() const;
+
+  //------------------------------------------------------------------
   // Direct access to all of the sockaddr structures
   //------------------------------------------------------------------
   struct sockaddr &sockaddr() {
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to