QXmpp  Version: 1.15.1
QXmppStun.h
1 // SPDX-FileCopyrightText: 2010 Jeremy LainĂ© <jeremy.laine@m4x.org>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 
5 #ifndef QXMPPSTUN_H
6 #define QXMPPSTUN_H
7 
8 #include "QXmppJingleIq.h"
9 #include "QXmppLogger.h"
10 
11 #include <QObject>
12 #include <QSet>
13 
14 class CandidatePair;
15 class QDataStream;
16 class QUdpSocket;
17 class QTimer;
18 class QXmppIceComponentPrivate;
19 class QXmppIceConnectionPrivate;
20 class QXmppIcePrivate;
21 class QXmppIceTransport;
22 class QXmppStunTransaction;
23 
24 namespace QXmpp {
25 struct StunServer;
26 struct TurnServer;
27 } // namespace QXmpp
28 
34 class QXMPP_EXPORT QXmppStunMessage
35 {
36 public:
37  enum MethodType {
38  Binding = 0x1,
39  SharedSecret = 0x2,
40  Allocate = 0x3,
41  Refresh = 0x4,
42  Send = 0x6,
43  Data = 0x7,
44  CreatePermission = 0x8,
45  ChannelBind = 0x9
46  };
47 
48  enum ClassType {
49  Request = 0x000,
50  Indication = 0x010,
51  Response = 0x100,
52  Error = 0x110
53  };
54 
55  QXmppStunMessage();
56 
57  quint32 cookie() const;
58  void setCookie(quint32 cookie);
59 
60  QByteArray id() const;
61  void setId(const QByteArray &id);
62 
63  quint16 messageClass() const;
64  quint16 messageMethod() const;
65 
66  quint16 type() const;
67  void setType(quint16 type);
68 
69  // attributes
70 
71  quint32 changeRequest() const;
72  void setChangeRequest(quint32 changeRequest);
73 
74  quint16 channelNumber() const;
75  void setChannelNumber(quint16 channelNumber);
76 
77  QByteArray data() const;
78  void setData(const QByteArray &data);
79 
80  quint32 lifetime() const;
81  void setLifetime(quint32 changeRequest);
82 
83  QByteArray nonce() const;
84  void setNonce(const QByteArray &nonce);
85 
86  quint32 priority() const;
87  void setPriority(quint32 priority);
88 
89  QString realm() const;
90  void setRealm(const QString &realm);
91 
92  QByteArray reservationToken() const;
93  void setReservationToken(const QByteArray &reservationToken);
94 
95  quint8 requestedTransport() const;
96  void setRequestedTransport(quint8 requestedTransport);
97 
98  QString software() const;
99  void setSoftware(const QString &software);
100 
101  QString username() const;
102  void setUsername(const QString &username);
103 
104  QByteArray encode(const QByteArray &key = QByteArray(), bool addFingerprint = true) const;
105  bool decode(const QByteArray &buffer, const QByteArray &key = QByteArray(), QStringList *errors = nullptr);
106  QString toString() const;
107  static quint16 peekType(const QByteArray &buffer, quint32 &cookie, QByteArray &id);
108 
109  // attributes
110  int errorCode = 0;
111  QString errorPhrase;
112  QByteArray iceControlling;
113  QByteArray iceControlled;
114  QHostAddress changedHost;
115  quint16 changedPort = 0;
116  QHostAddress mappedHost;
117  quint16 mappedPort = 0;
118  QHostAddress otherHost;
119  quint16 otherPort = 0;
120  QHostAddress sourceHost;
121  quint16 sourcePort = 0;
122  QHostAddress xorMappedHost;
123  quint16 xorMappedPort = 0;
124  QHostAddress xorPeerHost;
125  quint16 xorPeerPort = 0;
126  QHostAddress xorRelayedHost;
127  quint16 xorRelayedPort = 0;
128  bool useCandidate = false;
129 
130 private:
131  quint32 m_cookie;
132  QByteArray m_id;
133  quint16 m_type = 0;
134 
135  // attributes
136  QSet<quint16> m_attributes;
137  quint32 m_changeRequest = 0;
138  quint16 m_channelNumber = 0;
139  QByteArray m_data;
140  quint32 m_lifetime = 0;
141  QByteArray m_nonce;
142  quint32 m_priority = 0;
143  QString m_realm;
144  quint8 m_requestedTransport = 0;
145  QByteArray m_reservationToken;
146  QString m_software;
147  QString m_username;
148 };
149 
155 class QXMPP_EXPORT QXmppIceComponent : public QXmppLoggable
156 {
157  Q_OBJECT
158 
159 public:
160  ~QXmppIceComponent() override;
161 
162  int component() const;
163  bool isConnected() const;
164  QList<QXmppJingleCandidate> localCandidates() const;
165 
166  static QList<QHostAddress> discoverAddresses();
167  static QList<QUdpSocket *> reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent = nullptr);
168 
169  Q_SLOT void close();
170  Q_SLOT void connectToHost();
171  Q_SLOT qint64 sendDatagram(const QByteArray &datagram);
172 
174  Q_SIGNAL void connected();
175 
177  Q_SIGNAL void datagramReceived(const QByteArray &datagram);
178 
180  Q_SIGNAL void gatheringStateChanged();
181 
183  Q_SIGNAL void localCandidatesChanged();
184 
185 private:
186  QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent = nullptr);
187 
188  Q_SLOT void checkCandidates();
189  Q_SLOT void handleDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port);
190  Q_SLOT void turnConnected();
191  void handleStunResponse(QXmppIceTransport *transport, const QXmppStunMessage &response);
192  Q_SLOT void updateGatheringState();
193 
194  const std::unique_ptr<QXmppIceComponentPrivate> d;
195  friend class QXmppIceComponentPrivate;
196  friend class QXmppIceConnection;
197 };
198 
227 class QXMPP_EXPORT QXmppIceConnection : public QXmppLoggable
228 {
229  Q_OBJECT
230 
236  Q_PROPERTY(QXmppIceConnection::GatheringState gatheringState READ gatheringState NOTIFY gatheringStateChanged)
237 
238 public:
245  NewGatheringState,
246  BusyGatheringState,
247  CompleteGatheringState
248  };
249  Q_ENUM(GatheringState)
250 
251  QXmppIceConnection(QObject *parent = nullptr);
252  ~QXmppIceConnection() override;
253 
254  QXmppIceComponent *component(int component);
255  void addComponent(int component);
256  void setIceControlling(bool controlling);
257 
258  QList<QXmppJingleCandidate> localCandidates() const;
259  QString localUser() const;
260  QString localPassword() const;
261 
262  void addRemoteCandidate(const QXmppJingleCandidate &candidate);
263  void setRemoteUser(const QString &user);
264  void setRemotePassword(const QString &password);
265 
266  void setStunServers(const QList<QXmpp::StunServer> &servers);
267  void setTurnServer(const QXmpp::TurnServer &);
268 #if QXMPP_DEPRECATED_SINCE(1, 11)
269  [[deprecated("Use setStunServers(QList<StunServer>)")]]
270  void setStunServers(const QList<QPair<QHostAddress, quint16>> &servers);
271  [[deprecated("Use setStunServers(QList<StunServer>)")]]
272  void setStunServer(const QHostAddress &host, quint16 port = 3478);
273  [[deprecated("Use setTurnServer(TurnServer)")]]
274  void setTurnServer(const QHostAddress &host, quint16 port = 3478);
275  [[deprecated("Use setTurnServer(TurnServer)")]]
276  void setTurnUser(const QString &user);
277  [[deprecated("Use setTurnServer(TurnServer)")]]
278  void setTurnPassword(const QString &password);
279 #endif
280 
281  bool bind(const QList<QHostAddress> &addresses);
282  bool isConnected() const;
283 
284  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
291  GatheringState gatheringState() const;
292 
294  Q_SIGNAL void connected();
295 
297  Q_SIGNAL void disconnected();
298 
304  Q_SIGNAL void gatheringStateChanged();
305 
307  Q_SIGNAL void localCandidatesChanged();
308 
309  Q_SLOT void close();
310  Q_SLOT void connectToHost();
311 
312 private:
313  Q_SLOT void slotConnected();
314  Q_SLOT void slotGatheringStateChanged();
315  Q_SLOT void slotTimeout();
316 
317  const std::unique_ptr<QXmppIceConnectionPrivate> d;
318 };
319 
320 #endif
The QXmppIceConnection class represents a set of UDP sockets capable of performing Interactive Connec...
Definition: QXmppStun.h:227
GatheringState
Definition: QXmppStun.h:244
TURN server address.
Definition: QXmppTurnServer.h:19
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:108
The QXmppIceComponent class represents a piece of a media stream requiring a single transport address...
Definition: QXmppStun.h:155
The QXmppJingleCandidate class represents a transport candidate as specified by XEP-0176: Jingle ICE-...
Definition: QXmppJingleData.h:287