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