1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 | qint64 bytesToWrite() const;
|
---|
104 |
|
---|
105 | int option(SocketOption option) const;
|
---|
106 | bool setOption(SocketOption option, int value);
|
---|
107 |
|
---|
108 | bool waitForRead(int msecs = 30000, bool *timedOut = 0);
|
---|
109 | bool waitForWrite(int msecs = 30000, bool *timedOut = 0);
|
---|
110 | bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
|
---|
111 | bool checkRead, bool checkWrite,
|
---|
112 | int msecs = 30000, bool *timedOut = 0);
|
---|
113 |
|
---|
114 | bool isReadNotificationEnabled() const;
|
---|
115 | void setReadNotificationEnabled(bool enable);
|
---|
116 | bool isWriteNotificationEnabled() const;
|
---|
117 | void setWriteNotificationEnabled(bool enable);
|
---|
118 | bool isExceptionNotificationEnabled() const;
|
---|
119 | void setExceptionNotificationEnabled(bool enable);
|
---|
120 |
|
---|
121 | private:
|
---|
122 | Q_DECLARE_PRIVATE(QSocks5SocketEngine)
|
---|
123 | Q_DISABLE_COPY(QSocks5SocketEngine)
|
---|
124 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketConnected())
|
---|
125 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketReadNotification())
|
---|
126 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketError(QAbstractSocket::SocketError))
|
---|
127 | #ifndef QT_NO_UDPSOCKET
|
---|
128 | Q_PRIVATE_SLOT(d_func(), void _q_udpSocketReadNotification())
|
---|
129 | #endif
|
---|
130 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketBytesWritten())
|
---|
131 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingReadNotification())
|
---|
132 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingWriteNotification())
|
---|
133 | Q_PRIVATE_SLOT(d_func(), void _q_emitPendingConnectionNotification())
|
---|
134 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketDisconnected())
|
---|
135 | Q_PRIVATE_SLOT(d_func(), void _q_controlSocketStateChanged(QAbstractSocket::SocketState))
|
---|
136 |
|
---|
137 | };
|
---|
138 |
|
---|
139 |
|
---|
140 | class QTcpSocket;
|
---|
141 |
|
---|
142 | class QSocks5Authenticator
|
---|
143 | {
|
---|
144 | public:
|
---|
145 | QSocks5Authenticator();
|
---|
146 | virtual ~QSocks5Authenticator();
|
---|
147 | virtual char methodId();
|
---|
148 | virtual bool beginAuthenticate(QTcpSocket *socket, bool *completed);
|
---|
149 | virtual bool continueAuthenticate(QTcpSocket *socket, bool *completed);
|
---|
150 |
|
---|
151 | virtual bool seal(const QByteArray buf, QByteArray *sealedBuf);
|
---|
152 | virtual bool unSeal(const QByteArray sealedBuf, QByteArray *buf);
|
---|
153 | virtual bool unSeal(QTcpSocket *sealedSocket, QByteArray *buf);
|
---|
154 |
|
---|
155 | virtual QString errorString() { return QString(); }
|
---|
156 | };
|
---|
157 |
|
---|
158 | class QSocks5PasswordAuthenticator : public QSocks5Authenticator
|
---|
159 | {
|
---|
160 | public:
|
---|
161 | QSocks5PasswordAuthenticator(const QString &userName, const QString &password);
|
---|
162 | char methodId();
|
---|
163 | bool beginAuthenticate(QTcpSocket *socket, bool *completed);
|
---|
164 | bool continueAuthenticate(QTcpSocket *socket, bool *completed);
|
---|
165 |
|
---|
166 | QString errorString();
|
---|
167 |
|
---|
168 | private:
|
---|
169 | QString userName;
|
---|
170 | QString password;
|
---|
171 | };
|
---|
172 |
|
---|
173 | struct QSocks5Data;
|
---|
174 | struct QSocks5ConnectData;
|
---|
175 | struct QSocks5UdpAssociateData;
|
---|
176 | struct QSocks5BindData;
|
---|
177 |
|
---|
178 | class QSocks5SocketEnginePrivate : public QAbstractSocketEnginePrivate
|
---|
179 | {
|
---|
180 | Q_DECLARE_PUBLIC(QSocks5SocketEngine)
|
---|
181 | public:
|
---|
182 | QSocks5SocketEnginePrivate();
|
---|
183 | ~QSocks5SocketEnginePrivate();
|
---|
184 |
|
---|
185 | enum Socks5State
|
---|
186 | {
|
---|
187 | Uninitialized = 0,
|
---|
188 | ConnectError,
|
---|
189 | AuthenticationMethodsSent,
|
---|
190 | Authenticating,
|
---|
191 | AuthenticatingError,
|
---|
192 | RequestMethodSent,
|
---|
193 | RequestError,
|
---|
194 | Connected,
|
---|
195 | UdpAssociateSuccess,
|
---|
196 | BindSuccess,
|
---|
197 | ControlSocketError,
|
---|
198 | SocksError,
|
---|
199 | HostNameLookupError
|
---|
200 | };
|
---|
201 | Socks5State socks5State;
|
---|
202 |
|
---|
203 | enum Socks5Mode
|
---|
204 | {
|
---|
205 | NoMode,
|
---|
206 | ConnectMode,
|
---|
207 | BindMode,
|
---|
208 | UdpAssociateMode
|
---|
209 | };
|
---|
210 | Socks5Mode mode;
|
---|
211 |
|
---|
212 | enum Socks5Error
|
---|
213 | {
|
---|
214 | SocksFailure = 0x01,
|
---|
215 | ConnectionNotAllowed = 0x02,
|
---|
216 | NetworkUnreachable = 0x03,
|
---|
217 | HostUnreachable = 0x04,
|
---|
218 | ConnectionRefused = 0x05,
|
---|
219 | TTLExpired = 0x06,
|
---|
220 | CommandNotSupported = 0x07,
|
---|
221 | AddressTypeNotSupported = 0x08,
|
---|
222 | LastKnownError = AddressTypeNotSupported,
|
---|
223 | UnknownError
|
---|
224 | };
|
---|
225 |
|
---|
226 | void initialize(Socks5Mode socks5Mode);
|
---|
227 |
|
---|
228 | void setErrorState(Socks5State state, const QString &extraMessage = QString());
|
---|
229 | void setErrorState(Socks5State state, Socks5Error socks5error);
|
---|
230 |
|
---|
231 | void reauthenticate();
|
---|
232 | void parseAuthenticationMethodReply();
|
---|
233 | void parseAuthenticatingReply();
|
---|
234 | void sendRequestMethod();
|
---|
235 | void parseRequestMethodReply();
|
---|
236 | void parseNewConnection();
|
---|
237 |
|
---|
238 | bool waitForConnected(int msecs, bool *timedOut);
|
---|
239 |
|
---|
240 | void _q_controlSocketConnected();
|
---|
241 | void _q_controlSocketReadNotification();
|
---|
242 | void _q_controlSocketError(QAbstractSocket::SocketError);
|
---|
243 | #ifndef QT_NO_UDPSOCKET
|
---|
244 | void checkForDatagrams() const;
|
---|
245 | void _q_udpSocketReadNotification();
|
---|
246 | #endif
|
---|
247 | void _q_controlSocketBytesWritten();
|
---|
248 | void _q_controlSocketDisconnected();
|
---|
249 | void _q_controlSocketStateChanged(QAbstractSocket::SocketState);
|
---|
250 |
|
---|
251 | QNetworkProxy proxyInfo;
|
---|
252 |
|
---|
253 | bool readNotificationEnabled, writeNotificationEnabled, exceptNotificationEnabled;
|
---|
254 |
|
---|
255 | int socketDescriptor;
|
---|
256 |
|
---|
257 | QSocks5Data *data;
|
---|
258 | QSocks5ConnectData *connectData;
|
---|
259 | #ifndef QT_NO_UDPSOCKET
|
---|
260 | QSocks5UdpAssociateData *udpData;
|
---|
261 | #endif
|
---|
262 | QSocks5BindData *bindData;
|
---|
263 | QString peerName;
|
---|
264 |
|
---|
265 | mutable bool readNotificationActivated;
|
---|
266 | mutable bool writeNotificationActivated;
|
---|
267 |
|
---|
268 | bool readNotificationPending;
|
---|
269 | void _q_emitPendingReadNotification();
|
---|
270 | void emitReadNotification();
|
---|
271 | bool writeNotificationPending;
|
---|
272 | void _q_emitPendingWriteNotification();
|
---|
273 | void emitWriteNotification();
|
---|
274 | bool connectionNotificationPending;
|
---|
275 | void _q_emitPendingConnectionNotification();
|
---|
276 | void emitConnectionNotification();
|
---|
277 | };
|
---|
278 |
|
---|
279 | class Q_AUTOTEST_EXPORT QSocks5SocketEngineHandler : public QSocketEngineHandler
|
---|
280 | {
|
---|
281 | public:
|
---|
282 | virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
|
---|
283 | const QNetworkProxy &, QObject *parent);
|
---|
284 | virtual QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent);
|
---|
285 | };
|
---|
286 |
|
---|
287 |
|
---|
288 | QT_END_NAMESPACE
|
---|
289 | #endif // QT_NO_SOCKS5
|
---|
290 | #endif // QSOCKS5SOCKETENGINE_H
|
---|