diff --git a/lldb/include/lldb/Host/SocketAddress.h b/lldb/include/lldb/Host/SocketAddress.h index b0cda5009656..979f8fad4744 100644 --- a/lldb/include/lldb/Host/SocketAddress.h +++ b/lldb/include/lldb/Host/SocketAddress.h @@ -75,13 +75,6 @@ public: 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 //------------------------------------------------------------------ diff --git a/lldb/source/Core/ConnectionFileDescriptor.cpp b/lldb/source/Core/ConnectionFileDescriptor.cpp index c560718f0dce..5c0e97d71e21 100644 --- a/lldb/source/Core/ConnectionFileDescriptor.cpp +++ b/lldb/source/Core/ConnectionFileDescriptor.cpp @@ -842,14 +842,10 @@ ConnectionFileDescriptor::ConnectUDP (const char *host_and_port, Error *error_pt else { // Socket was created, now lets bind to the requested port - struct sockaddr_in sin; - ::memset (&sin, 0, sizeof(sin)); - sin.sin_len = sizeof(sin); - sin.sin_family = AF_INET; - sin.sin_port = 0; - sin.sin_addr.s_addr = htonl (INADDR_ANY); + SocketAddress addr; + addr.SetToLocalhost (AF_INET, 0); - if (::bind (m_fd_recv, (struct sockaddr *)&sin, sizeof(sin)) == -1) + if (::bind (m_fd_recv, addr, addr.GetLength()) == -1) { // Bind failed... if (error_ptr) diff --git a/lldb/source/Host/common/SocketAddress.cpp b/lldb/source/Host/common/SocketAddress.cpp index 7e3929d55f12..a22dc7a01c15 100644 --- a/lldb/source/Host/common/SocketAddress.cpp +++ b/lldb/source/Host/common/SocketAddress.cpp @@ -11,6 +11,7 @@ #include // C Includes +#include #include // C++ Includes @@ -77,10 +78,25 @@ SocketAddress::IsValid () const return GetLength () != 0; } +static socklen_t +GetFamilyLength (sa_family_t family) +{ + switch (family) + { + case AF_INET: return sizeof(struct sockaddr_in); + case AF_INET6: return sizeof(struct sockaddr_in6); + } + assert(0 && "Unsupported address family"); +} + socklen_t SocketAddress::GetLength () const { +#if defined(__APPLE__) return m_socket_addr.sa.sa_len; +#else + return GetFamilyLength (GetFamily()); +#endif } socklen_t @@ -89,12 +105,6 @@ 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 { @@ -105,6 +115,9 @@ void SocketAddress::SetFamily (sa_family_t family) { m_socket_addr.sa.sa_family = family; +#if defined(__APPLE__) + m_socket_addr.sa.sa_len = GetFamilyLength (family); +#endif } in_port_t @@ -228,7 +241,6 @@ SocketAddress::SetToLocalhost (sa_family_t family, in_port_t port) 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; @@ -238,7 +250,6 @@ SocketAddress::SetToLocalhost (sa_family_t family, in_port_t port) if (SetPort (port)) { m_socket_addr.sa_ipv6.sin6_addr = in6addr_any; - SetLength (sizeof(m_socket_addr.sa_ipv6)); return true; } break;