Path: blob/main/contrib/llvm-project/lldb/tools/lldb-server/Acceptor.cpp
34879 views
//===-- Acceptor.cpp --------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Acceptor.h"910#include "llvm/ADT/StringRef.h"11#include "llvm/Support/ScopedPrinter.h"1213#include "lldb/Host/ConnectionFileDescriptor.h"14#include "lldb/Host/common/TCPSocket.h"15#include "lldb/Utility/StreamString.h"16#include "lldb/Utility/UriParser.h"17#include <optional>1819using namespace lldb;20using namespace lldb_private;21using namespace lldb_private::lldb_server;22using namespace llvm;2324namespace {2526struct SocketScheme {27const char *m_scheme;28const Socket::SocketProtocol m_protocol;29};3031SocketScheme socket_schemes[] = {32{"tcp", Socket::ProtocolTcp},33{"udp", Socket::ProtocolUdp},34{"unix", Socket::ProtocolUnixDomain},35{"unix-abstract", Socket::ProtocolUnixAbstract},36};3738bool FindProtocolByScheme(const char *scheme,39Socket::SocketProtocol &protocol) {40for (auto s : socket_schemes) {41if (!strcmp(s.m_scheme, scheme)) {42protocol = s.m_protocol;43return true;44}45}46return false;47}4849const char *FindSchemeByProtocol(const Socket::SocketProtocol protocol) {50for (auto s : socket_schemes) {51if (s.m_protocol == protocol)52return s.m_scheme;53}54return nullptr;55}56}5758Status Acceptor::Listen(int backlog) {59return m_listener_socket_up->Listen(StringRef(m_name), backlog);60}6162Status Acceptor::Accept(const bool child_processes_inherit, Connection *&conn) {63Socket *conn_socket = nullptr;64auto error = m_listener_socket_up->Accept(conn_socket);65if (error.Success())66conn = new ConnectionFileDescriptor(conn_socket);6768return error;69}7071Socket::SocketProtocol Acceptor::GetSocketProtocol() const {72return m_listener_socket_up->GetSocketProtocol();73}7475const char *Acceptor::GetSocketScheme() const {76return FindSchemeByProtocol(GetSocketProtocol());77}7879std::string Acceptor::GetLocalSocketId() const { return m_local_socket_id(); }8081std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,82const bool child_processes_inherit,83Status &error) {84error.Clear();8586Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;87// Try to match socket name as URL - e.g., tcp://localhost:555588if (std::optional<URI> res = URI::Parse(name)) {89if (!FindProtocolByScheme(res->scheme.str().c_str(), socket_protocol))90error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"",91res->scheme.str().c_str());92else93name = name.drop_front(res->scheme.size() + strlen("://"));94} else {95// Try to match socket name as $host:port - e.g., localhost:555596if (!llvm::errorToBool(Socket::DecodeHostAndPort(name).takeError()))97socket_protocol = Socket::ProtocolTcp;98}99100if (error.Fail())101return std::unique_ptr<Acceptor>();102103std::unique_ptr<Socket> listener_socket_up =104Socket::Create(socket_protocol, child_processes_inherit, error);105106LocalSocketIdFunc local_socket_id;107if (error.Success()) {108if (listener_socket_up->GetSocketProtocol() == Socket::ProtocolTcp) {109TCPSocket *tcp_socket =110static_cast<TCPSocket *>(listener_socket_up.get());111local_socket_id = [tcp_socket]() {112auto local_port = tcp_socket->GetLocalPortNumber();113return (local_port != 0) ? llvm::to_string(local_port) : "";114};115} else {116const std::string socket_name = std::string(name);117local_socket_id = [socket_name]() { return socket_name; };118}119120return std::unique_ptr<Acceptor>(121new Acceptor(std::move(listener_socket_up), name, local_socket_id));122}123124return std::unique_ptr<Acceptor>();125}126127Acceptor::Acceptor(std::unique_ptr<Socket> &&listener_socket, StringRef name,128const LocalSocketIdFunc &local_socket_id)129: m_listener_socket_up(std::move(listener_socket)), m_name(name.str()),130m_local_socket_id(local_socket_id) {}131132133