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";
34 UDPSocket::UDPSocket(NativeSocket socket)
35 : Socket(socket, ProtocolUdp, true)
39 UDPSocket::UDPSocket(bool child_processes_inherit, Error &error)
40 : UDPSocket(CreateSocket(kDomain, kType, 0, child_processes_inherit, error))
45 UDPSocket::Send(const void *buf, const size_t num_bytes)
47 return ::sendto (m_socket,
48 static_cast<const char*>(buf),
52 m_send_sockaddr.GetLength());
56 UDPSocket::Connect(llvm::StringRef name)
58 return Error("%s", g_not_supported_error);
62 UDPSocket::Listen(llvm::StringRef name, int backlog)
64 return Error("%s", g_not_supported_error);
68 UDPSocket::Accept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
70 return Error("%s", g_not_supported_error);
74 UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket)
76 std::unique_ptr<UDPSocket> final_send_socket;
77 std::unique_ptr<UDPSocket> final_recv_socket;
79 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
81 log->Printf ("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());
86 int32_t port = INT32_MIN;
87 if (!DecodeHostAndPort (name, host_str, port_str, port, &error))
90 // Setup the receiving end of the UDP connection on this localhost
91 // on port zero. After we bind to port zero we can read the port.
92 final_recv_socket.reset(new UDPSocket(child_processes_inherit, error));
95 // Socket was created, now lets bind to the requested port
97 addr.SetToAnyAddress (AF_INET, 0);
99 if (::bind (final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) == -1)
102 SetLastError (error);
106 assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
110 // At this point we have setup the receive port, now we need to
111 // setup the UDP send socket
113 struct addrinfo hints;
114 struct addrinfo *service_info_list = nullptr;
116 ::memset (&hints, 0, sizeof(hints));
117 hints.ai_family = kDomain;
118 hints.ai_socktype = kType;
119 int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list);
122 error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
130 for (struct addrinfo *service_info_ptr = service_info_list;
131 service_info_ptr != nullptr;
132 service_info_ptr = service_info_ptr->ai_next)
134 auto send_fd = CreateSocket (service_info_ptr->ai_family,
135 service_info_ptr->ai_socktype,
136 service_info_ptr->ai_protocol,
137 child_processes_inherit,
141 final_send_socket.reset(new UDPSocket(send_fd));
142 final_send_socket->m_send_sockaddr = service_info_ptr;
149 :: freeaddrinfo (service_info_list);
151 if (!final_send_socket)
154 send_socket = final_send_socket.release();
155 recv_socket = final_recv_socket.release();