11 #include "lldb/Host/Config.h"
16 #include <arpa/inet.h>
17 #include <sys/socket.h>
26 static const int kType = SOCK_DGRAM;
35 :
Socket(ProtocolUdp, should_close, child_processes_inherit) {}
38 return ::sendto(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0,
54 llvm::Expected<std::unique_ptr<UDPSocket>>
56 std::unique_ptr<UDPSocket> socket;
59 LLDB_LOG(log,
"host/port = {0}", name);
64 return host_port.takeError();
69 struct addrinfo hints;
70 struct addrinfo *service_info_list =
nullptr;
72 ::memset(&hints, 0,
sizeof(hints));
74 hints.ai_socktype =
kType;
75 int err = ::getaddrinfo(host_port->hostname.c_str(), std::to_string(host_port->port).c_str(), &hints,
78 error.SetErrorStringWithFormat(
79 #
if defined(_WIN32) && defined(UNICODE)
80 "getaddrinfo(%s, %d, &hints, &info) returned error %i (%S)",
82 "getaddrinfo(%s, %d, &hints, &info) returned error %i (%s)",
84 host_port->hostname.c_str(), host_port->port, err, gai_strerror(err));
85 return error.ToError();
88 for (
struct addrinfo *service_info_ptr = service_info_list;
89 service_info_ptr !=
nullptr;
90 service_info_ptr = service_info_ptr->ai_next) {
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()) {
96 socket->m_sockaddr = service_info_ptr;
102 ::freeaddrinfo(service_info_list);
105 return error.ToError();
111 const bool bind_addr_success = (host_port->hostname ==
"127.0.0.1" || host_port->hostname ==
"localhost")
115 if (!bind_addr_success) {
116 error.SetErrorString(
"Failed to get hostspec to bind for");
117 return error.ToError();
122 err = ::bind(socket->GetNativeSocket(), bind_addr, bind_addr.
GetLength());
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);
129 return std::move(socket);