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 } // namespace 32 33 UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit) 34 : Socket(ProtocolUdp, should_close, child_processes_inherit) {} 35 36 UDPSocket::UDPSocket(NativeSocket socket, const UDPSocket &listen_socket) 37 : Socket(ProtocolUdp, listen_socket.m_should_close_fd, 38 listen_socket.m_child_processes_inherit) { 39 m_socket = socket; 40 } 41 42 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) { 43 return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0, 44 m_sockaddr, m_sockaddr.GetLength()); 45 } 46 47 Error UDPSocket::Connect(llvm::StringRef name) { 48 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); 49 if (log) 50 log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data()); 51 52 Error error; 53 if (error.Fail()) 54 return error; 55 56 std::string host_str; 57 std::string port_str; 58 int32_t port = INT32_MIN; 59 if (!DecodeHostAndPort(name, host_str, port_str, port, &error)) 60 return error; 61 62 // At this point we have setup the receive port, now we need to 63 // setup the UDP send socket 64 65 struct addrinfo hints; 66 struct addrinfo *service_info_list = nullptr; 67 68 ::memset(&hints, 0, sizeof(hints)); 69 hints.ai_family = kDomain; 70 hints.ai_socktype = kType; 71 int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints, 72 &service_info_list); 73 if (err != 0) { 74 error.SetErrorStringWithFormat( 75 #if defined(_MSC_VER) && defined(UNICODE) 76 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)", 77 #else 78 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", 79 #endif 80 host_str.c_str(), port_str.c_str(), err, gai_strerror(err)); 81 return error; 82 } 83 84 for (struct addrinfo *service_info_ptr = service_info_list; 85 service_info_ptr != nullptr; 86 service_info_ptr = service_info_ptr->ai_next) { 87 m_socket = Socket::CreateSocket( 88 service_info_ptr->ai_family, service_info_ptr->ai_socktype, 89 service_info_ptr->ai_protocol, m_child_processes_inherit, error); 90 if (error.Success()) { 91 m_sockaddr = service_info_ptr; 92 break; 93 } else 94 continue; 95 } 96 97 ::freeaddrinfo(service_info_list); 98 99 if (IsValid()) 100 return error; 101 102 SocketAddress bind_addr; 103 104 // Only bind to the loopback address if we are expecting a connection from 105 // localhost to avoid any firewall issues. 106 const bool bind_addr_success = 107 (host_str == "127.0.0.1" || host_str == "localhost") 108 ? bind_addr.SetToLocalhost(kDomain, port) 109 : bind_addr.SetToAnyAddress(kDomain, port); 110 111 if (!bind_addr_success) { 112 error.SetErrorString("Failed to get hostspec to bind for"); 113 return error; 114 } 115 116 bind_addr.SetPort(0); // Let the source port # be determined dynamically 117 118 err = ::bind(m_socket, bind_addr, bind_addr.GetLength()); 119 120 error.Clear(); 121 return error; 122 } 123 124 Error UDPSocket::Listen(llvm::StringRef name, int backlog) { 125 return Error("%s", g_not_supported_error); 126 } 127 128 Error UDPSocket::Accept(Socket *&socket) { 129 return Error("%s", g_not_supported_error); 130 } 131 132 Error UDPSocket::CreateSocket() { 133 Error error; 134 if (IsValid()) 135 error = Close(); 136 if (error.Fail()) 137 return error; 138 m_socket = 139 Socket::CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error); 140 return error; 141 } 142 143 Error UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit, 144 Socket *&socket) { 145 std::unique_ptr<UDPSocket> final_socket( 146 new UDPSocket(true, child_processes_inherit)); 147 Error error = final_socket->Connect(name); 148 if (!error.Fail()) 149 socket = final_socket.release(); 150 return error; 151 } 152