1 //===-- UDPSocket.cpp -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/common/UDPSocket.h" 11 12 #include "lldb/Host/Config.h" 13 #include "lldb/Utility/Log.h" 14 15 #ifndef LLDB_DISABLE_POSIX 16 #include <arpa/inet.h> 17 #include <sys/socket.h> 18 #endif 19 20 #include <memory> 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 namespace { 26 27 const int kDomain = AF_INET; 28 const int kType = SOCK_DGRAM; 29 30 static const char *g_not_supported_error = "Not supported"; 31 } 32 33 UDPSocket::UDPSocket(NativeSocket socket) : Socket(ProtocolUdp, true, true) { 34 m_socket = socket; 35 } 36 37 UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit) 38 : Socket(ProtocolUdp, should_close, child_processes_inherit) {} 39 40 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) { 41 return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0, 42 m_sockaddr, m_sockaddr.GetLength()); 43 } 44 45 Status UDPSocket::Connect(llvm::StringRef name) { 46 return Status("%s", g_not_supported_error); 47 } 48 49 Status UDPSocket::Listen(llvm::StringRef name, int backlog) { 50 return Status("%s", g_not_supported_error); 51 } 52 53 Status UDPSocket::Accept(Socket *&socket) { 54 return Status("%s", g_not_supported_error); 55 } 56 57 Status UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit, 58 Socket *&socket) { 59 std::unique_ptr<UDPSocket> final_socket; 60 61 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); 62 if (log) 63 log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data()); 64 65 Status error; 66 std::string host_str; 67 std::string port_str; 68 int32_t port = INT32_MIN; 69 if (!DecodeHostAndPort(name, host_str, port_str, port, &error)) 70 return error; 71 72 // At this point we have setup the receive port, now we need to 73 // setup the UDP send socket 74 75 struct addrinfo hints; 76 struct addrinfo *service_info_list = nullptr; 77 78 ::memset(&hints, 0, sizeof(hints)); 79 hints.ai_family = kDomain; 80 hints.ai_socktype = kType; 81 int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints, 82 &service_info_list); 83 if (err != 0) { 84 error.SetErrorStringWithFormat( 85 #if defined(_MSC_VER) && defined(UNICODE) 86 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)", 87 #else 88 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", 89 #endif 90 host_str.c_str(), port_str.c_str(), err, gai_strerror(err)); 91 return error; 92 } 93 94 for (struct addrinfo *service_info_ptr = service_info_list; 95 service_info_ptr != nullptr; 96 service_info_ptr = service_info_ptr->ai_next) { 97 auto send_fd = CreateSocket( 98 service_info_ptr->ai_family, service_info_ptr->ai_socktype, 99 service_info_ptr->ai_protocol, child_processes_inherit, error); 100 if (error.Success()) { 101 final_socket.reset(new UDPSocket(send_fd)); 102 final_socket->m_sockaddr = service_info_ptr; 103 break; 104 } else 105 continue; 106 } 107 108 ::freeaddrinfo(service_info_list); 109 110 if (!final_socket) 111 return error; 112 113 SocketAddress bind_addr; 114 115 // Only bind to the loopback address if we are expecting a connection from 116 // localhost to avoid any firewall issues. 117 const bool bind_addr_success = (host_str == "127.0.0.1" || host_str == "localhost") 118 ? bind_addr.SetToLocalhost(kDomain, port) 119 : bind_addr.SetToAnyAddress(kDomain, port); 120 121 if (!bind_addr_success) { 122 error.SetErrorString("Failed to get hostspec to bind for"); 123 return error; 124 } 125 126 bind_addr.SetPort(0); // Let the source port # be determined dynamically 127 128 err = ::bind(final_socket->GetNativeSocket(), bind_addr, bind_addr.GetLength()); 129 130 struct sockaddr_in source_info; 131 socklen_t address_len = sizeof (struct sockaddr_in); 132 err = ::getsockname(final_socket->GetNativeSocket(), (struct sockaddr *) &source_info, &address_len); 133 134 socket = final_socket.release(); 135 error.Clear(); 136 return error; 137 } 138