/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/UDPSocket.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- UDPSocket.cpp -----------------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "lldb/Host/common/UDPSocket.h" |
10 | | |
11 | | #include "lldb/Host/Config.h" |
12 | | #include "lldb/Utility/LLDBLog.h" |
13 | | #include "lldb/Utility/Log.h" |
14 | | |
15 | | #if LLDB_ENABLE_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 | | static const int kDomain = AF_INET; |
26 | | static const int kType = SOCK_DGRAM; |
27 | | |
28 | | static const char *g_not_supported_error = "Not supported"; |
29 | | |
30 | 4 | UDPSocket::UDPSocket(NativeSocket socket) : Socket(ProtocolUdp, true, true) { |
31 | 4 | m_socket = socket; |
32 | 4 | } |
33 | | |
34 | | UDPSocket::UDPSocket(bool should_close, bool child_processes_inherit) |
35 | 0 | : Socket(ProtocolUdp, should_close, child_processes_inherit) {} |
36 | | |
37 | 0 | size_t UDPSocket::Send(const void *buf, const size_t num_bytes) { |
38 | 0 | return ::sendto(m_socket, static_cast<const char *>(buf), num_bytes, 0, |
39 | 0 | m_sockaddr, m_sockaddr.GetLength()); |
40 | 0 | } |
41 | | |
42 | 0 | Status UDPSocket::Connect(llvm::StringRef name) { |
43 | 0 | return Status("%s", g_not_supported_error); |
44 | 0 | } |
45 | | |
46 | 0 | Status UDPSocket::Listen(llvm::StringRef name, int backlog) { |
47 | 0 | return Status("%s", g_not_supported_error); |
48 | 0 | } |
49 | | |
50 | 0 | Status UDPSocket::Accept(Socket *&socket) { |
51 | 0 | return Status("%s", g_not_supported_error); |
52 | 0 | } |
53 | | |
54 | | llvm::Expected<std::unique_ptr<UDPSocket>> |
55 | 4 | UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit) { |
56 | 4 | std::unique_ptr<UDPSocket> socket; |
57 | | |
58 | 4 | Log *log = GetLog(LLDBLog::Connection); |
59 | 4 | LLDB_LOG(log, "host/port = {0}", name); |
60 | | |
61 | 4 | Status error; |
62 | 4 | llvm::Expected<HostAndPort> host_port = DecodeHostAndPort(name); |
63 | 4 | if (!host_port) |
64 | 0 | return host_port.takeError(); |
65 | | |
66 | | // At this point we have setup the receive port, now we need to setup the UDP |
67 | | // send socket |
68 | | |
69 | 4 | struct addrinfo hints; |
70 | 4 | struct addrinfo *service_info_list = nullptr; |
71 | | |
72 | 4 | ::memset(&hints, 0, sizeof(hints)); |
73 | 4 | hints.ai_family = kDomain; |
74 | 4 | hints.ai_socktype = kType; |
75 | 4 | int err = ::getaddrinfo(host_port->hostname.c_str(), std::to_string(host_port->port).c_str(), &hints, |
76 | 4 | &service_info_list); |
77 | 4 | if (err != 0) { |
78 | 0 | error.SetErrorStringWithFormat( |
79 | | #if defined(_WIN32) && defined(UNICODE) |
80 | | "getaddrinfo(%s, %d, &hints, &info) returned error %i (%S)", |
81 | | #else |
82 | 0 | "getaddrinfo(%s, %d, &hints, &info) returned error %i (%s)", |
83 | 0 | #endif |
84 | 0 | host_port->hostname.c_str(), host_port->port, err, gai_strerror(err)); |
85 | 0 | return error.ToError(); |
86 | 0 | } |
87 | | |
88 | 4 | for (struct addrinfo *service_info_ptr = service_info_list; |
89 | 4 | service_info_ptr != nullptr; |
90 | 4 | service_info_ptr = service_info_ptr->ai_next0 ) { |
91 | 4 | auto send_fd = CreateSocket( |
92 | 4 | service_info_ptr->ai_family, service_info_ptr->ai_socktype, |
93 | 4 | service_info_ptr->ai_protocol, child_processes_inherit, error); |
94 | 4 | if (error.Success()) { |
95 | 4 | socket.reset(new UDPSocket(send_fd)); |
96 | 4 | socket->m_sockaddr = service_info_ptr; |
97 | 4 | break; |
98 | 4 | } else |
99 | 0 | continue; |
100 | 4 | } |
101 | | |
102 | 4 | ::freeaddrinfo(service_info_list); |
103 | | |
104 | 4 | if (!socket) |
105 | 0 | return error.ToError(); |
106 | | |
107 | 4 | SocketAddress bind_addr; |
108 | | |
109 | | // Only bind to the loopback address if we are expecting a connection from |
110 | | // localhost to avoid any firewall issues. |
111 | 4 | const bool bind_addr_success = (host_port->hostname == "127.0.0.1" || host_port->hostname == "localhost"0 ) |
112 | 4 | ? bind_addr.SetToLocalhost(kDomain, host_port->port) |
113 | 4 | : bind_addr.SetToAnyAddress(kDomain, host_port->port)0 ; |
114 | | |
115 | 4 | if (!bind_addr_success) { |
116 | 0 | error.SetErrorString("Failed to get hostspec to bind for"); |
117 | 0 | return error.ToError(); |
118 | 0 | } |
119 | | |
120 | 4 | bind_addr.SetPort(0); // Let the source port # be determined dynamically |
121 | | |
122 | 4 | err = ::bind(socket->GetNativeSocket(), bind_addr, bind_addr.GetLength()); |
123 | | |
124 | 4 | struct sockaddr_in source_info; |
125 | 4 | socklen_t address_len = sizeof (struct sockaddr_in); |
126 | 4 | err = ::getsockname(socket->GetNativeSocket(), |
127 | 4 | (struct sockaddr *)&source_info, &address_len); |
128 | | |
129 | 4 | return std::move(socket); |
130 | 4 | } |
131 | | |
132 | 2 | std::string UDPSocket::GetRemoteConnectionURI() const { |
133 | 2 | if (m_socket != kInvalidSocketValue) { |
134 | 2 | return std::string(llvm::formatv( |
135 | 2 | "udp://[{0}]:{1}", m_sockaddr.GetIPAddress(), m_sockaddr.GetPort())); |
136 | 2 | } |
137 | 0 | return ""; |
138 | 2 | } |