[Lldb-commits] [lldb] r135488 - in /lldb/trunk: include/lldb/Core/ConnectionFileDescriptor.h include/lldb/Host/SocketAddress.h lldb.xcodeproj/project.pbxproj source/Core/ConnectionFileDescriptor.cpp source/Host/common/SocketAddress.cpp

Greg Clayton gclayton at apple.com
Tue Jul 19 09:44:54 PDT 2011


Author: gclayton
Date: Tue Jul 19 11:44:54 2011
New Revision: 135488

URL: http://llvm.org/viewvc/llvm-project?rev=135488&view=rev
Log:
Abstract "struct sockaddr", "struct sockaddr_in", "struct sockaddr_in6" and
"struct sockaddr_storage" into a new host class called SocketAddress. This
will allow us to control the host specific implementations (such as how to
get the length) into a single Host specific class.


Added:
    lldb/trunk/include/lldb/Host/SocketAddress.h
    lldb/trunk/source/Host/common/SocketAddress.cpp
Modified:
    lldb/trunk/include/lldb/Core/ConnectionFileDescriptor.h
    lldb/trunk/lldb.xcodeproj/project.pbxproj
    lldb/trunk/source/Core/ConnectionFileDescriptor.cpp

Modified: lldb/trunk/include/lldb/Core/ConnectionFileDescriptor.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ConnectionFileDescriptor.h?rev=135488&r1=135487&r2=135488&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ConnectionFileDescriptor.h (original)
+++ lldb/trunk/include/lldb/Core/ConnectionFileDescriptor.h Tue Jul 19 11:44:54 2011
@@ -19,6 +19,7 @@
 // Other libraries and framework includes
 // Project includes
 #include "lldb/Core/Connection.h"
+#include "lldb/Host/SocketAddress.h"
 
 namespace lldb_private {
 
@@ -96,20 +97,11 @@
         eFDTypeSocketUDP    // Unconnected UDP socket requiring sendto/recvfrom
     } FDType;
     
-    typedef union sockaddr_tag
-    {
-        struct sockaddr         sa;
-        struct sockaddr_in      sa_ipv4;
-        struct sockaddr_in6     sa_ipv6;
-        struct sockaddr_storage sa_storage;
-    } sockaddr_t;
-    
-
     int m_fd_send;
     int m_fd_recv;
     FDType m_fd_send_type;
     FDType m_fd_recv_type;
-    sockaddr_t m_udp_send_sockaddr;
+    SocketAddress m_udp_send_sockaddr;
     bool m_should_close_fd; // True if this class should close the file descriptor when it goes away.
     uint32_t m_socket_timeout_usec;
     

Added: lldb/trunk/include/lldb/Host/SocketAddress.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/SocketAddress.h?rev=135488&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/SocketAddress.h (added)
+++ lldb/trunk/include/lldb/Host/SocketAddress.h Tue Jul 19 11:44:54 2011
@@ -0,0 +1,180 @@
+//===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_SocketAddress_h_
+#define liblldb_SocketAddress_h_
+
+// C Includes
+#include <stdint.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+
+// C++ Includes
+// Other libraries and framework includes
+// Project includes
+
+namespace lldb_private {
+
+class SocketAddress
+{
+public:
+    //------------------------------------------------------------------
+    // Constructors and Destructors
+    //------------------------------------------------------------------
+    SocketAddress();
+    SocketAddress(const SocketAddress& rhs);
+    ~SocketAddress();
+
+    //------------------------------------------------------------------
+    // Operators
+    //------------------------------------------------------------------
+    const SocketAddress&
+    operator=(const SocketAddress& rhs);
+
+    const SocketAddress&
+    operator=(const struct addrinfo *addr_info);
+
+    void
+    Clear ();
+
+    socklen_t
+    GetLength () const;
+
+    static socklen_t
+    GetMaxLength ();
+
+    void
+    SetLength (socklen_t len);
+    
+    sa_family_t
+    GetFamily () const;
+
+    void
+    SetFamily (sa_family_t family);
+
+    in_port_t
+    GetPort () const;
+
+    bool
+    IsValid () const;
+
+    struct sockaddr &
+    sockaddr ()
+    {
+        return m_socket_addr.sa;
+    }
+
+    const struct sockaddr &
+    sockaddr () const
+    {
+        return m_socket_addr.sa;
+    }
+    
+    struct sockaddr_in &
+    sockaddr_in ()
+    {
+        return m_socket_addr.sa_ipv4;
+    }
+    
+    const struct sockaddr_in &
+    sockaddr_in () const
+    {
+        return m_socket_addr.sa_ipv4;
+    }
+    
+    struct sockaddr_in6 &
+    sockaddr_in6 ()
+    {
+        return m_socket_addr.sa_ipv6;
+    }
+    
+    const struct sockaddr_in6 &
+    sockaddr_in6 () const
+    {
+        return m_socket_addr.sa_ipv6;
+    }
+    
+    struct sockaddr_storage &
+    sockaddr_storage ()
+    {
+        return m_socket_addr.sa_storage;
+    }
+
+    
+    const struct sockaddr_storage &
+    sockaddr_storage () const
+    {
+        return m_socket_addr.sa_storage;
+    }
+    //------------------------------------------------------------------
+    // Conversion operators to allow getting the contents of this class
+    // as a subclass
+    //------------------------------------------------------------------
+    
+    operator struct sockaddr * ()
+    {
+        return &m_socket_addr.sa;
+    }
+    
+    operator const struct sockaddr * () const
+    {
+        return &m_socket_addr.sa;
+    }
+
+    operator struct sockaddr_in * ()
+    {
+        return &m_socket_addr.sa_ipv4;
+    }
+    
+    operator const struct sockaddr_in * () const
+    {
+        return &m_socket_addr.sa_ipv4;
+    }
+
+    operator struct sockaddr_in6 * ()
+    {
+        return &m_socket_addr.sa_ipv6;
+    }
+    
+    operator const struct sockaddr_in6 * () const
+    {
+        return &m_socket_addr.sa_ipv6;
+    }
+
+    operator const struct sockaddr_storage * () const
+    {
+        return &m_socket_addr.sa_storage;
+    }
+
+    operator struct sockaddr_storage * ()
+    {
+        return &m_socket_addr.sa_storage;
+    }
+
+protected:
+    typedef union sockaddr_tag
+    {
+        struct sockaddr         sa;
+        struct sockaddr_in      sa_ipv4;
+        struct sockaddr_in6     sa_ipv6;
+        struct sockaddr_storage sa_storage;
+    } sockaddr_t;
+
+    //------------------------------------------------------------------
+    // Classes that inherit from SocketAddress can see and modify these
+    //------------------------------------------------------------------
+    sockaddr_t m_socket_addr;
+};
+
+
+} // namespace lldb_private
+
+
+#endif  // liblldb_SocketAddress_h_

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=135488&r1=135487&r2=135488&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Tue Jul 19 11:44:54 2011
@@ -367,6 +367,7 @@
 		26D265BC136B4269002EEE45 /* lldb-public.h in Headers */ = {isa = PBXBuildFile; fileRef = 26651A14133BEC76005B64B7 /* lldb-public.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		26D5E15F135BAEA2006EA0A7 /* OptionGroupArchitecture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26D5E15E135BAEA2006EA0A7 /* OptionGroupArchitecture.cpp */; };
 		26D5E163135BB054006EA0A7 /* OptionGroupPlatform.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26D5E162135BB054006EA0A7 /* OptionGroupPlatform.cpp */; };
+		26D7E45D13D5E30A007FD12B /* SocketAddress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26D7E45C13D5E30A007FD12B /* SocketAddress.cpp */; };
 		26DB3E161379E7AD0080DC73 /* ABIMacOSX_arm.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26DB3E071379E7AD0080DC73 /* ABIMacOSX_arm.cpp */; };
 		26DB3E1C1379E7AD0080DC73 /* ABIMacOSX_i386.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26DB3E0F1379E7AD0080DC73 /* ABIMacOSX_i386.cpp */; };
 		26DB3E1F1379E7AD0080DC73 /* ABISysV_x86_64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26DB3E131379E7AD0080DC73 /* ABISysV_x86_64.cpp */; };
@@ -1040,6 +1041,8 @@
 		26D5E160135BAEB0006EA0A7 /* OptionGroupArchitecture.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OptionGroupArchitecture.h; path = include/lldb/Interpreter/OptionGroupArchitecture.h; sourceTree = "<group>"; };
 		26D5E161135BB040006EA0A7 /* OptionGroupPlatform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OptionGroupPlatform.h; path = include/lldb/Interpreter/OptionGroupPlatform.h; sourceTree = "<group>"; };
 		26D5E162135BB054006EA0A7 /* OptionGroupPlatform.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = OptionGroupPlatform.cpp; path = source/Interpreter/OptionGroupPlatform.cpp; sourceTree = "<group>"; };
+		26D7E45B13D5E2F9007FD12B /* SocketAddress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SocketAddress.h; path = include/lldb/Host/SocketAddress.h; sourceTree = "<group>"; };
+		26D7E45C13D5E30A007FD12B /* SocketAddress.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SocketAddress.cpp; path = source/Host/common/SocketAddress.cpp; sourceTree = "<group>"; };
 		26D9FDC612F784E60003F2EE /* EmulateInstruction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EmulateInstruction.h; path = include/lldb/Core/EmulateInstruction.h; sourceTree = "<group>"; };
 		26D9FDC812F784FD0003F2EE /* EmulateInstruction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = EmulateInstruction.cpp; path = source/Core/EmulateInstruction.cpp; sourceTree = "<group>"; };
 		26DAFD9711529BC7005A394E /* ExecutionContextScope.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ExecutionContextScope.h; path = include/lldb/Target/ExecutionContextScope.h; sourceTree = "<group>"; };
@@ -2293,6 +2296,8 @@
 				26BC7DD410F1B7D500F91463 /* Host.h */,
 				26BC7DD510F1B7D500F91463 /* Mutex.h */,
 				26BC7DD610F1B7D500F91463 /* Predicate.h */,
+				26D7E45B13D5E2F9007FD12B /* SocketAddress.h */,
+				26D7E45C13D5E30A007FD12B /* SocketAddress.cpp */,
 				2689B0A4113EE3CD00A4AEDB /* Symbols.h */,
 				268DA871130095D000C9483A /* Terminal.h */,
 				26B4E26E112F35F700AB3F64 /* TimeValue.h */,
@@ -3349,6 +3354,7 @@
 				265205AC13D3E3F700132FE2 /* RegisterContextKDP_x86_64.cpp in Sources */,
 				2628A4D513D4977900F5487A /* ThreadKDP.cpp in Sources */,
 				2676045A13D49D2300AB1B6A /* ProcessControl-mig.defs in Sources */,
+				26D7E45D13D5E30A007FD12B /* SocketAddress.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: lldb/trunk/source/Core/ConnectionFileDescriptor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ConnectionFileDescriptor.cpp?rev=135488&r1=135487&r2=135488&view=diff
==============================================================================
--- lldb/trunk/source/Core/ConnectionFileDescriptor.cpp (original)
+++ lldb/trunk/source/Core/ConnectionFileDescriptor.cpp Tue Jul 19 11:44:54 2011
@@ -71,11 +71,10 @@
     m_fd_recv (-1),
     m_fd_send_type (eFDTypeFile),
     m_fd_recv_type (eFDTypeFile),
+    m_udp_send_sockaddr (),
     m_should_close_fd (false), 
     m_socket_timeout_usec(0)
 {
-    memset (&m_udp_send_sockaddr, 0, sizeof(m_udp_send_sockaddr));
-    
     LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION |  LIBLLDB_LOG_OBJECT));
     if (log)
         log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", this);
@@ -87,10 +86,10 @@
     m_fd_recv (fd),
     m_fd_send_type (eFDTypeFile),
     m_fd_recv_type (eFDTypeFile),
+    m_udp_send_sockaddr (),
     m_should_close_fd (owns_fd),
     m_socket_timeout_usec(0)
 {
-    memset (&m_udp_send_sockaddr, 0, sizeof(m_udp_send_sockaddr));
     LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION |  LIBLLDB_LOG_OBJECT));
     if (log)
         log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = %i, owns_fd = %i)", this, fd, owns_fd);
@@ -285,8 +284,8 @@
         if (SetSocketReceiveTimeout (timeout_usec))
         {
             status = eConnectionStatusSuccess;
-            sockaddr_t from = m_udp_send_sockaddr;
-            socklen_t from_len = m_udp_send_sockaddr.sa.sa_len;
+            SocketAddress from (m_udp_send_sockaddr);
+            socklen_t from_len = m_udp_send_sockaddr.GetLength();
             bytes_read = ::recvfrom (m_fd_recv, dst, dst_len, 0, (struct sockaddr *)&from, &from_len);
         }
         break;
@@ -401,13 +400,13 @@
             break;
             
         case eFDTypeSocketUDP:  // Unconnected UDP socket requiring sendto/recvfrom
-            assert (m_udp_send_sockaddr.sa_storage.ss_family != 0);
+            assert (m_udp_send_sockaddr.GetFamily() != 0);
             bytes_sent = ::sendto (m_fd_send, 
                                    src, 
                                    src_len, 
                                    0, 
-                                   &m_udp_send_sockaddr.sa, 
-                                   m_udp_send_sockaddr.sa.sa_len);
+                                   m_udp_send_sockaddr, 
+                                   m_udp_send_sockaddr.GetLength());
             break;
     }
 
@@ -896,10 +895,7 @@
         
         if (m_fd_send != -1)
         {
-            ::memset (&m_udp_send_sockaddr, 0, sizeof(m_udp_send_sockaddr));
-            ::memcpy (&m_udp_send_sockaddr, 
-                      service_info_ptr->ai_addr, 
-                      service_info_ptr->ai_addrlen);
+            m_udp_send_sockaddr = service_info_ptr;
             break;
         }
         else
@@ -977,18 +973,12 @@
 ConnectionFileDescriptor::GetSocketPort (int fd)
 {
     // We bound to port zero, so we need to figure out which port we actually bound to
-    sockaddr_t sock_addr;
-    socklen_t sock_addr_len = sizeof (sock_addr);
-    if (::getsockname (fd, &sock_addr.sa, &sock_addr_len) == 0)
-    {
-        switch (sock_addr.sa.sa_family)
-        {
-            case AF_INET:   return sock_addr.sa_ipv4.sin_port;
-            case AF_INET6:  return sock_addr.sa_ipv6.sin6_port;
-        }
-    }
-    return 0;
+    SocketAddress sock_addr;
+    socklen_t sock_addr_len = sock_addr.GetMaxLength ();
+    if (::getsockname (fd, sock_addr, &sock_addr_len) == 0)
+        return sock_addr.GetPort ();
 
+    return 0;
 }
 
 // If the read file descriptor is a socket, then return

Added: lldb/trunk/source/Host/common/SocketAddress.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/SocketAddress.cpp?rev=135488&view=auto
==============================================================================
--- lldb/trunk/source/Host/common/SocketAddress.cpp (added)
+++ lldb/trunk/source/Host/common/SocketAddress.cpp Tue Jul 19 11:44:54 2011
@@ -0,0 +1,126 @@
+//===-- SocketAddress.cpp ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/SocketAddress.h"
+#include <stddef.h>
+
+// C Includes
+#include <string.h>
+
+// C++ Includes
+// Other libraries and framework includes
+// Project includes
+
+using namespace lldb_private;
+
+//----------------------------------------------------------------------
+// SocketAddress constructor
+//----------------------------------------------------------------------
+SocketAddress::SocketAddress()
+{
+    Clear ();
+}
+
+//----------------------------------------------------------------------
+// SocketAddress copy constructor
+//----------------------------------------------------------------------
+SocketAddress::SocketAddress (const SocketAddress& rhs) :
+    m_socket_addr (rhs.m_socket_addr)
+{
+}
+
+//----------------------------------------------------------------------
+// Destructor
+//----------------------------------------------------------------------
+SocketAddress::~SocketAddress()
+{
+}
+
+void
+SocketAddress::Clear ()
+{
+    memset (&m_socket_addr, 0, sizeof(m_socket_addr));
+}
+
+bool
+SocketAddress::IsValid () const
+{
+    return GetLength () != 0;
+}
+
+socklen_t
+SocketAddress::GetLength () const
+{
+    return m_socket_addr.sa.sa_len;
+}
+
+socklen_t
+SocketAddress::GetMaxLength ()
+{
+    return sizeof (sockaddr_t);
+}
+
+void
+SocketAddress::SetLength (socklen_t len)
+{
+    m_socket_addr.sa.sa_len = len;
+}
+
+sa_family_t
+SocketAddress::GetFamily () const
+{
+    return m_socket_addr.sa.sa_family;
+}
+
+void
+SocketAddress::SetFamily (sa_family_t family)
+{
+    m_socket_addr.sa.sa_family = family;
+}
+
+in_port_t
+SocketAddress::GetPort () const
+{
+    switch (GetFamily())
+    {
+        case AF_INET:   return m_socket_addr.sa_ipv4.sin_port;
+        case AF_INET6:  return m_socket_addr.sa_ipv6.sin6_port;
+    }
+    return 0;
+}
+
+//----------------------------------------------------------------------
+// SocketAddress assignment operator
+//----------------------------------------------------------------------
+const SocketAddress&
+SocketAddress::operator=(const SocketAddress& rhs)
+{
+    if (this != &rhs)
+        m_socket_addr = rhs.m_socket_addr;
+    return *this;
+}
+
+const SocketAddress&
+SocketAddress::operator=(const struct addrinfo *addr_info)
+{
+    Clear();
+    if (addr_info && 
+        addr_info->ai_addr &&
+        addr_info->ai_addrlen > 0&& 
+        addr_info->ai_addrlen <= sizeof m_socket_addr)
+    {
+        ::memcpy (&m_socket_addr, 
+                  addr_info->ai_addr, 
+                  addr_info->ai_addrlen);
+    }
+    return *this;
+}
+
+
+





More information about the lldb-commits mailing list