1dda28197Spatrick //===-- UDPSocket.cpp -----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Host/common/UDPSocket.h"
10061da546Spatrick
11061da546Spatrick #include "lldb/Host/Config.h"
12*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
13061da546Spatrick #include "lldb/Utility/Log.h"
14061da546Spatrick
15061da546Spatrick #if LLDB_ENABLE_POSIX
16061da546Spatrick #include <arpa/inet.h>
17061da546Spatrick #include <sys/socket.h>
18061da546Spatrick #endif
19061da546Spatrick
20061da546Spatrick #include <memory>
21061da546Spatrick
22061da546Spatrick using namespace lldb;
23061da546Spatrick using namespace lldb_private;
24061da546Spatrick
25*f6aab3d8Srobert static const int kDomain = AF_INET;
26*f6aab3d8Srobert static const int kType = SOCK_DGRAM;
27061da546Spatrick
28061da546Spatrick static const char *g_not_supported_error = "Not supported";
29061da546Spatrick
UDPSocket(NativeSocket socket)30061da546Spatrick UDPSocket::UDPSocket(NativeSocket socket) : Socket(ProtocolUdp, true, true) {
31061da546Spatrick m_socket = socket;
32061da546Spatrick }
33061da546Spatrick
UDPSocket(bool should_close,bool child_processes_inherit)34061da546Spatrick UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit)
35061da546Spatrick : Socket(ProtocolUdp, should_close, child_processes_inherit) {}
36061da546Spatrick
Send(const void * buf,const size_t num_bytes)37061da546Spatrick size_t UDPSocket::Send(const void *buf, const size_t num_bytes) {
38061da546Spatrick return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0,
39061da546Spatrick m_sockaddr, m_sockaddr.GetLength());
40061da546Spatrick }
41061da546Spatrick
Connect(llvm::StringRef name)42061da546Spatrick Status UDPSocket::Connect(llvm::StringRef name) {
43061da546Spatrick return Status("%s", g_not_supported_error);
44061da546Spatrick }
45061da546Spatrick
Listen(llvm::StringRef name,int backlog)46061da546Spatrick Status UDPSocket::Listen(llvm::StringRef name, int backlog) {
47061da546Spatrick return Status("%s", g_not_supported_error);
48061da546Spatrick }
49061da546Spatrick
Accept(Socket * & socket)50061da546Spatrick Status UDPSocket::Accept(Socket *&socket) {
51061da546Spatrick return Status("%s", g_not_supported_error);
52061da546Spatrick }
53061da546Spatrick
54dda28197Spatrick llvm::Expected<std::unique_ptr<UDPSocket>>
Connect(llvm::StringRef name,bool child_processes_inherit)55dda28197Spatrick UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit) {
56dda28197Spatrick std::unique_ptr<UDPSocket> socket;
57061da546Spatrick
58*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Connection);
59dda28197Spatrick LLDB_LOG(log, "host/port = {0}", name);
60061da546Spatrick
61061da546Spatrick Status error;
62*f6aab3d8Srobert llvm::Expected<HostAndPort> host_port = DecodeHostAndPort(name);
63*f6aab3d8Srobert if (!host_port)
64*f6aab3d8Srobert return host_port.takeError();
65061da546Spatrick
66061da546Spatrick // At this point we have setup the receive port, now we need to setup the UDP
67061da546Spatrick // send socket
68061da546Spatrick
69061da546Spatrick struct addrinfo hints;
70061da546Spatrick struct addrinfo *service_info_list = nullptr;
71061da546Spatrick
72061da546Spatrick ::memset(&hints, 0, sizeof(hints));
73061da546Spatrick hints.ai_family = kDomain;
74061da546Spatrick hints.ai_socktype = kType;
75*f6aab3d8Srobert int err = ::getaddrinfo(host_port->hostname.c_str(), std::to_string(host_port->port).c_str(), &hints,
76061da546Spatrick &service_info_list);
77061da546Spatrick if (err != 0) {
78061da546Spatrick error.SetErrorStringWithFormat(
79061da546Spatrick #if defined(_WIN32) && defined(UNICODE)
80*f6aab3d8Srobert "getaddrinfo(%s, %d, &hints, &info) returned error %i (%S)",
81061da546Spatrick #else
82*f6aab3d8Srobert "getaddrinfo(%s, %d, &hints, &info) returned error %i (%s)",
83061da546Spatrick #endif
84*f6aab3d8Srobert host_port->hostname.c_str(), host_port->port, err, gai_strerror(err));
85dda28197Spatrick return error.ToError();
86061da546Spatrick }
87061da546Spatrick
88061da546Spatrick for (struct addrinfo *service_info_ptr = service_info_list;
89061da546Spatrick service_info_ptr != nullptr;
90061da546Spatrick service_info_ptr = service_info_ptr->ai_next) {
91061da546Spatrick auto send_fd = CreateSocket(
92061da546Spatrick service_info_ptr->ai_family, service_info_ptr->ai_socktype,
93061da546Spatrick service_info_ptr->ai_protocol, child_processes_inherit, error);
94061da546Spatrick if (error.Success()) {
95dda28197Spatrick socket.reset(new UDPSocket(send_fd));
96dda28197Spatrick socket->m_sockaddr = service_info_ptr;
97061da546Spatrick break;
98061da546Spatrick } else
99061da546Spatrick continue;
100061da546Spatrick }
101061da546Spatrick
102061da546Spatrick ::freeaddrinfo(service_info_list);
103061da546Spatrick
104dda28197Spatrick if (!socket)
105dda28197Spatrick return error.ToError();
106061da546Spatrick
107061da546Spatrick SocketAddress bind_addr;
108061da546Spatrick
109061da546Spatrick // Only bind to the loopback address if we are expecting a connection from
110061da546Spatrick // localhost to avoid any firewall issues.
111*f6aab3d8Srobert const bool bind_addr_success = (host_port->hostname == "127.0.0.1" || host_port->hostname == "localhost")
112*f6aab3d8Srobert ? bind_addr.SetToLocalhost(kDomain, host_port->port)
113*f6aab3d8Srobert : bind_addr.SetToAnyAddress(kDomain, host_port->port);
114061da546Spatrick
115061da546Spatrick if (!bind_addr_success) {
116061da546Spatrick error.SetErrorString("Failed to get hostspec to bind for");
117dda28197Spatrick return error.ToError();
118061da546Spatrick }
119061da546Spatrick
120061da546Spatrick bind_addr.SetPort(0); // Let the source port # be determined dynamically
121061da546Spatrick
122dda28197Spatrick err = ::bind(socket->GetNativeSocket(), bind_addr, bind_addr.GetLength());
123061da546Spatrick
124061da546Spatrick struct sockaddr_in source_info;
125061da546Spatrick socklen_t address_len = sizeof (struct sockaddr_in);
126dda28197Spatrick err = ::getsockname(socket->GetNativeSocket(),
127dda28197Spatrick (struct sockaddr *)&source_info, &address_len);
128061da546Spatrick
129dda28197Spatrick return std::move(socket);
130061da546Spatrick }
131061da546Spatrick
GetRemoteConnectionURI() const132061da546Spatrick std::string UDPSocket::GetRemoteConnectionURI() const {
133061da546Spatrick if (m_socket != kInvalidSocketValue) {
134dda28197Spatrick return std::string(llvm::formatv(
135dda28197Spatrick "udp://[{0}]:{1}", m_sockaddr.GetIPAddress(), m_sockaddr.GetPort()));
136061da546Spatrick }
137061da546Spatrick return "";
138061da546Spatrick }
139