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