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/Status.h"
20 
21 #ifdef _WIN32
23 #include <winsock2.h>
24 #include <ws2tcpip.h>
25 #endif
26 
27 namespace llvm {
28 class StringRef;
29 }
30 
31 namespace lldb_private {
32 
33 #if defined(_WIN32)
34 typedef SOCKET NativeSocket;
35 #else
36 typedef int NativeSocket;
37 #endif
38 class TCPSocket;
39 class UDPSocket;
40 
41 class Socket : public IOObject {
42 public:
48  };
49 
50  struct HostAndPort {
53 
54  bool operator==(const HostAndPort &R) const {
55  return port == R.port && hostname == R.hostname;
56  }
57  };
58 
60 
61  ~Socket() override;
62 
63  static llvm::Error Initialize();
64  static void Terminate();
65 
66  static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
67  bool child_processes_inherit,
68  Status &error);
69 
70  virtual Status Connect(llvm::StringRef name) = 0;
71  virtual Status Listen(llvm::StringRef name, int backlog) = 0;
72  virtual Status Accept(Socket *&socket) = 0;
73 
74  // Initialize a Tcp Socket object in listening mode. listen and accept are
75  // implemented separately because the caller may wish to manipulate or query
76  // the socket after it is initialized, but before entering a blocking accept.
77  static llvm::Expected<std::unique_ptr<TCPSocket>>
78  TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
79  int backlog = 5);
80 
81  static llvm::Expected<std::unique_ptr<Socket>>
82  TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
83 
84  static llvm::Expected<std::unique_ptr<UDPSocket>>
85  UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
86 
87  int GetOption(int level, int option_name, int &option_value);
88  int SetOption(int level, int option_name, int option_value);
89 
92 
93  Status Read(void *buf, size_t &num_bytes) override;
94  Status Write(const void *buf, size_t &num_bytes) override;
95 
96  virtual Status PreDisconnect();
97  Status Close() override;
98 
99  bool IsValid() const override { return m_socket != kInvalidSocketValue; }
101 
102  static llvm::Expected<HostAndPort>
103  DecodeHostAndPort(llvm::StringRef host_and_port);
104 
105  // If this Socket is connected then return the URI used to connect.
106  virtual std::string GetRemoteConnectionURI() const { return ""; };
107 
108 protected:
109  Socket(SocketProtocol protocol, bool should_close,
110  bool m_child_process_inherit);
111 
112  virtual size_t Send(const void *buf, const size_t num_bytes);
113 
114  static void SetLastError(Status &error);
115  static NativeSocket CreateSocket(const int domain, const int type,
116  const int protocol,
117  bool child_processes_inherit, Status &error);
118  static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
119  socklen_t *addrlen,
120  bool child_processes_inherit, Status &error);
121 
126 };
127 
128 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
129  const Socket::HostAndPort &HP);
130 
131 } // namespace lldb_private
132 
133 #endif // LLDB_HOST_SOCKET_H
lldb_private::Socket::kInvalidSocketValue
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:59
lldb_private::Socket::SetLastError
static void SetLastError(Status &error)
lldb_private::Socket::m_should_close_fd
bool m_should_close_fd
Definition: Socket.h:125
llvm
Definition: Debugger.h:49
lldb_private::Socket::m_child_processes_inherit
bool m_child_processes_inherit
Definition: Socket.h:124
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::HostAndPort::port
uint16_t port
Definition: Socket.h:52
lldb_private::Socket::GetRemoteConnectionURI
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:106
lldb_private::Socket::SocketProtocol
SocketProtocol
Definition: Socket.h:43
lldb_private::Socket::Read
Status Read(void *buf, size_t &num_bytes) override
lldb_private::Socket
Definition: Socket.h:41
lldb_private::Socket::Initialize
static llvm::Error Initialize()
Definition: Socket.cpp:79
lldb_private::Socket::Create
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:104
lldb_private::Socket::HostAndPort
Definition: Socket.h:50
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:123
lldb_private::Socket::HostAndPort::hostname
std::string hostname
Definition: Socket.h:51
lldb_private::Socket::GetNativeSocket
NativeSocket GetNativeSocket() const
Definition: Socket.h:90
lldb_private::Socket::~Socket
~Socket() override
Definition: Socket.cpp:77
lldb_private::Socket::Write
Status Write(const void *buf, size_t &num_bytes) override
lldb_private::Socket::ProtocolUnixDomain
@ ProtocolUnixDomain
Definition: Socket.h:46
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:146
lldb_private::NativeSocket
int NativeSocket
Definition: Socket.h:36
lldb_private::IOObject::WaitableHandle
int WaitableHandle
Definition: IOObject.h:29
lldb_private::Socket::IsValid
bool IsValid() const override
Definition: Socket.h:99
lldb_private::IOObject
Definition: IOObject.h:20
lldb-private.h
lldb_private::Socket::HostAndPort::operator==
bool operator==(const HostAndPort &R) const
Definition: Socket.h:54
lldb_private::Socket::Socket
Socket(SocketProtocol protocol, bool should_close, bool m_child_process_inherit)
Definition: Socket.cpp:70
SocketAddress.h
lldb_private::Socket::ProtocolUnixAbstract
@ ProtocolUnixAbstract
Definition: Socket.h:47
lldb_private::Status
Definition: Status.h:44
lldb_private::Socket::ProtocolTcp
@ ProtocolTcp
Definition: Socket.h:44
lldb_private::Socket::DecodeHostAndPort
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Definition: Socket.cpp:186
lldb_private::Socket::UdpConnect
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:181
lldb_private::operator<<
Stream & operator<<(Stream &s, const SourceLocationSpec &loc)
Dump a SourceLocationSpec object to a stream.
Definition: SourceLocationSpec.cpp:40
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:45
lldb_private::Socket::Listen
virtual Status Listen(llvm::StringRef name, int backlog)=0
lldb_private::Socket::Terminate
static void Terminate()
Definition: Socket.cpp:98
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::m_protocol
SocketProtocol m_protocol
Definition: Socket.h:122
lldb_private::Socket::TcpListen
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, int backlog=5)
Definition: Socket.cpp:165
lldb_private::Socket::GetWaitableHandle
WaitableHandle GetWaitableHandle() override
IOObject.h
lldb_private::Socket::GetSocketProtocol
SocketProtocol GetSocketProtocol() const
Definition: Socket.h:91