QXmpp  Version:0.9.3
QXmppStun.h
00001 /*
00002  * Copyright (C) 2008-2014 The QXmpp developers
00003  *
00004  * Author:
00005  *  Jeremy Lainé
00006  *
00007  * Source:
00008  *  https://github.com/qxmpp-project/qxmpp
00009  *
00010  * This file is a part of QXmpp library.
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  */
00023 
00024 #ifndef QXMPPSTUN_H
00025 #define QXMPPSTUN_H
00026 
00027 #include <QObject>
00028 
00029 #include "QXmppLogger.h"
00030 #include "QXmppJingleIq.h"
00031 
00032 class CandidatePair;
00033 class QDataStream;
00034 class QUdpSocket;
00035 class QTimer;
00036 class QXmppIceComponentPrivate;
00037 class QXmppIceConnectionPrivate;
00038 class QXmppIcePrivate;
00039 
00044 
00045 class QXMPP_EXPORT QXmppStunMessage
00046 {
00047 public:
00048     enum MethodType {
00049         Binding      = 0x1,
00050         SharedSecret = 0x2,
00051         Allocate     = 0x3,
00052         Refresh      = 0x4,
00053         Send         = 0x6,
00054         Data         = 0x7,
00055         CreatePermission = 0x8,
00056         ChannelBind  = 0x9
00057     };
00058 
00059     enum ClassType {
00060         Request    = 0x000,
00061         Indication = 0x010,
00062         Response   = 0x100,
00063         Error      = 0x110
00064     };
00065 
00066     QXmppStunMessage();
00067 
00068     quint32 cookie() const;
00069     void setCookie(quint32 cookie);
00070 
00071     QByteArray id() const;
00072     void setId(const QByteArray &id);
00073 
00074     quint16 messageClass() const;
00075     quint16 messageMethod() const;
00076 
00077     quint16 type() const;
00078     void setType(quint16 type);
00079 
00080     // attributes
00081 
00082     quint32 changeRequest() const;
00083     void setChangeRequest(quint32 changeRequest);
00084 
00085     quint16 channelNumber() const;
00086     void setChannelNumber(quint16 channelNumber);
00087 
00088     QByteArray data() const;
00089     void setData(const QByteArray &data);
00090 
00091     quint32 lifetime() const;
00092     void setLifetime(quint32 changeRequest);
00093 
00094     QByteArray nonce() const;
00095     void setNonce(const QByteArray &nonce);
00096 
00097     quint32 priority() const;
00098     void setPriority(quint32 priority);
00099 
00100     QString realm() const;
00101     void setRealm(const QString &realm);
00102 
00103     QByteArray reservationToken() const;
00104     void setReservationToken(const QByteArray &reservationToken);
00105 
00106     quint8 requestedTransport() const;
00107     void setRequestedTransport(quint8 requestedTransport);
00108 
00109     QString software() const;
00110     void setSoftware(const QString &software);
00111 
00112     QString username() const;
00113     void setUsername(const QString &username);
00114 
00115     QByteArray encode(const QByteArray &key = QByteArray(), bool addFingerprint = true) const;
00116     bool decode(const QByteArray &buffer, const QByteArray &key = QByteArray(), QStringList *errors = 0);
00117     QString toString() const;
00118     static quint16 peekType(const QByteArray &buffer, quint32 &cookie, QByteArray &id);
00119 
00120     // attributes
00121     int errorCode;
00122     QString errorPhrase;
00123     QByteArray iceControlling;
00124     QByteArray iceControlled;
00125     QHostAddress changedHost;
00126     quint16 changedPort;
00127     QHostAddress mappedHost;
00128     quint16 mappedPort;
00129     QHostAddress otherHost;
00130     quint16 otherPort;
00131     QHostAddress sourceHost;
00132     quint16 sourcePort;
00133     QHostAddress xorMappedHost;
00134     quint16 xorMappedPort;
00135     QHostAddress xorPeerHost;
00136     quint16 xorPeerPort;
00137     QHostAddress xorRelayedHost;
00138     quint16 xorRelayedPort;
00139     bool useCandidate;
00140 
00141 private:
00142     quint32 m_cookie;
00143     QByteArray m_id;
00144     quint16 m_type;
00145 
00146     // attributes
00147     QSet<quint16> m_attributes;
00148     quint32 m_changeRequest;
00149     quint16 m_channelNumber;
00150     QByteArray m_data;
00151     quint32 m_lifetime;
00152     QByteArray m_nonce;
00153     quint32 m_priority;
00154     QString m_realm;
00155     quint8 m_requestedTransport;
00156     QByteArray m_reservationToken;
00157     QString m_software;
00158     QString m_username;
00159 };
00160 
00164 
00165 class QXMPP_EXPORT QXmppIceComponent : public QXmppLoggable
00166 {
00167     Q_OBJECT
00168 
00169 public:
00170     ~QXmppIceComponent();
00171 
00172     int component() const;
00173     bool isConnected() const;
00174     QList<QXmppJingleCandidate> localCandidates() const;
00175 
00176     static QList<QHostAddress> discoverAddresses();
00177     static QList<QUdpSocket*> reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent = 0);
00178 
00179 public slots:
00180     void close();
00181     void connectToHost();
00182     qint64 sendDatagram(const QByteArray &datagram);
00183 
00184 private slots:
00185     void checkCandidates();
00186     void handleDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port);
00187     void turnConnected();
00188     void transactionFinished();
00189     void updateGatheringState();
00190     void writeStun(const QXmppStunMessage &request);
00191 
00192 signals:
00194     void connected();
00195 
00197     void datagramReceived(const QByteArray &datagram);
00198 
00200     void gatheringStateChanged();
00201 
00203     void localCandidatesChanged();
00204 
00205 private:
00206     QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent=0);
00207 
00208     QXmppIceComponentPrivate *d;
00209     friend class QXmppIceComponentPrivate;
00210     friend class QXmppIceConnection;
00211 };
00212 
00240 
00241 class QXMPP_EXPORT QXmppIceConnection : public QXmppLoggable
00242 {
00243     Q_OBJECT
00244     Q_ENUMS(GatheringState)
00245     Q_PROPERTY(QXmppIceConnection::GatheringState gatheringState READ gatheringState NOTIFY gatheringStateChanged)
00246 
00247 public:
00248     enum GatheringState
00249     {
00250         NewGatheringState,
00251         BusyGatheringState,
00252         CompleteGatheringState
00253     };
00254 
00255     QXmppIceConnection(QObject *parent = 0);
00256     ~QXmppIceConnection();
00257 
00258     QXmppIceComponent *component(int component);
00259     void addComponent(int component);
00260     void setIceControlling(bool controlling);
00261 
00262     QList<QXmppJingleCandidate> localCandidates() const;
00263     QString localUser() const;
00264     QString localPassword() const;
00265 
00266     void addRemoteCandidate(const QXmppJingleCandidate &candidate);
00267     void setRemoteUser(const QString &user);
00268     void setRemotePassword(const QString &password);
00269 
00270     void setStunServer(const QHostAddress &host, quint16 port = 3478);
00271     void setTurnServer(const QHostAddress &host, quint16 port = 3478);
00272     void setTurnUser(const QString &user);
00273     void setTurnPassword(const QString &password);
00274 
00275     bool bind(const QList<QHostAddress> &addresses);
00276     bool isConnected() const;
00277 
00278     GatheringState gatheringState() const;
00279 
00280 signals:
00282     void connected();
00283 
00285     void disconnected();
00286 
00288     void gatheringStateChanged();
00289 
00291     void localCandidatesChanged();
00292 
00293 public slots:
00294     void close();
00295     void connectToHost();
00296 
00297 private slots:
00298     void slotConnected();
00299     void slotGatheringStateChanged();
00300     void slotTimeout();
00301 
00302 private:
00303     QXmppIceConnectionPrivate *d;
00304 };
00305 
00306 #endif
 All Classes Functions Enumerations Enumerator Properties