]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/Socket.h
Merge ^/head r317281 through r317502.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / Socket.h
1 //===-- Socket.h ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Host_Socket_h_
11 #define liblldb_Host_Socket_h_
12
13 #include <memory>
14 #include <string>
15
16 #include "lldb/lldb-private.h"
17
18 #include "lldb/Host/IOObject.h"
19 #include "lldb/Host/Predicate.h"
20 #include "lldb/Host/SocketAddress.h"
21 #include "lldb/Utility/Error.h"
22
23 #ifdef _WIN32
24 #include "lldb/Host/windows/windows.h"
25 #include <winsock2.h>
26 #include <ws2tcpip.h>
27 #endif
28
29 namespace llvm {
30 class StringRef;
31 }
32
33 namespace lldb_private {
34
35 #if defined(_MSC_VER)
36 typedef SOCKET NativeSocket;
37 #else
38 typedef int NativeSocket;
39 #endif
40
41 class Socket : public IOObject {
42 public:
43   typedef enum {
44     ProtocolTcp,
45     ProtocolUdp,
46     ProtocolUnixDomain,
47     ProtocolUnixAbstract
48   } SocketProtocol;
49
50   static const NativeSocket kInvalidSocketValue;
51
52   ~Socket() override;
53
54   static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
55                                         bool child_processes_inherit,
56                                         Error &error);
57
58   virtual Error Connect(llvm::StringRef name) = 0;
59   virtual Error Listen(llvm::StringRef name, int backlog) = 0;
60   virtual Error Accept(llvm::StringRef name, bool child_processes_inherit,
61                        Socket *&socket) = 0;
62
63   // Initialize a Tcp Socket object in listening mode.  listen and accept are
64   // implemented
65   // separately because the caller may wish to manipulate or query the socket
66   // after it is
67   // initialized, but before entering a blocking accept.
68   static Error TcpListen(llvm::StringRef host_and_port,
69                          bool child_processes_inherit, Socket *&socket,
70                          Predicate<uint16_t> *predicate, int backlog = 5);
71   static Error TcpConnect(llvm::StringRef host_and_port,
72                           bool child_processes_inherit, Socket *&socket);
73   static Error UdpConnect(llvm::StringRef host_and_port,
74                           bool child_processes_inherit, Socket *&socket);
75   static Error UnixDomainConnect(llvm::StringRef host_and_port,
76                                  bool child_processes_inherit, Socket *&socket);
77   static Error UnixDomainAccept(llvm::StringRef host_and_port,
78                                 bool child_processes_inherit, Socket *&socket);
79   static Error UnixAbstractConnect(llvm::StringRef host_and_port,
80                                    bool child_processes_inherit,
81                                    Socket *&socket);
82   static Error 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   Error Read(void *buf, size_t &num_bytes) override;
93   Error Write(const void *buf, size_t &num_bytes) override;
94
95   virtual Error PreDisconnect();
96   Error 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, Error *error_ptr);
104
105 protected:
106   Socket(NativeSocket socket, SocketProtocol protocol, bool should_close);
107
108   virtual size_t Send(const void *buf, const size_t num_bytes);
109
110   static void SetLastError(Error &error);
111   static NativeSocket CreateSocket(const int domain, const int type,
112                                    const int protocol,
113                                    bool child_processes_inherit, Error &error);
114   static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
115                                    socklen_t *addrlen,
116                                    bool child_processes_inherit, Error &error);
117
118   SocketProtocol m_protocol;
119   NativeSocket m_socket;
120 };
121
122 } // namespace lldb_private
123
124 #endif // liblldb_Host_Socket_h_