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
15#include "lldb/lldb-private.h"
16
19#include "lldb/Utility/Status.h"
20
21#ifdef _WIN32
23#include <winsock2.h>
24#include <ws2tcpip.h>
25#endif
26
27namespace llvm {
28class StringRef;
29}
30
31namespace lldb_private {
32
33#if defined(_WIN32)
34typedef SOCKET NativeSocket;
35#else
36typedef int NativeSocket;
37#endif
38class TCPSocket;
39class UDPSocket;
40
41class Socket : public IOObject {
42public:
48 };
49
50 struct HostAndPort {
51 std::string hostname;
52 uint16_t port;
53
54 bool operator==(const HostAndPort &R) const {
55 return port == R.port && hostname == R.hostname;
56 }
57 };
58
60
61 ~Socket() override;
62
63 static const char *FindSchemeByProtocol(const SocketProtocol protocol);
64 static bool FindProtocolByScheme(const char *scheme,
65 SocketProtocol &protocol);
66
67 static llvm::Error Initialize();
68 static void Terminate();
69
70 static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
71 bool child_processes_inherit,
72 Status &error);
73
74 virtual Status Connect(llvm::StringRef name) = 0;
75 virtual Status Listen(llvm::StringRef name, int backlog) = 0;
76 virtual Status Accept(Socket *&socket) = 0;
77
78 // Initialize a Tcp Socket object in listening mode. listen and accept are
79 // implemented separately because the caller may wish to manipulate or query
80 // the socket after it is initialized, but before entering a blocking accept.
81 static llvm::Expected<std::unique_ptr<TCPSocket>>
82 TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
83 int backlog = 5);
84
85 static llvm::Expected<std::unique_ptr<Socket>>
86 TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
87
88 static llvm::Expected<std::unique_ptr<UDPSocket>>
89 UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
90
91 int GetOption(int level, int option_name, int &option_value);
92 int SetOption(int level, int option_name, int option_value);
93
96
97 Status Read(void *buf, size_t &num_bytes) override;
98 Status Write(const void *buf, size_t &num_bytes) override;
99
100 Status Close() override;
101
102 bool IsValid() const override { return m_socket != kInvalidSocketValue; }
104
105 static llvm::Expected<HostAndPort>
106 DecodeHostAndPort(llvm::StringRef host_and_port);
107
108 // If this Socket is connected then return the URI used to connect.
109 virtual std::string GetRemoteConnectionURI() const { return ""; };
110
111protected:
112 Socket(SocketProtocol protocol, bool should_close,
113 bool m_child_process_inherit);
114
115 virtual size_t Send(const void *buf, const size_t num_bytes);
116
117 static void SetLastError(Status &error);
118 static NativeSocket CreateSocket(const int domain, const int type,
119 const int protocol,
120 bool child_processes_inherit, Status &error);
121 static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
122 socklen_t *addrlen,
123 bool child_processes_inherit, Status &error);
124
129};
130
131llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
132 const Socket::HostAndPort &HP);
133
134} // namespace lldb_private
135
136#endif // LLDB_HOST_SOCKET_H
static llvm::raw_ostream & error(Stream &strm)
virtual Status Listen(llvm::StringRef name, int backlog)=0
Status Read(void *buf, size_t &num_bytes) override
Definition: Socket.cpp:248
NativeSocket GetNativeSocket() const
Definition: Socket.h:94
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:215
virtual Status Connect(llvm::StringRef name)=0
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:59
WaitableHandle GetWaitableHandle() override
Definition: Socket.cpp:243
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:109
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:353
SocketProtocol m_protocol
Definition: Socket.h:125
virtual size_t Send(const void *buf, const size_t num_bytes)
Definition: Socket.cpp:341
int SetOption(int level, int option_name, int option_value)
Definition: Socket.cpp:334
bool m_child_processes_inherit
Definition: Socket.h:127
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Definition: Socket.cpp:220
int GetOption(int level, int option_name, int &option_value)
Definition: Socket.cpp:326
SocketProtocol GetSocketProtocol() const
Definition: Socket.h:95
static bool FindProtocolByScheme(const char *scheme, SocketProtocol &protocol)
Definition: Socket.cpp:94
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:369
bool IsValid() const override
Definition: Socket.h:102
static void SetLastError(Status &error)
Definition: Socket.cpp:345
static llvm::Error Initialize()
Definition: Socket.cpp:114
bool m_should_close_fd
Definition: Socket.h:128
static const char * FindSchemeByProtocol(const SocketProtocol protocol)
Definition: Socket.cpp:86
NativeSocket m_socket
Definition: Socket.h:126
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:180
Status Write(const void *buf, size_t &num_bytes) override
Definition: Socket.cpp:275
~Socket() override
Definition: Socket.cpp:112
virtual Status Accept(Socket *&socket)=0
Status Close() override
Definition: Socket.cpp:303
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:138
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, int backlog=5)
Definition: Socket.cpp:199
static void Terminate()
Definition: Socket.cpp:132
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
int NativeSocket
Definition: Socket.h:36
Definition: Debugger.h:54
bool operator==(const HostAndPort &R) const
Definition: Socket.h:54