LLDB mainline
common/DomainSocket.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_HOST_COMMON_DOMAINSOCKET_H
10#define LLDB_HOST_COMMON_DOMAINSOCKET_H
11
12#include "lldb/Host/Socket.h"
13#include <string>
14#include <vector>
15
16namespace lldb_private {
17
18/// Cross-platform AF_UNIX domain-socket logic.
19///
20/// Every operation on a domain socket (connect, listen, accept, name
21/// lookup) is identical on POSIX and Windows, so it all lives here. The only
22/// operation which differs between platforms is the CreatePair() factory: POSIX
23/// has socketpair(2) while Windows must emulate it. That factory therefore
24/// lives in the DomainSocketPosix / DomainSocketWindows implementation classes.
25class DomainSocket : public Socket {
26public:
27 DomainSocket(NativeSocket socket, bool should_close);
28 explicit DomainSocket(bool should_close);
29
30 using Pair =
31 std::pair<std::unique_ptr<DomainSocket>, std::unique_ptr<DomainSocket>>;
32
33 Status Connect(llvm::StringRef name) override;
34 Status Listen(llvm::StringRef name, int backlog) override;
35
36 using Socket::Accept;
37 llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>>
38 Accept(MainLoopBase &loop,
39 std::function<void(std::unique_ptr<Socket> socket)> sock_cb) override;
40
41 std::string GetRemoteConnectionURI() const override;
42
43 std::vector<std::string> GetListeningConnectionURI() const override;
44
45 static llvm::Expected<std::unique_ptr<DomainSocket>>
46 FromBoundNativeSocket(NativeSocket sockfd, bool should_close);
47
48 /// Convert between a native filesystem path and the path component of a
49 /// domain-socket URI. This is a pure, content-driven string transformation
50 /// (independent of the build host) so that, for example, a Unix build of
51 /// LLDB remote debugging a Windows target can still interpret Windows paths.
52 /// A drive-letter path (e.g. "C:\\dir\\sock") is not a valid URI authority,
53 /// so it is carried in the RFC 8089 file-URI form "/C:/dir/sock" (leading
54 /// slash, forward slashes). A UNC path (e.g. "\\\\server\\share") only needs
55 /// its backslashes replaced to become the URI path "//server/share". Any
56 /// other path is already a valid URI path and is returned unchanged.
57 /// @{
58 static std::string NativePathToURIPath(llvm::StringRef path);
59 static std::string URIPathToNativePath(llvm::StringRef path);
60 /// @}
61
62protected:
64 DomainSocket(SocketProtocol protocol, NativeSocket socket, bool should_close);
65
66 virtual size_t GetNameOffset() const;
67 virtual void DeleteSocketFile(llvm::StringRef name);
68 std::string GetSocketName() const;
69
70private:
71 DomainSocket(NativeSocket socket, const DomainSocket &listen_socket);
72};
73} // namespace lldb_private
74
75#endif // LLDB_HOST_COMMON_DOMAINSOCKET_H
DomainSocket(NativeSocket socket, bool should_close)
static std::string NativePathToURIPath(llvm::StringRef path)
Convert between a native filesystem path and the path component of a domain-socket URI.
Status Listen(llvm::StringRef name, int backlog) override
llvm::Expected< std::vector< MainLoopBase::ReadHandleUP > > Accept(MainLoopBase &loop, std::function< void(std::unique_ptr< Socket > socket)> sock_cb) override
std::pair< std::unique_ptr< DomainSocket >, std::unique_ptr< DomainSocket > > Pair
virtual size_t GetNameOffset() const
virtual void DeleteSocketFile(llvm::StringRef name)
Status Connect(llvm::StringRef name) override
static llvm::Expected< std::unique_ptr< DomainSocket > > FromBoundNativeSocket(NativeSocket sockfd, bool should_close)
std::string GetSocketName() const
std::string GetRemoteConnectionURI() const override
static std::string URIPathToNativePath(llvm::StringRef path)
std::vector< std::string > GetListeningConnectionURI() const override
virtual llvm::Expected< std::vector< MainLoopBase::ReadHandleUP > > Accept(MainLoopBase &loop, std::function< void(std::unique_ptr< Socket > socket)> sock_cb)=0
Socket(SocketProtocol protocol, bool should_close)
Definition Socket.cpp:170
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
int NativeSocket
Definition Socket.h:41