]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/UDPSocket.cpp
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / UDPSocket.cpp
1 //===-- UDPSocket.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/common/UDPSocket.h"
11
12 #include "lldb/Core/Log.h"
13 #include "lldb/Host/Config.h"
14
15 #ifndef LLDB_DISABLE_POSIX
16 #include <arpa/inet.h>
17 #include <sys/socket.h>
18 #endif
19
20 #include <memory>
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 namespace {
26
27 const int kDomain = AF_INET;
28 const int kType = SOCK_DGRAM;
29
30 static const char *g_not_supported_error = "Not supported";
31 }
32
33 UDPSocket::UDPSocket(NativeSocket socket) : Socket(socket, ProtocolUdp, true) {}
34
35 UDPSocket::UDPSocket(bool child_processes_inherit, Error &error)
36     : UDPSocket(
37           CreateSocket(kDomain, kType, 0, child_processes_inherit, error)) {}
38
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());
42 }
43
44 Error UDPSocket::Connect(llvm::StringRef name) {
45   return Error("%s", g_not_supported_error);
46 }
47
48 Error UDPSocket::Listen(llvm::StringRef name, int backlog) {
49   return Error("%s", g_not_supported_error);
50 }
51
52 Error UDPSocket::Accept(llvm::StringRef name, bool child_processes_inherit,
53                         Socket *&socket) {
54   return Error("%s", g_not_supported_error);
55 }
56
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;
61
62   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
63   if (log)
64     log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());
65
66   Error error;
67   std::string host_str;
68   std::string port_str;
69   int32_t port = INT32_MIN;
70   if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
71     return error;
72
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
78     SocketAddress addr;
79     addr.SetToAnyAddress(AF_INET, 0);
80
81     if (::bind(final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) ==
82         -1) {
83       // Bind failed...
84       SetLastError(error);
85     }
86   }
87
88   assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
89   if (error.Fail())
90     return error;
91
92   // At this point we have setup the receive port, now we need to
93   // setup the UDP send socket
94
95   struct addrinfo hints;
96   struct addrinfo *service_info_list = nullptr;
97
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,
102                           &service_info_list);
103   if (err != 0) {
104     error.SetErrorStringWithFormat(
105 #if defined(_MSC_VER) && defined(UNICODE)
106         "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)",
107 #else
108         "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
109 #endif
110         host_str.c_str(), port_str.c_str(), err, gai_strerror(err));
111     return error;
112   }
113
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;
123       break;
124     } else
125       continue;
126   }
127
128   ::freeaddrinfo(service_info_list);
129
130   if (!final_send_socket)
131     return error;
132
133   send_socket = final_send_socket.release();
134   recv_socket = final_recv_socket.release();
135   error.Clear();
136   return error;
137 }