xref: /llvm-project/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h (revision 0964328c2960159f66ad232bb2257fbabab3c0ec)
1 //===--------------------- SocketTestUtilities.h ----------------*- C++ -*-===//
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 #ifndef LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H
10 #define LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H
11 
12 #include <cstdio>
13 #include <functional>
14 #include <thread>
15 
16 #include "lldb/Host/Config.h"
17 #include "lldb/Host/Socket.h"
18 #include "lldb/Host/common/TCPSocket.h"
19 #include "lldb/Host/common/UDPSocket.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Testing/Support/Error.h"
23 
24 #if LLDB_ENABLE_POSIX
25 #include "lldb/Host/posix/DomainSocket.h"
26 #endif
27 
28 namespace lldb_private {
29 template <typename SocketType>
30 void CreateConnectedSockets(
31     llvm::StringRef listen_remote_address,
32     const std::function<std::string(const SocketType &)> &get_connect_addr,
33     std::unique_ptr<SocketType> *a_up, std::unique_ptr<SocketType> *b_up);
34 bool CreateTCPConnectedSockets(std::string listen_remote_ip,
35                                std::unique_ptr<TCPSocket> *a_up,
36                                std::unique_ptr<TCPSocket> *b_up);
37 #if LLDB_ENABLE_POSIX
38 void CreateDomainConnectedSockets(llvm::StringRef path,
39                                   std::unique_ptr<DomainSocket> *a_up,
40                                   std::unique_ptr<DomainSocket> *b_up);
41 #endif
42 
43 bool HostSupportsIPv6();
44 bool HostSupportsIPv4();
45 
46 /// Returns true if the name `localhost` maps to a loopback IPv4 address.
47 bool HostSupportsLocalhostToIPv4();
48 /// Returns true if the name `localhost` maps to a loopback IPv6 address.
49 bool HostSupportsLocalhostToIPv6();
50 
51 /// Return an IP for localhost based on host support.
52 ///
53 /// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6
54 /// is detected. If neither are detected, return an error.
55 llvm::Expected<std::string> GetLocalhostIP();
56 
57 } // namespace lldb_private
58 
59 #endif
60