// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - The voice media plane: listener, dial, channels, framing, TLS wrap #pragma once #include #include #include #include #include #include #include #include #include #include #include #include "core/security/IdentityCertificate.h" class QTcpServer; class QTimer; namespace koutnet { // The voice media plane, stood up out of NetworkManager: the listening server, // the parallel dial, the channel set a call rides, the frame reassembly and // the TLS wrap. Signalling (call_request/accept/reject) is not here, and // neither is the audio layer - VoiceCallManager sits above this transport and // the call buckets sit in NetworkManager. What this class owns is sockets, and // everything a socket does: who asks for a seat through the pin gate, who // files an authenticated identity into the peer table - those walls belong to // the owner, so they are pushed in as seams. // // Media travels as length-prefixed frames inside a TCP stream; over TLS the // stream is the same, wrapped in the identity certificate. A call lives on // until the last channel for the peer closes, because a peer that dials and // gets dialed joins back with a second channel carrying the mirror direction. class VoiceTransport : public QObject { Q_OBJECT public: explicit VoiceTransport(QObject *parent = nullptr); // The certificate the identity seed produced and whether new sockets wrap // in TLS, pushed together at start() (and cleared by stop()). void setCredentials(const IdentityCertificate &identity, bool overTls); bool overTls() const; // The owner's pin gate: "the chat channel already pinned this identity" is // the only thing an inbound voice handshake is admitted on. void setPinGate(std::function trusted); // Peer-table seams: identity id -> the address it is filed under (to find // the seat a handshake belongs to), and a way to file one back, since a // voice handshake authenticates like a chat-channel one. void setSeatLookup(std::function filedSeat); void setSeatFiler(std::function fileSeat); // Binds the voice listener on preferredPort, falling back to an OS-picked // port when it is taken; false only when both fails (reported, so the // owner treats it as a hard failure). bool listen(quint16 preferredPort); quint16 port() const; // 0 until listen() succeeds // The outgoing side of a call. connectTo() dials every candidate at once, // the first to connect settles the call and the rest are spared; a second // dial for a peer already up or already dialing reports true without doing // anything, mirroring the caller's "an attempt is in flight" contract. bool isCallUp(const QString &peer) const; bool isDialing(const QString &peer) const; bool connectTo(const QString &peer, const QStringList &candidates); // false when empty or over the receiver's cap, or when no channel is up. bool sendMedia(const QString &peer, const QByteArray &data); void hangUp(const QString &peer); // Tears every socket and the listener down; a stop()/start() cycle starts // a clean transport. void stop(); Q_SIGNALS: void voiceConnected(QString ip); void voiceDisconnected(QString ip); void callEnded(QString ip); void media(QString ip, QByteArray frame); void errorOccurred(QString message); private: void acceptDescriptor(qintptr socketDescriptor); void addChannel(const QString &peer, QAbstractSocket *sock); void onSocketReady(QAbstractSocket *sock, const QString &ip); void onSocketDisconnected(QAbstractSocket *sock, const QString &peer); void adoptDial(const QString &peer, QAbstractSocket *sock, QTimer *watchdog); QSslConfiguration tlsConfig(bool server) const; QTcpServer *m_listener = nullptr; quint16 m_port = 0; IdentityCertificate m_identity; bool m_overTls = false; std::function m_trusted; std::function m_seatFor; std::function m_fileSeat; // peer (the address the call is keyed by, always the announced host // address) -> every live voice socket for it: the outbound dial and, on a // LAN, the inbound accept of the peer's own dial. Both carry the same // media, so the call survives either half closing. QHash> m_channels; // peer -> candidate sockets still connecting. A dial has several attempts // in flight; the watchdog below bounds the whole set. QMap> m_pending; // one watchdog per dial, so a black-holed peer stops ringing in fifteen-ish // seconds instead of milking the OS connect timeout for a minute or more. QHash m_watchdogs; // Keyed on the socket rather than the IP: the inbound and outbound socket // for one peer are different streams and must not share a buffer. QHash m_rx; static constexpr int kConnectTimeoutMs = 15000; }; } // namespace koutnet