QXmpp  Version: 1.10.4
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 
27 class QXMPP_EXPORT QXmppStunMessage
28 {
29 public:
30  enum MethodType {
31  Binding = 0x1,
32  SharedSecret = 0x2,
33  Allocate = 0x3,
34  Refresh = 0x4,
35  Send = 0x6,
36  Data = 0x7,
37  CreatePermission = 0x8,
38  ChannelBind = 0x9
39  };
40 
41  enum ClassType {
42  Request = 0x000,
43  Indication = 0x010,
44  Response = 0x100,
45  Error = 0x110
46  };
47 
48  QXmppStunMessage();
49 
50  quint32 cookie() const;
51  void setCookie(quint32 cookie);
52 
53  QByteArray id() const;
54  void setId(const QByteArray &id);
55 
56  quint16 messageClass() const;
57  quint16 messageMethod() const;
58 
59  quint16 type() const;
60  void setType(quint16 type);
61 
62  // attributes
63 
64  quint32 changeRequest() const;
65  void setChangeRequest(quint32 changeRequest);
66 
67  quint16 channelNumber() const;
68  void setChannelNumber(quint16 channelNumber);
69 
70  QByteArray data() const;
71  void setData(const QByteArray &data);
72 
73  quint32 lifetime() const;
74  void setLifetime(quint32 changeRequest);
75 
76  QByteArray nonce() const;
77  void setNonce(const QByteArray &nonce);
78 
79  quint32 priority() const;
80  void setPriority(quint32 priority);
81 
82  QString realm() const;
83  void setRealm(const QString &realm);
84 
85  QByteArray reservationToken() const;
86  void setReservationToken(const QByteArray &reservationToken);
87 
88  quint8 requestedTransport() const;
89  void setRequestedTransport(quint8 requestedTransport);
90 
91  QString software() const;
92  void setSoftware(const QString &software);
93 
94  QString username() const;
95  void setUsername(const QString &username);
96 
97  QByteArray encode(const QByteArray &key = QByteArray(), bool addFingerprint = true) const;
98  bool decode(const QByteArray &buffer, const QByteArray &key = QByteArray(), QStringList *errors = nullptr);
99  QString toString() const;
100  static quint16 peekType(const QByteArray &buffer, quint32 &cookie, QByteArray &id);
101 
102  // attributes
103  int errorCode;
104  QString errorPhrase;
105  QByteArray iceControlling;
106  QByteArray iceControlled;
107  QHostAddress changedHost;
108  quint16 changedPort;
109  QHostAddress mappedHost;
110  quint16 mappedPort;
111  QHostAddress otherHost;
112  quint16 otherPort;
113  QHostAddress sourceHost;
114  quint16 sourcePort;
115  QHostAddress xorMappedHost;
116  quint16 xorMappedPort;
117  QHostAddress xorPeerHost;
118  quint16 xorPeerPort;
119  QHostAddress xorRelayedHost;
120  quint16 xorRelayedPort;
121  bool useCandidate;
122 
123 private:
124  quint32 m_cookie;
125  QByteArray m_id;
126  quint16 m_type;
127 
128  // attributes
129  QSet<quint16> m_attributes;
130  quint32 m_changeRequest;
131  quint16 m_channelNumber;
132  QByteArray m_data;
133  quint32 m_lifetime;
134  QByteArray m_nonce;
135  quint32 m_priority;
136  QString m_realm;
137  quint8 m_requestedTransport;
138  QByteArray m_reservationToken;
139  QString m_software;
140  QString m_username;
141 };
142 
148 class QXMPP_EXPORT QXmppIceComponent : public QXmppLoggable
149 {
150  Q_OBJECT
151 
152 public:
153  ~QXmppIceComponent() override;
154 
155  int component() const;
156  bool isConnected() const;
157  QList<QXmppJingleCandidate> localCandidates() const;
158 
159  static QList<QHostAddress> discoverAddresses();
160  static QList<QUdpSocket *> reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent = nullptr);
161 
162 public Q_SLOTS:
163  void close();
164  void connectToHost();
165  qint64 sendDatagram(const QByteArray &datagram);
166 
167 private Q_SLOTS:
168  void checkCandidates();
169  void handleDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port);
170  void turnConnected();
171  void transactionFinished();
172  void updateGatheringState();
173  void writeStun(const QXmppStunMessage &request);
174 
175 Q_SIGNALS:
177  void connected();
178 
180  void datagramReceived(const QByteArray &datagram);
181 
183  void gatheringStateChanged();
184 
186  void localCandidatesChanged();
187 
188 private:
189  QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent = nullptr);
190 
191  const std::unique_ptr<QXmppIceComponentPrivate> d;
192  friend class QXmppIceComponentPrivate;
193  friend class QXmppIceConnection;
194 };
195 
224 class QXMPP_EXPORT QXmppIceConnection : public QXmppLoggable
225 {
226  Q_OBJECT
227 
233  Q_PROPERTY(QXmppIceConnection::GatheringState gatheringState READ gatheringState NOTIFY gatheringStateChanged)
234 
235 public:
242  NewGatheringState,
243  BusyGatheringState,
244  CompleteGatheringState
245  };
246  Q_ENUM(GatheringState)
247 
248  QXmppIceConnection(QObject *parent = nullptr);
249  ~QXmppIceConnection() override;
250 
251  QXmppIceComponent *component(int component);
252  void addComponent(int component);
253  void setIceControlling(bool controlling);
254 
255  QList<QXmppJingleCandidate> localCandidates() const;
256  QString localUser() const;
257  QString localPassword() const;
258 
259  void addRemoteCandidate(const QXmppJingleCandidate &candidate);
260  void setRemoteUser(const QString &user);
261  void setRemotePassword(const QString &password);
262 
263  void setStunServers(const QList<QPair<QHostAddress, quint16>> &servers);
264  void setStunServer(const QHostAddress &host, quint16 port = 3478);
265  void setTurnServer(const QHostAddress &host, quint16 port = 3478);
266  void setTurnUser(const QString &user);
267  void setTurnPassword(const QString &password);
268 
269  bool bind(const QList<QHostAddress> &addresses);
270  bool isConnected() const;
271 
272  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
279  GatheringState gatheringState() const;
280 
281 Q_SIGNALS:
283  void connected();
284 
286  void disconnected();
287 
293  void gatheringStateChanged();
294 
296  void localCandidatesChanged();
297 
298 public Q_SLOTS:
299  void close();
300  void connectToHost();
301 
302 private Q_SLOTS:
303  void slotConnected();
304  void slotGatheringStateChanged();
305  void slotTimeout();
306 
307 private:
308  const std::unique_ptr<QXmppIceConnectionPrivate> d;
309 };
310 
311 #endif
The QXmppIceConnection class represents a set of UDP sockets capable of performing Interactive Connec...
Definition: QXmppStun.h:224
GatheringState
Definition: QXmppStun.h:241
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:109
The QXmppIceComponent class represents a piece of a media stream requiring a single transport address...
Definition: QXmppStun.h:148
The QXmppJingleCandidate class represents a transport candidate as specified by XEP-0176: Jingle ICE-...
Definition: QXmppJingleData.h:277