[Lldb-commits] [lldb] r300112 - [NFC] Adding a new wrapper for getaddrinfo
Chris Bieneman via lldb-commits
lldb-commits at lists.llvm.org
Wed Apr 12 14:56:33 PDT 2017
Author: cbieneman
Date: Wed Apr 12 16:56:33 2017
New Revision: 300112
URL: http://llvm.org/viewvc/llvm-project?rev=300112&view=rev
Log:
[NFC] Adding a new wrapper for getaddrinfo
Summary: 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.
Reviewers: zturner
Reviewed By: zturner
Subscribers: lldb-commits
Differential Revision: https://reviews.llvm.org/D31822
Modified:
lldb/trunk/include/lldb/Host/SocketAddress.h
lldb/trunk/source/Host/common/SocketAddress.cpp
Modified: lldb/trunk/include/lldb/Host/SocketAddress.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/SocketAddress.h?rev=300112&r1=300111&r2=300112&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/SocketAddress.h (original)
+++ lldb/trunk/include/lldb/Host/SocketAddress.h Wed Apr 12 16:56:33 2017
@@ -32,15 +32,23 @@ typedef ADDRESS_FAMILY sa_family_t;
// 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 @@ public:
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 @@ public:
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() {
Modified: lldb/trunk/source/Host/common/SocketAddress.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/SocketAddress.cpp?rev=300112&r1=300111&r2=300112&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/SocketAddress.cpp (original)
+++ lldb/trunk/source/Host/common/SocketAddress.cpp Wed Apr 12 16:56:33 2017
@@ -89,6 +89,10 @@ SocketAddress::SocketAddress(const struc
m_socket_addr.sa_storage = s;
}
+SocketAddress::SocketAddress(const struct addrinfo *addr_info) {
+ *this = addr_info;
+}
+
//----------------------------------------------------------------------
// SocketAddress copy constructor
//----------------------------------------------------------------------
@@ -244,6 +248,24 @@ bool SocketAddress::getaddrinfo(const ch
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.emplace_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 @@ bool SocketAddress::SetToAnyAddress(sa_f
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);
+}
More information about the lldb-commits
mailing list