// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - RFC 8489 STUN binding-request client over TCP #pragma once #include #include #include #include #include namespace koutnet { // STUN servers share this port; Google runs a free-open public one on it, so // there is nothing to configure before room calls can learn a reflexive // candidate. Only the address discovery is someone else's: the media never // touches the server. inline constexpr quint16 kStunDefaultPort = 19302; // A binding request asks a STUN server what this connection looks like on the // far side of the NAT; the reply carries it back as XOR-MAPPED-ADDRESS. The // codec is free of the socket so the unit tests can drive it without a network. QByteArray encodeStunBindingRequest(const QByteArray &transactionId); // false unless the message is a binding success response for this transaction // with a mapped address in it, or unless the wire form is broken. bool parseStunBindingResponse(const QByteArray &message, const QByteArray &transactionId, QHostAddress *address, quint16 *port); class StunClient : public QObject { Q_OBJECT public: explicit StunClient(const QString &server = QStringLiteral("stun.l.google.com"), quint16 port = kStunDefaultPort, QObject *parent = nullptr); // The server the next flight will talk to. A change only takes effect on // the next refresh(): a flight already in the air finishes against the // server it left for, and the reflexive address in hand is not invalidated // by the mere act of reconfiguring. void setServer(const QString &host, quint16 port); QString server() const; quint16 port() const; // "ip:port" of the reflexive address the server last saw this station // from; empty until one flight succeeds, and re-emptied when a flight // comes back without an answer. The state lives here so the owner only // ever reads a string, never owns a STUN answer. Callers that publish a // candidate keep the offer control flow: the refresh side is one-way. QString reflexiveAddress() const; // One flight per call; the timeout and the socket are released on both // outcomes, so repeated calls can re-ask if the mapping has moved on. void refresh(); Q_SIGNALS: // emitted with the just-stored value after every flight, empty when the // flight failed. Carry it or read reflexiveAddress(), the value is the same. void reflexiveChanged(QString reflexive); protected: // The two outcomes, split out from the socket handling so the state rule // (success stores, failure clears, neither touches pending flights) can be // driven directly by a test that opens no socket. void setReflexive(const QString &address, quint16 port); void clearReflexive(); private: void onSocketReady(); void onDeadline(); void finish(); QString m_server; quint16 m_port = kStunDefaultPort; QString m_reflexive; QByteArray m_transactionId; QByteArray m_rx; QTcpSocket *m_socket = nullptr; QTimer *m_timeout = nullptr; }; } // namespace koutnet