LLDB  mainline
UDPSocket.cpp
Go to the documentation of this file.
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 
10 
11 #include "lldb/Host/Config.h"
12 #include "lldb/Utility/LLDBLog.h"
13 #include "lldb/Utility/Log.h"
14 
15 #if LLDB_ENABLE_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 static const int kDomain = AF_INET;
26 static const int kType = SOCK_DGRAM;
27 
28 static const char *g_not_supported_error = "Not supported";
29 
30 UDPSocket::UDPSocket(NativeSocket socket) : Socket(ProtocolUdp, true, true) {
31  m_socket = socket;
32 }
33 
34 UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit)
35  : Socket(ProtocolUdp, should_close, child_processes_inherit) {}
36 
37 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) {
38  return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0,
40 }
41 
42 Status UDPSocket::Connect(llvm::StringRef name) {
43  return Status("%s", g_not_supported_error);
44 }
45 
46 Status UDPSocket::Listen(llvm::StringRef name, int backlog) {
47  return Status("%s", g_not_supported_error);
48 }
49 
51  return Status("%s", g_not_supported_error);
52 }
53 
54 llvm::Expected<std::unique_ptr<UDPSocket>>
55 UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit) {
56  std::unique_ptr<UDPSocket> socket;
57 
59  LLDB_LOG(log, "host/port = {0}", name);
60 
61  Status error;
62  llvm::Expected<HostAndPort> host_port = DecodeHostAndPort(name);
63  if (!host_port)
64  return host_port.takeError();
65 
66  // At this point we have setup the receive port, now we need to setup the UDP
67  // send socket
68 
69  struct addrinfo hints;
70  struct addrinfo *service_info_list = nullptr;
71 
72  ::memset(&hints, 0, sizeof(hints));
73  hints.ai_family = kDomain;
74  hints.ai_socktype = kType;
75  int err = ::getaddrinfo(host_port->hostname.c_str(), std::to_string(host_port->port).c_str(), &hints,
76  &service_info_list);
77  if (err != 0) {
78  error.SetErrorStringWithFormat(
79 #if defined(_WIN32) && defined(UNICODE)
80  "getaddrinfo(%s, %d, &hints, &info) returned error %i (%S)",
81 #else
82  "getaddrinfo(%s, %d, &hints, &info) returned error %i (%s)",
83 #endif
84  host_port->hostname.c_str(), host_port->port, err, gai_strerror(err));
85  return error.ToError();
86  }
87 
88  for (struct addrinfo *service_info_ptr = service_info_list;
89  service_info_ptr != nullptr;
90  service_info_ptr = service_info_ptr->ai_next) {
91  auto send_fd = CreateSocket(
92  service_info_ptr->ai_family, service_info_ptr->ai_socktype,
93  service_info_ptr->ai_protocol, child_processes_inherit, error);
94  if (error.Success()) {
95  socket.reset(new UDPSocket(send_fd));
96  socket->m_sockaddr = service_info_ptr;
97  break;
98  } else
99  continue;
100  }
101 
102  ::freeaddrinfo(service_info_list);
103 
104  if (!socket)
105  return error.ToError();
106 
107  SocketAddress bind_addr;
108 
109  // Only bind to the loopback address if we are expecting a connection from
110  // localhost to avoid any firewall issues.
111  const bool bind_addr_success = (host_port->hostname == "127.0.0.1" || host_port->hostname == "localhost")
112  ? bind_addr.SetToLocalhost(kDomain, host_port->port)
113  : bind_addr.SetToAnyAddress(kDomain, host_port->port);
114 
115  if (!bind_addr_success) {
116  error.SetErrorString("Failed to get hostspec to bind for");
117  return error.ToError();
118  }
119 
120  bind_addr.SetPort(0); // Let the source port # be determined dynamically
121 
122  err = ::bind(socket->GetNativeSocket(), bind_addr, bind_addr.GetLength());
123 
124  struct sockaddr_in source_info;
125  socklen_t address_len = sizeof (struct sockaddr_in);
126  err = ::getsockname(socket->GetNativeSocket(),
127  (struct sockaddr *)&source_info, &address_len);
128 
129  return std::move(socket);
130 }
131 
133  if (m_socket != kInvalidSocketValue) {
134  return std::string(llvm::formatv(
135  "udp://[{0}]:{1}", m_sockaddr.GetIPAddress(), m_sockaddr.GetPort()));
136  }
137  return "";
138 }
lldb_private::Socket::kInvalidSocketValue
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:59
lldb_private::SocketAddress::GetIPAddress
std::string GetIPAddress() const
Definition: SocketAddress.cpp:136
lldb_private::SocketAddress::GetPort
uint16_t GetPort() const
Definition: SocketAddress.cpp:153
lldb_private::Socket
Definition: Socket.h:41
lldb_private::UDPSocket::Send
size_t Send(const void *buf, const size_t num_bytes) override
Definition: UDPSocket.cpp:37
lldb_private::SocketAddress::SetToAnyAddress
bool SetToAnyAddress(sa_family_t family, uint16_t port)
Definition: SocketAddress.cpp:269
lldb_private::UDPSocket::Accept
Status Accept(Socket *&socket) override
Definition: UDPSocket.cpp:50
lldb_private::SocketAddress::SetPort
bool SetPort(uint16_t port)
Definition: SocketAddress.cpp:163
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::LLDBLog::Connection
@ Connection
Log.h
lldb_private::Socket::m_socket
NativeSocket m_socket
Definition: Socket.h:123
kDomain
static const int kDomain
Definition: UDPSocket.cpp:25
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::SocketAddress::GetLength
socklen_t GetLength() const
Definition: SocketAddress.cpp:115
lldb_private::NativeSocket
int NativeSocket
Definition: Socket.h:36
lldb_private::UDPSocket::Listen
Status Listen(llvm::StringRef name, int backlog) override
Definition: UDPSocket.cpp:46
lldb_private::Status
Definition: Status.h:44
lldb_private::UDPSocket::UDPSocket
UDPSocket(bool should_close, bool child_processes_inherit)
Definition: UDPSocket.cpp:34
lldb_private::Socket::DecodeHostAndPort
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Definition: Socket.cpp:188
lldb_private::Socket::CreateSocket
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
lldb_private::SocketAddress::SetToLocalhost
bool SetToLocalhost(sa_family_t family, uint16_t port)
Definition: SocketAddress.cpp:247
g_not_supported_error
static const char * g_not_supported_error
Definition: UDPSocket.cpp:28
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:336
lldb_private::UDPSocket::Connect
static llvm::Expected< std::unique_ptr< UDPSocket > > Connect(llvm::StringRef name, bool child_processes_inherit)
Definition: UDPSocket.cpp:55
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
kType
static const int kType
Definition: UDPSocket.cpp:26
lldb_private::UDPSocket::m_sockaddr
SocketAddress m_sockaddr
Definition: UDPSocket.h:32
lldb_private::Log
Definition: Log.h:115
lldb_private::UDPSocket::GetRemoteConnectionURI
std::string GetRemoteConnectionURI() const override
Definition: UDPSocket.cpp:132
UDPSocket.h
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
lldb_private::SocketAddress
Definition: SocketAddress.h:34
lldb
Definition: SBAddress.h:15
LLDBLog.h
endif
LLDBWrapPython cpp endif() if(LLDB_ENABLE_LUA) get_target_property(lua_bindings_dir swig_wrapper_lua BINARY_DIR) set(lldb_lua_wrapper $
Definition: API/CMakeLists.txt:11