]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/UDPSocket.cpp
Import testcase updates with code contributed back to NetBSD
[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
34 UDPSocket::UDPSocket(NativeSocket socket)
35     : Socket(socket, ProtocolUdp, true)
36 {
37 }
38
39 UDPSocket::UDPSocket(bool child_processes_inherit, Error &error)
40     : UDPSocket(CreateSocket(kDomain, kType, 0, child_processes_inherit, error))
41 {
42 }
43
44 size_t
45 UDPSocket::Send(const void *buf, const size_t num_bytes)
46 {
47     return ::sendto (m_socket,
48                      static_cast<const char*>(buf),
49                      num_bytes,
50                      0,
51                      m_send_sockaddr,
52                      m_send_sockaddr.GetLength());
53 }
54
55 Error
56 UDPSocket::Connect(llvm::StringRef name)
57 {
58     return Error("%s", g_not_supported_error);
59 }
60
61 Error
62 UDPSocket::Listen(llvm::StringRef name, int backlog)
63 {
64     return Error("%s", g_not_supported_error);
65 }
66
67 Error
68 UDPSocket::Accept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
69 {
70     return Error("%s", g_not_supported_error);
71 }
72
73 Error
74 UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket)
75 {
76     std::unique_ptr<UDPSocket> final_send_socket;
77     std::unique_ptr<UDPSocket> final_recv_socket;
78
79     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
80     if (log)
81         log->Printf ("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());
82
83     Error error;
84     std::string host_str;
85     std::string port_str;
86     int32_t port = INT32_MIN;
87     if (!DecodeHostAndPort (name, host_str, port_str, port, &error))
88         return error;
89
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));
93     if (error.Success())
94     {
95         // Socket was created, now lets bind to the requested port
96         SocketAddress addr;
97         addr.SetToAnyAddress (AF_INET, 0);
98
99         if (::bind (final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) == -1)
100         {
101             // Bind failed...
102             SetLastError (error);
103         }
104     }
105
106     assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
107     if (error.Fail())
108         return error;
109
110     // At this point we have setup the receive port, now we need to
111     // setup the UDP send socket
112
113     struct addrinfo hints;
114     struct addrinfo *service_info_list = nullptr;
115
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);
120     if (err != 0)
121     {
122         error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
123                                        host_str.c_str(),
124                                        port_str.c_str(),
125                                        err,
126                                        gai_strerror(err));
127         return error;
128     }
129
130     for (struct addrinfo *service_info_ptr = service_info_list;
131          service_info_ptr != nullptr;
132          service_info_ptr = service_info_ptr->ai_next)
133     {
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,
138                                      error);
139         if (error.Success())
140         {
141             final_send_socket.reset(new UDPSocket(send_fd));
142             final_send_socket->m_send_sockaddr = service_info_ptr;
143             break;
144         }
145         else
146             continue;
147     }
148
149     :: freeaddrinfo (service_info_list);
150
151     if (!final_send_socket)
152         return error;
153
154     send_socket = final_send_socket.release();
155     recv_socket = final_recv_socket.release();
156     error.Clear();
157     return error;
158 }