QXmpp  Version: 1.4.0
QXmppStun.h
1 /*
2  * Copyright (C) 2008-2021 The QXmpp developers
3  *
4  * Author:
5  * Jeremy LainĂ©
6  *
7  * Source:
8  * https://github.com/qxmpp-project/qxmpp
9  *
10  * This file is a part of QXmpp library.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  */
23 
24 #ifndef QXMPPSTUN_H
25 #define QXMPPSTUN_H
26 
27 #include "QXmppJingleIq.h"
28 #include "QXmppLogger.h"
29 
30 #include <QObject>
31 
32 class CandidatePair;
33 class QDataStream;
34 class QUdpSocket;
35 class QTimer;
36 class QXmppIceComponentPrivate;
37 class QXmppIceConnectionPrivate;
38 class QXmppIcePrivate;
39 
44 
45 class QXMPP_EXPORT QXmppStunMessage
46 {
47 public:
48  enum MethodType {
49  Binding = 0x1,
50  SharedSecret = 0x2,
51  Allocate = 0x3,
52  Refresh = 0x4,
53  Send = 0x6,
54  Data = 0x7,
55  CreatePermission = 0x8,
56  ChannelBind = 0x9
57  };
58 
59  enum ClassType {
60  Request = 0x000,
61  Indication = 0x010,
62  Response = 0x100,
63  Error = 0x110
64  };
65 
66  QXmppStunMessage();
67 
68  quint32 cookie() const;
69  void setCookie(quint32 cookie);
70 
71  QByteArray id() const;
72  void setId(const QByteArray &id);
73 
74  quint16 messageClass() const;
75  quint16 messageMethod() const;
76 
77  quint16 type() const;
78  void setType(quint16 type);
79 
80  // attributes
81 
82  quint32 changeRequest() const;
83  void setChangeRequest(quint32 changeRequest);
84 
85  quint16 channelNumber() const;
86  void setChannelNumber(quint16 channelNumber);
87 
88  QByteArray data() const;
89  void setData(const QByteArray &data);
90 
91  quint32 lifetime() const;
92  void setLifetime(quint32 changeRequest);
93 
94  QByteArray nonce() const;
95  void setNonce(const QByteArray &nonce);
96 
97  quint32 priority() const;
98  void setPriority(quint32 priority);
99 
100  QString realm() const;
101  void setRealm(const QString &realm);
102 
103  QByteArray reservationToken() const;
104  void setReservationToken(const QByteArray &reservationToken);
105 
106  quint8 requestedTransport() const;
107  void setRequestedTransport(quint8 requestedTransport);
108 
109  QString software() const;
110  void setSoftware(const QString &software);
111 
112  QString username() const;
113  void setUsername(const QString &username);
114 
115  QByteArray encode(const QByteArray &key = QByteArray(), bool addFingerprint = true) const;
116  bool decode(const QByteArray &buffer, const QByteArray &key = QByteArray(), QStringList *errors = nullptr);
117  QString toString() const;
118  static quint16 peekType(const QByteArray &buffer, quint32 &cookie, QByteArray &id);
119 
120  // attributes
121  int errorCode;
122  QString errorPhrase;
123  QByteArray iceControlling;
124  QByteArray iceControlled;
125  QHostAddress changedHost;
126  quint16 changedPort;
127  QHostAddress mappedHost;
128  quint16 mappedPort;
129  QHostAddress otherHost;
130  quint16 otherPort;
131  QHostAddress sourceHost;
132  quint16 sourcePort;
133  QHostAddress xorMappedHost;
134  quint16 xorMappedPort;
135  QHostAddress xorPeerHost;
136  quint16 xorPeerPort;
137  QHostAddress xorRelayedHost;
138  quint16 xorRelayedPort;
139  bool useCandidate;
140 
141 private:
142  quint32 m_cookie;
143  QByteArray m_id;
144  quint16 m_type;
145 
146  // attributes
147  QSet<quint16> m_attributes;
148  quint32 m_changeRequest;
149  quint16 m_channelNumber;
150  QByteArray m_data;
151  quint32 m_lifetime;
152  QByteArray m_nonce;
153  quint32 m_priority;
154  QString m_realm;
155  quint8 m_requestedTransport;
156  QByteArray m_reservationToken;
157  QString m_software;
158  QString m_username;
159 };
160 
164 
165 class QXMPP_EXPORT QXmppIceComponent : public QXmppLoggable
166 {
167  Q_OBJECT
168 
169 public:
170  ~QXmppIceComponent() override;
171 
172  int component() const;
173  bool isConnected() const;
174  QList<QXmppJingleCandidate> localCandidates() const;
175 
176  static QList<QHostAddress> discoverAddresses();
177  static QList<QUdpSocket *> reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent = nullptr);
178 
179 public Q_SLOTS:
180  void close();
181  void connectToHost();
182  qint64 sendDatagram(const QByteArray &datagram);
183 
184 private Q_SLOTS:
185  void checkCandidates();
186  void handleDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port);
187  void turnConnected();
188  void transactionFinished();
189  void updateGatheringState();
190  void writeStun(const QXmppStunMessage &request);
191 
192 Q_SIGNALS:
194  void connected();
195 
197  void datagramReceived(const QByteArray &datagram);
198 
200  void gatheringStateChanged();
201 
204 
205 private:
206  QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent = nullptr);
207 
208  QXmppIceComponentPrivate *d;
209  friend class QXmppIceComponentPrivate;
210  friend class QXmppIceConnection;
211 };
212 
240 
241 class QXMPP_EXPORT QXmppIceConnection : public QXmppLoggable
242 {
243  Q_OBJECT
244 
250  Q_PROPERTY(QXmppIceConnection::GatheringState gatheringState READ gatheringState NOTIFY gatheringStateChanged)
251 
252 public:
259  NewGatheringState,
260  BusyGatheringState,
261  CompleteGatheringState
262  };
263  Q_ENUM(GatheringState)
264 
265  QXmppIceConnection(QObject *parent = nullptr);
266  ~QXmppIceConnection() override;
267 
268  QXmppIceComponent *component(int component);
269  void addComponent(int component);
270  void setIceControlling(bool controlling);
271 
272  QList<QXmppJingleCandidate> localCandidates() const;
273  QString localUser() const;
274  QString localPassword() const;
275 
276  void addRemoteCandidate(const QXmppJingleCandidate &candidate);
277  void setRemoteUser(const QString &user);
278  void setRemotePassword(const QString &password);
279 
280  void setStunServers(const QList<QPair<QHostAddress, quint16>> &servers);
281  void setStunServer(const QHostAddress &host, quint16 port = 3478);
282  void setTurnServer(const QHostAddress &host, quint16 port = 3478);
283  void setTurnUser(const QString &user);
284  void setTurnPassword(const QString &password);
285 
286  bool bind(const QList<QHostAddress> &addresses);
287  bool isConnected() const;
288 
289  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
296  GatheringState gatheringState() const;
297 
298 Q_SIGNALS:
300  void connected();
301 
303  void disconnected();
304 
311 
314 
315 public Q_SLOTS:
316  void close();
317  void connectToHost();
318 
319 private Q_SLOTS:
320  void slotConnected();
321  void slotGatheringStateChanged();
322  void slotTimeout();
323 
324 private:
325  QXmppIceConnectionPrivate *d;
326 };
327 
328 #endif
The QXmppIceComponent class represents a piece of a media stream requiring a single transport address...
Definition: QXmppStun.h:166
void localCandidatesChanged()
This signal is emitted when the list of local candidates changes.
void datagramReceived(const QByteArray &datagram)
This signal is emitted when a data packet is received.
void connected()
This signal is emitted once ICE negotiation succeeds.
The QXmppIceConnection class represents a set of UDP sockets capable of performing Interactive Connec...
Definition: QXmppStun.h:242
void localCandidatesChanged()
This signal is emitted when the list of local candidates changes.
void gatheringStateChanged()
This signal is emitted when the gathering state of local candidates changes.
void disconnected()
This signal is emitted when ICE negotiation fails.
void connected()
This signal is emitted once ICE negotiation succeeds.
GatheringState
Definition: QXmppStun.h:258
The QXmppJingleCandidate class represents a transport candidate as specified by XEP-0176: Jingle ICE-...
Definition: QXmppJingleIq.h:85
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:124