Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/hyprwire/core/ServerSocket.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <hyprutils/memory/SharedPtr.hpp>
#include <functional>
#include <string_view>

namespace Hyprwire {
class IProtocolServerImplementation;
Expand All @@ -12,6 +14,13 @@ namespace Hyprwire {

virtual int getPID() = 0;

/*
Set a protocol filter. If the function returns false,
the protocol will not be exposed in the handshake, and will not
be bindable.
*/
virtual void setProtocolFilter(std::function<bool(std::string_view)>&& fn) = 0;

protected:
IServerClient() = default;
};
Expand Down Expand Up @@ -57,6 +66,12 @@ namespace Hyprwire {
*/
virtual bool removeClient(int fd) = 0;

/*
Add a listener to each new client. This is emitted before we even
start a handshake, and is mostly exposed for setting up filters.
*/
virtual void setNewClientHandler(std::function<void(Hyprutils::Memory::CSharedPointer<IServerClient>)>&& fn) = 0;
Comment thread
vaxerski marked this conversation as resolved.
Resolved

protected:
IServerSocket() = default;
};
Expand Down
5 changes: 5 additions & 0 deletions src/core/message/MessageParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ size_t CMessageParser::parseSingleMessage(SSocketRawParsedMessage& raw, size_t o
std::vector<std::string> protocolNames;
protocolNames.reserve(client->m_server->m_impls.size());
for (const auto& impl : client->m_server->m_impls) {

// skip filtered out protocols
if (!std::ranges::contains(client->m_exposedProtocols, impl->protocol()->specName()))
continue;
Comment thread
vaxerski marked this conversation as resolved.
Resolved

protocolNames.emplace_back(std::format("{}@{}", impl->protocol()->specName(), impl->protocol()->specVer()));
}
client->sendMessage(CHandshakeProtocolsMessage(protocolNames));
Expand Down
49 changes: 49 additions & 0 deletions src/core/server/ServerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

#include <hyprwire/core/implementation/ServerImpl.hpp>
#include <hyprwire/core/implementation/Spec.hpp>
#include <hyprutils/utils/ScopeGuard.hpp>

#include <sys/socket.h>
#include <sys/types.h>

#include <algorithm>

#if defined(__FreeBSD__)
#include <sys/ucred.h>
#include <sys/un.h>
Expand All @@ -38,6 +41,18 @@ void CServerClient::dispatchFirstPoll() {
if (m_firstPollDone)
return;

auto x = Hyprutils::Utils::CScopeGuard([this] {
if (!m_server) {
TRACE(Debug::log(TRACE, "[{}] dispatchFirstPoll: no server?", m_fd.get()));
return;
}

if (m_server->m_newClientHandler)
Comment thread
vaxerski marked this conversation as resolved.
Resolved
m_server->m_newClientHandler(m_self.lock());

setupExposedProtocols();
});

m_firstPollDone = true;

// get peer's pid
Expand Down Expand Up @@ -124,6 +139,13 @@ SP<CServerObject> CServerClient::createObject(const std::string& protocol, const
if (p->protocol()->specName() != protocol)
continue;

if (!std::ranges::contains(m_exposedProtocols, protocol)) {
Debug::log(ERR, "[{} @ {:.3f}] Error: createObject on a non-exposed protocol", m_fd.get(), steadyMillis());
sendMessage(CFatalErrorMessage(obj->m_id, static_cast<uint32_t>(-1), "Invalid protocol"));
m_error = true;
return nullptr;
Comment thread
vaxerski marked this conversation as resolved.
Resolved
}

for (const auto& s : p->protocol()->objects()) {
if (s->objectName() != object && !object.empty())
continue;
Expand All @@ -136,13 +158,15 @@ SP<CServerObject> CServerClient::createObject(const std::string& protocol, const

if (!obj->m_spec) {
Debug::log(ERR, "[{} @ {:.3f}] Error: createObject has no spec", m_fd.get(), steadyMillis());
sendMessage(CFatalErrorMessage(obj->m_id, static_cast<uint32_t>(-1), "Invalid protocol"));
m_error = true;
return nullptr;
}

if (p->protocol()->specVer() < version) {
Debug::log(ERR, "[{} @ {:.3f}] Error: createObject for protocol {} object {} for version {}, but we have only {}", m_fd.get(), steadyMillis(), obj->m_protocolName,
object, version, p->protocol()->specVer());
sendMessage(CFatalErrorMessage(obj->m_id, static_cast<uint32_t>(-1), std::format("Invalid protocol version ({} > {})", version, p->protocol()->specVer())));
m_error = true;
return nullptr;
}
Expand All @@ -152,6 +176,7 @@ SP<CServerObject> CServerClient::createObject(const std::string& protocol, const

if (!obj->m_spec) {
Debug::log(ERR, "[{} @ {:.3f}] Error: createObject has no spec", m_fd.get(), steadyMillis());
sendMessage(CFatalErrorMessage(obj->m_id, static_cast<uint32_t>(-1), "Invalid protocol"));
m_error = true;
return nullptr;
}
Expand Down Expand Up @@ -210,3 +235,27 @@ void CServerClient::onGeneric(const CGenericProtocolMessage& msg) {
int CServerClient::getPID() {
return m_pid;
}

void CServerClient::setProtocolFilter(std::function<bool(std::string_view)>&& fn) {
m_protocolFilter = std::move(fn);
}

void CServerClient::setupExposedProtocols() {
for (const auto& i : m_server->m_impls) {
const auto NAME = i->protocol()->specName();

if (!m_protocolFilter) {
Debug::log(TRACE, "[{} @ {:.3f}] Exposing {} (no filter set up)", m_fd.get(), steadyMillis(), NAME);
m_exposedProtocols.emplace_back(NAME);
continue;
}

if (!m_protocolFilter(NAME)) {
Debug::log(TRACE, "[{} @ {:.3f}] Not exposing {} (filter failed)", m_fd.get(), steadyMillis(), NAME);
continue;
}

Debug::log(TRACE, "[{} @ {:.3f}] Exposing {} (filter passed)", m_fd.get(), steadyMillis(), NAME);
m_exposedProtocols.emplace_back(NAME);
}
}
37 changes: 21 additions & 16 deletions src/core/server/ServerClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ namespace Hyprwire {
CServerClient(int fd);
virtual ~CServerClient();

virtual int getPID();
virtual int getPID() override;
virtual void setProtocolFilter(std::function<bool(std::string_view)>&& fn) override;

void sendMessage(const IMessage& message);
SP<CServerObject> createObject(const std::string& protocol, const std::string& object, uint32_t version, uint32_t seq);
void destroyObject(uint32_t id);
void onBind(SP<CServerObject> obj);
void onGeneric(const CGenericProtocolMessage& msg);
void dispatchFirstPoll();
void sendMessage(const IMessage& message);
SP<CServerObject> createObject(const std::string& protocol, const std::string& object, uint32_t version, uint32_t seq);
void destroyObject(uint32_t id);
void onBind(SP<CServerObject> obj);
void onGeneric(const CGenericProtocolMessage& msg);
void dispatchFirstPoll();
void setupExposedProtocols();

Hyprutils::OS::CFileDescriptor m_fd;
Hyprutils::OS::CFileDescriptor m_fd;

int m_pid = -1;
bool m_firstPollDone = false;
int m_pid = -1;
bool m_firstPollDone = false;

uint32_t m_version = 0, m_maxId = 1;
bool m_error = false;
uint32_t m_version = 0, m_maxId = 1;
bool m_error = false;

uint32_t m_scheduledRoundtripSeq = 0;
uint32_t m_scheduledRoundtripSeq = 0;

std::vector<SP<CServerObject>> m_objects;
std::vector<SP<CServerObject>> m_objects;

WP<CServerSocket> m_server;
WP<CServerClient> m_self;
WP<CServerSocket> m_server;
WP<CServerClient> m_self;

std::function<bool(std::string_view)> m_protocolFilter;
std::vector<std::string> m_exposedProtocols;
};
};
22 changes: 18 additions & 4 deletions src/core/server/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void CServerSocket::clearWakeupFd() {
clearFd(m_wakeupFd);
}

SP<IServerClient> CServerSocket::addClient(int fd) {
SP<CServerClient> CServerSocket::createClient(int fd) {
auto x = makeShared<CServerClient>(fd);
if (x->m_fd.isClosed() || !x->m_fd.isValid())
return nullptr;
Expand All @@ -215,6 +215,12 @@ SP<IServerClient> CServerSocket::addClient(int fd) {
x->m_server = m_self;
m_clients.emplace_back(x);

return x;
}

SP<IServerClient> CServerSocket::addClient(int fd) {
auto x = createClient(fd);

recheckPollFds();

// wake up any poller
Expand Down Expand Up @@ -275,9 +281,13 @@ bool CServerSocket::dispatchNewConnections() {
sockaddr_in clientAddress = {};
socklen_t clientSize = sizeof(clientAddress);

auto x = m_clients.emplace_back(makeShared<CServerClient>(accept(m_fd.get(), (sockaddr*)&clientAddress, &clientSize)));
x->m_server = m_self;
x->m_self = x;
int fd = accept(m_fd.get(), (sockaddr*)&clientAddress, &clientSize);
auto x = createClient(fd);

if (!x) {
Debug::log(ERR, "[{} @ {:.3f}] Failed to create a new client", fd, steadyMillis());
return false;
}

recheckPollFds();

Expand Down Expand Up @@ -437,3 +447,7 @@ SP<IObject> CServerSocket::createObject(SP<IServerClient> clientIface, SP<IObjec

return newObject;
}

void CServerSocket::setNewClientHandler(std::function<void(Hyprutils::Memory::CSharedPointer<IServerClient>)>&& fn) {
m_newClientHandler = std::move(fn);
}
18 changes: 12 additions & 6 deletions src/core/server/ServerSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ namespace Hyprwire {
bool attempt(const std::string& path);
bool attemptEmpty();

virtual void addImplementation(SP<IProtocolServerImplementation>&&);
virtual bool dispatchEvents(bool block);
virtual int extractLoopFD();
virtual SP<IObject> createObject(SP<IServerClient> client, SP<IObject> reference, const std::string& object, uint32_t seq);
virtual SP<IServerClient> addClient(int fd);
virtual bool removeClient(int fd);
virtual void addImplementation(SP<IProtocolServerImplementation>&&) override;
virtual bool dispatchEvents(bool block) override;
virtual int extractLoopFD() override;
virtual SP<IObject> createObject(SP<IServerClient> client, SP<IObject> reference, const std::string& object, uint32_t seq) override;
virtual SP<IServerClient> addClient(int fd) override;
virtual bool removeClient(int fd) override;
virtual void setNewClientHandler(std::function<void(Hyprutils::Memory::CSharedPointer<IServerClient>)>&& fn) override;

void recheckPollFds();
bool dispatchNewConnections();
Expand Down Expand Up @@ -59,5 +60,10 @@ namespace Hyprwire {

bool m_isEmptyListener = false;
std::string m_path;

std::function<void(Hyprutils::Memory::CSharedPointer<IServerClient>)> m_newClientHandler;

private:
SP<CServerClient> createClient(int fd);
};
};
Loading
Loading