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 llvm::Error Initialize();
64 static void Terminate();
65
66 static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
67 bool child_processes_inherit,
68 Status &error);
69
70 virtual Status Connect(llvm::StringRef name) = 0;
71 virtual Status Listen(llvm::StringRef name, int backlog) = 0;
72 virtual Status Accept(Socket *&socket) = 0;
73
74 // Initialize a Tcp Socket object in listening mode. listen and accept are
75 // implemented separately because the caller may wish to manipulate or query
76 // the socket after it is initialized, but before entering a blocking accept.
77 static llvm::Expected<std::unique_ptr<TCPSocket>>
78 TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
79 int backlog = 5);
80
81 static llvm::Expected<std::unique_ptr<Socket>>
82 TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
83
84 static llvm::Expected<std::unique_ptr<UDPSocket>>
85 UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
86
87 int GetOption(int level, int option_name, int &option_value);
88 int SetOption(int level, int option_name, int option_value);
89
92
93 Status Read(void *buf, size_t &num_bytes) override;
94 Status Write(const void *buf, size_t &num_bytes) override;
95
96 Status Close() override;
97
98 bool IsValid() const override { return m_socket != kInvalidSocketValue; }
100
101 static llvm::Expected<HostAndPort>
102 DecodeHostAndPort(llvm::StringRef host_and_port);
103
104 // If this Socket is connected then return the URI used to connect.
105 virtual std::string GetRemoteConnectionURI() const { return ""; };
106
107protected:
108 Socket(SocketProtocol protocol, bool should_close,
109 bool m_child_process_inherit);
110
111 virtual size_t Send(const void *buf, const size_t num_bytes);
112
113 static void SetLastError(Status &error);
114 static NativeSocket CreateSocket(const int domain, const int type,
115 const int protocol,
116 bool child_processes_inherit, Status &error);
117 static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
118 socklen_t *addrlen,
119 bool child_processes_inherit, Status &error);
120
125};
126
127llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
128 const Socket::HostAndPort &HP);
129
130} // namespace lldb_private
131
132#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:217
NativeSocket GetNativeSocket() const
Definition: Socket.h:90
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:184
virtual Status Connect(llvm::StringRef name)=0
static const NativeSocket kInvalidSocketValue
Definition: Socket.h:59
WaitableHandle GetWaitableHandle() override
Definition: Socket.cpp:212
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:105
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:322
SocketProtocol m_protocol
Definition: Socket.h:121
virtual size_t Send(const void *buf, const size_t num_bytes)
Definition: Socket.cpp:310
int SetOption(int level, int option_name, int option_value)
Definition: Socket.cpp:303
bool m_child_processes_inherit
Definition: Socket.h:123
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Definition: Socket.cpp:189
int GetOption(int level, int option_name, int &option_value)
Definition: Socket.cpp:295
SocketProtocol GetSocketProtocol() const
Definition: Socket.h:91
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:338
bool IsValid() const override
Definition: Socket.h:98
static void SetLastError(Status &error)
Definition: Socket.cpp:314
static llvm::Error Initialize()
Definition: Socket.cpp:82
bool m_should_close_fd
Definition: Socket.h:124
NativeSocket m_socket
Definition: Socket.h:122
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:149
Status Write(const void *buf, size_t &num_bytes) override
Definition: Socket.cpp:244
~Socket() override
Definition: Socket.cpp:80
virtual Status Accept(Socket *&socket)=0
Status Close() override
Definition: Socket.cpp:272
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:107
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, int backlog=5)
Definition: Socket.cpp:168
static void Terminate()
Definition: Socket.cpp:101
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Stream & operator<<(Stream &s, const Mangled &obj)
int NativeSocket
Definition: Socket.h:36
Definition: Debugger.h:53
bool operator==(const HostAndPort &R) const
Definition: Socket.h:54