11#include "lldb/Host/Config.h"
17#include <sys/socket.h>
26static const int kType = SOCK_DGRAM;
31 :
Socket(ProtocolUdp, true) {
38 return ::sendto(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0,
50llvm::Expected<std::unique_ptr<UDPSocket>>
52 std::unique_ptr<UDPSocket> socket;
55 LLDB_LOG(log,
"host/port = {0}", name);
60 return host_port.takeError();
65 struct addrinfo hints;
66 struct addrinfo *service_info_list =
nullptr;
68 ::memset(&hints, 0,
sizeof(hints));
70 hints.ai_socktype =
kType;
71 int err = ::getaddrinfo(host_port->hostname.c_str(), std::to_string(host_port->port).c_str(), &hints,
75#
if defined(_WIN32) && defined(UNICODE)
76 "getaddrinfo(%s, %d, &hints, &info) returned error %i (%S)",
78 "getaddrinfo(%s, %d, &hints, &info) returned error %i (%s)",
80 host_port->hostname.c_str(), host_port->port, err, gai_strerror(err));
81 return error.ToError();
84 for (
struct addrinfo *service_info_ptr = service_info_list;
85 service_info_ptr !=
nullptr;
86 service_info_ptr = service_info_ptr->ai_next) {
88 CreateSocket(service_info_ptr->ai_family, service_info_ptr->ai_socktype,
89 service_info_ptr->ai_protocol,
error);
90 if (
error.Success()) {
92 socket->m_sockaddr = service_info_ptr;
98 ::freeaddrinfo(service_info_list);
101 return error.ToError();
107 const bool bind_addr_success = (host_port->hostname ==
"127.0.0.1" || host_port->hostname ==
"localhost")
111 if (!bind_addr_success) {
113 return error.ToError();
118 err = ::bind(socket->GetNativeSocket(), bind_addr, bind_addr.
GetLength());
120 struct sockaddr_in source_info;
121 socklen_t address_len =
sizeof (
struct sockaddr_in);
122 err = ::getsockname(socket->GetNativeSocket(),
123 (
struct sockaddr *)&source_info, &address_len);
125 return std::move(socket);
130 return std::string(llvm::formatv(
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
static const char * g_not_supported_error
bool SetToLocalhost(sa_family_t family, uint16_t port)
bool SetToAnyAddress(sa_family_t family, uint16_t port)
socklen_t GetLength() const
std::string GetIPAddress() const
bool SetPort(uint16_t port)
static const NativeSocket kInvalidSocketValue
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, Status &error)
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
static llvm::Expected< std::unique_ptr< UDPSocket > > CreateConnected(llvm::StringRef name)
Status Listen(llvm::StringRef name, int backlog) override
std::string GetRemoteConnectionURI() const override
Status Connect(llvm::StringRef name) override
size_t Send(const void *buf, const size_t num_bytes) override
UDPSocket(bool should_close)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.