xref: /NextBSD/contrib/llvm/tools/lldb/include/lldb/Host/Socket.h (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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 <string>
14 
15 #include "lldb/lldb-private.h"
16 
17 #include "lldb/Core/Error.h"
18 #include "lldb/Host/IOObject.h"
19 #include "lldb/Host/Predicate.h"
20 #include "lldb/Host/SocketAddress.h"
21 
22 #ifdef _WIN32
23 #include "lldb/Host/windows/windows.h"
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #endif
27 
28 namespace llvm
29 {
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 {
43 public:
44     typedef enum
45     {
46         ProtocolTcp,
47         ProtocolUdp,
48         ProtocolUnixDomain
49     } SocketProtocol;
50 
51     static const NativeSocket kInvalidSocketValue;
52 
53     Socket(NativeSocket socket, SocketProtocol protocol, bool should_close);
54     ~Socket();
55 
56     // Initialize a Tcp Socket object in listening mode.  listen and accept are implemented
57     // separately because the caller may wish to manipulate or query the socket after it is
58     // initialized, but before entering a blocking accept.
59     static Error TcpListen(
60         llvm::StringRef host_and_port,
61         bool child_processes_inherit,
62         Socket *&socket,
63         Predicate<uint16_t>* predicate,
64         int backlog = 5);
65     static Error TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
66     static Error UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
67     static Error UnixDomainConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
68     static Error UnixDomainAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
69 
70     // Blocks on a listening socket until a connection is received.  This method assumes that
71     // |this->m_socket| is a listening socket, created via either TcpListen() or via the native
72     // constructor that takes a NativeSocket, which itself was created via a call to |listen()|
73     Error BlockingAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
74 
75     int GetOption (int level, int option_name, int &option_value);
76     int SetOption (int level, int option_name, int option_value);
77 
78     // returns port number or 0 if error
79     static uint16_t GetLocalPortNumber (const NativeSocket& socket);
80 
81     // returns port number or 0 if error
82     uint16_t GetLocalPortNumber () const;
83 
84     // returns ip address string or empty string if error
85     std::string GetLocalIPAddress () const;
86 
87     // must be connected
88     // returns port number or 0 if error
89     uint16_t GetRemotePortNumber () const;
90 
91     // must be connected
92     // returns ip address string or empty string if error
93     std::string GetRemoteIPAddress () const;
94 
GetNativeSocket()95     NativeSocket GetNativeSocket () const { return m_socket; }
GetSocketProtocol()96     SocketProtocol GetSocketProtocol () const { return m_protocol; }
97 
98     virtual Error Read (void *buf, size_t &num_bytes);
99     virtual Error Write (const void *buf, size_t &num_bytes);
100 
101     virtual Error PreDisconnect ();
102     virtual Error Close ();
103 
IsValid()104     virtual bool IsValid () const { return m_socket != kInvalidSocketValue; }
105     virtual WaitableHandle GetWaitableHandle ();
106 
107     static bool
108     DecodeHostAndPort (llvm::StringRef host_and_port,
109                        std::string &host_str,
110                        std::string &port_str,
111                        int32_t& port,
112                        Error *error_ptr);
113 
114 protected:
115     SocketProtocol m_protocol;
116     NativeSocket m_socket;
117     SocketAddress m_udp_send_sockaddr;    // Send address used for UDP connections.
118 };
119 }
120 
121 #endif
122