Coverage Report

Created: 2022-01-25 06:29

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