src/network/socket/qsocks5socketengine_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QSOCKS5SOCKETENGINE_P_H
       
    43 #define QSOCKS5SOCKETENGINE_P_H
       
    44 
       
    45 //
       
    46 //  W A R N I N G
       
    47 //  -------------
       
    48 //
       
    49 // This file is not part of the Qt API.  It exists purely as an
       
    50 // implementation detail.  This header file may change from version to
       
    51 // version without notice, or even be removed.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 
       
    56 #include "qabstractsocketengine_p.h"
       
    57 #include "qnetworkproxy.h"
       
    58 
       
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 #ifndef QT_NO_SOCKS5
       
    62 
       
    63 class QSocks5SocketEnginePrivate;
       
    64 
       
    65 class Q_AUTOTEST_EXPORT QSocks5SocketEngine : public QAbstractSocketEngine
       
    66 {
       
    67     Q_OBJECT
       
    68 public:
       
    69     QSocks5SocketEngine(QObject *parent = 0);
       
    70     ~QSocks5SocketEngine();
       
    71 
       
    72     bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol);
       
    73     bool initialize(int socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState);
       
    74 
       
    75     void setProxy(const QNetworkProxy &networkProxy);
       
    76 
       
    77     int socketDescriptor() const;
       
    78 
       
    79     bool isValid() const;
       
    80 
       
    81     bool connectInternal();
       
    82     bool connectToHost(const QHostAddress &address, quint16 port);
       
    83     bool connectToHostByName(const QString &name, quint16 port);
       
    84     bool bind(const QHostAddress &address, quint16 port);
       
    85     bool listen();
       
    86     int accept();
       
    87     void close();
       
    88 
       
    89     qint64 bytesAvailable() const;
       
    90 
       
    91     qint64 read(char *data, qint64 maxlen);
       
    92     qint64 write(const char *data, qint64 len);
       
    93 
       
    94 #ifndef QT_NO_UDPSOCKET
       
    95     qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr = 0,
       
    96         quint16 *port = 0);
       
    97     qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr,
       
    98         quint16 port);
       
    99     bool hasPendingDatagrams() const;
       
   100     qint64 pendingDatagramSize() const;
       
   101 #endif // QT_NO_UDPSOCKET
       
   102 
       
   103     int option(SocketOption option) const;
       
   104     bool setOption(SocketOption option, int value);
       
   105 
       
   106     bool waitForRead(int msecs = 30000, bool *timedOut = 0);
       
   107     bool waitForWrite(int msecs = 30000, bool *timedOut = 0);
       
   108     bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
       
   109                             bool checkRead, bool checkWrite,
       
   110                             int msecs = 30000, bool *timedOut = 0);
       
   111 
       
   112     bool isReadNotificationEnabled() const;
       
   113     void setReadNotificationEnabled(bool enable);
       
   114     bool isWriteNotificationEnabled() const;
       
   115     void setWriteNotificationEnabled(bool enable);
       
   116     bool isExceptionNotificationEnabled() const;
       
   117     void setExceptionNotificationEnabled(bool enable);
       
   118 
       
   119 private:
       
   120     Q_DECLARE_PRIVATE(QSocks5SocketEngine)
       
   121     Q_DISABLE_COPY(QSocks5SocketEngine)
       
   122     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketConnected())
       
   123     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketReadNotification())
       
   124     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketError(QAbstractSocket::SocketError))
       
   125 #ifndef QT_NO_UDPSOCKET
       
   126     Q_PRIVATE_SLOT(d_func(), void _q_udpSocketReadNotification())
       
   127 #endif
       
   128     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketBytesWritten())
       
   129     Q_PRIVATE_SLOT(d_func(), void _q_emitPendingReadNotification())
       
   130     Q_PRIVATE_SLOT(d_func(), void _q_emitPendingWriteNotification())
       
   131     Q_PRIVATE_SLOT(d_func(), void _q_emitPendingConnectionNotification())
       
   132     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketDisconnected())
       
   133     Q_PRIVATE_SLOT(d_func(), void _q_controlSocketStateChanged(QAbstractSocket::SocketState))
       
   134 
       
   135 };
       
   136 
       
   137 
       
   138 class QTcpSocket;
       
   139 
       
   140 class QSocks5Authenticator
       
   141 {
       
   142 public:
       
   143     QSocks5Authenticator();
       
   144     virtual ~QSocks5Authenticator();
       
   145     virtual char methodId();
       
   146     virtual bool beginAuthenticate(QTcpSocket *socket, bool *completed);
       
   147     virtual bool continueAuthenticate(QTcpSocket *socket, bool *completed);
       
   148 
       
   149     virtual bool seal(const QByteArray buf, QByteArray *sealedBuf);
       
   150     virtual bool unSeal(const QByteArray sealedBuf, QByteArray *buf);
       
   151     virtual bool unSeal(QTcpSocket *sealedSocket, QByteArray *buf);
       
   152 
       
   153     virtual QString errorString() { return QString(); }
       
   154 };
       
   155 
       
   156 class QSocks5PasswordAuthenticator : public QSocks5Authenticator
       
   157 {
       
   158 public:
       
   159     QSocks5PasswordAuthenticator(const QString &userName, const QString &password);
       
   160     char methodId();
       
   161     bool beginAuthenticate(QTcpSocket *socket, bool *completed);
       
   162     bool continueAuthenticate(QTcpSocket *socket, bool *completed);
       
   163 
       
   164     QString errorString();
       
   165 
       
   166 private:
       
   167     QString userName;
       
   168     QString password;
       
   169 };
       
   170 
       
   171 struct QSocks5Data;
       
   172 struct QSocks5ConnectData;
       
   173 struct QSocks5UdpAssociateData;
       
   174 struct QSocks5BindData;
       
   175 
       
   176 class QSocks5SocketEnginePrivate : public QAbstractSocketEnginePrivate
       
   177 {
       
   178     Q_DECLARE_PUBLIC(QSocks5SocketEngine)
       
   179 public:
       
   180     QSocks5SocketEnginePrivate();
       
   181     ~QSocks5SocketEnginePrivate();
       
   182 
       
   183    enum Socks5State
       
   184     {
       
   185         Uninitialized = 0,
       
   186         ConnectError,
       
   187         AuthenticationMethodsSent,
       
   188         Authenticating,
       
   189         AuthenticatingError,
       
   190         RequestMethodSent,
       
   191         RequestError,
       
   192         Connected,
       
   193         UdpAssociateSuccess,
       
   194         BindSuccess,
       
   195         ControlSocketError,
       
   196         SocksError,
       
   197         HostNameLookupError
       
   198     };
       
   199     Socks5State socks5State;
       
   200 
       
   201     enum Socks5Mode
       
   202     {
       
   203         NoMode,
       
   204         ConnectMode,
       
   205         BindMode,
       
   206         UdpAssociateMode
       
   207     };
       
   208     Socks5Mode mode;
       
   209 
       
   210     enum Socks5Error
       
   211     {
       
   212         SocksFailure = 0x01,
       
   213         ConnectionNotAllowed = 0x02,
       
   214         NetworkUnreachable = 0x03,
       
   215         HostUnreachable = 0x04,
       
   216         ConnectionRefused = 0x05,
       
   217         TTLExpired = 0x06,
       
   218         CommandNotSupported = 0x07,
       
   219         AddressTypeNotSupported = 0x08,
       
   220         LastKnownError = AddressTypeNotSupported,
       
   221         UnknownError
       
   222     };
       
   223 
       
   224     void initialize(Socks5Mode socks5Mode);
       
   225 
       
   226     void setErrorState(Socks5State state, const QString &extraMessage = QString());
       
   227     void setErrorState(Socks5State state, Socks5Error socks5error);
       
   228 
       
   229     void reauthenticate();
       
   230     void parseAuthenticationMethodReply();
       
   231     void parseAuthenticatingReply();
       
   232     void sendRequestMethod();
       
   233     void parseRequestMethodReply();
       
   234     void parseNewConnection();
       
   235 
       
   236     bool waitForConnected(int msecs, bool *timedOut);
       
   237 
       
   238     void _q_controlSocketConnected();
       
   239     void _q_controlSocketReadNotification();
       
   240     void _q_controlSocketError(QAbstractSocket::SocketError);
       
   241 #ifndef QT_NO_UDPSOCKET
       
   242     void checkForDatagrams() const;
       
   243     void _q_udpSocketReadNotification();
       
   244 #endif
       
   245     void _q_controlSocketBytesWritten();
       
   246     void _q_controlSocketDisconnected();
       
   247     void _q_controlSocketStateChanged(QAbstractSocket::SocketState);
       
   248 
       
   249     QNetworkProxy proxyInfo;
       
   250 
       
   251     bool readNotificationEnabled, writeNotificationEnabled, exceptNotificationEnabled;
       
   252 
       
   253     int socketDescriptor;
       
   254 
       
   255     QSocks5Data *data;
       
   256     QSocks5ConnectData *connectData;
       
   257 #ifndef QT_NO_UDPSOCKET
       
   258     QSocks5UdpAssociateData *udpData;
       
   259 #endif
       
   260     QSocks5BindData *bindData;
       
   261     QString peerName;
       
   262 
       
   263     mutable bool readNotificationActivated;
       
   264     mutable bool writeNotificationActivated;
       
   265 
       
   266     bool readNotificationPending;
       
   267     void _q_emitPendingReadNotification();
       
   268     void emitReadNotification();
       
   269     bool writeNotificationPending;
       
   270     void _q_emitPendingWriteNotification();
       
   271     void emitWriteNotification();
       
   272     bool connectionNotificationPending;
       
   273     void _q_emitPendingConnectionNotification();
       
   274     void emitConnectionNotification();
       
   275 };
       
   276 
       
   277 class Q_AUTOTEST_EXPORT QSocks5SocketEngineHandler : public QSocketEngineHandler
       
   278 {
       
   279 public:
       
   280     virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
       
   281                                                       const QNetworkProxy &, QObject *parent);
       
   282     virtual QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent);
       
   283 };
       
   284 
       
   285 
       
   286 QT_END_NAMESPACE
       
   287 #endif // QT_NO_SOCKS5
       
   288 #endif // QSOCKS5SOCKETENGINE_H