LLDB  mainline
Socket.h
Go to the documentation of this file.
1 //===-- Socket.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_HOST_SOCKET_H
10 #define LLDB_HOST_SOCKET_H
11 
12 #include <memory>
13 #include <string>
14 
15 #include "lldb/lldb-private.h"
16 
18 #include "lldb/Utility/IOObject.h"
19 #include "lldb/Utility/Predicate.h"
20 #include "lldb/Utility/Status.h"
21 
22 #ifdef _WIN32
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #endif
27 
28 namespace llvm {
29 class StringRef;
30 }
31 
32 namespace lldb_private {
33 
34 #if defined(_WIN32)
35 typedef SOCKET NativeSocket;
36 #else
37 typedef int NativeSocket;
38 #endif
39 class TCPSocket;
40 class UDPSocket;
41 
42 class Socket : public IOObject {
43 public:
49  };
50 
52 
53  ~Socket() override;
54 
55  static llvm::Error Initialize();
56  static void Terminate();
57 
58  static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
59  bool child_processes_inherit,
60  Status &error);
61 
62  virtual Status Connect(llvm::StringRef name) = 0;
63  virtual Status Listen(llvm::StringRef name, int backlog) = 0;
64  virtual Status Accept(Socket *&socket) = 0;
65 
66  // Initialize a Tcp Socket object in listening mode. listen and accept are
67  // implemented separately because the caller may wish to manipulate or query
68  // the socket after it is initialized, but before entering a blocking accept.
69  static llvm::Expected<std::unique_ptr<TCPSocket>>
70  TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
71  Predicate<uint16_t> *predicate, int backlog = 5);
72 
73  static llvm::Expected<std::unique_ptr<Socket>>
74  TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
75 
76  static llvm::Expected<std::unique_ptr<UDPSocket>>
77  UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
78 
79  static Status UnixDomainConnect(llvm::StringRef host_and_port,
80  bool child_processes_inherit,
81  Socket *&socket);
82  static Status UnixDomainAccept(llvm::StringRef host_and_port,
83  bool child_processes_inherit, Socket *&socket);
84  static Status UnixAbstractConnect(llvm::StringRef host_and_port,
85  bool child_processes_inherit,
86  Socket *&socket);
87  static Status UnixAbstractAccept(llvm::StringRef host_and_port,
88  bool child_processes_inherit,
89  Socket *&socket);
90 
91  int GetOption(int level, int option_name, int &option_value);
92  int SetOption(int level, int option_name, int option_value);
93 
96 
97  Status Read(void *buf, size_t &num_bytes) override;
98  Status Write(const void *buf, size_t &num_bytes) override;
99 
100  virtual Status PreDisconnect();
101  Status Close() override;
102 
103  bool IsValid() const override { return m_socket != kInvalidSocketValue; }
105 
106  static llvm::Error DecodeHostAndPort(llvm::StringRef host_and_port,
107  std::string &host_str,
108  std::string &port_str, uint16_t &port);
109 
110  // If this Socket is connected then return the URI used to connect.
111  virtual std::string GetRemoteConnectionURI() const { return ""; };
112 
113 protected:
114  Socket(SocketProtocol protocol, bool should_close,
115  bool m_child_process_inherit);
116 
117  virtual size_t Send(const void *buf, const size_t num_bytes);
118 
119  static void SetLastError(Status &error);
120  static NativeSocket CreateSocket(const int domain, const int type,
121  const int protocol,
122  bool child_processes_inherit, Status &error);
123  static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
124  socklen_t *addrlen,
125  bool child_processes_inherit, Status &error);
126 
131 };
132 
133 } // namespace lldb_private
134 
135 #endif // LLDB_HOST_SOCKET_H
lldb_private::Socket::kInvalidSocketValue
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:51
lldb_private::Socket::SetLastError
static void SetLastError(Status &error)
lldb_private::Socket::m_should_close_fd
bool m_should_close_fd
Definition: Socket.h:130
llvm
Definition: Debugger.h:49
lldb_private::Socket::m_child_processes_inherit
bool m_child_processes_inherit
Definition: Socket.h:129
lldb_private::Socket::Accept
virtual Status Accept(Socket *&socket)=0
lldb_private::Socket::Send
virtual size_t Send(const void *buf, const size_t num_bytes)
lldb_private::Socket::GetRemoteConnectionURI
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:111
lldb_private::Socket::SocketProtocol
SocketProtocol
Definition: Socket.h:44
lldb_private::Predicate< uint16_t >
lldb_private::Socket::Read
Status Read(void *buf, size_t &num_bytes) override
lldb_private::Socket
Definition: Socket.h:42
lldb_private::Socket::Initialize
static llvm::Error Initialize()
Definition: Socket.cpp:82
lldb_private::Socket::Create
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:107
Predicate.h
lldb_private::Socket::SetOption
int SetOption(int level, int option_name, int option_value)
lldb_private::Socket::Connect
virtual Status Connect(llvm::StringRef name)=0
lldb_private::Socket::Close
Status Close() override
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::UDPSocket
Definition: UDPSocket.h:15
lldb_private::Socket::GetOption
int GetOption(int level, int option_name, int &option_value)
lldb_private::Socket::m_socket
NativeSocket m_socket
Definition: Socket.h:128
lldb_private::Socket::DecodeHostAndPort
static llvm::Error DecodeHostAndPort(llvm::StringRef host_and_port, std::string &host_str, std::string &port_str, uint16_t &port)
Definition: Socket.cpp:273
lldb_private::Socket::GetNativeSocket
NativeSocket GetNativeSocket() const
Definition: Socket.h:94
lldb_private::Socket::~Socket
~Socket() override
Definition: Socket.cpp:80
lldb_private::Socket::Write
Status Write(const void *buf, size_t &num_bytes) override
lldb_private::Socket::ProtocolUnixDomain
@ ProtocolUnixDomain
Definition: Socket.h:47
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Socket::TcpConnect
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:149
lldb_private::NativeSocket
int NativeSocket
Definition: Socket.h:37
lldb_private::IOObject::WaitableHandle
int WaitableHandle
Definition: IOObject.h:29
lldb_private::Socket::IsValid
bool IsValid() const override
Definition: Socket.h:103
lldb_private::IOObject
Definition: IOObject.h:20
lldb-private.h
lldb_private::Socket::Socket
Socket(SocketProtocol protocol, bool should_close, bool m_child_process_inherit)
Definition: Socket.cpp:73
SocketAddress.h
lldb_private::Socket::UnixAbstractAccept
static Status UnixAbstractAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:256
lldb_private::Socket::ProtocolUnixAbstract
@ ProtocolUnixAbstract
Definition: Socket.h:48
lldb_private::Status
Definition: Status.h:44
lldb_private::Socket::ProtocolTcp
@ ProtocolTcp
Definition: Socket.h:45
lldb_private::Socket::UdpConnect
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:204
lldb_private::Socket::PreDisconnect
virtual Status PreDisconnect()
lldb_private::Socket::CreateSocket
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
lldb_private::Socket::ProtocolUdp
@ ProtocolUdp
Definition: Socket.h:46
lldb_private::Socket::Listen
virtual Status Listen(llvm::StringRef name, int backlog)=0
lldb_private::Socket::Terminate
static void Terminate()
Definition: Socket.cpp:101
lldb_private::Socket::UnixAbstractConnect
static Status UnixAbstractConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:241
uint16_t
windows.h
lldb_private::Socket::AcceptSocket
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit, Status &error)
Status.h
lldb_private::TCPSocket
Definition: TCPSocket.h:17
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
lldb_private::Socket::UnixDomainConnect
static Status UnixDomainConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:209
lldb_private::Socket::m_protocol
SocketProtocol m_protocol
Definition: Socket.h:127
lldb_private::Socket::TcpListen
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, Predicate< uint16_t > *predicate, int backlog=5)
Definition: Socket.cpp:168
lldb_private::Socket::GetWaitableHandle
WaitableHandle GetWaitableHandle() override
IOObject.h
lldb_private::Socket::UnixDomainAccept
static Status UnixDomainAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:225
lldb_private::Socket::GetSocketProtocol
SocketProtocol GetSocketProtocol() const
Definition: Socket.h:95