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 liblldb_Host_Socket_h_
10 #define liblldb_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(_MSC_VER)
35 typedef SOCKET NativeSocket;
36 #else
37 typedef int NativeSocket;
38 #endif
39 
40 class Socket : public IOObject {
41 public:
42  typedef enum {
46  ProtocolUnixAbstract
48 
49  static const NativeSocket kInvalidSocketValue;
50 
51  ~Socket() override;
52 
53  static llvm::Error Initialize();
54  static void Terminate();
55 
56  static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
57  bool child_processes_inherit,
58  Status &error);
59 
60  virtual Status Connect(llvm::StringRef name) = 0;
61  virtual Status Listen(llvm::StringRef name, int backlog) = 0;
62  virtual Status Accept(Socket *&socket) = 0;
63 
64  // Initialize a Tcp Socket object in listening mode. listen and accept are
65  // implemented separately because the caller may wish to manipulate or query
66  // the socket after it is initialized, but before entering a blocking accept.
67  static Status TcpListen(llvm::StringRef host_and_port,
68  bool child_processes_inherit, Socket *&socket,
69  Predicate<uint16_t> *predicate, int backlog = 5);
70  static Status TcpConnect(llvm::StringRef host_and_port,
71  bool child_processes_inherit, Socket *&socket);
72  static Status UdpConnect(llvm::StringRef host_and_port,
73  bool child_processes_inherit, Socket *&socket);
74  static Status UnixDomainConnect(llvm::StringRef host_and_port,
75  bool child_processes_inherit,
76  Socket *&socket);
77  static Status UnixDomainAccept(llvm::StringRef host_and_port,
78  bool child_processes_inherit, Socket *&socket);
79  static Status UnixAbstractConnect(llvm::StringRef host_and_port,
80  bool child_processes_inherit,
81  Socket *&socket);
82  static Status UnixAbstractAccept(llvm::StringRef host_and_port,
83  bool child_processes_inherit,
84  Socket *&socket);
85 
86  int GetOption(int level, int option_name, int &option_value);
87  int SetOption(int level, int option_name, int option_value);
88 
89  NativeSocket GetNativeSocket() const { return m_socket; }
90  SocketProtocol GetSocketProtocol() const { return m_protocol; }
91 
92  Status Read(void *buf, size_t &num_bytes) override;
93  Status Write(const void *buf, size_t &num_bytes) override;
94 
95  virtual Status PreDisconnect();
96  Status Close() override;
97 
98  bool IsValid() const override { return m_socket != kInvalidSocketValue; }
99  WaitableHandle GetWaitableHandle() override;
100 
101  static bool DecodeHostAndPort(llvm::StringRef host_and_port,
102  std::string &host_str, std::string &port_str,
103  int32_t &port, Status *error_ptr);
104 
105 protected:
106  Socket(SocketProtocol protocol, bool should_close,
107  bool m_child_process_inherit);
108 
109  virtual size_t Send(const void *buf, const size_t num_bytes);
110 
111  static void SetLastError(Status &error);
112  static NativeSocket CreateSocket(const int domain, const int type,
113  const int protocol,
114  bool child_processes_inherit, Status &error);
115  static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
116  socklen_t *addrlen,
117  bool child_processes_inherit, Status &error);
118 
119  SocketProtocol m_protocol;
120  NativeSocket m_socket;
122 };
123 
124 } // namespace lldb_private
125 
126 #endif // liblldb_Host_Socket_h_
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:49
Definition: Debugger.h:71
bool m_child_processes_inherit
Definition: Socket.h:121
llvm::Error Error
bool IsValid() const override
Definition: Socket.h:98
NativeSocket m_socket
Definition: Socket.h:120
NativeSocket GetNativeSocket() const
Definition: Socket.h:89
int NativeSocket
Definition: Socket.h:37
SocketProtocol GetSocketProtocol() const
Definition: Socket.h:90
SocketProtocol m_protocol
Definition: Socket.h:119
An error handling class.
Definition: Status.h:44