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#include <vector>
15
18#include "lldb/lldb-private.h"
19
22#include "lldb/Utility/Status.h"
23
24#ifdef _WIN32
25#include "lldb/Host/Pipe.h"
27#include <winsock2.h>
28#include <ws2tcpip.h>
29#endif
30
31namespace llvm {
32class StringRef;
33}
34
35namespace lldb_private {
36
37#if defined(_WIN32)
38typedef SOCKET NativeSocket;
40#else
41typedef int NativeSocket;
43#endif
44class Socket;
45class TCPSocket;
46class UDPSocket;
47
49public:
50 static const shared_fd_t kInvalidFD;
51
52 SharedSocket(const Socket *socket, Status &error);
53
55
57
59
60private:
61#ifdef _WIN32
62 Pipe m_socket_pipe;
63 NativeSocket m_socket;
64#endif
66};
67
68class Socket : public IOObject {
69public:
76
81
82 struct HostAndPort {
83 std::string hostname;
84 uint16_t port;
85
86 bool operator==(const HostAndPort &R) const {
87 return port == R.port && hostname == R.hostname;
88 }
89 };
90
91 using ProtocolModePair = std::pair<SocketProtocol, SocketMode>;
92 static std::optional<ProtocolModePair>
93 GetProtocolAndMode(llvm::StringRef scheme);
94
96
97 ~Socket() override;
98
99 static const char *FindSchemeByProtocol(const SocketProtocol protocol);
100 static bool FindProtocolByScheme(const char *scheme,
101 SocketProtocol &protocol);
102
103 static llvm::Error Initialize();
104 static void Terminate();
105
106 static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
107 Status &error);
108
109 using Pair = std::pair<std::unique_ptr<Socket>, std::unique_ptr<Socket>>;
110 static llvm::Expected<Pair>
111 CreatePair(std::optional<SocketProtocol> protocol = std::nullopt);
112
113 virtual Status Connect(llvm::StringRef name) = 0;
114 virtual Status Listen(llvm::StringRef name, int backlog) = 0;
115
116 // Use the provided main loop instance to accept new connections. The callback
117 // will be called (from MainLoop::Run) for each new connection. This function
118 // does not block.
119 virtual llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>>
121 std::function<void(std::unique_ptr<Socket> socket)> sock_cb) = 0;
122
123 // Accept a single connection and "return" it in the pointer argument. This
124 // function blocks until the connection arrives.
125 virtual Status Accept(const Timeout<std::micro> &timeout, Socket *&socket);
126
127 // Initialize a Tcp Socket object in listening mode. listen and accept are
128 // implemented separately because the caller may wish to manipulate or query
129 // the socket after it is initialized, but before entering a blocking accept.
130 static llvm::Expected<std::unique_ptr<TCPSocket>>
131 TcpListen(llvm::StringRef host_and_port, int backlog = 5);
132
133 static llvm::Expected<std::unique_ptr<Socket>>
134 TcpConnect(llvm::StringRef host_and_port);
135
136 static llvm::Expected<std::unique_ptr<UDPSocket>>
137 UdpConnect(llvm::StringRef host_and_port);
138
139 static int GetOption(NativeSocket sockfd, int level, int option_name,
140 int &option_value);
141 int GetOption(int level, int option_name, int &option_value) {
142 return GetOption(m_socket, level, option_name, option_value);
143 };
144
145 static int SetOption(NativeSocket sockfd, int level, int option_name,
146 int option_value);
147 int SetOption(int level, int option_name, int option_value) {
148 return SetOption(m_socket, level, option_name, option_value);
149 };
150
153
154 Status Read(void *buf, size_t &num_bytes) override;
155 Status Write(const void *buf, size_t &num_bytes) override;
156
157 Status Close() override;
158
159 bool IsValid() const override { return m_socket != kInvalidSocketValue; }
161
162 static llvm::Expected<HostAndPort>
163 DecodeHostAndPort(llvm::StringRef host_and_port);
164
165 // If this Socket is connected then return the URI used to connect.
166 virtual std::string GetRemoteConnectionURI() const { return ""; };
167
168 // If the Socket is listening then return the URI for clients to connect.
169 virtual std::vector<std::string> GetListeningConnectionURI() const {
170 return {};
171 }
172
173protected:
174 Socket(SocketProtocol protocol, bool should_close);
175
176 virtual size_t Send(const void *buf, const size_t num_bytes);
177
178 static int CloseSocket(NativeSocket sockfd);
179 static Status GetLastError();
180 static void SetLastError(Status &error);
181 static NativeSocket CreateSocket(const int domain, const int type,
182 const int protocol, Status &error);
183 static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
184 socklen_t *addrlen, Status &error);
185
189};
190
191llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
192 const Socket::HostAndPort &HP);
193
194} // namespace lldb_private
195
196#endif // LLDB_HOST_SOCKET_H
static llvm::raw_ostream & error(Stream &strm)
IOObject(FDType type)
Definition IOObject.h:33
lldb::file_t WaitableHandle
Definition IOObject.h:29
static const shared_fd_t kInvalidFD
Definition Socket.h:50
SharedSocket(const Socket *socket, Status &error)
Definition Socket.cpp:66
Status CompleteSending(lldb::pid_t child_pid)
Definition Socket.cpp:83
shared_fd_t GetSendableFD()
Definition Socket.h:54
static Status GetNativeSocket(shared_fd_t fd, NativeSocket &socket)
Definition Socket.cpp:108
virtual Status Listen(llvm::StringRef name, int backlog)=0
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, Status &error)
Definition Socket.cpp:200
std::pair< std::unique_ptr< Socket >, std::unique_ptr< Socket > > Pair
Definition Socket.h:109
Status Read(void *buf, size_t &num_bytes) override
Definition Socket.cpp:319
NativeSocket GetNativeSocket() const
Definition Socket.h:151
virtual Status Connect(llvm::StringRef name)=0
static const NativeSocket kInvalidSocketValue
Definition Socket.h:95
WaitableHandle GetWaitableHandle() override
Definition Socket.cpp:315
virtual std::string GetRemoteConnectionURI() const
Definition Socket.h:166
SocketProtocol m_protocol
Definition Socket.h:186
std::pair< SocketProtocol, SocketMode > ProtocolModePair
Definition Socket.h:91
virtual size_t Send(const void *buf, const size_t num_bytes)
Definition Socket.cpp:410
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port)
Definition Socket.cpp:287
virtual llvm::Expected< std::vector< MainLoopBase::ReadHandleUP > > Accept(MainLoopBase &loop, std::function< void(std::unique_ptr< Socket > socket)> sock_cb)=0
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, Status &error)
Definition Socket.cpp:440
static int GetOption(NativeSocket sockfd, int level, int option_name, int &option_value)
Definition Socket.cpp:393
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Definition Socket.cpp:292
int GetOption(int level, int option_name, int &option_value)
Definition Socket.h:141
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, Status &error)
Definition Socket.cpp:476
virtual std::vector< std::string > GetListeningConnectionURI() const
Definition Socket.h:169
SocketProtocol GetSocketProtocol() const
Definition Socket.h:152
static bool FindProtocolByScheme(const char *scheme, SocketProtocol &protocol)
Definition Socket.cpp:159
bool IsValid() const override
Definition Socket.h:159
static Status GetLastError()
Definition Socket.cpp:422
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port)
Definition Socket.cpp:255
static void SetLastError(Status &error)
Definition Socket.cpp:414
static llvm::Error Initialize()
Definition Socket.cpp:176
static int CloseSocket(NativeSocket sockfd)
Definition Socket.cpp:432
static const char * FindSchemeByProtocol(const SocketProtocol protocol)
Definition Socket.cpp:151
NativeSocket m_socket
Definition Socket.h:187
static int SetOption(NativeSocket sockfd, int level, int option_name, int option_value)
Definition Socket.cpp:402
Status Write(const void *buf, size_t &num_bytes) override
Definition Socket.cpp:346
~Socket() override
Definition Socket.cpp:174
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, int backlog=5)
Definition Socket.cpp:272
Status Close() override
Definition Socket.cpp:374
static std::optional< ProtocolModePair > GetProtocolAndMode(llvm::StringRef scheme)
Definition Socket.cpp:498
int SetOption(int level, int option_name, int option_value)
Definition Socket.h:147
static llvm::Expected< Pair > CreatePair(std::optional< SocketProtocol > protocol=std::nullopt)
Definition Socket.cpp:238
Socket(SocketProtocol protocol, bool should_close)
Definition Socket.cpp:170
static void Terminate()
Definition Socket.cpp:194
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
NativeSocket shared_fd_t
Definition Socket.h:42
PipePosix Pipe
Definition Pipe.h:20
int NativeSocket
Definition Socket.h:41
int pipe_t
Definition lldb-types.h:64
uint64_t pid_t
Definition lldb-types.h:83
bool operator==(const HostAndPort &R) const
Definition Socket.h:86