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

Greg Clayton gclayton at apple.com
Tue Jul 19 13:03:42 PDT 2011


Author: gclayton
Date: Tue Jul 19 15:03:42 2011
New Revision: 135511

URL: http://llvm.org/viewvc/llvm-project?rev=135511&view=rev
Log:
Added some more functionality to SocketAddress and modified 
ConnectionFileDescriptor to use it.


Modified:
    lldb/trunk/include/lldb/Host/SocketAddress.h
    lldb/trunk/source/Core/ConnectionFileDescriptor.cpp
    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=135511&r1=135510&r2=135511&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/SocketAddress.h (original)
+++ lldb/trunk/include/lldb/Host/SocketAddress.h Tue Jul 19 15:03:42 2011
@@ -28,9 +28,13 @@
     //------------------------------------------------------------------
     // Constructors and Destructors
     //------------------------------------------------------------------
-    SocketAddress();
-    SocketAddress(const SocketAddress& rhs);
-    ~SocketAddress();
+    SocketAddress ();
+    SocketAddress (const struct sockaddr &s);
+    SocketAddress (const struct sockaddr_in &s);
+    SocketAddress (const struct sockaddr_in6 &s);
+    SocketAddress (const struct sockaddr_storage &s);
+    SocketAddress (const SocketAddress& rhs);
+    ~SocketAddress ();
 
     //------------------------------------------------------------------
     // Operators
@@ -41,30 +45,99 @@
     const SocketAddress&
     operator=(const struct addrinfo *addr_info);
 
+    const SocketAddress&
+    operator=(const struct sockaddr &s);
+
+    const SocketAddress&
+    operator=(const struct sockaddr_in &s);
+
+    const SocketAddress&
+    operator=(const struct sockaddr_in6 &s);
+
+    const SocketAddress&
+    operator=(const struct sockaddr_storage &s);
+    
+    //------------------------------------------------------------------
+    // Clear the contents of this socket address
+    //------------------------------------------------------------------
     void
     Clear ();
 
+    //------------------------------------------------------------------
+    // Get the length for the current socket address family
+    //------------------------------------------------------------------
     socklen_t
     GetLength () const;
 
+    //------------------------------------------------------------------
+    // Get the mex length for the the largest socket address supported.
+    //------------------------------------------------------------------
     static socklen_t
     GetMaxLength ();
 
+    //------------------------------------------------------------------
+    // Set the length manually if supported in the socket address 
+    // structures
+    //------------------------------------------------------------------
     void
     SetLength (socklen_t len);
     
+    //------------------------------------------------------------------
+    // Get the socket address family 
+    //------------------------------------------------------------------
     sa_family_t
     GetFamily () const;
 
+    //------------------------------------------------------------------
+    // Set the socket address family 
+    //------------------------------------------------------------------
     void
     SetFamily (sa_family_t family);
 
+    //------------------------------------------------------------------
+    // Get the port if the socket address for the family has a port
+    //------------------------------------------------------------------
     in_port_t
     GetPort () const;
 
+    //------------------------------------------------------------------
+    // Set the port if the socket address for the family has a port. 
+    // The family must be set correctly prior to calling this function.
+    //------------------------------------------------------------------
+    bool
+    SetPort (in_port_t port);
+
+    //------------------------------------------------------------------
+    // Set the socket address according to the first match from a call
+    // to getaddrinfo() (or equivalent functions for systems that don't
+    // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
+    // filled in with the match that was used to populate this socket
+    // address.
+    //------------------------------------------------------------------
+    bool
+    SetAddress (const struct addrinfo *hints_ptr,   // Optional hints where the family, protocol and other things can be specified.
+                const char *host,                   // Hostname ("foo.bar.com" or "foo" or IP address string ("123.234.12.1" or "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
+                const char *service,                // Protocol name ("tcp", "http", etc) or a raw port number string ("81")
+                struct addrinfo *addr_info_ptr);    // If non-NULL, this will get filled in with the match
+
+    //------------------------------------------------------------------
+    // Quick way to set the SocketAddress to localhost given the family.
+    // Returns true if successful, false if "family" doesn't support 
+    // localhost or if "family" is not supported by this class.
+    //------------------------------------------------------------------
+    bool
+    SetToLocalhost (sa_family_t family, 
+                    in_port_t port);
+
+    //------------------------------------------------------------------
+    // Returns true if there is a valid socket address in this object.
+    //------------------------------------------------------------------
     bool
     IsValid () const;
 
+    //------------------------------------------------------------------
+    // Direct access to all of the sockaddr structures
+    //------------------------------------------------------------------
     struct sockaddr &
     sockaddr ()
     {
@@ -113,9 +186,14 @@
     {
         return m_socket_addr.sa_storage;
     }
+    
+    
     //------------------------------------------------------------------
     // Conversion operators to allow getting the contents of this class
-    // as a subclass
+    // as a pointer to the appropriate structure. This allows an instance
+    // of this class to be used in calls that take one of the sockaddr
+    // structure variants without having to manally use the correct
+    // accessor function.
     //------------------------------------------------------------------
     
     operator struct sockaddr * ()
@@ -158,6 +236,7 @@
         return &m_socket_addr.sa_storage;
     }
 
+    
 protected:
     typedef union sockaddr_tag
     {

Modified: lldb/trunk/source/Core/ConnectionFileDescriptor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ConnectionFileDescriptor.cpp?rev=135511&r1=135510&r2=135511&view=diff
==============================================================================
--- lldb/trunk/source/Core/ConnectionFileDescriptor.cpp (original)
+++ lldb/trunk/source/Core/ConnectionFileDescriptor.cpp Tue Jul 19 15:03:42 2011
@@ -697,37 +697,35 @@
     // enable local address reuse
     SetSocketOption (listen_port, SOL_SOCKET, SO_REUSEADDR, 1);
 
-    struct sockaddr_in sa;
-    ::memset (&sa, 0, sizeof sa);
-    sa.sin_family = AF_INET;
-    sa.sin_port = htons (listen_port_num);
-    sa.sin_addr.s_addr = htonl (INADDR_ANY);
-
-    int err = ::bind (listen_port, (struct sockaddr *) &sa, sizeof(sa));
-    if (err == -1)
+    SocketAddress localhost;
+    if (localhost.SetToLocalhost (AF_INET, listen_port_num))
     {
-        if (error_ptr)
-            error_ptr->SetErrorToErrno();
-        Close (listen_port, NULL);
-        return eConnectionStatusError;
-    }
+        int err = ::bind (listen_port, localhost, localhost.GetLength());
+        if (err == -1)
+        {
+            if (error_ptr)
+                error_ptr->SetErrorToErrno();
+            Close (listen_port, NULL);
+            return eConnectionStatusError;
+        }
 
-    err = ::listen (listen_port, 1);
-    if (err == -1)
-    {
-        if (error_ptr)
-            error_ptr->SetErrorToErrno();
-        Close (listen_port, NULL);
-        return eConnectionStatusError;
-    }
+        err = ::listen (listen_port, 1);
+        if (err == -1)
+        {
+            if (error_ptr)
+                error_ptr->SetErrorToErrno();
+            Close (listen_port, NULL);
+            return eConnectionStatusError;
+        }
 
-    m_fd_send = m_fd_recv = ::accept (listen_port, NULL, 0);
-    if (m_fd_send == -1)
-    {
-        if (error_ptr)
-            error_ptr->SetErrorToErrno();
-        Close (listen_port, NULL);
-        return eConnectionStatusError;
+        m_fd_send = m_fd_recv = ::accept (listen_port, NULL, 0);
+        if (m_fd_send == -1)
+        {
+            if (error_ptr)
+                error_ptr->SetErrorToErrno();
+            Close (listen_port, NULL);
+            return eConnectionStatusError;
+        }
     }
 
     // We are done with the listen port

Modified: lldb/trunk/source/Host/common/SocketAddress.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/SocketAddress.cpp?rev=135511&r1=135510&r2=135511&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/SocketAddress.cpp (original)
+++ lldb/trunk/source/Host/common/SocketAddress.cpp Tue Jul 19 15:03:42 2011
@@ -22,11 +22,34 @@
 //----------------------------------------------------------------------
 // SocketAddress constructor
 //----------------------------------------------------------------------
-SocketAddress::SocketAddress()
+SocketAddress::SocketAddress ()
 {
     Clear ();
 }
 
+SocketAddress::SocketAddress (const struct sockaddr &s)
+{
+    m_socket_addr.sa = s;
+}
+
+
+SocketAddress::SocketAddress (const struct sockaddr_in &s)
+{
+    m_socket_addr.sa_ipv4 = s;
+}
+
+
+SocketAddress::SocketAddress (const struct sockaddr_in6 &s)
+{
+    m_socket_addr.sa_ipv6 = s;
+}
+
+
+SocketAddress::SocketAddress (const struct sockaddr_storage &s)
+{
+    m_socket_addr.sa_storage = s;
+}
+
 //----------------------------------------------------------------------
 // SocketAddress copy constructor
 //----------------------------------------------------------------------
@@ -95,6 +118,22 @@
     return 0;
 }
 
+bool
+SocketAddress::SetPort (in_port_t port)
+{
+    switch (GetFamily())
+    {
+        case AF_INET:   
+            m_socket_addr.sa_ipv4.sin_port = htons(port);
+            return true;
+
+        case AF_INET6:  
+            m_socket_addr.sa_ipv6.sin6_port = htons(port);
+            return true;
+    }
+    return false;
+}
+
 //----------------------------------------------------------------------
 // SocketAddress assignment operator
 //----------------------------------------------------------------------
@@ -122,5 +161,89 @@
     return *this;
 }
 
+const SocketAddress&
+SocketAddress::operator=(const struct sockaddr &s)
+{
+    m_socket_addr.sa = s;
+    return *this;
+}
 
+const SocketAddress&
+SocketAddress::operator=(const struct sockaddr_in &s)
+{
+    m_socket_addr.sa_ipv4 = s;
+    return *this;
+}
+
+const SocketAddress&
+SocketAddress::operator=(const struct sockaddr_in6 &s)
+{
+    m_socket_addr.sa_ipv6 = s;
+    return *this;
+}
 
+const SocketAddress&
+SocketAddress::operator=(const struct sockaddr_storage &s)
+{
+    m_socket_addr.sa_storage = s;
+    return *this;
+}
+
+bool
+SocketAddress::SetAddress (const struct addrinfo *hints_ptr,
+                           const char *host, 
+                           const char *service,
+                           struct addrinfo *addr_info_ptr)
+{
+    struct addrinfo *service_info_list = NULL;
+    int err = ::getaddrinfo (host, service, hints_ptr, &service_info_list);
+    if (err == 0 && service_info_list)
+    {
+        if (addr_info_ptr)
+            *addr_info_ptr = *service_info_list;
+        *this = service_info_list;
+    }
+    else
+        Clear();
+    
+    :: freeaddrinfo (service_info_list);
+    
+    const bool is_valid = IsValid();
+    if (!is_valid)
+    {
+        if (addr_info_ptr)
+            ::memset (addr_info_ptr, 0, sizeof(struct addrinfo));
+    }
+    return IsValid();
+}
+
+
+bool
+SocketAddress::SetToLocalhost (sa_family_t family, in_port_t port)
+{
+    switch (family)
+    {
+        case AF_INET:   
+            SetFamily (AF_INET);
+            if (SetPort (port))
+            {
+                m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl (INADDR_ANY);
+                SetLength (sizeof(m_socket_addr.sa_ipv4));
+                return true;
+            }
+            break;
+
+        case AF_INET6:  
+            SetFamily (AF_INET6);
+            if (SetPort (port))
+            {
+                m_socket_addr.sa_ipv6.sin6_addr = in6addr_any;
+                SetLength (sizeof(m_socket_addr.sa_ipv6));
+                return true;
+            }            
+            break;
+
+    }
+    Clear();
+    return false;
+}





More information about the lldb-commits mailing list