QXmpp  Version:1.0.0
QXmppStun.h
1 /*
2  * Copyright (C) 2008-2019 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 <QObject>
28 
29 #include "QXmppLogger.h"
30 #include "QXmppJingleIq.h"
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 = 0);
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:
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 = 0);
178 
179 public slots:
180  void close();
181  void connectToHost();
182  qint64 sendDatagram(const QByteArray &datagram);
183 
184 private 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 signals:
194  void connected();
195 
197  void datagramReceived(const QByteArray &datagram);
198 
200  void gatheringStateChanged();
201 
203  void localCandidatesChanged();
204 
205 private:
206  QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent=0);
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  Q_ENUMS(GatheringState)
245  Q_PROPERTY(QXmppIceConnection::GatheringState gatheringState READ gatheringState NOTIFY gatheringStateChanged)
246 
247 public:
248  enum GatheringState
249  {
250  NewGatheringState,
251  BusyGatheringState,
252  CompleteGatheringState
253  };
254 
255  QXmppIceConnection(QObject *parent = 0);
257 
258  QXmppIceComponent *component(int component);
259  void addComponent(int component);
260  void setIceControlling(bool controlling);
261 
262  QList<QXmppJingleCandidate> localCandidates() const;
263  QString localUser() const;
264  QString localPassword() const;
265 
266  void addRemoteCandidate(const QXmppJingleCandidate &candidate);
267  void setRemoteUser(const QString &user);
268  void setRemotePassword(const QString &password);
269 
270  void setStunServer(const QHostAddress &host, quint16 port = 3478);
271  void setTurnServer(const QHostAddress &host, quint16 port = 3478);
272  void setTurnUser(const QString &user);
273  void setTurnPassword(const QString &password);
274 
275  bool bind(const QList<QHostAddress> &addresses);
276  bool isConnected() const;
277 
278  GatheringState gatheringState() const;
279 
280 signals:
282  void connected();
283 
285  void disconnected();
286 
288  void gatheringStateChanged();
289 
291  void localCandidatesChanged();
292 
293 public slots:
294  void close();
295  void connectToHost();
296 
297 private slots:
298  void slotConnected();
299  void slotGatheringStateChanged();
300  void slotTimeout();
301 
302 private:
303  QXmppIceConnectionPrivate *d;
304 };
305 
306 #endif
QXmppIceConnection
The QXmppIceConnection class represents a set of UDP sockets capable of performing Interactive Connec...
Definition: QXmppStun.h:241
QXmppJingleCandidate
The QXmppJingleCandidate class represents a transport candidate as specified by XEP-0176: Jingle ICE-...
Definition: QXmppJingleIq.h:84
QXmppIceComponent
The QXmppIceComponent class represents a piece of a media stream requiring a single transport address...
Definition: QXmppStun.h:165
QXmppLoggable
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:111