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