// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - The frame router and call-signalling state machine #pragma once #include #include #include #include #include namespace koutnet { // What a decoded frame arriving at the station becomes. Routed frames are // presence, messages (sealed or clear, over either transport), the call // signalling state machine and the group-key invite plane. Everything an // incoming frame can do in response is pinned to the owner through seams - // which host tells us what is ours, where a broadcast goes, what the rate gate // and the key stores say, and where a busy reply is posted - so the router // owns no sockets, no crypto and no persistence of its own. The one thing it // does own is the call buckets: "we sent call_req and have not heard back", // "their call_req is on screen waiting for the user", and the mirror of the // UI's active calls. Nothing crosses a boundary without first sitting in the // bucket its sender belongs in - that is what keeps a forged call_accept from // opening the microphone. The send side of those buckets is driven by the // owner (openRequest() when it dials, resolveRing() on its own accept/reject, // setActiveCalls() from the call manager), the receive side by route(). class FrameRouter : public QObject { Q_OBJECT public: explicit FrameRouter(QObject *parent = nullptr); // the crypto-backed rate gate; absent on a crypto-less owner. void setRateGate(std::function gate); // the addresses this station owns; broadcast and private frames are keyed // against them, and an echo of our own frame is dropped on them. void setOwnNetwork(std::function()> myIps); // where accepted presence frames go (PresenceService::acceptPresence). void setPresenceSink(std::function sink); // the reply posted when a call_req arrives while a call is live. void setBusyReply(std::function reply); // the sealed-key store: the key a group's bodies open under ("cipher key", // passphrase fallback included) and the pinned key a group's invites are // matched against, plus the filing of a freshly pinned key. void setKeySeams(std::function cipherKey, std::function pinnedKey, std::function setKey); // the crypto seam: whether the owner has a crypto manager, and the // asynchronous salted-KDF decrypt that preserves arrival order. void setCrypto( std::function hasCrypto, std::function &done)> decryptAsync); // route one decoded frame stamped with its arrival host (from_ip already // filled by the transport that carried it). Drops are silent, by design: // a flood is a flood and a forged accept is not worth answering. void route(const QString &host, QJsonObject msg); // send-side call buckets, driven by the owner. void openRequest(const QString &ip); void resolveRing(const QString &ip); void cancelRequest(const QString &ip); void setActiveCalls(const QSet &ips); Q_SIGNALS: // a chat/group/receipt frame whose text has been opened (or was clear). void message(QJsonObject msg); void callRequest(QString username, QString ip); void callAccepted(QString username, QString ip); void callRejected(QString ip); void callEnded(QString ip); // host is the address the frame arrived on, passed on so the receiving side // can bind the transfer to one sender. void fileMeta(QString host, QJsonObject meta); // an invite arrived: (groupId, name, fromIp). void groupInvite(QString groupId, QString name, QString fromIp); // an invite proposed a different key for an already-pinned group; nothing // was rewritten, the human decides whether to rotate. void groupKeyConfirmationRequired(QString groupId, QString name, QString fromIp); void typing(QString username, QString chatId, QString fromIp); private: void decryptText(QJsonObject msg, const std::function &done); std::function m_rateGate; std::function()> m_ownNetwork; std::function m_presenceSink; std::function m_busyReply; std::function m_cipherKey; std::function m_pinnedKey; std::function m_setKey; std::function m_hasCrypto; std::function &)> m_decryptAsync; QSet m_pendingCalls; QSet m_ringingCalls; QSet m_activeCalls; }; } // namespace koutnet