diff options
| author | Jason Molenda <jmolenda@apple.com> | 2017-04-06 01:21:44 +0000 |
|---|---|---|
| committer | Jason Molenda <jmolenda@apple.com> | 2017-04-06 01:21:44 +0000 |
| commit | 90dce06f24e7a1587ffe15cd77ce64c508dc320a (patch) | |
| tree | d3d6bba623a7445cd4799e8b8ecbcac6c81f5eb9 | |
| parent | 9cf881e62a7b38dc0bb3cb0f17b9214e0e1c899e (diff) | |
| download | bcm5719-llvm-90dce06f24e7a1587ffe15cd77ce64c508dc320a.tar.gz bcm5719-llvm-90dce06f24e7a1587ffe15cd77ce64c508dc320a.zip | |
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>
llvm-svn: 299608
| -rw-r--r-- | lldb/include/lldb/Host/Socket.h | 3 | ||||
| -rw-r--r-- | lldb/include/lldb/Host/common/UDPSocket.h | 4 | ||||
| -rw-r--r-- | lldb/source/Host/common/Socket.cpp | 6 | ||||
| -rw-r--r-- | lldb/source/Host/common/UDPSocket.cpp | 56 | ||||
| -rw-r--r-- | lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp | 10 |
5 files changed, 37 insertions, 42 deletions
diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h index ba0bc8a122a..386133e9695 100644 --- a/lldb/include/lldb/Host/Socket.h +++ b/lldb/include/lldb/Host/Socket.h @@ -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, diff --git a/lldb/include/lldb/Host/common/UDPSocket.h b/lldb/include/lldb/Host/common/UDPSocket.h index 153804be260..507c9827caf 100644 --- a/lldb/include/lldb/Host/common/UDPSocket.h +++ b/lldb/include/lldb/Host/common/UDPSocket.h @@ -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; }; } diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp index 7e5f2106fc8..2a665ddacb6 100644 --- a/lldb/source/Host/common/Socket.cpp +++ b/lldb/source/Host/common/Socket.cpp @@ -172,15 +172,13 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, } 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, diff --git a/lldb/source/Host/common/UDPSocket.cpp b/lldb/source/Host/common/UDPSocket.cpp index 830a2c2ce29..7ca62e7496b 100644 --- a/lldb/source/Host/common/UDPSocket.cpp +++ b/lldb/source/Host/common/UDPSocket.cpp @@ -38,7 +38,7 @@ UDPSocket::UDPSocket(bool child_processes_inherit, Error &error) 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 name, bool child_processes_inherit, } 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 name, bool child_processes_inherit, 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 name, bool child_processes_inherit, 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 name, bool child_processes_inherit, ::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; } diff --git a/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp b/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp index d5a2f5ea4ba..a3ac36558e3 100644 --- a/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp +++ b/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp @@ -748,14 +748,12 @@ ConnectionStatus ConnectionFileDescriptor::ConnectTCP(llvm::StringRef s, 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; } |

