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