[Lldb-commits] [lldb] r299608 - Change how UDP sockets are set up -- use the same one socket for

Jason Molenda via lldb-commits lldb-commits at lists.llvm.org
Wed Apr 5 18:21:44 PDT 2017


Author: jmolenda
Date: Wed Apr  5 20:21:44 2017
New Revision: 299608

URL: http://llvm.org/viewvc/llvm-project?rev=299608&view=rev
Log:
Change how UDP sockets are set up -- use the same one socket for
both sending and receiving information, instead of using one socket
to send and another to receive.  The two socket arrangement fails over
when a firewall is between the two systems.
<rdar://problem/31286757> 

Modified:
    lldb/trunk/include/lldb/Host/Socket.h
    lldb/trunk/include/lldb/Host/common/UDPSocket.h
    lldb/trunk/source/Host/common/Socket.cpp
    lldb/trunk/source/Host/common/UDPSocket.cpp
    lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp

Modified: lldb/trunk/include/lldb/Host/Socket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Socket.h?rev=299608&r1=299607&r2=299608&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Socket.h (original)
+++ lldb/trunk/include/lldb/Host/Socket.h Wed Apr  5 20:21:44 2017
@@ -71,8 +71,7 @@ public:
   static Error TcpConnect(llvm::StringRef host_and_port,
                           bool child_processes_inherit, Socket *&socket);
   static Error UdpConnect(llvm::StringRef host_and_port,
-                          bool child_processes_inherit, Socket *&send_socket,
-                          Socket *&recv_socket);
+                          bool child_processes_inherit, Socket *&socket);
   static Error UnixDomainConnect(llvm::StringRef host_and_port,
                                  bool child_processes_inherit, Socket *&socket);
   static Error UnixDomainAccept(llvm::StringRef host_and_port,

Modified: lldb/trunk/include/lldb/Host/common/UDPSocket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/common/UDPSocket.h?rev=299608&r1=299607&r2=299608&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/common/UDPSocket.h (original)
+++ lldb/trunk/include/lldb/Host/common/UDPSocket.h Wed Apr  5 20:21:44 2017
@@ -18,7 +18,7 @@ public:
   UDPSocket(bool child_processes_inherit, Error &error);
 
   static Error Connect(llvm::StringRef name, bool child_processes_inherit,
-                       Socket *&send_socket, Socket *&recv_socket);
+                       Socket *&socket);
 
 private:
   UDPSocket(NativeSocket socket);
@@ -29,7 +29,7 @@ private:
   Error Accept(llvm::StringRef name, bool child_processes_inherit,
                Socket *&socket) override;
 
-  SocketAddress m_send_sockaddr;
+  SocketAddress m_sockaddr;
 };
 }
 

Modified: lldb/trunk/source/Host/common/Socket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Socket.cpp?rev=299608&r1=299607&r2=299608&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Socket.cpp (original)
+++ lldb/trunk/source/Host/common/Socket.cpp Wed Apr  5 20:21:44 2017
@@ -172,15 +172,13 @@ Error Socket::TcpListen(llvm::StringRef
 }
 
 Error Socket::UdpConnect(llvm::StringRef host_and_port,
-                         bool child_processes_inherit, Socket *&send_socket,
-                         Socket *&recv_socket) {
+                         bool child_processes_inherit, Socket *&socket) {
   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
   if (log)
     log->Printf("Socket::%s (host/port = %s)", __FUNCTION__,
                 host_and_port.data());
 
-  return UDPSocket::Connect(host_and_port, child_processes_inherit, send_socket,
-                            recv_socket);
+  return UDPSocket::Connect(host_and_port, child_processes_inherit, socket);
 }
 
 Error Socket::UnixDomainConnect(llvm::StringRef name,

Modified: lldb/trunk/source/Host/common/UDPSocket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/UDPSocket.cpp?rev=299608&r1=299607&r2=299608&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/UDPSocket.cpp (original)
+++ lldb/trunk/source/Host/common/UDPSocket.cpp Wed Apr  5 20:21:44 2017
@@ -38,7 +38,7 @@ UDPSocket::UDPSocket(bool child_processe
 
 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) {
   return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0,
-                  m_send_sockaddr, m_send_sockaddr.GetLength());
+                  m_sockaddr, m_sockaddr.GetLength());
 }
 
 Error UDPSocket::Connect(llvm::StringRef name) {
@@ -55,9 +55,8 @@ Error UDPSocket::Accept(llvm::StringRef
 }
 
 Error UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit,
-                         Socket *&send_socket, Socket *&recv_socket) {
-  std::unique_ptr<UDPSocket> final_send_socket;
-  std::unique_ptr<UDPSocket> final_recv_socket;
+                         Socket *&socket) {
+  std::unique_ptr<UDPSocket> final_socket;
 
   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
   if (log)
@@ -70,25 +69,6 @@ Error UDPSocket::Connect(llvm::StringRef
   if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
     return error;
 
-  // Setup the receiving end of the UDP connection on this localhost
-  // on port zero. After we bind to port zero we can read the port.
-  final_recv_socket.reset(new UDPSocket(child_processes_inherit, error));
-  if (error.Success()) {
-    // Socket was created, now lets bind to the requested port
-    SocketAddress addr;
-    addr.SetToAnyAddress(AF_INET, 0);
-
-    if (::bind(final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) ==
-        -1) {
-      // Bind failed...
-      SetLastError(error);
-    }
-  }
-
-  assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
-  if (error.Fail())
-    return error;
-
   // At this point we have setup the receive port, now we need to
   // setup the UDP send socket
 
@@ -118,8 +98,8 @@ Error UDPSocket::Connect(llvm::StringRef
         service_info_ptr->ai_family, service_info_ptr->ai_socktype,
         service_info_ptr->ai_protocol, child_processes_inherit, error);
     if (error.Success()) {
-      final_send_socket.reset(new UDPSocket(send_fd));
-      final_send_socket->m_send_sockaddr = service_info_ptr;
+      final_socket.reset(new UDPSocket(send_fd));
+      final_socket->m_sockaddr = service_info_ptr;
       break;
     } else
       continue;
@@ -127,11 +107,31 @@ Error UDPSocket::Connect(llvm::StringRef
 
   ::freeaddrinfo(service_info_list);
 
-  if (!final_send_socket)
+  if (!final_socket)
+    return error;
+
+  SocketAddress bind_addr;
+
+  // Only bind to the loopback address if we are expecting a connection from
+  // localhost to avoid any firewall issues.
+  const bool bind_addr_success = (host_str == "127.0.0.1" || host_str == "localhost")
+                                     ? bind_addr.SetToLocalhost(kDomain, port)
+                                     : bind_addr.SetToAnyAddress(kDomain, port);
+
+  if (!bind_addr_success) {
+    error.SetErrorString("Failed to get hostspec to bind for");
     return error;
+  }
+
+  bind_addr.SetPort(0); // Let the source port # be determined dynamically
+
+  err = ::bind(final_socket->GetNativeSocket(), bind_addr, bind_addr.GetLength());
+
+  struct sockaddr_in source_info;
+  socklen_t address_len = sizeof (struct sockaddr_in);
+  err = ::getsockname(final_socket->GetNativeSocket(), (struct sockaddr *) &source_info, &address_len);
 
-  send_socket = final_send_socket.release();
-  recv_socket = final_recv_socket.release();
+  socket = final_socket.release();
   error.Clear();
   return error;
 }

Modified: lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp?rev=299608&r1=299607&r2=299608&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp Wed Apr  5 20:21:44 2017
@@ -748,14 +748,12 @@ ConnectionStatus ConnectionFileDescripto
 
 ConnectionStatus ConnectionFileDescriptor::ConnectUDP(llvm::StringRef s,
                                                       Error *error_ptr) {
-  Socket *send_socket = nullptr;
-  Socket *recv_socket = nullptr;
-  Error error = Socket::UdpConnect(s, m_child_processes_inherit, send_socket,
-                                   recv_socket);
+  Socket *socket = nullptr;
+  Error error = Socket::UdpConnect(s, m_child_processes_inherit, socket);
   if (error_ptr)
     *error_ptr = error;
-  m_write_sp.reset(send_socket);
-  m_read_sp.reset(recv_socket);
+  m_write_sp.reset(socket);
+  m_read_sp = m_write_sp;
   if (error.Fail()) {
     return eConnectionStatusError;
   }




More information about the lldb-commits mailing list