1 //===-- UDPSocket.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/common/UDPSocket.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/Host/Config.h"
15 #ifndef LLDB_DISABLE_POSIX
16 #include <arpa/inet.h>
17 #include <sys/socket.h>
23 using namespace lldb_private;
27 const int kDomain = AF_INET;
28 const int kType = SOCK_DGRAM;
30 static const char *g_not_supported_error = "Not supported";
33 UDPSocket::UDPSocket(NativeSocket socket) : Socket(socket, ProtocolUdp, true) {}
35 UDPSocket::UDPSocket(bool child_processes_inherit, Error &error)
37 CreateSocket(kDomain, kType, 0, child_processes_inherit, error)) {}
39 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) {
40 return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0,
41 m_send_sockaddr, m_send_sockaddr.GetLength());
44 Error UDPSocket::Connect(llvm::StringRef name) {
45 return Error("%s", g_not_supported_error);
48 Error UDPSocket::Listen(llvm::StringRef name, int backlog) {
49 return Error("%s", g_not_supported_error);
52 Error UDPSocket::Accept(llvm::StringRef name, bool child_processes_inherit,
54 return Error("%s", g_not_supported_error);
57 Error UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit,
58 Socket *&send_socket, Socket *&recv_socket) {
59 std::unique_ptr<UDPSocket> final_send_socket;
60 std::unique_ptr<UDPSocket> final_recv_socket;
62 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
64 log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());
69 int32_t port = INT32_MIN;
70 if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
73 // Setup the receiving end of the UDP connection on this localhost
74 // on port zero. After we bind to port zero we can read the port.
75 final_recv_socket.reset(new UDPSocket(child_processes_inherit, error));
76 if (error.Success()) {
77 // Socket was created, now lets bind to the requested port
79 addr.SetToAnyAddress(AF_INET, 0);
81 if (::bind(final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) ==
88 assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
92 // At this point we have setup the receive port, now we need to
93 // setup the UDP send socket
95 struct addrinfo hints;
96 struct addrinfo *service_info_list = nullptr;
98 ::memset(&hints, 0, sizeof(hints));
99 hints.ai_family = kDomain;
100 hints.ai_socktype = kType;
101 int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints,
104 error.SetErrorStringWithFormat(
105 #if defined(_MSC_VER) && defined(UNICODE)
106 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)",
108 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
110 host_str.c_str(), port_str.c_str(), err, gai_strerror(err));
114 for (struct addrinfo *service_info_ptr = service_info_list;
115 service_info_ptr != nullptr;
116 service_info_ptr = service_info_ptr->ai_next) {
117 auto send_fd = CreateSocket(
118 service_info_ptr->ai_family, service_info_ptr->ai_socktype,
119 service_info_ptr->ai_protocol, child_processes_inherit, error);
120 if (error.Success()) {
121 final_send_socket.reset(new UDPSocket(send_fd));
122 final_send_socket->m_send_sockaddr = service_info_ptr;
128 ::freeaddrinfo(service_info_list);
130 if (!final_send_socket)
133 send_socket = final_send_socket.release();
134 recv_socket = final_recv_socket.release();