QXmpp: QXmppStun_p.h Source File

QXmpp Version: 1.14.2

Loading...
Searching...
No Matches
QXmppStun_p.h
1// SPDX-FileCopyrightText: 2015 Jeremy Lainé <jeremy.laine@m4x.org>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPSTUN_P_H
6#define QXMPPSTUN_P_H
7
8#include "QXmppStun.h"
9
10class QUdpSocket;
11class QTimer;
12
13//
14// W A R N I N G
15// -------------
16//
17// This file is not part of the QXmpp API.
18//
19// This header file may change from version to version without notice,
20// or even be removed.
21//
22// We mean it.
23//
24
25//
26// The QXmppStunTransaction class represents a STUN transaction.
27//
28class QXMPP_EXPORT QXmppStunTransaction : public QXmppLoggable
29{
30 Q_OBJECT
31
32public:
33 QXmppStunTransaction(const QXmppStunMessage &request, std::function<void(const QXmppStunMessage &)> sendStunMessage, std::function<void(QXmppStunTransaction *)> onFinished, QObject *parent);
34 const QXmppStunMessage &request() const { return m_request; }
35 const QXmppStunMessage &response() const { return m_response; }
36
37 Q_SIGNAL void finished();
38 Q_SIGNAL void writeStun(const QXmppStunMessage &request);
39
40 Q_SLOT void readStun(const QXmppStunMessage &response);
41
42private:
43 Q_SLOT void retry();
44
45 QXmppStunMessage m_request;
46 QXmppStunMessage m_response;
47 QTimer *m_retryTimer;
48 int m_tries = 0;
49};
50
51class QXMPP_EXPORT QXmppIceTransport : public QXmppLoggable
52{
53 Q_OBJECT
54
55public:
56 QXmppIceTransport(QObject *parent = nullptr);
57 ~QXmppIceTransport() override;
58
59 virtual QXmppJingleCandidate localCandidate(int component) const = 0;
60 virtual qint64 writeDatagram(const QByteArray &data, const QHostAddress &host, quint16 port) = 0;
61
62 Q_SLOT virtual void disconnectFromHost() = 0;
63
65 Q_SIGNAL void datagramReceived(const QByteArray &data, const QHostAddress &host, quint16 port);
66};
67
68//
69// The QXmppTurnAllocation class represents a TURN allocation as defined
70// by RFC 5766 Traversal Using Relays around NAT (TURN).
71//
72class QXMPP_EXPORT QXmppTurnAllocation : public QXmppIceTransport
73{
74 Q_OBJECT
75
76public:
77 enum AllocationState {
78 UnconnectedState,
79 ConnectingState,
80 ConnectedState,
81 ClosingState
82 };
83
84 QXmppTurnAllocation(QObject *parent = nullptr);
85 ~QXmppTurnAllocation() override;
86
87 QHostAddress relayedHost() const;
88 quint16 relayedPort() const;
89 AllocationState state() const;
90
91 void setServer(const QHostAddress &host, quint16 port = 3478);
92 void setUser(const QString &user);
93 void setPassword(const QString &password);
94
95 QXmppJingleCandidate localCandidate(int component) const override;
96 qint64 writeDatagram(const QByteArray &data, const QHostAddress &host, quint16 port) override;
97
99 Q_SIGNAL void connected();
100
102 Q_SIGNAL void disconnected();
103
104 Q_SLOT void connectToHost();
105 Q_SLOT void disconnectFromHost() override;
106
107private:
108 void sendStunMessage(QXmppStunMessage &&message);
109 Q_SLOT void readyRead();
110 Q_SLOT void refresh();
111 Q_SLOT void refreshChannels();
112 void handleTransactionFinished(QXmppStunTransaction *);
113 void writeStun(const QXmppStunMessage &message);
114
115 void handleDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port);
116 void setState(AllocationState state);
117
118 QUdpSocket *socket = nullptr;
119 QTimer *m_timer = nullptr;
120 QTimer *m_channelTimer = nullptr;
121 QString m_password;
122 QString m_username;
123 QHostAddress m_relayedHost;
124 quint16 m_relayedPort = 0;
125 QHostAddress m_turnHost;
126 quint16 m_turnPort = 0;
127
128 // channels
129 typedef QPair<QHostAddress, quint16> Address;
130 quint16 m_channelNumber = 0x4000;
131 QMap<quint16, Address> m_channels;
132
133 // state
134 quint32 m_lifetime = 600;
135 QByteArray m_key;
136 QString m_realm;
137 QByteArray m_nonce;
138 AllocationState m_state = UnconnectedState;
139 QList<QXmppStunTransaction *> m_transactions;
140};
141
142//
143// The QXmppUdpTransport class represents a UDP transport.
144//
145class QXMPP_EXPORT QXmppUdpTransport : public QXmppIceTransport
146{
147 Q_OBJECT
148
149public:
150 QXmppUdpTransport(QUdpSocket *socket, QObject *parent = nullptr);
151 ~QXmppUdpTransport() override;
152
153 QXmppJingleCandidate localCandidate(int component) const override;
154 qint64 writeDatagram(const QByteArray &data, const QHostAddress &host, quint16 port) override;
155
156 Q_SLOT void disconnectFromHost() override;
157
158private:
159 Q_SLOT void readyRead();
160
161 QUdpSocket *m_socket;
162};
163
164#endif
QXmppLoggable
The QXmppLoggable class represents a source of logging messages.
Definition QXmppLogger.h:109