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/SocketAddress.h"
19 #include "lldb/Utility/IOObject.h"
20 #include "lldb/Utility/Predicate.h"
21 #include "lldb/Utility/Status.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                                         Status &error);
57 
58   virtual Status Connect(llvm::StringRef name) = 0;
59   virtual Status Listen(llvm::StringRef name, int backlog) = 0;
60   virtual Status Accept(Socket *&socket) = 0;
61 
62   // Initialize a Tcp Socket object in listening mode.  listen and accept are
63   // implemented separately because the caller may wish to manipulate or query
64   // the socket after it is initialized, but before entering a blocking accept.
65   static Status TcpListen(llvm::StringRef host_and_port,
66                           bool child_processes_inherit, Socket *&socket,
67                           Predicate<uint16_t> *predicate, int backlog = 5);
68   static Status TcpConnect(llvm::StringRef host_and_port,
69                            bool child_processes_inherit, Socket *&socket);
70   static Status UdpConnect(llvm::StringRef host_and_port,
71                            bool child_processes_inherit, Socket *&socket);
72   static Status UnixDomainConnect(llvm::StringRef host_and_port,
73                                   bool child_processes_inherit,
74                                   Socket *&socket);
75   static Status UnixDomainAccept(llvm::StringRef host_and_port,
76                                  bool child_processes_inherit, Socket *&socket);
77   static Status UnixAbstractConnect(llvm::StringRef host_and_port,
78                                     bool child_processes_inherit,
79                                     Socket *&socket);
80   static Status UnixAbstractAccept(llvm::StringRef host_and_port,
81                                    bool child_processes_inherit,
82                                    Socket *&socket);
83 
84   int GetOption(int level, int option_name, int &option_value);
85   int SetOption(int level, int option_name, int option_value);
86 
GetNativeSocket()87   NativeSocket GetNativeSocket() const { return m_socket; }
GetSocketProtocol()88   SocketProtocol GetSocketProtocol() const { return m_protocol; }
89 
90   Status Read(void *buf, size_t &num_bytes) override;
91   Status Write(const void *buf, size_t &num_bytes) override;
92 
93   virtual Status PreDisconnect();
94   Status Close() override;
95 
IsValid()96   bool IsValid() const override { return m_socket != kInvalidSocketValue; }
97   WaitableHandle GetWaitableHandle() override;
98 
99   static bool DecodeHostAndPort(llvm::StringRef host_and_port,
100                                 std::string &host_str, std::string &port_str,
101                                 int32_t &port, Status *error_ptr);
102 
103 protected:
104   Socket(SocketProtocol protocol, bool should_close,
105          bool m_child_process_inherit);
106 
107   virtual size_t Send(const void *buf, const size_t num_bytes);
108 
109   static void SetLastError(Status &error);
110   static NativeSocket CreateSocket(const int domain, const int type,
111                                    const int protocol,
112                                    bool child_processes_inherit, Status &error);
113   static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
114                                    socklen_t *addrlen,
115                                    bool child_processes_inherit, Status &error);
116 
117   SocketProtocol m_protocol;
118   NativeSocket m_socket;
119   bool m_child_processes_inherit;
120 };
121 
122 } // namespace lldb_private
123 
124 #endif // liblldb_Host_Socket_h_
125