10#define _WINSOCK_DEPRECATED_NO_WARNINGS
15#include "lldb/Host/Config.h"
20#include "llvm/Config/llvm-config.h"
21#include "llvm/Support/Errno.h"
22#include "llvm/Support/WindowsError.h"
23#include "llvm/Support/raw_ostream.h"
27#include <netinet/tcp.h>
28#include <sys/socket.h>
36#define CLOSE_SOCKET closesocket
40#define CLOSE_SOCKET ::close
50 EC = llvm::mapWindowsError(WSAGetLastError());
52 EC = std::error_code(errno, std::generic_category());
57static const int kType = SOCK_STREAM;
60 :
Socket(ProtocolTcp, should_close, child_processes_inherit) {}
63 :
Socket(ProtocolTcp, listen_socket.m_should_close_fd,
64 listen_socket.m_child_processes_inherit) {
69 bool child_processes_inherit)
70 :
Socket(ProtocolTcp, should_close, child_processes_inherit) {
85 if (::getsockname(
m_socket, sock_addr, &sock_addr_len) == 0)
103 if (::getsockname(
m_socket, sock_addr, &sock_addr_len) == 0)
113 if (::getpeername(
m_socket, sock_addr, &sock_addr_len) == 0)
125 if (::getpeername(
m_socket, sock_addr, &sock_addr_len) == 0)
133 return std::string(llvm::formatv(
153 LLDB_LOG(log,
"Connect to host/port {0}", name);
158 return Status(host_port.takeError());
160 std::vector<SocketAddress> addresses =
162 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
168 address.SetPort(host_port->port);
172 address.GetLength()) == -1) {
186 error.SetErrorString(
"Failed to connect port");
192 LLDB_LOG(log,
"Listen to {0}", name);
197 return Status(host_port.takeError());
199 if (host_port->hostname ==
"*")
200 host_port->hostname =
"0.0.0.0";
202 host_port->hostname.c_str(),
nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
206 if (
error.Fail() || fd < 0)
210 int option_value = 1;
213 if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
214 sizeof(option_value)) == -1) {
223 listen_address.
SetPort(host_port->port);
228 err = ::listen(fd, backlog);
236 if (host_port->port == 0) {
237 socklen_t sa_len = address.GetLength();
238 if (getsockname(fd, &address.sockaddr(), &sa_len) == 0)
239 host_port->port = address.GetPort();
245 assert(
error.Fail());
260 error.SetErrorString(
"No open listening sockets!");
268 std::vector<MainLoopBase::ReadHandleUP> handles;
270 auto fd = socket.first;
274 io_sp, [fd, inherit, &sock, &AcceptAddr, &
error,
276 socklen_t sa_len = AcceptAddr.GetMaxLength();
277 sock = AcceptSocket(fd, &AcceptAddr.sockaddr(), &sa_len, inherit,
280 loop.RequestTermination();
286 bool accept_connection =
false;
287 std::unique_ptr<TCPSocket> accepted_socket;
289 while (!accept_connection) {
296 if (!AddrIn.
IsAnyAddr() && AcceptAddr != AddrIn) {
301 llvm::errs() << llvm::formatv(
302 "error: rejecting incoming connection from {0} (expecting {1})",
306 accept_connection =
true;
307 accepted_socket.reset(
new TCPSocket(sock, *
this));
310 if (!accepted_socket)
314 accepted_socket->SetOptionNoDelay();
316 conn_socket = accepted_socket.release();
321 return SetOption(IPPROTO_TCP, TCP_NODELAY, 1);
325 return SetOption(SOL_SOCKET, SO_REUSEADDR, 1);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
const void * set_socket_option_arg_type
const void * set_socket_option_arg_type
static Status GetLastSocketError()
ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Status &error) override
static std::vector< SocketAddress > GetAddressInfo(const char *hostname, const char *servname, int ai_family, int ai_socktype, int ai_protocol, int ai_flags=0)
bool SetToAnyAddress(sa_family_t family, uint16_t port)
socklen_t GetLength() const
std::string GetIPAddress() const
static socklen_t GetMaxLength()
struct sockaddr & sockaddr()
bool SetPort(uint16_t port)
NativeSocket GetNativeSocket() const
static const NativeSocket kInvalidSocketValue
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
int SetOption(int level, int option_name, int option_value)
bool m_child_processes_inherit
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
Status Listen(llvm::StringRef name, int backlog) override
bool IsValid() const override
void CloseListenSockets()
Status Accept(Socket *&conn_socket) override
uint16_t GetRemotePortNumber() const
Status CreateSocket(int domain)
std::string GetRemoteConnectionURI() const override
std::string GetLocalIPAddress() const
std::string GetRemoteIPAddress() const
uint16_t GetLocalPortNumber() const
TCPSocket(bool should_close, bool child_processes_inherit)
int SetOptionReuseAddress()
std::map< int, SocketAddress > m_listen_sockets
Status Connect(llvm::StringRef name) override
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::IOObject > IOObjectSP