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/Host/Config.h"
13 #include "lldb/Utility/Log.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(ProtocolUdp, true, true) {
37 UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit)
38 : Socket(ProtocolUdp, should_close, child_processes_inherit) {}
40 size_t UDPSocket::Send(const void *buf, const size_t num_bytes) {
41 return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0,
42 m_sockaddr, m_sockaddr.GetLength());
45 Status UDPSocket::Connect(llvm::StringRef name) {
46 return Status("%s", g_not_supported_error);
49 Status UDPSocket::Listen(llvm::StringRef name, int backlog) {
50 return Status("%s", g_not_supported_error);
53 Status UDPSocket::Accept(Socket *&socket) {
54 return Status("%s", g_not_supported_error);
57 Status UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit,
59 std::unique_ptr<UDPSocket> final_socket;
61 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
63 log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());
68 int32_t port = INT32_MIN;
69 if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
72 // At this point we have setup the receive port, now we need to
73 // setup the UDP send socket
75 struct addrinfo hints;
76 struct addrinfo *service_info_list = nullptr;
78 ::memset(&hints, 0, sizeof(hints));
79 hints.ai_family = kDomain;
80 hints.ai_socktype = kType;
81 int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints,
84 error.SetErrorStringWithFormat(
85 #if defined(_MSC_VER) && defined(UNICODE)
86 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)",
88 "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
90 host_str.c_str(), port_str.c_str(), err, gai_strerror(err));
94 for (struct addrinfo *service_info_ptr = service_info_list;
95 service_info_ptr != nullptr;
96 service_info_ptr = service_info_ptr->ai_next) {
97 auto send_fd = CreateSocket(
98 service_info_ptr->ai_family, service_info_ptr->ai_socktype,
99 service_info_ptr->ai_protocol, child_processes_inherit, error);
100 if (error.Success()) {
101 final_socket.reset(new UDPSocket(send_fd));
102 final_socket->m_sockaddr = service_info_ptr;
108 ::freeaddrinfo(service_info_list);
113 SocketAddress bind_addr;
115 // Only bind to the loopback address if we are expecting a connection from
116 // localhost to avoid any firewall issues.
117 const bool bind_addr_success = (host_str == "127.0.0.1" || host_str == "localhost")
118 ? bind_addr.SetToLocalhost(kDomain, port)
119 : bind_addr.SetToAnyAddress(kDomain, port);
121 if (!bind_addr_success) {
122 error.SetErrorString("Failed to get hostspec to bind for");
126 bind_addr.SetPort(0); // Let the source port # be determined dynamically
128 err = ::bind(final_socket->GetNativeSocket(), bind_addr, bind_addr.GetLength());
130 struct sockaddr_in source_info;
131 socklen_t address_len = sizeof (struct sockaddr_in);
132 err = ::getsockname(final_socket->GetNativeSocket(), (struct sockaddr *) &source_info, &address_len);
134 socket = final_socket.release();