Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/socket/qsocks5socketengine.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qsocks5socketengine_p.h" | - | ||||||||||||
35 | - | |||||||||||||
36 | #ifndef QT_NO_SOCKS5 | - | ||||||||||||
37 | - | |||||||||||||
38 | #include "qtcpsocket.h" | - | ||||||||||||
39 | #include "qudpsocket.h" | - | ||||||||||||
40 | #include "qtcpserver.h" | - | ||||||||||||
41 | #include "qdebug.h" | - | ||||||||||||
42 | #include "qhash.h" | - | ||||||||||||
43 | #include "qqueue.h" | - | ||||||||||||
44 | #include "qelapsedtimer.h" | - | ||||||||||||
45 | #include "qmutex.h" | - | ||||||||||||
46 | #include "qthread.h" | - | ||||||||||||
47 | #include "qcoreapplication.h" | - | ||||||||||||
48 | #include "qurl.h" | - | ||||||||||||
49 | #include "qauthenticator.h" | - | ||||||||||||
50 | #include "private/qiodevice_p.h" | - | ||||||||||||
51 | #include <qendian.h> | - | ||||||||||||
52 | #include <qnetworkinterface.h> | - | ||||||||||||
53 | - | |||||||||||||
54 | QT_BEGIN_NAMESPACE | - | ||||||||||||
55 | - | |||||||||||||
56 | static const int MaxWriteBufferSize = 128*1024; | - | ||||||||||||
57 | - | |||||||||||||
58 | //#define QSOCKS5SOCKETLAYER_DEBUG | - | ||||||||||||
59 | - | |||||||||||||
60 | #define MAX_DATA_DUMP 256 | - | ||||||||||||
61 | #if !defined(Q_OS_WINCE) | - | ||||||||||||
62 | #define SOCKS5_BLOCKING_BIND_TIMEOUT 5000 | - | ||||||||||||
63 | #else | - | ||||||||||||
64 | #define SOCKS5_BLOCKING_BIND_TIMEOUT 10000 | - | ||||||||||||
65 | #endif | - | ||||||||||||
66 | - | |||||||||||||
67 | #define Q_INIT_CHECK(returnValue) do { \ | - | ||||||||||||
68 | if (!d->data) { \ | - | ||||||||||||
69 | return returnValue; \ | - | ||||||||||||
70 | } } while (0) | - | ||||||||||||
71 | - | |||||||||||||
72 | #define S5_VERSION_5 0x05 | - | ||||||||||||
73 | #define S5_CONNECT 0x01 | - | ||||||||||||
74 | #define S5_BIND 0x02 | - | ||||||||||||
75 | #define S5_UDP_ASSOCIATE 0x03 | - | ||||||||||||
76 | #define S5_IP_V4 0x01 | - | ||||||||||||
77 | #define S5_DOMAINNAME 0x03 | - | ||||||||||||
78 | #define S5_IP_V6 0x04 | - | ||||||||||||
79 | #define S5_SUCCESS 0x00 | - | ||||||||||||
80 | #define S5_R_ERROR_SOCKS_FAILURE 0x01 | - | ||||||||||||
81 | #define S5_R_ERROR_CON_NOT_ALLOWED 0x02 | - | ||||||||||||
82 | #define S5_R_ERROR_NET_UNREACH 0x03 | - | ||||||||||||
83 | #define S5_R_ERROR_HOST_UNREACH 0x04 | - | ||||||||||||
84 | #define S5_R_ERROR_CONN_REFUSED 0x05 | - | ||||||||||||
85 | #define S5_R_ERROR_TTL 0x06 | - | ||||||||||||
86 | #define S5_R_ERROR_CMD_NOT_SUPPORTED 0x07 | - | ||||||||||||
87 | #define S5_R_ERROR_ADD_TYPE_NOT_SUPORTED 0x08 | - | ||||||||||||
88 | - | |||||||||||||
89 | #define S5_AUTHMETHOD_NONE 0x00 | - | ||||||||||||
90 | #define S5_AUTHMETHOD_PASSWORD 0x02 | - | ||||||||||||
91 | #define S5_AUTHMETHOD_NOTACCEPTABLE 0xFF | - | ||||||||||||
92 | - | |||||||||||||
93 | #define S5_PASSWORDAUTH_VERSION 0x01 | - | ||||||||||||
94 | - | |||||||||||||
95 | #ifdef QSOCKS5SOCKETLAYER_DEBUG | - | ||||||||||||
96 | # define QSOCKS5_Q_DEBUG qDebug() << this | - | ||||||||||||
97 | # define QSOCKS5_D_DEBUG qDebug() << q_ptr | - | ||||||||||||
98 | # define QSOCKS5_DEBUG qDebug() << "[QSocks5]" | - | ||||||||||||
99 | static QString s5StateToString(QSocks5SocketEnginePrivate::Socks5State s) | - | ||||||||||||
100 | { | - | ||||||||||||
101 | switch (s) { | - | ||||||||||||
102 | case QSocks5SocketEnginePrivate::Uninitialized: return QLatin1String("Uninitialized"); | - | ||||||||||||
103 | case QSocks5SocketEnginePrivate::ConnectError: return QLatin1String("ConnectError"); | - | ||||||||||||
104 | case QSocks5SocketEnginePrivate::AuthenticationMethodsSent: return QLatin1String("AuthenticationMethodsSent"); | - | ||||||||||||
105 | case QSocks5SocketEnginePrivate::Authenticating: return QLatin1String("Authenticating"); | - | ||||||||||||
106 | case QSocks5SocketEnginePrivate::AuthenticatingError: return QLatin1String("AuthenticatingError"); | - | ||||||||||||
107 | case QSocks5SocketEnginePrivate::RequestMethodSent: return QLatin1String("RequestMethodSent"); | - | ||||||||||||
108 | case QSocks5SocketEnginePrivate::RequestError: return QLatin1String("RequestError"); | - | ||||||||||||
109 | case QSocks5SocketEnginePrivate::Connected: return QLatin1String("Connected"); | - | ||||||||||||
110 | case QSocks5SocketEnginePrivate::UdpAssociateSuccess: return QLatin1String("UdpAssociateSuccess"); | - | ||||||||||||
111 | case QSocks5SocketEnginePrivate::BindSuccess: return QLatin1String("BindSuccess"); | - | ||||||||||||
112 | case QSocks5SocketEnginePrivate::ControlSocketError: return QLatin1String("ControlSocketError"); | - | ||||||||||||
113 | case QSocks5SocketEnginePrivate::SocksError: return QLatin1String("SocksError"); | - | ||||||||||||
114 | case QSocks5SocketEnginePrivate::HostNameLookupError: return QLatin1String("HostNameLookupError"); | - | ||||||||||||
115 | default: break; | - | ||||||||||||
116 | } | - | ||||||||||||
117 | return QLatin1String("unknown state"); | - | ||||||||||||
118 | } | - | ||||||||||||
119 | - | |||||||||||||
120 | static QString dump(const QByteArray &buf) | - | ||||||||||||
121 | { | - | ||||||||||||
122 | QString data; | - | ||||||||||||
123 | for (int i = 0; i < qMin<int>(MAX_DATA_DUMP, buf.size()); ++i) { | - | ||||||||||||
124 | if (i) data += QLatin1Char(' '); | - | ||||||||||||
125 | uint val = (unsigned char)buf.at(i); | - | ||||||||||||
126 | // data += QString("0x%1").arg(val, 3, 16, QLatin1Char('0')); | - | ||||||||||||
127 | data += QString::number(val); | - | ||||||||||||
128 | } | - | ||||||||||||
129 | if (buf.size() > MAX_DATA_DUMP) | - | ||||||||||||
130 | data += QLatin1String(" ..."); | - | ||||||||||||
131 | - | |||||||||||||
132 | return QString::fromLatin1("size: %1 data: { %2 }").arg(buf.size()).arg(data); | - | ||||||||||||
133 | } | - | ||||||||||||
134 | - | |||||||||||||
135 | #else | - | ||||||||||||
136 | # define QSOCKS5_DEBUG if (0) qDebug() | - | ||||||||||||
137 | # define QSOCKS5_Q_DEBUG if (0) qDebug() | - | ||||||||||||
138 | # define QSOCKS5_D_DEBUG if (0) qDebug() | - | ||||||||||||
139 | - | |||||||||||||
140 | static never executed: inline QString s5StateToString(QSocks5SocketEnginePrivate::Socks5State) { return QString(); }return QString(); never executed: return QString(); | 0 | ||||||||||||
141 | static never executed: inline QString dump(const QByteArray &) { return QString(); }return QString(); never executed: return QString(); | 0 | ||||||||||||
142 | #endif | - | ||||||||||||
143 | - | |||||||||||||
144 | /* | - | ||||||||||||
145 | inserts the host address in buf at pos and updates pos. | - | ||||||||||||
146 | if the func fails the data in buf and the vallue of pos is undefined | - | ||||||||||||
147 | */ | - | ||||||||||||
148 | static bool qt_socks5_set_host_address_and_port(const QHostAddress &address, quint16 port, QByteArray *pBuf) | - | ||||||||||||
149 | { | - | ||||||||||||
150 | QSOCKS5_DEBUG << "setting [" << address << ':' << port << ']'; dead code: QMessageLogger(__FILE__, 150, __PRETTY_FUNCTION__).debug() << "setting [" << address << ':' << port << ']'; | - | ||||||||||||
151 | - | |||||||||||||
152 | union { | - | ||||||||||||
153 | quint16 port; | - | ||||||||||||
154 | quint32 ipv4; | - | ||||||||||||
155 | QIPv6Address ipv6; | - | ||||||||||||
156 | char ptr; | - | ||||||||||||
157 | } data; | - | ||||||||||||
158 | - | |||||||||||||
159 | // add address | - | ||||||||||||
160 | if (address.protocol() == QAbstractSocket::IPv4Protocol) {
| 1-246 | ||||||||||||
161 | data.ipv4 = qToBigEndian<quint32>(address.toIPv4Address()); | - | ||||||||||||
162 | pBuf->append(S5_IP_V4); | - | ||||||||||||
163 | pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.ipv4)); | - | ||||||||||||
164 | } else if (address.protocol() == QAbstractSocket::IPv6Protocol) { executed 246 times by 4 tests: end of block Executed by:
| 0-246 | ||||||||||||
165 | data.ipv6 = address.toIPv6Address(); | - | ||||||||||||
166 | pBuf->append(S5_IP_V6); | - | ||||||||||||
167 | pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.ipv6)); | - | ||||||||||||
168 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
169 | return false; never executed: return false; | 0 | ||||||||||||
170 | } | - | ||||||||||||
171 | - | |||||||||||||
172 | // add port | - | ||||||||||||
173 | data.port = qToBigEndian<quint16>(port); | - | ||||||||||||
174 | pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.port)); | - | ||||||||||||
175 | return true; executed 247 times by 4 tests: return true; Executed by:
| 247 | ||||||||||||
176 | } | - | ||||||||||||
177 | - | |||||||||||||
178 | /* | - | ||||||||||||
179 | like above, but for a hostname | - | ||||||||||||
180 | */ | - | ||||||||||||
181 | static bool qt_socks5_set_host_name_and_port(const QString &hostname, quint16 port, QByteArray *pBuf) | - | ||||||||||||
182 | { | - | ||||||||||||
183 | QSOCKS5_DEBUG << "setting [" << hostname << ':' << port << ']'; dead code: QMessageLogger(__FILE__, 183, __PRETTY_FUNCTION__).debug() << "setting [" << hostname << ':' << port << ']'; | - | ||||||||||||
184 | - | |||||||||||||
185 | QByteArray encodedHostName = QUrl::toAce(hostname); | - | ||||||||||||
186 | QByteArray &buf = *pBuf; | - | ||||||||||||
187 | - | |||||||||||||
188 | if (encodedHostName.length() > 255)
| 0-457 | ||||||||||||
189 | return false; never executed: return false; | 0 | ||||||||||||
190 | - | |||||||||||||
191 | buf.append(S5_DOMAINNAME); | - | ||||||||||||
192 | buf.append(uchar(encodedHostName.length())); | - | ||||||||||||
193 | buf.append(encodedHostName); | - | ||||||||||||
194 | - | |||||||||||||
195 | // add port | - | ||||||||||||
196 | union { | - | ||||||||||||
197 | quint16 port; | - | ||||||||||||
198 | char ptr; | - | ||||||||||||
199 | } data; | - | ||||||||||||
200 | data.port = qToBigEndian<quint16>(port); | - | ||||||||||||
201 | buf.append(QByteArray::fromRawData(&data.ptr, sizeof data.port)); | - | ||||||||||||
202 | - | |||||||||||||
203 | return true; executed 457 times by 7 tests: return true; Executed by:
| 457 | ||||||||||||
204 | } | - | ||||||||||||
205 | - | |||||||||||||
206 | - | |||||||||||||
207 | /* | - | ||||||||||||
208 | retrives the host address in buf at pos and updates pos. | - | ||||||||||||
209 | return 1 if OK, 0 if need more data, -1 if error | - | ||||||||||||
210 | if the func fails the value of the address and the pos is undefined | - | ||||||||||||
211 | */ | - | ||||||||||||
212 | static int qt_socks5_get_host_address_and_port(const QByteArray &buf, QHostAddress *pAddress, quint16 *pPort, int *pPos) | - | ||||||||||||
213 | { | - | ||||||||||||
214 | int ret = -1; | - | ||||||||||||
215 | int pos = *pPos; | - | ||||||||||||
216 | const unsigned char *pBuf = reinterpret_cast<const unsigned char*>(buf.constData()); | - | ||||||||||||
217 | QHostAddress address; | - | ||||||||||||
218 | quint16 port = 0; | - | ||||||||||||
219 | - | |||||||||||||
220 | if (buf.size() - pos < 1) {
| 4-693 | ||||||||||||
221 | QSOCKS5_DEBUG << "need more data address/port"; dead code: QMessageLogger(__FILE__, 221, __PRETTY_FUNCTION__).debug() << "need more data address/port"; | - | ||||||||||||
222 | return 0; executed 4 times by 1 test: return 0; Executed by:
| 4 | ||||||||||||
223 | } | - | ||||||||||||
224 | if (pBuf[pos] == S5_IP_V4) {
| 54-639 | ||||||||||||
225 | pos++; | - | ||||||||||||
226 | if (buf.size() - pos < 4) {
| 8-631 | ||||||||||||
227 | QSOCKS5_DEBUG << "need more data for ip4 address"; dead code: QMessageLogger(__FILE__, 227, __PRETTY_FUNCTION__).debug() << "need more data for ip4 address"; | - | ||||||||||||
228 | return 0; executed 8 times by 1 test: return 0; Executed by:
| 8 | ||||||||||||
229 | } | - | ||||||||||||
230 | address.setAddress(qFromBigEndian<quint32>(&pBuf[pos])); | - | ||||||||||||
231 | pos += 4; | - | ||||||||||||
232 | ret = 1; | - | ||||||||||||
233 | } else if (pBuf[pos] == S5_IP_V6) { executed 631 times by 7 tests: end of block Executed by:
| 0-631 | ||||||||||||
234 | pos++; | - | ||||||||||||
235 | if (buf.size() - pos < 16) {
| 22-32 | ||||||||||||
236 | QSOCKS5_DEBUG << "need more data for ip6 address"; dead code: QMessageLogger(__FILE__, 236, __PRETTY_FUNCTION__).debug() << "need more data for ip6 address"; | - | ||||||||||||
237 | return 0; executed 32 times by 1 test: return 0; Executed by:
| 32 | ||||||||||||
238 | } | - | ||||||||||||
239 | QIPv6Address add; | - | ||||||||||||
240 | for (int i = 0; i < 16; ++i)
| 22-352 | ||||||||||||
241 | add[i] = buf[pos++]; executed 352 times by 1 test: add[i] = buf[pos++]; Executed by:
| 352 | ||||||||||||
242 | address.setAddress(add); | - | ||||||||||||
243 | ret = 1; | - | ||||||||||||
244 | } else if (pBuf[pos] == S5_DOMAINNAME){ executed 22 times by 1 test: end of block Executed by:
| 0-22 | ||||||||||||
245 | // just skip it | - | ||||||||||||
246 | pos++; | - | ||||||||||||
247 | qDebug() << "skipping hostname of len" << uint(pBuf[pos]); | - | ||||||||||||
248 | pos += uchar(pBuf[pos]); | - | ||||||||||||
249 | } else { never executed: end of block | 0 | ||||||||||||
250 | QSOCKS5_DEBUG << "invalid address type" << (int)pBuf[pos]; dead code: QMessageLogger(__FILE__, 250, __PRETTY_FUNCTION__).debug() << "invalid address type" << (int)pBuf[pos]; | - | ||||||||||||
251 | ret = -1; | - | ||||||||||||
252 | } never executed: end of block | 0 | ||||||||||||
253 | - | |||||||||||||
254 | if (ret == 1) {
| 0-653 | ||||||||||||
255 | if (buf.size() - pos < 2) {
| 4-649 | ||||||||||||
256 | QSOCKS5_DEBUG << "need more data for port"; dead code: QMessageLogger(__FILE__, 256, __PRETTY_FUNCTION__).debug() << "need more data for port"; | - | ||||||||||||
257 | return 0; executed 4 times by 1 test: return 0; Executed by:
| 4 | ||||||||||||
258 | } | - | ||||||||||||
259 | port = qFromBigEndian<quint16>(&pBuf[pos]); | - | ||||||||||||
260 | pos += 2; | - | ||||||||||||
261 | } executed 649 times by 7 tests: end of block Executed by:
| 649 | ||||||||||||
262 | - | |||||||||||||
263 | if (ret == 1) {
| 0-649 | ||||||||||||
264 | QSOCKS5_DEBUG << "got [" << address << ':' << port << ']'; dead code: QMessageLogger(__FILE__, 264, __PRETTY_FUNCTION__).debug() << "got [" << address << ':' << port << ']'; | - | ||||||||||||
265 | *pAddress = address; | - | ||||||||||||
266 | *pPort = port; | - | ||||||||||||
267 | *pPos = pos; | - | ||||||||||||
268 | } executed 649 times by 7 tests: end of block Executed by:
| 649 | ||||||||||||
269 | - | |||||||||||||
270 | return ret; executed 649 times by 7 tests: return ret; Executed by:
| 649 | ||||||||||||
271 | } | - | ||||||||||||
272 | - | |||||||||||||
273 | struct QSocks5Data | - | ||||||||||||
274 | { | - | ||||||||||||
275 | QTcpSocket *controlSocket; | - | ||||||||||||
276 | QSocks5Authenticator *authenticator; | - | ||||||||||||
277 | }; | - | ||||||||||||
278 | - | |||||||||||||
279 | struct QSocks5ConnectData : public QSocks5Data | - | ||||||||||||
280 | { | - | ||||||||||||
281 | QByteArray readBuffer; | - | ||||||||||||
282 | }; | - | ||||||||||||
283 | - | |||||||||||||
284 | struct QSocks5BindData : public QSocks5Data | - | ||||||||||||
285 | { | - | ||||||||||||
286 | QHostAddress localAddress; | - | ||||||||||||
287 | quint16 localPort; | - | ||||||||||||
288 | QHostAddress peerAddress; | - | ||||||||||||
289 | quint16 peerPort; | - | ||||||||||||
290 | QElapsedTimer timeStamp; | - | ||||||||||||
291 | }; | - | ||||||||||||
292 | - | |||||||||||||
293 | struct QSocks5RevivedDatagram | - | ||||||||||||
294 | { | - | ||||||||||||
295 | QByteArray data; | - | ||||||||||||
296 | QHostAddress address; | - | ||||||||||||
297 | quint16 port; | - | ||||||||||||
298 | }; | - | ||||||||||||
299 | - | |||||||||||||
300 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
301 | struct QSocks5UdpAssociateData : public QSocks5Data | - | ||||||||||||
302 | { | - | ||||||||||||
303 | QUdpSocket *udpSocket; | - | ||||||||||||
304 | QHostAddress associateAddress; | - | ||||||||||||
305 | quint16 associatePort; | - | ||||||||||||
306 | QQueue<QSocks5RevivedDatagram> pendingDatagrams; | - | ||||||||||||
307 | }; | - | ||||||||||||
308 | #endif | - | ||||||||||||
309 | - | |||||||||||||
310 | // needs to be thread safe | - | ||||||||||||
311 | class QSocks5BindStore : public QObject | - | ||||||||||||
312 | { | - | ||||||||||||
313 | public: | - | ||||||||||||
314 | QSocks5BindStore(); | - | ||||||||||||
315 | ~QSocks5BindStore(); | - | ||||||||||||
316 | - | |||||||||||||
317 | void add(qintptr socketDescriptor, QSocks5BindData *bindData); | - | ||||||||||||
318 | bool contains(qintptr socketDescriptor); | - | ||||||||||||
319 | QSocks5BindData *retrieve(qintptr socketDescriptor); | - | ||||||||||||
320 | - | |||||||||||||
321 | protected: | - | ||||||||||||
322 | void timerEvent(QTimerEvent * event) Q_DECL_OVERRIDE; | - | ||||||||||||
323 | - | |||||||||||||
324 | QMutex mutex; | - | ||||||||||||
325 | int sweepTimerId; | - | ||||||||||||
326 | //socket descriptor, data, timestamp | - | ||||||||||||
327 | QHash<int, QSocks5BindData *> store; | - | ||||||||||||
328 | }; | - | ||||||||||||
329 | - | |||||||||||||
330 | Q_GLOBAL_STATIC(QSocks5BindStore, socks5BindStore) executed 17 times by 17 tests: end of block Executed by:
executed 17 times by 17 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 1005 times by 18 tests: return &holder.value; Executed by:
| 0-1005 | ||||||||||||
331 | - | |||||||||||||
332 | QSocks5BindStore::QSocks5BindStore() | - | ||||||||||||
333 | : mutex(QMutex::Recursive) | - | ||||||||||||
334 | , sweepTimerId(-1) | - | ||||||||||||
335 | { | - | ||||||||||||
336 | QCoreApplication *app = QCoreApplication::instance(); | - | ||||||||||||
337 | if (app && app->thread() != thread())
| 0-18 | ||||||||||||
338 | moveToThread(app->thread()); executed 3 times by 3 tests: moveToThread(app->thread()); Executed by:
| 3 | ||||||||||||
339 | } executed 18 times by 18 tests: end of block Executed by:
| 18 | ||||||||||||
340 | - | |||||||||||||
341 | QSocks5BindStore::~QSocks5BindStore() | - | ||||||||||||
342 | { | - | ||||||||||||
343 | } | - | ||||||||||||
344 | - | |||||||||||||
345 | void QSocks5BindStore::add(qintptr socketDescriptor, QSocks5BindData *bindData) | - | ||||||||||||
346 | { | - | ||||||||||||
347 | QMutexLocker lock(&mutex); | - | ||||||||||||
348 | if (store.contains(socketDescriptor)) {
| 0-6 | ||||||||||||
349 | // qDebug() << "delete it"; | - | ||||||||||||
350 | } never executed: end of block | 0 | ||||||||||||
351 | bindData->timeStamp.start(); | - | ||||||||||||
352 | store.insert(socketDescriptor, bindData); | - | ||||||||||||
353 | // start sweep timer if not started | - | ||||||||||||
354 | if (sweepTimerId == -1)
| 0-6 | ||||||||||||
355 | sweepTimerId = startTimer(60000); executed 6 times by 3 tests: sweepTimerId = startTimer(60000); Executed by:
| 6 | ||||||||||||
356 | } executed 6 times by 3 tests: end of block Executed by:
| 6 | ||||||||||||
357 | - | |||||||||||||
358 | bool QSocks5BindStore::contains(qintptr socketDescriptor) | - | ||||||||||||
359 | { | - | ||||||||||||
360 | QMutexLocker lock(&mutex); | - | ||||||||||||
361 | return store.contains(socketDescriptor); executed 993 times by 18 tests: return store.contains(socketDescriptor); Executed by:
| 993 | ||||||||||||
362 | } | - | ||||||||||||
363 | - | |||||||||||||
364 | QSocks5BindData *QSocks5BindStore::retrieve(qintptr socketDescriptor) | - | ||||||||||||
365 | { | - | ||||||||||||
366 | QMutexLocker lock(&mutex); | - | ||||||||||||
367 | if (!store.contains(socketDescriptor))
| 0-6 | ||||||||||||
368 | return 0; never executed: return 0; | 0 | ||||||||||||
369 | QSocks5BindData *bindData = store.take(socketDescriptor); | - | ||||||||||||
370 | if (bindData) {
| 0-6 | ||||||||||||
371 | if (bindData->controlSocket->thread() != QThread::currentThread()) {
| 0-6 | ||||||||||||
372 | qWarning("Can not access socks5 bind data from different thread"); | - | ||||||||||||
373 | return 0; never executed: return 0; | 0 | ||||||||||||
374 | } | - | ||||||||||||
375 | } else { executed 6 times by 3 tests: end of block Executed by:
| 6 | ||||||||||||
376 | QSOCKS5_DEBUG << "__ERROR__ binddata == 0"; dead code: QMessageLogger(__FILE__, 376, __PRETTY_FUNCTION__).debug() << "__ERROR__ binddata == 0"; | - | ||||||||||||
377 | } never executed: end of block | 0 | ||||||||||||
378 | // stop the sweep timer if not needed | - | ||||||||||||
379 | if (store.isEmpty()) {
| 0-6 | ||||||||||||
380 | killTimer(sweepTimerId); | - | ||||||||||||
381 | sweepTimerId = -1; | - | ||||||||||||
382 | } executed 6 times by 3 tests: end of block Executed by:
| 6 | ||||||||||||
383 | return bindData; executed 6 times by 3 tests: return bindData; Executed by:
| 6 | ||||||||||||
384 | } | - | ||||||||||||
385 | - | |||||||||||||
386 | void QSocks5BindStore::timerEvent(QTimerEvent * event) | - | ||||||||||||
387 | { | - | ||||||||||||
388 | QMutexLocker lock(&mutex); | - | ||||||||||||
389 | if (event->timerId() == sweepTimerId) {
| 0 | ||||||||||||
390 | QSOCKS5_DEBUG << "QSocks5BindStore performing sweep"; dead code: QMessageLogger(__FILE__, 390, __PRETTY_FUNCTION__).debug() << "QSocks5BindStore performing sweep"; | - | ||||||||||||
391 | QMutableHashIterator<int, QSocks5BindData *> it(store); | - | ||||||||||||
392 | while (it.hasNext()) {
| 0 | ||||||||||||
393 | it.next(); | - | ||||||||||||
394 | if (it.value()->timeStamp.hasExpired(350000)) {
| 0 | ||||||||||||
395 | QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ"; dead code: QMessageLogger(__FILE__, 395, __PRETTY_FUNCTION__).debug() << "QSocks5BindStore removing JJJJ"; | - | ||||||||||||
396 | it.remove(); | - | ||||||||||||
397 | } never executed: end of block | 0 | ||||||||||||
398 | } never executed: end of block | 0 | ||||||||||||
399 | } never executed: end of block | 0 | ||||||||||||
400 | } never executed: end of block | 0 | ||||||||||||
401 | - | |||||||||||||
402 | QSocks5Authenticator::QSocks5Authenticator() | - | ||||||||||||
403 | { | - | ||||||||||||
404 | } | - | ||||||||||||
405 | - | |||||||||||||
406 | QSocks5Authenticator::~QSocks5Authenticator() | - | ||||||||||||
407 | { | - | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | char QSocks5Authenticator::methodId() | - | ||||||||||||
411 | { | - | ||||||||||||
412 | return 0x00; executed 653 times by 9 tests: return 0x00; Executed by:
| 653 | ||||||||||||
413 | } | - | ||||||||||||
414 | - | |||||||||||||
415 | bool QSocks5Authenticator::beginAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
416 | { | - | ||||||||||||
417 | Q_UNUSED(socket); | - | ||||||||||||
418 | *completed = true; | - | ||||||||||||
419 | return true; never executed: return true; | 0 | ||||||||||||
420 | } | - | ||||||||||||
421 | - | |||||||||||||
422 | bool QSocks5Authenticator::continueAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
423 | { | - | ||||||||||||
424 | Q_UNUSED(socket); | - | ||||||||||||
425 | *completed = true; | - | ||||||||||||
426 | return true; never executed: return true; | 0 | ||||||||||||
427 | } | - | ||||||||||||
428 | - | |||||||||||||
429 | bool QSocks5Authenticator::seal(const QByteArray &buf, QByteArray *sealedBuf) | - | ||||||||||||
430 | { | - | ||||||||||||
431 | *sealedBuf = buf; | - | ||||||||||||
432 | return true; executed 2591 times by 9 tests: return true; Executed by:
| 2591 | ||||||||||||
433 | } | - | ||||||||||||
434 | - | |||||||||||||
435 | bool QSocks5Authenticator::unSeal(const QByteArray &sealedBuf, QByteArray *buf) | - | ||||||||||||
436 | { | - | ||||||||||||
437 | *buf = sealedBuf; | - | ||||||||||||
438 | return true; executed 3662 times by 8 tests: return true; Executed by:
| 3662 | ||||||||||||
439 | } | - | ||||||||||||
440 | - | |||||||||||||
441 | bool QSocks5Authenticator::unSeal(QTcpSocket *sealedSocket, QByteArray *buf) | - | ||||||||||||
442 | { | - | ||||||||||||
443 | return unSeal(sealedSocket->readAll(), buf); executed 3662 times by 8 tests: return unSeal(sealedSocket->readAll(), buf); Executed by:
| 3662 | ||||||||||||
444 | } | - | ||||||||||||
445 | - | |||||||||||||
446 | QSocks5PasswordAuthenticator::QSocks5PasswordAuthenticator(const QString &userName, const QString &password) | - | ||||||||||||
447 | { | - | ||||||||||||
448 | this->userName = userName; | - | ||||||||||||
449 | this->password = password; | - | ||||||||||||
450 | } executed 147 times by 7 tests: end of block Executed by:
| 147 | ||||||||||||
451 | - | |||||||||||||
452 | char QSocks5PasswordAuthenticator::methodId() | - | ||||||||||||
453 | { | - | ||||||||||||
454 | return 0x02; executed 236 times by 7 tests: return 0x02; Executed by:
| 236 | ||||||||||||
455 | } | - | ||||||||||||
456 | - | |||||||||||||
457 | bool QSocks5PasswordAuthenticator::beginAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
458 | { | - | ||||||||||||
459 | *completed = false; | - | ||||||||||||
460 | QByteArray uname = userName.toLatin1(); | - | ||||||||||||
461 | QByteArray passwd = password.toLatin1(); | - | ||||||||||||
462 | QByteArray dataBuf(3 + uname.size() + passwd.size(), 0); | - | ||||||||||||
463 | char *buf = dataBuf.data(); | - | ||||||||||||
464 | int pos = 0; | - | ||||||||||||
465 | buf[pos++] = S5_PASSWORDAUTH_VERSION; | - | ||||||||||||
466 | buf[pos++] = uname.size(); | - | ||||||||||||
467 | memcpy(&buf[pos], uname.data(), uname.size()); | - | ||||||||||||
468 | pos += uname.size(); | - | ||||||||||||
469 | buf[pos++] = passwd.size(); | - | ||||||||||||
470 | memcpy(&buf[pos], passwd.data(), passwd.size()); | - | ||||||||||||
471 | return socket->write(dataBuf) == dataBuf.size(); executed 89 times by 7 tests: return socket->write(dataBuf) == dataBuf.size(); Executed by:
| 89 | ||||||||||||
472 | } | - | ||||||||||||
473 | - | |||||||||||||
474 | bool QSocks5PasswordAuthenticator::continueAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
475 | { | - | ||||||||||||
476 | *completed = false; | - | ||||||||||||
477 | - | |||||||||||||
478 | if (socket->bytesAvailable() < 2)
| 2-88 | ||||||||||||
479 | return true; executed 2 times by 1 test: return true; Executed by:
| 2 | ||||||||||||
480 | - | |||||||||||||
481 | QByteArray buf = socket->read(2); | - | ||||||||||||
482 | if (buf.at(0) == S5_PASSWORDAUTH_VERSION && buf.at(1) == 0x00) {
| 0-87 | ||||||||||||
483 | *completed = true; | - | ||||||||||||
484 | return true; executed 87 times by 7 tests: return true; Executed by:
| 87 | ||||||||||||
485 | } | - | ||||||||||||
486 | - | |||||||||||||
487 | // must disconnect | - | ||||||||||||
488 | socket->close(); | - | ||||||||||||
489 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
490 | } | - | ||||||||||||
491 | - | |||||||||||||
492 | QString QSocks5PasswordAuthenticator::errorString() | - | ||||||||||||
493 | { | - | ||||||||||||
494 | return QLatin1String("Socks5 user name or password incorrect"); never executed: return QLatin1String("Socks5 user name or password incorrect"); | 0 | ||||||||||||
495 | } | - | ||||||||||||
496 | - | |||||||||||||
497 | - | |||||||||||||
498 | - | |||||||||||||
499 | QSocks5SocketEnginePrivate::QSocks5SocketEnginePrivate() | - | ||||||||||||
500 | : socks5State(Uninitialized) | - | ||||||||||||
501 | , readNotificationEnabled(false) | - | ||||||||||||
502 | , writeNotificationEnabled(false) | - | ||||||||||||
503 | , exceptNotificationEnabled(false) | - | ||||||||||||
504 | , socketDescriptor(-1) | - | ||||||||||||
505 | , data(0) | - | ||||||||||||
506 | , connectData(0) | - | ||||||||||||
507 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
508 | , udpData(0) | - | ||||||||||||
509 | #endif | - | ||||||||||||
510 | , bindData(0) | - | ||||||||||||
511 | , readNotificationActivated(false) | - | ||||||||||||
512 | , writeNotificationActivated(false) | - | ||||||||||||
513 | , readNotificationPending(false) | - | ||||||||||||
514 | , writeNotificationPending(false) | - | ||||||||||||
515 | , connectionNotificationPending(false) | - | ||||||||||||
516 | { | - | ||||||||||||
517 | mode = NoMode; | - | ||||||||||||
518 | } executed 726 times by 9 tests: end of block Executed by:
| 726 | ||||||||||||
519 | - | |||||||||||||
520 | QSocks5SocketEnginePrivate::~QSocks5SocketEnginePrivate() | - | ||||||||||||
521 | { | - | ||||||||||||
522 | } | - | ||||||||||||
523 | - | |||||||||||||
524 | void QSocks5SocketEnginePrivate::initialize(Socks5Mode socks5Mode) | - | ||||||||||||
525 | { | - | ||||||||||||
526 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
527 | - | |||||||||||||
528 | mode = socks5Mode; | - | ||||||||||||
529 | if (mode == ConnectMode) {
| 15-704 | ||||||||||||
530 | connectData = new QSocks5ConnectData; | - | ||||||||||||
531 | data = connectData; | - | ||||||||||||
532 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
533 | } else if (mode == UdpAssociateMode) { executed 704 times by 9 tests: end of block Executed by:
| 2-704 | ||||||||||||
534 | udpData = new QSocks5UdpAssociateData; | - | ||||||||||||
535 | data = udpData; | - | ||||||||||||
536 | udpData->udpSocket = new QUdpSocket(q); | - | ||||||||||||
537 | #ifndef QT_NO_BEARERMANAGEMENT | - | ||||||||||||
538 | udpData->udpSocket->setProperty("_q_networksession", q->property("_q_networksession")); | - | ||||||||||||
539 | #endif | - | ||||||||||||
540 | udpData->udpSocket->setProxy(QNetworkProxy::NoProxy); | - | ||||||||||||
541 | QObject::connect(udpData->udpSocket, SIGNAL(readyRead()), | - | ||||||||||||
542 | q, SLOT(_q_udpSocketReadNotification()), | - | ||||||||||||
543 | Qt::DirectConnection); | - | ||||||||||||
544 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
545 | } else if (mode == BindMode) { executed 2 times by 1 test: end of block Executed by:
| 0-13 | ||||||||||||
546 | bindData = new QSocks5BindData; | - | ||||||||||||
547 | data = bindData; | - | ||||||||||||
548 | } executed 13 times by 3 tests: end of block Executed by:
| 13 | ||||||||||||
549 | - | |||||||||||||
550 | data->controlSocket = new QTcpSocket(q); | - | ||||||||||||
551 | #ifndef QT_NO_BEARERMANAGEMENT | - | ||||||||||||
552 | data->controlSocket->setProperty("_q_networksession", q->property("_q_networksession")); | - | ||||||||||||
553 | #endif | - | ||||||||||||
554 | data->controlSocket->setProxy(QNetworkProxy::NoProxy); | - | ||||||||||||
555 | QObject::connect(data->controlSocket, SIGNAL(connected()), q, SLOT(_q_controlSocketConnected()), | - | ||||||||||||
556 | Qt::DirectConnection); | - | ||||||||||||
557 | QObject::connect(data->controlSocket, SIGNAL(readyRead()), q, SLOT(_q_controlSocketReadNotification()), | - | ||||||||||||
558 | Qt::DirectConnection); | - | ||||||||||||
559 | QObject::connect(data->controlSocket, SIGNAL(bytesWritten(qint64)), q, SLOT(_q_controlSocketBytesWritten()), | - | ||||||||||||
560 | Qt::DirectConnection); | - | ||||||||||||
561 | QObject::connect(data->controlSocket, SIGNAL(error(QAbstractSocket::SocketError)), | - | ||||||||||||
562 | q, SLOT(_q_controlSocketError(QAbstractSocket::SocketError)), | - | ||||||||||||
563 | Qt::DirectConnection); | - | ||||||||||||
564 | QObject::connect(data->controlSocket, SIGNAL(disconnected()), q, SLOT(_q_controlSocketDisconnected()), | - | ||||||||||||
565 | Qt::DirectConnection); | - | ||||||||||||
566 | QObject::connect(data->controlSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), | - | ||||||||||||
567 | q, SLOT(_q_controlSocketStateChanged(QAbstractSocket::SocketState)), | - | ||||||||||||
568 | Qt::DirectConnection); | - | ||||||||||||
569 | - | |||||||||||||
570 | if (!proxyInfo.user().isEmpty() || !proxyInfo.password().isEmpty()) {
| 0-657 | ||||||||||||
571 | QSOCKS5_D_DEBUG << "using username/password authentication; user =" << proxyInfo.user(); dead code: QMessageLogger(__FILE__, 571, __PRETTY_FUNCTION__).debug() << "using username/password authentication; user =" << proxyInfo.user(); | - | ||||||||||||
572 | data->authenticator = new QSocks5PasswordAuthenticator(proxyInfo.user(), proxyInfo.password()); | - | ||||||||||||
573 | } else { executed 62 times by 1 test: end of block Executed by:
| 62 | ||||||||||||
574 | QSOCKS5_D_DEBUG << "not using authentication"; dead code: QMessageLogger(__FILE__, 574, __PRETTY_FUNCTION__).debug() << "not using authentication"; | - | ||||||||||||
575 | data->authenticator = new QSocks5Authenticator(); | - | ||||||||||||
576 | } executed 657 times by 9 tests: end of block Executed by:
| 657 | ||||||||||||
577 | } | - | ||||||||||||
578 | - | |||||||||||||
579 | void QSocks5SocketEnginePrivate::setErrorState(Socks5State state, const QString &extraMessage) | - | ||||||||||||
580 | { | - | ||||||||||||
581 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
582 | - | |||||||||||||
583 | switch (state) { | - | ||||||||||||
584 | case Uninitialized: never executed: case Uninitialized: | 0 | ||||||||||||
585 | case Authenticating: never executed: case Authenticating: | 0 | ||||||||||||
586 | case AuthenticationMethodsSent: never executed: case AuthenticationMethodsSent: | 0 | ||||||||||||
587 | case RequestMethodSent: never executed: case RequestMethodSent: | 0 | ||||||||||||
588 | case Connected: never executed: case Connected: | 0 | ||||||||||||
589 | case UdpAssociateSuccess: never executed: case UdpAssociateSuccess: | 0 | ||||||||||||
590 | case BindSuccess: never executed: case BindSuccess: | 0 | ||||||||||||
591 | // these aren't error states | - | ||||||||||||
592 | return; never executed: return; | 0 | ||||||||||||
593 | - | |||||||||||||
594 | case ConnectError: executed 6 times by 4 tests: case ConnectError: Executed by:
| 6 | ||||||||||||
595 | case ControlSocketError: { executed 37 times by 4 tests: case ControlSocketError: Executed by:
| 37 | ||||||||||||
596 | QAbstractSocket::SocketError controlSocketError = data->controlSocket->error(); | - | ||||||||||||
597 | if (socks5State != Connected) {
| 0-43 | ||||||||||||
598 | switch (controlSocketError) { | - | ||||||||||||
599 | case QAbstractSocket::ConnectionRefusedError: executed 2 times by 2 tests: case QAbstractSocket::ConnectionRefusedError: Executed by:
| 2 | ||||||||||||
600 | q->setError(QAbstractSocket::ProxyConnectionRefusedError, | - | ||||||||||||
601 | QSocks5SocketEngine::tr("Connection to proxy refused")); | - | ||||||||||||
602 | break; executed 2 times by 2 tests: break; Executed by:
| 2 | ||||||||||||
603 | case QAbstractSocket::RemoteHostClosedError: executed 30 times by 1 test: case QAbstractSocket::RemoteHostClosedError: Executed by:
| 30 | ||||||||||||
604 | q->setError(QAbstractSocket::ProxyConnectionClosedError, | - | ||||||||||||
605 | QSocks5SocketEngine::tr("Connection to proxy closed prematurely")); | - | ||||||||||||
606 | break; executed 30 times by 1 test: break; Executed by:
| 30 | ||||||||||||
607 | case QAbstractSocket::HostNotFoundError: executed 4 times by 3 tests: case QAbstractSocket::HostNotFoundError: Executed by:
| 4 | ||||||||||||
608 | q->setError(QAbstractSocket::ProxyNotFoundError, | - | ||||||||||||
609 | QSocks5SocketEngine::tr("Proxy host not found")); | - | ||||||||||||
610 | break; executed 4 times by 3 tests: break; Executed by:
| 4 | ||||||||||||
611 | case QAbstractSocket::SocketTimeoutError: executed 7 times by 3 tests: case QAbstractSocket::SocketTimeoutError: Executed by:
| 7 | ||||||||||||
612 | if (state == ConnectError) {
| 0-7 | ||||||||||||
613 | q->setError(QAbstractSocket::ProxyConnectionTimeoutError, | - | ||||||||||||
614 | QSocks5SocketEngine::tr("Connection to proxy timed out")); | - | ||||||||||||
615 | break; never executed: break; | 0 | ||||||||||||
616 | } | - | ||||||||||||
617 | /* fall through */ | - | ||||||||||||
618 | default: code before this statement executed 7 times by 3 tests: default: Executed by:
never executed: default: | 0-7 | ||||||||||||
619 | q->setError(controlSocketError, data->controlSocket->errorString()); | - | ||||||||||||
620 | break; executed 7 times by 3 tests: break; Executed by:
| 7 | ||||||||||||
621 | } | - | ||||||||||||
622 | } else { | - | ||||||||||||
623 | q->setError(controlSocketError, data->controlSocket->errorString()); | - | ||||||||||||
624 | } never executed: end of block | 0 | ||||||||||||
625 | break; executed 43 times by 5 tests: break; Executed by:
| 43 | ||||||||||||
626 | } | - | ||||||||||||
627 | - | |||||||||||||
628 | case AuthenticatingError: executed 6 times by 2 tests: case AuthenticatingError: Executed by:
| 6 | ||||||||||||
629 | q->setError(QAbstractSocket::ProxyAuthenticationRequiredError, | - | ||||||||||||
630 | extraMessage.isEmpty() ? | - | ||||||||||||
631 | QSocks5SocketEngine::tr("Proxy authentication failed") : | - | ||||||||||||
632 | QSocks5SocketEngine::tr("Proxy authentication failed: %1").arg(extraMessage)); | - | ||||||||||||
633 | break; executed 6 times by 2 tests: break; Executed by:
| 6 | ||||||||||||
634 | - | |||||||||||||
635 | case RequestError: executed 17 times by 3 tests: case RequestError: Executed by:
| 17 | ||||||||||||
636 | // error code set by caller (overload) | - | ||||||||||||
637 | break; executed 17 times by 3 tests: break; Executed by:
| 17 | ||||||||||||
638 | - | |||||||||||||
639 | case SocksError: executed 2 times by 1 test: case SocksError: Executed by:
| 2 | ||||||||||||
640 | q->setError(QAbstractSocket::ProxyProtocolError, | - | ||||||||||||
641 | QSocks5SocketEngine::tr("SOCKS version 5 protocol error")); | - | ||||||||||||
642 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||
643 | - | |||||||||||||
644 | case HostNameLookupError: executed 4 times by 2 tests: case HostNameLookupError: Executed by:
| 4 | ||||||||||||
645 | q->setError(QAbstractSocket::HostNotFoundError, | - | ||||||||||||
646 | QAbstractSocket::tr("Host not found")); | - | ||||||||||||
647 | break; executed 4 times by 2 tests: break; Executed by:
| 4 | ||||||||||||
648 | } | - | ||||||||||||
649 | - | |||||||||||||
650 | q->setState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
651 | socks5State = state; | - | ||||||||||||
652 | } executed 72 times by 7 tests: end of block Executed by:
| 72 | ||||||||||||
653 | - | |||||||||||||
654 | void QSocks5SocketEnginePrivate::setErrorState(Socks5State state, Socks5Error socks5error) | - | ||||||||||||
655 | { | - | ||||||||||||
656 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
657 | switch (socks5error) { | - | ||||||||||||
658 | case SocksFailure: never executed: case SocksFailure: | 0 | ||||||||||||
659 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
660 | QSocks5SocketEngine::tr("General SOCKSv5 server failure")); | - | ||||||||||||
661 | break; never executed: break; | 0 | ||||||||||||
662 | case ConnectionNotAllowed: executed 2 times by 1 test: case ConnectionNotAllowed: Executed by:
| 2 | ||||||||||||
663 | q->setError(QAbstractSocket::SocketAccessError, | - | ||||||||||||
664 | QSocks5SocketEngine::tr("Connection not allowed by SOCKSv5 server")); | - | ||||||||||||
665 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||
666 | case NetworkUnreachable: executed 2 times by 1 test: case NetworkUnreachable: Executed by:
| 2 | ||||||||||||
667 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
668 | QAbstractSocket::tr("Network unreachable")); | - | ||||||||||||
669 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||
670 | case HostUnreachable: executed 8 times by 3 tests: case HostUnreachable: Executed by:
| 8 | ||||||||||||
671 | q->setError(QAbstractSocket::HostNotFoundError, | - | ||||||||||||
672 | QAbstractSocket::tr("Host not found")); | - | ||||||||||||
673 | break; executed 8 times by 3 tests: break; Executed by:
| 8 | ||||||||||||
674 | case ConnectionRefused: executed 5 times by 2 tests: case ConnectionRefused: Executed by:
| 5 | ||||||||||||
675 | q->setError(QAbstractSocket::ConnectionRefusedError, | - | ||||||||||||
676 | QAbstractSocket::tr("Connection refused")); | - | ||||||||||||
677 | break; executed 5 times by 2 tests: break; Executed by:
| 5 | ||||||||||||
678 | case TTLExpired: never executed: case TTLExpired: | 0 | ||||||||||||
679 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
680 | QSocks5SocketEngine::tr("TTL expired")); | - | ||||||||||||
681 | break; never executed: break; | 0 | ||||||||||||
682 | case CommandNotSupported: never executed: case CommandNotSupported: | 0 | ||||||||||||
683 | q->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
684 | QSocks5SocketEngine::tr("SOCKSv5 command not supported")); | - | ||||||||||||
685 | break; never executed: break; | 0 | ||||||||||||
686 | case AddressTypeNotSupported: never executed: case AddressTypeNotSupported: | 0 | ||||||||||||
687 | q->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
688 | QSocks5SocketEngine::tr("Address type not supported")); | - | ||||||||||||
689 | break; never executed: break; | 0 | ||||||||||||
690 | - | |||||||||||||
691 | default: never executed: default: | 0 | ||||||||||||
692 | q->setError(QAbstractSocket::UnknownSocketError, | - | ||||||||||||
693 | QSocks5SocketEngine::tr("Unknown SOCKSv5 proxy error code 0x%1").arg(int(socks5error), 16)); | - | ||||||||||||
694 | break; never executed: break; | 0 | ||||||||||||
695 | } | - | ||||||||||||
696 | - | |||||||||||||
697 | setErrorState(state, QString()); | - | ||||||||||||
698 | } executed 17 times by 3 tests: end of block Executed by:
| 17 | ||||||||||||
699 | - | |||||||||||||
700 | void QSocks5SocketEnginePrivate::reauthenticate() | - | ||||||||||||
701 | { | - | ||||||||||||
702 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
703 | - | |||||||||||||
704 | // we require authentication | - | ||||||||||||
705 | QAuthenticator auth; | - | ||||||||||||
706 | emit q->proxyAuthenticationRequired(proxyInfo, &auth); | - | ||||||||||||
707 | - | |||||||||||||
708 | if (!auth.user().isEmpty() || !auth.password().isEmpty()) {
| 0-85 | ||||||||||||
709 | // we have new credentials, let's try again | - | ||||||||||||
710 | QSOCKS5_DEBUG << "authentication failure: retrying connection"; dead code: QMessageLogger(__FILE__, 710, __PRETTY_FUNCTION__).debug() << "authentication failure: retrying connection"; | - | ||||||||||||
711 | socks5State = QSocks5SocketEnginePrivate::Uninitialized; | - | ||||||||||||
712 | - | |||||||||||||
713 | delete data->authenticator; | - | ||||||||||||
714 | proxyInfo.setUser(auth.user()); | - | ||||||||||||
715 | proxyInfo.setPassword(auth.password()); | - | ||||||||||||
716 | data->authenticator = new QSocks5PasswordAuthenticator(proxyInfo.user(), proxyInfo.password()); | - | ||||||||||||
717 | - | |||||||||||||
718 | { | - | ||||||||||||
719 | const QSignalBlocker blocker(data->controlSocket); | - | ||||||||||||
720 | data->controlSocket->abort(); | - | ||||||||||||
721 | } | - | ||||||||||||
722 | data->controlSocket->connectToHost(proxyInfo.hostName(), proxyInfo.port()); | - | ||||||||||||
723 | } else { executed 85 times by 7 tests: end of block Executed by:
| 85 | ||||||||||||
724 | // authentication failure | - | ||||||||||||
725 | - | |||||||||||||
726 | setErrorState(AuthenticatingError); | - | ||||||||||||
727 | data->controlSocket->close(); | - | ||||||||||||
728 | emitConnectionNotification(); | - | ||||||||||||
729 | } executed 4 times by 2 tests: end of block Executed by:
| 4 | ||||||||||||
730 | } | - | ||||||||||||
731 | - | |||||||||||||
732 | void QSocks5SocketEnginePrivate::parseAuthenticationMethodReply() | - | ||||||||||||
733 | { | - | ||||||||||||
734 | // not enough data to begin | - | ||||||||||||
735 | if (data->controlSocket->bytesAvailable() < 2)
| 2-794 | ||||||||||||
736 | return; executed 2 times by 1 test: return; Executed by:
| 2 | ||||||||||||
737 | - | |||||||||||||
738 | QByteArray buf = data->controlSocket->read(2); | - | ||||||||||||
739 | if (buf.at(0) != S5_VERSION_5) {
| 1-793 | ||||||||||||
740 | QSOCKS5_D_DEBUG << "Socks5 version incorrect"; dead code: QMessageLogger(__FILE__, 740, __PRETTY_FUNCTION__).debug() << "Socks5 version incorrect"; | - | ||||||||||||
741 | setErrorState(SocksError); | - | ||||||||||||
742 | data->controlSocket->close(); | - | ||||||||||||
743 | emitConnectionNotification(); | - | ||||||||||||
744 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
745 | } | - | ||||||||||||
746 | - | |||||||||||||
747 | bool authComplete = false; | - | ||||||||||||
748 | if (uchar(buf.at(1)) == S5_AUTHMETHOD_NONE) {
| 179-614 | ||||||||||||
749 | authComplete = true; | - | ||||||||||||
750 | } else if (uchar(buf.at(1)) == S5_AUTHMETHOD_NOTACCEPTABLE) { executed 614 times by 8 tests: end of block Executed by:
| 88-614 | ||||||||||||
751 | reauthenticate(); | - | ||||||||||||
752 | return; executed 88 times by 7 tests: return; Executed by:
| 88 | ||||||||||||
753 | } else if (buf.at(1) != data->authenticator->methodId()
| 2-89 | ||||||||||||
754 | || !data->authenticator->beginAuthenticate(data->controlSocket, &authComplete)) {
| 0-89 | ||||||||||||
755 | setErrorState(AuthenticatingError, QLatin1String("Socks5 host did not support authentication method.")); | - | ||||||||||||
756 | socketError = QAbstractSocket::SocketAccessError; // change the socket error | - | ||||||||||||
757 | emitConnectionNotification(); | - | ||||||||||||
758 | return; executed 2 times by 1 test: return; Executed by:
| 2 | ||||||||||||
759 | } | - | ||||||||||||
760 | - | |||||||||||||
761 | if (authComplete)
| 89-614 | ||||||||||||
762 | sendRequestMethod(); executed 614 times by 8 tests: sendRequestMethod(); Executed by:
| 614 | ||||||||||||
763 | else | - | ||||||||||||
764 | socks5State = Authenticating; executed 89 times by 7 tests: socks5State = Authenticating; Executed by:
| 89 | ||||||||||||
765 | } | - | ||||||||||||
766 | - | |||||||||||||
767 | void QSocks5SocketEnginePrivate::parseAuthenticatingReply() | - | ||||||||||||
768 | { | - | ||||||||||||
769 | bool authComplete = false; | - | ||||||||||||
770 | if (!data->authenticator->continueAuthenticate(data->controlSocket, &authComplete)) {
| 1-89 | ||||||||||||
771 | reauthenticate(); | - | ||||||||||||
772 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
773 | } | - | ||||||||||||
774 | if (authComplete)
| 2-87 | ||||||||||||
775 | sendRequestMethod(); executed 87 times by 7 tests: sendRequestMethod(); Executed by:
| 87 | ||||||||||||
776 | } executed 89 times by 7 tests: end of block Executed by:
| 89 | ||||||||||||
777 | - | |||||||||||||
778 | void QSocks5SocketEnginePrivate::sendRequestMethod() | - | ||||||||||||
779 | { | - | ||||||||||||
780 | QHostAddress address; | - | ||||||||||||
781 | quint16 port = 0; | - | ||||||||||||
782 | char command = 0; | - | ||||||||||||
783 | if (mode == ConnectMode) {
| 13-688 | ||||||||||||
784 | command = S5_CONNECT; | - | ||||||||||||
785 | address = peerAddress; | - | ||||||||||||
786 | port = peerPort; | - | ||||||||||||
787 | } else if (mode == BindMode) { executed 688 times by 9 tests: end of block Executed by:
| 2-688 | ||||||||||||
788 | command = S5_BIND; | - | ||||||||||||
789 | address = localAddress; | - | ||||||||||||
790 | port = localPort; | - | ||||||||||||
791 | } else { executed 11 times by 3 tests: end of block Executed by:
| 11 | ||||||||||||
792 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
793 | command = S5_UDP_ASSOCIATE; | - | ||||||||||||
794 | address = localAddress; //data->controlSocket->localAddress(); | - | ||||||||||||
795 | port = localPort; | - | ||||||||||||
796 | #endif | - | ||||||||||||
797 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
798 | - | |||||||||||||
799 | QByteArray buf; | - | ||||||||||||
800 | buf.reserve(270); // big enough for domain name; | - | ||||||||||||
801 | buf[0] = S5_VERSION_5; | - | ||||||||||||
802 | buf[1] = command; | - | ||||||||||||
803 | buf[2] = 0x00; | - | ||||||||||||
804 | if (peerName.isEmpty() && !qt_socks5_set_host_address_and_port(address, port, &buf)) {
| 0-457 | ||||||||||||
805 | QSOCKS5_DEBUG << "error setting address" << address << " : " << port; dead code: QMessageLogger(__FILE__, 805, __PRETTY_FUNCTION__).debug() << "error setting address" << address << " : " << port; | - | ||||||||||||
806 | //### set error code .... | - | ||||||||||||
807 | return; never executed: return; | 0 | ||||||||||||
808 | } else if (!peerName.isEmpty() && !qt_socks5_set_host_name_and_port(peerName, port, &buf)) {
| 0-457 | ||||||||||||
809 | QSOCKS5_DEBUG << "error setting peer name" << peerName << " : " << port; dead code: QMessageLogger(__FILE__, 809, __PRETTY_FUNCTION__).debug() << "error setting peer name" << peerName << " : " << port; | - | ||||||||||||
810 | //### set error code .... | - | ||||||||||||
811 | return; never executed: return; | 0 | ||||||||||||
812 | } | - | ||||||||||||
813 | QSOCKS5_DEBUG << "sending" << dump(buf); dead code: QMessageLogger(__FILE__, 813, __PRETTY_FUNCTION__).debug() << "sending" << dump(buf); | - | ||||||||||||
814 | QByteArray sealedBuf; | - | ||||||||||||
815 | if (!data->authenticator->seal(buf, &sealedBuf)) {
| 0-701 | ||||||||||||
816 | // ### Handle this error. | - | ||||||||||||
817 | } never executed: end of block | 0 | ||||||||||||
818 | data->controlSocket->write(sealedBuf); | - | ||||||||||||
819 | data->controlSocket->flush(); | - | ||||||||||||
820 | socks5State = RequestMethodSent; | - | ||||||||||||
821 | } executed 701 times by 9 tests: end of block Executed by:
| 701 | ||||||||||||
822 | - | |||||||||||||
823 | void QSocks5SocketEnginePrivate::parseRequestMethodReply() | - | ||||||||||||
824 | { | - | ||||||||||||
825 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
826 | QSOCKS5_DEBUG << "parseRequestMethodReply()"; dead code: QMessageLogger(__FILE__, 826, __PRETTY_FUNCTION__).debug() << "parseRequestMethodReply()"; | - | ||||||||||||
827 | - | |||||||||||||
828 | QByteArray inBuf; | - | ||||||||||||
829 | if (!data->authenticator->unSeal(data->controlSocket, &inBuf)) {
| 0-727 | ||||||||||||
830 | // ### check error and not just not enough data | - | ||||||||||||
831 | QSOCKS5_DEBUG << "unSeal failed, needs more data"; dead code: QMessageLogger(__FILE__, 831, __PRETTY_FUNCTION__).debug() << "unSeal failed, needs more data"; | - | ||||||||||||
832 | return; never executed: return; | 0 | ||||||||||||
833 | } | - | ||||||||||||
834 | - | |||||||||||||
835 | inBuf.prepend(receivedHeaderFragment); | - | ||||||||||||
836 | receivedHeaderFragment.clear(); | - | ||||||||||||
837 | QSOCKS5_DEBUG << dump(inBuf); dead code: QMessageLogger(__FILE__, 837, __PRETTY_FUNCTION__).debug() << dump(inBuf); | - | ||||||||||||
838 | if (inBuf.size() < 3) {
| 8-719 | ||||||||||||
839 | QSOCKS5_DEBUG << "need more data for request reply header .. put this data somewhere"; dead code: QMessageLogger(__FILE__, 839, __PRETTY_FUNCTION__).debug() << "need more data for request reply header .. put this data somewhere"; | - | ||||||||||||
840 | receivedHeaderFragment = inBuf; | - | ||||||||||||
841 | return; executed 8 times by 1 test: return; Executed by:
| 8 | ||||||||||||
842 | } | - | ||||||||||||
843 | - | |||||||||||||
844 | QHostAddress address; | - | ||||||||||||
845 | quint16 port = 0; | - | ||||||||||||
846 | - | |||||||||||||
847 | if (inBuf.at(0) != S5_VERSION_5 || inBuf.at(2) != 0x00) {
| 0-718 | ||||||||||||
848 | QSOCKS5_DEBUG << "socks protocol error"; dead code: QMessageLogger(__FILE__, 848, __PRETTY_FUNCTION__).debug() << "socks protocol error"; | - | ||||||||||||
849 | setErrorState(SocksError); | - | ||||||||||||
850 | } else if (inBuf.at(1) != S5_SUCCESS) { executed 1 time by 1 test: end of block Executed by:
| 1-697 | ||||||||||||
851 | Socks5Error socks5Error = Socks5Error(inBuf.at(1)); | - | ||||||||||||
852 | QSOCKS5_DEBUG << "Request error :" << socks5Error; dead code: QMessageLogger(__FILE__, 852, __PRETTY_FUNCTION__).debug() << "Request error :" << socks5Error; | - | ||||||||||||
853 | if ((socks5Error == SocksFailure || socks5Error == ConnectionNotAllowed)
| 2-19 | ||||||||||||
854 | && !peerName.isEmpty()) {
| 2-4 | ||||||||||||
855 | // Dante seems to use this error code to indicate hostname resolution failure | - | ||||||||||||
856 | setErrorState(HostNameLookupError); | - | ||||||||||||
857 | } else { executed 4 times by 2 tests: end of block Executed by:
| 4 | ||||||||||||
858 | setErrorState(RequestError, socks5Error); | - | ||||||||||||
859 | } executed 17 times by 3 tests: end of block Executed by:
| 17 | ||||||||||||
860 | } else { | - | ||||||||||||
861 | // connection success, retrieve the remote addresses | - | ||||||||||||
862 | int pos = 3; | - | ||||||||||||
863 | int err = qt_socks5_get_host_address_and_port(inBuf, &address, &port, &pos); | - | ||||||||||||
864 | if (err == -1) {
| 0-697 | ||||||||||||
865 | QSOCKS5_DEBUG << "error getting address"; dead code: QMessageLogger(__FILE__, 865, __PRETTY_FUNCTION__).debug() << "error getting address"; | - | ||||||||||||
866 | setErrorState(SocksError); | - | ||||||||||||
867 | } else if (err == 0) { never executed: end of block
| 0-649 | ||||||||||||
868 | //need more data | - | ||||||||||||
869 | receivedHeaderFragment = inBuf; | - | ||||||||||||
870 | return; executed 48 times by 1 test: return; Executed by:
| 48 | ||||||||||||
871 | } else { | - | ||||||||||||
872 | inBuf.remove(0, pos); | - | ||||||||||||
873 | for (int i = inBuf.size() - 1; i >= 0 ; --i)
| 0-649 | ||||||||||||
874 | data->controlSocket->ungetChar(inBuf.at(i)); never executed: data->controlSocket->ungetChar(inBuf.at(i)); | 0 | ||||||||||||
875 | } executed 649 times by 7 tests: end of block Executed by:
| 649 | ||||||||||||
876 | } | - | ||||||||||||
877 | - | |||||||||||||
878 | if (socks5State == RequestMethodSent) {
| 24-647 | ||||||||||||
879 | // no error | - | ||||||||||||
880 | localAddress = address; | - | ||||||||||||
881 | localPort = port; | - | ||||||||||||
882 | - | |||||||||||||
883 | if (mode == ConnectMode) {
| 13-634 | ||||||||||||
884 | socks5State = Connected; | - | ||||||||||||
885 | // notify the upper layer that we're done | - | ||||||||||||
886 | q->setState(QAbstractSocket::ConnectedState); | - | ||||||||||||
887 | emitConnectionNotification(); | - | ||||||||||||
888 | } else if (mode == BindMode) { executed 634 times by 7 tests: end of block Executed by:
| 2-634 | ||||||||||||
889 | socks5State = BindSuccess; | - | ||||||||||||
890 | q->setState(QAbstractSocket::ListeningState); | - | ||||||||||||
891 | } else { executed 11 times by 3 tests: end of block Executed by:
| 11 | ||||||||||||
892 | socks5State = UdpAssociateSuccess; | - | ||||||||||||
893 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
894 | } else if (socks5State == BindSuccess) {
| 2-22 | ||||||||||||
895 | // no error and we got a connection | - | ||||||||||||
896 | bindData->peerAddress = address; | - | ||||||||||||
897 | bindData->peerPort = port; | - | ||||||||||||
898 | - | |||||||||||||
899 | emitReadNotification(); | - | ||||||||||||
900 | } else { executed 2 times by 2 tests: end of block Executed by:
| 2 | ||||||||||||
901 | // got an error | - | ||||||||||||
902 | data->controlSocket->close(); | - | ||||||||||||
903 | emitConnectionNotification(); | - | ||||||||||||
904 | } executed 22 times by 4 tests: end of block Executed by:
| 22 | ||||||||||||
905 | } | - | ||||||||||||
906 | - | |||||||||||||
907 | void QSocks5SocketEnginePrivate::_q_emitPendingReadNotification() | - | ||||||||||||
908 | { | - | ||||||||||||
909 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
910 | readNotificationPending = false; | - | ||||||||||||
911 | if (readNotificationEnabled) {
| 0-3112 | ||||||||||||
912 | QSOCKS5_D_DEBUG << "emitting readNotification"; dead code: QMessageLogger(__FILE__, 912, __PRETTY_FUNCTION__).debug() << "emitting readNotification"; | - | ||||||||||||
913 | QPointer<QSocks5SocketEngine> qq = q; | - | ||||||||||||
914 | emit q->readNotification(); | - | ||||||||||||
915 | if (!qq)
| 356-2756 | ||||||||||||
916 | return; executed 356 times by 2 tests: return; Executed by:
| 356 | ||||||||||||
917 | // check if there needs to be a new zero read notification | - | ||||||||||||
918 | if (data && data->controlSocket->state() == QAbstractSocket::UnconnectedState
| 0-2752 | ||||||||||||
919 | && data->controlSocket->error() == QAbstractSocket::RemoteHostClosedError) {
| 0 | ||||||||||||
920 | connectData->readBuffer.clear(); | - | ||||||||||||
921 | emitReadNotification(); | - | ||||||||||||
922 | } never executed: end of block | 0 | ||||||||||||
923 | } executed 2756 times by 4 tests: end of block Executed by:
| 2756 | ||||||||||||
924 | } executed 2756 times by 4 tests: end of block Executed by:
| 2756 | ||||||||||||
925 | - | |||||||||||||
926 | void QSocks5SocketEnginePrivate::emitReadNotification() | - | ||||||||||||
927 | { | - | ||||||||||||
928 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
929 | readNotificationActivated = true; | - | ||||||||||||
930 | if (readNotificationEnabled && !readNotificationPending) {
| 24-3289 | ||||||||||||
931 | QSOCKS5_D_DEBUG << "queueing readNotification"; dead code: QMessageLogger(__FILE__, 931, __PRETTY_FUNCTION__).debug() << "queueing readNotification"; | - | ||||||||||||
932 | readNotificationPending = true; | - | ||||||||||||
933 | QMetaObject::invokeMethod(q, "_q_emitPendingReadNotification", Qt::QueuedConnection); | - | ||||||||||||
934 | } executed 3200 times by 5 tests: end of block Executed by:
| 3200 | ||||||||||||
935 | } executed 3313 times by 6 tests: end of block Executed by:
| 3313 | ||||||||||||
936 | - | |||||||||||||
937 | void QSocks5SocketEnginePrivate::_q_emitPendingWriteNotification() | - | ||||||||||||
938 | { | - | ||||||||||||
939 | writeNotificationPending = false; | - | ||||||||||||
940 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
941 | if (writeNotificationEnabled) {
| 1637-2135 | ||||||||||||
942 | QSOCKS5_D_DEBUG << "emitting writeNotification"; dead code: QMessageLogger(__FILE__, 942, __PRETTY_FUNCTION__).debug() << "emitting writeNotification"; | - | ||||||||||||
943 | emit q->writeNotification(); | - | ||||||||||||
944 | } executed 2135 times by 4 tests: end of block Executed by:
| 2135 | ||||||||||||
945 | } executed 3772 times by 4 tests: end of block Executed by:
| 3772 | ||||||||||||
946 | - | |||||||||||||
947 | void QSocks5SocketEnginePrivate::emitWriteNotification() | - | ||||||||||||
948 | { | - | ||||||||||||
949 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
950 | writeNotificationActivated = true; | - | ||||||||||||
951 | if (writeNotificationEnabled && !writeNotificationPending) {
| 357-4401 | ||||||||||||
952 | QSOCKS5_D_DEBUG << "queueing writeNotification"; dead code: QMessageLogger(__FILE__, 952, __PRETTY_FUNCTION__).debug() << "queueing writeNotification"; | - | ||||||||||||
953 | writeNotificationPending = true; | - | ||||||||||||
954 | QMetaObject::invokeMethod(q, "_q_emitPendingWriteNotification", Qt::QueuedConnection); | - | ||||||||||||
955 | } executed 3904 times by 7 tests: end of block Executed by:
| 3904 | ||||||||||||
956 | } executed 4758 times by 7 tests: end of block Executed by:
| 4758 | ||||||||||||
957 | - | |||||||||||||
958 | void QSocks5SocketEnginePrivate::_q_emitPendingConnectionNotification() | - | ||||||||||||
959 | { | - | ||||||||||||
960 | connectionNotificationPending = false; | - | ||||||||||||
961 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
962 | QSOCKS5_D_DEBUG << "emitting connectionNotification"; dead code: QMessageLogger(__FILE__, 962, __PRETTY_FUNCTION__).debug() << "emitting connectionNotification"; | - | ||||||||||||
963 | emit q->connectionNotification(); | - | ||||||||||||
964 | } executed 598 times by 5 tests: end of block Executed by:
| 598 | ||||||||||||
965 | - | |||||||||||||
966 | void QSocks5SocketEnginePrivate::emitConnectionNotification() | - | ||||||||||||
967 | { | - | ||||||||||||
968 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
969 | QSOCKS5_D_DEBUG << "queueing connectionNotification"; dead code: QMessageLogger(__FILE__, 969, __PRETTY_FUNCTION__).debug() << "queueing connectionNotification"; | - | ||||||||||||
970 | connectionNotificationPending = true; | - | ||||||||||||
971 | QMetaObject::invokeMethod(q, "_q_emitPendingConnectionNotification", Qt::QueuedConnection); | - | ||||||||||||
972 | } executed 699 times by 8 tests: end of block Executed by:
| 699 | ||||||||||||
973 | - | |||||||||||||
974 | QSocks5SocketEngine::QSocks5SocketEngine(QObject *parent) | - | ||||||||||||
975 | :QAbstractSocketEngine(*new QSocks5SocketEnginePrivate(), parent) | - | ||||||||||||
976 | { | - | ||||||||||||
977 | } executed 726 times by 9 tests: end of block Executed by:
| 726 | ||||||||||||
978 | - | |||||||||||||
979 | QSocks5SocketEngine::~QSocks5SocketEngine() | - | ||||||||||||
980 | { | - | ||||||||||||
981 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
982 | - | |||||||||||||
983 | if (d->data) {
| 7-719 | ||||||||||||
984 | delete d->data->authenticator; | - | ||||||||||||
985 | delete d->data->controlSocket; | - | ||||||||||||
986 | } executed 719 times by 9 tests: end of block Executed by:
| 719 | ||||||||||||
987 | if (d->connectData)
| 16-710 | ||||||||||||
988 | delete d->connectData; executed 710 times by 9 tests: delete d->connectData; Executed by:
| 710 | ||||||||||||
989 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
990 | if (d->udpData) {
| 2-724 | ||||||||||||
991 | delete d->udpData->udpSocket; | - | ||||||||||||
992 | delete d->udpData; | - | ||||||||||||
993 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
994 | #endif | - | ||||||||||||
995 | if (d->bindData)
| 7-719 | ||||||||||||
996 | delete d->bindData; executed 7 times by 1 test: delete d->bindData; Executed by:
| 7 | ||||||||||||
997 | } executed 726 times by 9 tests: end of block Executed by:
| 726 | ||||||||||||
998 | - | |||||||||||||
999 | static QBasicAtomicInt descriptorCounter = Q_BASIC_ATOMIC_INITIALIZER(1); | - | ||||||||||||
1000 | - | |||||||||||||
1001 | bool QSocks5SocketEngine::initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol) | - | ||||||||||||
1002 | { | - | ||||||||||||
1003 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1004 | - | |||||||||||||
1005 | d->socketDescriptor = descriptorCounter.fetchAndAddRelaxed(1); | - | ||||||||||||
1006 | - | |||||||||||||
1007 | d->socketType = type; | - | ||||||||||||
1008 | d->socketProtocol = protocol; | - | ||||||||||||
1009 | - | |||||||||||||
1010 | return true; executed 720 times by 9 tests: return true; Executed by:
| 720 | ||||||||||||
1011 | } | - | ||||||||||||
1012 | - | |||||||||||||
1013 | bool QSocks5SocketEngine::initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState) | - | ||||||||||||
1014 | { | - | ||||||||||||
1015 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1016 | - | |||||||||||||
1017 | QSOCKS5_Q_DEBUG << "initialize" << socketDescriptor; dead code: QMessageLogger(__FILE__, 1017, __PRETTY_FUNCTION__).debug() << "initialize" << socketDescriptor; | - | ||||||||||||
1018 | - | |||||||||||||
1019 | // this is only valid for the other side of a bind, nothing else is supported | - | ||||||||||||
1020 | - | |||||||||||||
1021 | if (socketState != QAbstractSocket::ConnectedState) {
| 0-6 | ||||||||||||
1022 | //### must be connected state ??? | - | ||||||||||||
1023 | return false; never executed: return false; | 0 | ||||||||||||
1024 | } | - | ||||||||||||
1025 | - | |||||||||||||
1026 | QSocks5BindData *bindData = socks5BindStore()->retrieve(socketDescriptor); | - | ||||||||||||
1027 | if (bindData) {
| 0-6 | ||||||||||||
1028 | - | |||||||||||||
1029 | d->socketState = QAbstractSocket::ConnectedState; | - | ||||||||||||
1030 | d->socketType = QAbstractSocket::TcpSocket; | - | ||||||||||||
1031 | d->connectData = new QSocks5ConnectData; | - | ||||||||||||
1032 | d->data = d->connectData; | - | ||||||||||||
1033 | d->mode = QSocks5SocketEnginePrivate::ConnectMode; | - | ||||||||||||
1034 | d->data->controlSocket = bindData->controlSocket; | - | ||||||||||||
1035 | bindData->controlSocket = 0; | - | ||||||||||||
1036 | d->data->controlSocket->setParent(this); | - | ||||||||||||
1037 | d->socketProtocol = d->data->controlSocket->localAddress().protocol(); | - | ||||||||||||
1038 | d->data->authenticator = bindData->authenticator; | - | ||||||||||||
1039 | bindData->authenticator = 0; | - | ||||||||||||
1040 | d->localPort = bindData->localPort; | - | ||||||||||||
1041 | d->localAddress = bindData->localAddress; | - | ||||||||||||
1042 | d->peerPort = bindData->peerPort; | - | ||||||||||||
1043 | d->peerAddress = bindData->peerAddress; | - | ||||||||||||
1044 | delete bindData; | - | ||||||||||||
1045 | - | |||||||||||||
1046 | QObject::connect(d->data->controlSocket, SIGNAL(connected()), this, SLOT(_q_controlSocketConnected()), | - | ||||||||||||
1047 | Qt::DirectConnection); | - | ||||||||||||
1048 | QObject::connect(d->data->controlSocket, SIGNAL(readyRead()), this, SLOT(_q_controlSocketReadNotification()), | - | ||||||||||||
1049 | Qt::DirectConnection); | - | ||||||||||||
1050 | QObject::connect(d->data->controlSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(_q_controlSocketBytesWritten()), | - | ||||||||||||
1051 | Qt::DirectConnection); | - | ||||||||||||
1052 | QObject::connect(d->data->controlSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(_q_controlSocketError(QAbstractSocket::SocketError)), | - | ||||||||||||
1053 | Qt::DirectConnection); | - | ||||||||||||
1054 | QObject::connect(d->data->controlSocket, SIGNAL(disconnected()), this, SLOT(_q_controlSocketDisconnected()), | - | ||||||||||||
1055 | Qt::DirectConnection); | - | ||||||||||||
1056 | QObject::connect(d->data->controlSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), | - | ||||||||||||
1057 | this, SLOT(_q_controlSocketStateChanged(QAbstractSocket::SocketState)), | - | ||||||||||||
1058 | Qt::DirectConnection); | - | ||||||||||||
1059 | - | |||||||||||||
1060 | d->socks5State = QSocks5SocketEnginePrivate::Connected; | - | ||||||||||||
1061 | - | |||||||||||||
1062 | if (d->data->controlSocket->bytesAvailable() != 0)
| 0-6 | ||||||||||||
1063 | d->_q_controlSocketReadNotification(); never executed: d->_q_controlSocketReadNotification(); | 0 | ||||||||||||
1064 | return true; executed 6 times by 3 tests: return true; Executed by:
| 6 | ||||||||||||
1065 | } | - | ||||||||||||
1066 | return false; never executed: return false; | 0 | ||||||||||||
1067 | } | - | ||||||||||||
1068 | - | |||||||||||||
1069 | void QSocks5SocketEngine::setProxy(const QNetworkProxy &networkProxy) | - | ||||||||||||
1070 | { | - | ||||||||||||
1071 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1072 | d->proxyInfo = networkProxy; | - | ||||||||||||
1073 | } executed 719 times by 9 tests: end of block Executed by:
| 719 | ||||||||||||
1074 | - | |||||||||||||
1075 | qintptr QSocks5SocketEngine::socketDescriptor() const | - | ||||||||||||
1076 | { | - | ||||||||||||
1077 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1078 | return d->socketDescriptor; executed 1090 times by 9 tests: return d->socketDescriptor; Executed by:
| 1090 | ||||||||||||
1079 | } | - | ||||||||||||
1080 | - | |||||||||||||
1081 | bool QSocks5SocketEngine::isValid() const | - | ||||||||||||
1082 | { | - | ||||||||||||
1083 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1084 | return d->socketType != QAbstractSocket::UnknownSocketType executed 6842 times by 6 tests: return d->socketType != QAbstractSocket::UnknownSocketType && d->socks5State != QSocks5SocketEnginePrivate::SocksError && (d->socketError == QAbstractSocket::UnknownSocketError || d->socketError == QAbstractSocket::SocketTimeoutError || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); Executed by:
| 1-6842 | ||||||||||||
1085 | && d->socks5State != QSocks5SocketEnginePrivate::SocksError executed 6842 times by 6 tests: return d->socketType != QAbstractSocket::UnknownSocketType && d->socks5State != QSocks5SocketEnginePrivate::SocksError && (d->socketError == QAbstractSocket::UnknownSocketError || d->socketError == QAbstractSocket::SocketTimeoutError || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); Executed by:
| 0-6842 | ||||||||||||
1086 | && (d->socketError == QAbstractSocket::UnknownSocketError executed 6842 times by 6 tests: return d->socketType != QAbstractSocket::UnknownSocketType && d->socks5State != QSocks5SocketEnginePrivate::SocksError && (d->socketError == QAbstractSocket::UnknownSocketError || d->socketError == QAbstractSocket::SocketTimeoutError || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); Executed by:
| 350-6842 | ||||||||||||
1087 | || d->socketError == QAbstractSocket::SocketTimeoutError executed 6842 times by 6 tests: return d->socketType != QAbstractSocket::UnknownSocketType && d->socks5State != QSocks5SocketEnginePrivate::SocksError && (d->socketError == QAbstractSocket::UnknownSocketError || d->socketError == QAbstractSocket::SocketTimeoutError || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); Executed by:
| 0-6842 | ||||||||||||
1088 | || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); executed 6842 times by 6 tests: return d->socketType != QAbstractSocket::UnknownSocketType && d->socks5State != QSocks5SocketEnginePrivate::SocksError && (d->socketError == QAbstractSocket::UnknownSocketError || d->socketError == QAbstractSocket::SocketTimeoutError || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); Executed by:
| 0-6842 | ||||||||||||
1089 | } | - | ||||||||||||
1090 | - | |||||||||||||
1091 | bool QSocks5SocketEngine::connectInternal() | - | ||||||||||||
1092 | { | - | ||||||||||||
1093 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1094 | - | |||||||||||||
1095 | if (!d->data) {
| 4-705 | ||||||||||||
1096 | if (socketType() == QAbstractSocket::TcpSocket) {
| 1-704 | ||||||||||||
1097 | d->initialize(QSocks5SocketEnginePrivate::ConnectMode); | - | ||||||||||||
1098 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1099 | } else if (socketType() == QAbstractSocket::UdpSocket) { executed 704 times by 9 tests: end of block Executed by:
| 0-704 | ||||||||||||
1100 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
1101 | // all udp needs to be bound | - | ||||||||||||
1102 | if (!bind(QHostAddress(QLatin1String("0.0.0.0")), 0))
| 0-1 | ||||||||||||
1103 | return false; never executed: return false; | 0 | ||||||||||||
1104 | - | |||||||||||||
1105 | setState(QAbstractSocket::ConnectedState); | - | ||||||||||||
1106 | return true; executed 1 time by 1 test: return true; Executed by:
| 1 | ||||||||||||
1107 | #endif | - | ||||||||||||
1108 | } else { | - | ||||||||||||
1109 | qFatal("QSocks5SocketEngine::connectToHost: in QTcpServer mode"); | - | ||||||||||||
1110 | return false; never executed: return false; | 0 | ||||||||||||
1111 | } | - | ||||||||||||
1112 | } | - | ||||||||||||
1113 | - | |||||||||||||
1114 | if (d->socketState != QAbstractSocket::ConnectingState) {
| 0-708 | ||||||||||||
1115 | if (d->socks5State == QSocks5SocketEnginePrivate::Uninitialized
| 4-704 | ||||||||||||
1116 | // We may have new auth credentials since an earlier failure: | - | ||||||||||||
1117 | || d->socks5State == QSocks5SocketEnginePrivate::AuthenticatingError) {
| 2 | ||||||||||||
1118 | setState(QAbstractSocket::ConnectingState); | - | ||||||||||||
1119 | //limit buffer in internal socket, data is buffered in the external socket under application control | - | ||||||||||||
1120 | d->data->controlSocket->setReadBufferSize(65536); | - | ||||||||||||
1121 | } executed 706 times by 9 tests: end of block Executed by:
| 706 | ||||||||||||
1122 | - | |||||||||||||
1123 | d->data->controlSocket->connectToHost(d->proxyInfo.hostName(), d->proxyInfo.port()); | - | ||||||||||||
1124 | } executed 708 times by 9 tests: end of block Executed by:
| 708 | ||||||||||||
1125 | - | |||||||||||||
1126 | return false; executed 708 times by 9 tests: return false; Executed by:
| 708 | ||||||||||||
1127 | } | - | ||||||||||||
1128 | - | |||||||||||||
1129 | bool QSocks5SocketEngine::connectToHost(const QHostAddress &address, quint16 port) | - | ||||||||||||
1130 | { | - | ||||||||||||
1131 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1132 | QSOCKS5_DEBUG << "connectToHost" << address << ':' << port; dead code: QMessageLogger(__FILE__, 1132, __PRETTY_FUNCTION__).debug() << "connectToHost" << address << ':' << port; | - | ||||||||||||
1133 | - | |||||||||||||
1134 | setPeerAddress(address); | - | ||||||||||||
1135 | setPeerPort(port); | - | ||||||||||||
1136 | d->peerName.clear(); | - | ||||||||||||
1137 | - | |||||||||||||
1138 | return connectInternal(); executed 249 times by 5 tests: return connectInternal(); Executed by:
| 249 | ||||||||||||
1139 | } | - | ||||||||||||
1140 | - | |||||||||||||
1141 | bool QSocks5SocketEngine::connectToHostByName(const QString &hostname, quint16 port) | - | ||||||||||||
1142 | { | - | ||||||||||||
1143 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1144 | - | |||||||||||||
1145 | setPeerAddress(QHostAddress()); | - | ||||||||||||
1146 | setPeerPort(port); | - | ||||||||||||
1147 | d->peerName = hostname; | - | ||||||||||||
1148 | - | |||||||||||||
1149 | return connectInternal(); executed 460 times by 7 tests: return connectInternal(); Executed by:
| 460 | ||||||||||||
1150 | } | - | ||||||||||||
1151 | - | |||||||||||||
1152 | void QSocks5SocketEnginePrivate::_q_controlSocketConnected() | - | ||||||||||||
1153 | { | - | ||||||||||||
1154 | QSOCKS5_DEBUG << "_q_controlSocketConnected"; dead code: QMessageLogger(__FILE__, 1154, __PRETTY_FUNCTION__).debug() << "_q_controlSocketConnected"; | - | ||||||||||||
1155 | QByteArray buf(3, 0); | - | ||||||||||||
1156 | buf[0] = S5_VERSION_5; | - | ||||||||||||
1157 | buf[1] = 0x01; | - | ||||||||||||
1158 | buf[2] = data->authenticator->methodId(); | - | ||||||||||||
1159 | data->controlSocket->write(buf); | - | ||||||||||||
1160 | socks5State = AuthenticationMethodsSent; | - | ||||||||||||
1161 | } executed 798 times by 9 tests: end of block Executed by:
| 798 | ||||||||||||
1162 | - | |||||||||||||
1163 | void QSocks5SocketEnginePrivate::_q_controlSocketReadNotification() | - | ||||||||||||
1164 | { | - | ||||||||||||
1165 | QSOCKS5_D_DEBUG << "_q_controlSocketReadNotification socks5state" << s5StateToString(socks5State) dead code: QMessageLogger(__FILE__, 1165, __PRETTY_FUNCTION__).debug() << "_q_controlSocketReadNotification socks5state" << s5StateToString(socks5State) << "bytes available" << data->controlSocket->bytesAvailable(); | - | ||||||||||||
1166 | << "bytes available" << data->controlSocket->bytesAvailable(); dead code: QMessageLogger(__FILE__, 1165, __PRETTY_FUNCTION__).debug() << "_q_controlSocketReadNotification socks5state" << s5StateToString(socks5State) << "bytes available" << data->controlSocket->bytesAvailable(); | - | ||||||||||||
1167 | - | |||||||||||||
1168 | if (data->controlSocket->bytesAvailable() == 0) {
| 0-4548 | ||||||||||||
1169 | QSOCKS5_D_DEBUG << "########## bogus read why do we get these ... on windows only"; dead code: QMessageLogger(__FILE__, 1169, __PRETTY_FUNCTION__).debug() << "########## bogus read why do we get these ... on windows only"; | - | ||||||||||||
1170 | return; never executed: return; | 0 | ||||||||||||
1171 | } | - | ||||||||||||
1172 | - | |||||||||||||
1173 | switch (socks5State) { | - | ||||||||||||
1174 | case AuthenticationMethodsSent: executed 796 times by 9 tests: case AuthenticationMethodsSent: Executed by:
| 796 | ||||||||||||
1175 | parseAuthenticationMethodReply(); | - | ||||||||||||
1176 | break; executed 796 times by 9 tests: break; Executed by:
| 796 | ||||||||||||
1177 | case Authenticating: executed 90 times by 7 tests: case Authenticating: Executed by:
| 90 | ||||||||||||
1178 | parseAuthenticatingReply(); | - | ||||||||||||
1179 | break; executed 90 times by 7 tests: break; Executed by:
| 90 | ||||||||||||
1180 | case RequestMethodSent: executed 725 times by 8 tests: case RequestMethodSent: Executed by:
| 725 | ||||||||||||
1181 | parseRequestMethodReply(); | - | ||||||||||||
1182 | break; executed 725 times by 8 tests: break; Executed by:
| 725 | ||||||||||||
1183 | case Connected: { executed 2935 times by 6 tests: case Connected: Executed by:
| 2935 | ||||||||||||
1184 | QByteArray buf; | - | ||||||||||||
1185 | if (!data->authenticator->unSeal(data->controlSocket, &buf)) {
| 0-2935 | ||||||||||||
1186 | // qDebug() << "unseal error maybe need to wait for more data"; | - | ||||||||||||
1187 | } never executed: end of block | 0 | ||||||||||||
1188 | if (buf.size()) {
| 0-2935 | ||||||||||||
1189 | QSOCKS5_DEBUG << dump(buf); dead code: QMessageLogger(__FILE__, 1189, __PRETTY_FUNCTION__).debug() << dump(buf); | - | ||||||||||||
1190 | connectData->readBuffer += buf; | - | ||||||||||||
1191 | emitReadNotification(); | - | ||||||||||||
1192 | } executed 2935 times by 6 tests: end of block Executed by:
| 2935 | ||||||||||||
1193 | break; executed 2935 times by 6 tests: break; Executed by:
| 2935 | ||||||||||||
1194 | } | - | ||||||||||||
1195 | case BindSuccess: executed 2 times by 2 tests: case BindSuccess: Executed by:
| 2 | ||||||||||||
1196 | // only get here if command is bind | - | ||||||||||||
1197 | if (mode == BindMode) {
| 0-2 | ||||||||||||
1198 | parseRequestMethodReply(); | - | ||||||||||||
1199 | break; executed 2 times by 2 tests: break; Executed by:
| 2 | ||||||||||||
1200 | } | - | ||||||||||||
1201 | - | |||||||||||||
1202 | // fall through | - | ||||||||||||
1203 | default: code before this statement never executed: default: never executed: default: | 0 | ||||||||||||
1204 | qWarning("QSocks5SocketEnginePrivate::_q_controlSocketReadNotification: " | - | ||||||||||||
1205 | "Unexpectedly received data while in state=%d and mode=%d", | - | ||||||||||||
1206 | socks5State, mode); | - | ||||||||||||
1207 | break; never executed: break; | 0 | ||||||||||||
1208 | }; | - | ||||||||||||
1209 | } | - | ||||||||||||
1210 | - | |||||||||||||
1211 | void QSocks5SocketEnginePrivate::_q_controlSocketBytesWritten() | - | ||||||||||||
1212 | { | - | ||||||||||||
1213 | QSOCKS5_DEBUG << "_q_controlSocketBytesWritten"; dead code: QMessageLogger(__FILE__, 1213, __PRETTY_FUNCTION__).debug() << "_q_controlSocketBytesWritten"; | - | ||||||||||||
1214 | - | |||||||||||||
1215 | if (socks5State != Connected
| 1588-1887 | ||||||||||||
1216 | || (mode == ConnectMode
| 0-1887 | ||||||||||||
1217 | && data->controlSocket->bytesToWrite()))
| 4-1883 | ||||||||||||
1218 | return; executed 1592 times by 9 tests: return; Executed by:
| 1592 | ||||||||||||
1219 | if (data->controlSocket->bytesToWrite() < MaxWriteBufferSize) {
| 0-1883 | ||||||||||||
1220 | emitWriteNotification(); | - | ||||||||||||
1221 | writeNotificationActivated = false; | - | ||||||||||||
1222 | } executed 1883 times by 6 tests: end of block Executed by:
| 1883 | ||||||||||||
1223 | } executed 1883 times by 6 tests: end of block Executed by:
| 1883 | ||||||||||||
1224 | - | |||||||||||||
1225 | void QSocks5SocketEnginePrivate::_q_controlSocketError(QAbstractSocket::SocketError error) | - | ||||||||||||
1226 | { | - | ||||||||||||
1227 | QSOCKS5_D_DEBUG << "controlSocketError" << error << data->controlSocket->errorString(); dead code: QMessageLogger(__FILE__, 1227, __PRETTY_FUNCTION__).debug() << "controlSocketError" << error << data->controlSocket->errorString(); | - | ||||||||||||
1228 | - | |||||||||||||
1229 | if (error == QAbstractSocket::SocketTimeoutError)
| 31-391 | ||||||||||||
1230 | return; // ignore this error -- comes from the waitFor* functions executed 31 times by 5 tests: return; Executed by:
| 31 | ||||||||||||
1231 | - | |||||||||||||
1232 | if (error == QAbstractSocket::RemoteHostClosedError
| 8-383 | ||||||||||||
1233 | && socks5State == Connected) {
| 30-353 | ||||||||||||
1234 | // clear the read buffer in connect mode so that bytes available returns 0 | - | ||||||||||||
1235 | // if there already is a read notification pending then this will be processed first | - | ||||||||||||
1236 | if (!readNotificationPending)
| 2-351 | ||||||||||||
1237 | connectData->readBuffer.clear(); executed 351 times by 2 tests: connectData->readBuffer.clear(); Executed by:
| 351 | ||||||||||||
1238 | emitReadNotification(); | - | ||||||||||||
1239 | data->controlSocket->close(); | - | ||||||||||||
1240 | // cause a disconnect in the outer socket | - | ||||||||||||
1241 | emitWriteNotification(); | - | ||||||||||||
1242 | } else if (socks5State == Uninitialized executed 353 times by 3 tests: end of block Executed by:
| 6-353 | ||||||||||||
1243 | || socks5State == AuthenticationMethodsSent
| 1-31 | ||||||||||||
1244 | || socks5State == Authenticating
| 1-30 | ||||||||||||
1245 | || socks5State == RequestMethodSent) {
| 2-28 | ||||||||||||
1246 | setErrorState(socks5State == Uninitialized ? ConnectError : ControlSocketError); | - | ||||||||||||
1247 | data->controlSocket->close(); | - | ||||||||||||
1248 | emitConnectionNotification(); | - | ||||||||||||
1249 | } else { executed 36 times by 4 tests: end of block Executed by:
| 36 | ||||||||||||
1250 | q_func()->setError(data->controlSocket->error(), data->controlSocket->errorString()); | - | ||||||||||||
1251 | emitReadNotification(); | - | ||||||||||||
1252 | emitWriteNotification(); | - | ||||||||||||
1253 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
1254 | } | - | ||||||||||||
1255 | - | |||||||||||||
1256 | void QSocks5SocketEnginePrivate::_q_controlSocketDisconnected() | - | ||||||||||||
1257 | { | - | ||||||||||||
1258 | QSOCKS5_D_DEBUG << "_q_controlSocketDisconnected"; dead code: QMessageLogger(__FILE__, 1258, __PRETTY_FUNCTION__).debug() << "_q_controlSocketDisconnected"; | - | ||||||||||||
1259 | } executed 713 times by 9 tests: end of block Executed by:
| 713 | ||||||||||||
1260 | - | |||||||||||||
1261 | void QSocks5SocketEnginePrivate::_q_controlSocketStateChanged(QAbstractSocket::SocketState state) | - | ||||||||||||
1262 | { | - | ||||||||||||
1263 | QSOCKS5_D_DEBUG << "_q_controlSocketStateChanged" << state; dead code: QMessageLogger(__FILE__, 1263, __PRETTY_FUNCTION__).debug() << "_q_controlSocketStateChanged" << state; | - | ||||||||||||
1264 | } executed 3835 times by 9 tests: end of block Executed by:
| 3835 | ||||||||||||
1265 | - | |||||||||||||
1266 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1267 | void QSocks5SocketEnginePrivate::checkForDatagrams() const | - | ||||||||||||
1268 | { | - | ||||||||||||
1269 | // udp should be unbuffered so we need to do some polling at certain points | - | ||||||||||||
1270 | if (udpData->udpSocket->hasPendingDatagrams())
| 0 | ||||||||||||
1271 | const_cast<QSocks5SocketEnginePrivate *>(this)->_q_udpSocketReadNotification(); never executed: const_cast<QSocks5SocketEnginePrivate *>(this)->_q_udpSocketReadNotification(); | 0 | ||||||||||||
1272 | } never executed: end of block | 0 | ||||||||||||
1273 | - | |||||||||||||
1274 | void QSocks5SocketEnginePrivate::_q_udpSocketReadNotification() | - | ||||||||||||
1275 | { | - | ||||||||||||
1276 | QSOCKS5_D_DEBUG << "_q_udpSocketReadNotification()"; dead code: QMessageLogger(__FILE__, 1276, __PRETTY_FUNCTION__).debug() << "_q_udpSocketReadNotification()"; | - | ||||||||||||
1277 | - | |||||||||||||
1278 | // check some state stuff | - | ||||||||||||
1279 | if (!udpData->udpSocket->hasPendingDatagrams()) {
| 0 | ||||||||||||
1280 | QSOCKS5_D_DEBUG << "false read ??"; dead code: QMessageLogger(__FILE__, 1280, __PRETTY_FUNCTION__).debug() << "false read ??"; | - | ||||||||||||
1281 | return; never executed: return; | 0 | ||||||||||||
1282 | } | - | ||||||||||||
1283 | - | |||||||||||||
1284 | while (udpData->udpSocket->hasPendingDatagrams()) {
| 0 | ||||||||||||
1285 | QByteArray sealedBuf(udpData->udpSocket->pendingDatagramSize(), 0); | - | ||||||||||||
1286 | QSOCKS5_D_DEBUG << "new datagram"; dead code: QMessageLogger(__FILE__, 1286, __PRETTY_FUNCTION__).debug() << "new datagram"; | - | ||||||||||||
1287 | udpData->udpSocket->readDatagram(sealedBuf.data(), sealedBuf.size()); | - | ||||||||||||
1288 | QByteArray inBuf; | - | ||||||||||||
1289 | if (!data->authenticator->unSeal(sealedBuf, &inBuf)) {
| 0 | ||||||||||||
1290 | QSOCKS5_D_DEBUG << "failed unsealing datagram discarding"; dead code: QMessageLogger(__FILE__, 1290, __PRETTY_FUNCTION__).debug() << "failed unsealing datagram discarding"; | - | ||||||||||||
1291 | return; never executed: return; | 0 | ||||||||||||
1292 | } | - | ||||||||||||
1293 | QSOCKS5_DEBUG << dump(inBuf); dead code: QMessageLogger(__FILE__, 1293, __PRETTY_FUNCTION__).debug() << dump(inBuf); | - | ||||||||||||
1294 | int pos = 0; | - | ||||||||||||
1295 | const char *buf = inBuf.constData(); | - | ||||||||||||
1296 | if (inBuf.size() < 4) {
| 0 | ||||||||||||
1297 | QSOCKS5_D_DEBUG << "bugus udp data, discarding"; dead code: QMessageLogger(__FILE__, 1297, __PRETTY_FUNCTION__).debug() << "bugus udp data, discarding"; | - | ||||||||||||
1298 | return; never executed: return; | 0 | ||||||||||||
1299 | } | - | ||||||||||||
1300 | QSocks5RevivedDatagram datagram; | - | ||||||||||||
1301 | if (buf[pos++] != 0 || buf[pos++] != 0) {
| 0 | ||||||||||||
1302 | QSOCKS5_D_DEBUG << "invalid datagram discarding"; dead code: QMessageLogger(__FILE__, 1302, __PRETTY_FUNCTION__).debug() << "invalid datagram discarding"; | - | ||||||||||||
1303 | return; never executed: return; | 0 | ||||||||||||
1304 | } | - | ||||||||||||
1305 | if (buf[pos++] != 0) { //### add fragmentation reading support
| 0 | ||||||||||||
1306 | QSOCKS5_D_DEBUG << "don't support fragmentation yet disgarding"; dead code: QMessageLogger(__FILE__, 1306, __PRETTY_FUNCTION__).debug() << "don't support fragmentation yet disgarding"; | - | ||||||||||||
1307 | return; never executed: return; | 0 | ||||||||||||
1308 | } | - | ||||||||||||
1309 | if (qt_socks5_get_host_address_and_port(inBuf, &datagram.address, &datagram.port, &pos) != 1) {
| 0 | ||||||||||||
1310 | QSOCKS5_D_DEBUG << "failed to get address from datagram disgarding"; dead code: QMessageLogger(__FILE__, 1310, __PRETTY_FUNCTION__).debug() << "failed to get address from datagram disgarding"; | - | ||||||||||||
1311 | return; never executed: return; | 0 | ||||||||||||
1312 | } | - | ||||||||||||
1313 | datagram.data = QByteArray(&buf[pos], inBuf.size() - pos); | - | ||||||||||||
1314 | udpData->pendingDatagrams.enqueue(datagram); | - | ||||||||||||
1315 | } never executed: end of block | 0 | ||||||||||||
1316 | emitReadNotification(); | - | ||||||||||||
1317 | } never executed: end of block | 0 | ||||||||||||
1318 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
1319 | - | |||||||||||||
1320 | bool QSocks5SocketEngine::bind(const QHostAddress &addr, quint16 port) | - | ||||||||||||
1321 | { | - | ||||||||||||
1322 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1323 | - | |||||||||||||
1324 | // when bind wee will block until the bind is finished as the info from the proxy server is needed | - | ||||||||||||
1325 | - | |||||||||||||
1326 | QHostAddress address; | - | ||||||||||||
1327 | if (addr.protocol() == QAbstractSocket::AnyIPProtocol)
| 7-8 | ||||||||||||
1328 | address = QHostAddress::AnyIPv4; //SOCKS5 doesn't support dual stack, and there isn't any implementation of udp on ipv6 yet executed 8 times by 1 test: address = QHostAddress::AnyIPv4; Executed by:
| 8 | ||||||||||||
1329 | else | - | ||||||||||||
1330 | address = addr; executed 7 times by 2 tests: address = addr; Executed by:
| 7 | ||||||||||||
1331 | - | |||||||||||||
1332 | if (!d->data) {
| 1-14 | ||||||||||||
1333 | if (socketType() == QAbstractSocket::TcpSocket) {
| 1-13 | ||||||||||||
1334 | d->initialize(QSocks5SocketEnginePrivate::BindMode); | - | ||||||||||||
1335 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1336 | } else if (socketType() == QAbstractSocket::UdpSocket) { executed 13 times by 3 tests: end of block Executed by:
| 0-13 | ||||||||||||
1337 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
1338 | #endif | - | ||||||||||||
1339 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
1340 | //### something invalid | - | ||||||||||||
1341 | return false; never executed: return false; | 0 | ||||||||||||
1342 | } | - | ||||||||||||
1343 | } | - | ||||||||||||
1344 | - | |||||||||||||
1345 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1346 | if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) {
| 2-13 | ||||||||||||
1347 | if (!d->udpData->udpSocket->bind(address, port)) {
| 0-2 | ||||||||||||
1348 | QSOCKS5_Q_DEBUG << "local udp bind failed"; dead code: QMessageLogger(__FILE__, 1348, __PRETTY_FUNCTION__).debug() << "local udp bind failed"; | - | ||||||||||||
1349 | setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); | - | ||||||||||||
1350 | return false; never executed: return false; | 0 | ||||||||||||
1351 | } | - | ||||||||||||
1352 | d->localAddress = d->udpData->udpSocket->localAddress(); | - | ||||||||||||
1353 | d->localPort = d->udpData->udpSocket->localPort(); | - | ||||||||||||
1354 | } else executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
1355 | #endif | - | ||||||||||||
1356 | if (d->mode == QSocks5SocketEnginePrivate::BindMode) {
| 0-13 | ||||||||||||
1357 | d->localAddress = address; | - | ||||||||||||
1358 | d->localPort = port; | - | ||||||||||||
1359 | } else { executed 13 times by 3 tests: end of block Executed by:
| 13 | ||||||||||||
1360 | //### something invalid | - | ||||||||||||
1361 | return false; never executed: return false; | 0 | ||||||||||||
1362 | } | - | ||||||||||||
1363 | - | |||||||||||||
1364 | int msecs = SOCKS5_BLOCKING_BIND_TIMEOUT; | - | ||||||||||||
1365 | QElapsedTimer stopWatch; | - | ||||||||||||
1366 | stopWatch.start(); | - | ||||||||||||
1367 | d->data->controlSocket->connectToHost(d->proxyInfo.hostName(), d->proxyInfo.port()); | - | ||||||||||||
1368 | if (!d->waitForConnected(msecs, 0) ||
| 1-14 | ||||||||||||
1369 | d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) {
| 1-13 | ||||||||||||
1370 | // waitForConnected sets the error state and closes the socket | - | ||||||||||||
1371 | QSOCKS5_Q_DEBUG << "waitForConnected to proxy server" << d->data->controlSocket->errorString(); dead code: QMessageLogger(__FILE__, 1371, __PRETTY_FUNCTION__).debug() << "waitForConnected to proxy server" << d->data->controlSocket->errorString(); | - | ||||||||||||
1372 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||
1373 | } | - | ||||||||||||
1374 | if (d->socks5State == QSocks5SocketEnginePrivate::BindSuccess) {
| 2-11 | ||||||||||||
1375 | setState(QAbstractSocket::BoundState); | - | ||||||||||||
1376 | return true; executed 11 times by 3 tests: return true; Executed by:
| 11 | ||||||||||||
1377 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1378 | } else if (d->socks5State == QSocks5SocketEnginePrivate::UdpAssociateSuccess) {
| 0-2 | ||||||||||||
1379 | setState(QAbstractSocket::BoundState); | - | ||||||||||||
1380 | d->udpData->associateAddress = d->localAddress; | - | ||||||||||||
1381 | d->localAddress = QHostAddress(); | - | ||||||||||||
1382 | d->udpData->associatePort = d->localPort; | - | ||||||||||||
1383 | d->localPort = 0; | - | ||||||||||||
1384 | QUdpSocket dummy; | - | ||||||||||||
1385 | #ifndef QT_NO_BEARERMANAGEMENT | - | ||||||||||||
1386 | dummy.setProperty("_q_networksession", property("_q_networksession")); | - | ||||||||||||
1387 | #endif | - | ||||||||||||
1388 | dummy.setProxy(QNetworkProxy::NoProxy); | - | ||||||||||||
1389 | if (!dummy.bind()
| 0-2 | ||||||||||||
1390 | || writeDatagram(0,0, QIpPacketHeader(d->data->controlSocket->localAddress(), dummy.localPort())) != 0
| 0-2 | ||||||||||||
1391 | || !dummy.waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))
| 0-2 | ||||||||||||
1392 | || dummy.readDatagram(0,0, &d->localAddress, &d->localPort) != 0) {
| 0-2 | ||||||||||||
1393 | QSOCKS5_DEBUG << "udp actual address and port lookup failed"; dead code: QMessageLogger(__FILE__, 1393, __PRETTY_FUNCTION__).debug() << "udp actual address and port lookup failed"; | - | ||||||||||||
1394 | setState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
1395 | setError(dummy.error(), dummy.errorString()); | - | ||||||||||||
1396 | d->data->controlSocket->close(); | - | ||||||||||||
1397 | //### reset and error | - | ||||||||||||
1398 | return false; never executed: return false; | 0 | ||||||||||||
1399 | } | - | ||||||||||||
1400 | QSOCKS5_DEBUG << "udp actual address and port" << d->localAddress << ':' << d->localPort; dead code: QMessageLogger(__FILE__, 1400, __PRETTY_FUNCTION__).debug() << "udp actual address and port" << d->localAddress << ':' << d->localPort; | - | ||||||||||||
1401 | return true; executed 2 times by 1 test: return true; Executed by:
| 2 | ||||||||||||
1402 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
1403 | } | - | ||||||||||||
1404 | - | |||||||||||||
1405 | // binding timed out | - | ||||||||||||
1406 | setError(QAbstractSocket::SocketTimeoutError, | - | ||||||||||||
1407 | QLatin1String(QT_TRANSLATE_NOOP("QSocks5SocketEngine", "Network operation timed out"))); | - | ||||||||||||
1408 | - | |||||||||||||
1409 | ///### delete d->udpSocket; | - | ||||||||||||
1410 | ///### d->udpSocket = 0; | - | ||||||||||||
1411 | return false; never executed: return false; | 0 | ||||||||||||
1412 | } | - | ||||||||||||
1413 | - | |||||||||||||
1414 | - | |||||||||||||
1415 | bool QSocks5SocketEngine::listen() | - | ||||||||||||
1416 | { | - | ||||||||||||
1417 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1418 | - | |||||||||||||
1419 | QSOCKS5_Q_DEBUG << "listen()"; dead code: QMessageLogger(__FILE__, 1419, __PRETTY_FUNCTION__).debug() << "listen()"; | - | ||||||||||||
1420 | - | |||||||||||||
1421 | // check that we are in bound and then go to listening. | - | ||||||||||||
1422 | if (d->socketState == QAbstractSocket::BoundState) {
| 0-11 | ||||||||||||
1423 | d->socketState = QAbstractSocket::ListeningState; | - | ||||||||||||
1424 | - | |||||||||||||
1425 | // check if we already have a connection | - | ||||||||||||
1426 | if (d->socks5State == QSocks5SocketEnginePrivate::BindSuccess)
| 0-11 | ||||||||||||
1427 | d->emitReadNotification(); executed 11 times by 3 tests: d->emitReadNotification(); Executed by:
| 11 | ||||||||||||
1428 | - | |||||||||||||
1429 | return true; executed 11 times by 3 tests: return true; Executed by:
| 11 | ||||||||||||
1430 | } | - | ||||||||||||
1431 | return false; never executed: return false; | 0 | ||||||||||||
1432 | } | - | ||||||||||||
1433 | - | |||||||||||||
1434 | int QSocks5SocketEngine::accept() | - | ||||||||||||
1435 | { | - | ||||||||||||
1436 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1437 | // check we are listing --- | - | ||||||||||||
1438 | - | |||||||||||||
1439 | QSOCKS5_Q_DEBUG << "accept()"; dead code: QMessageLogger(__FILE__, 1439, __PRETTY_FUNCTION__).debug() << "accept()"; | - | ||||||||||||
1440 | - | |||||||||||||
1441 | qintptr sd = -1; | - | ||||||||||||
1442 | switch (d->socks5State) { | - | ||||||||||||
1443 | case QSocks5SocketEnginePrivate::BindSuccess: executed 6 times by 3 tests: case QSocks5SocketEnginePrivate::BindSuccess: Executed by:
| 6 | ||||||||||||
1444 | QSOCKS5_Q_DEBUG << "BindSuccess adding" << d->socketDescriptor << "to the bind store"; dead code: QMessageLogger(__FILE__, 1444, __PRETTY_FUNCTION__).debug() << "BindSuccess adding" << d->socketDescriptor << "to the bind store"; | - | ||||||||||||
1445 | d->data->controlSocket->disconnect(); | - | ||||||||||||
1446 | d->data->controlSocket->setParent(0); | - | ||||||||||||
1447 | d->bindData->localAddress = d->localAddress; | - | ||||||||||||
1448 | d->bindData->localPort = d->localPort; | - | ||||||||||||
1449 | sd = d->socketDescriptor; | - | ||||||||||||
1450 | socks5BindStore()->add(sd, d->bindData); | - | ||||||||||||
1451 | d->data = 0; | - | ||||||||||||
1452 | d->bindData = 0; | - | ||||||||||||
1453 | d->socketDescriptor = 0; | - | ||||||||||||
1454 | //### do something about this socket layer ... set it closed and an error about why ... | - | ||||||||||||
1455 | // reset state and local port/address | - | ||||||||||||
1456 | d->socks5State = QSocks5SocketEnginePrivate::Uninitialized; // ..?? | - | ||||||||||||
1457 | d->socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
1458 | break; executed 6 times by 3 tests: break; Executed by:
| 6 | ||||||||||||
1459 | case QSocks5SocketEnginePrivate::ControlSocketError: never executed: case QSocks5SocketEnginePrivate::ControlSocketError: | 0 | ||||||||||||
1460 | setError(QAbstractSocket::ProxyProtocolError, QLatin1String("Control socket error")); | - | ||||||||||||
1461 | break; never executed: break; | 0 | ||||||||||||
1462 | default: never executed: default: | 0 | ||||||||||||
1463 | setError(QAbstractSocket::ProxyProtocolError, QLatin1String("SOCKS5 proxy error")); | - | ||||||||||||
1464 | break; never executed: break; | 0 | ||||||||||||
1465 | } | - | ||||||||||||
1466 | return sd; executed 6 times by 3 tests: return sd; Executed by:
| 6 | ||||||||||||
1467 | } | - | ||||||||||||
1468 | - | |||||||||||||
1469 | void QSocks5SocketEngine::close() | - | ||||||||||||
1470 | { | - | ||||||||||||
1471 | QSOCKS5_Q_DEBUG << "close()"; dead code: QMessageLogger(__FILE__, 1471, __PRETTY_FUNCTION__).debug() << "close()"; | - | ||||||||||||
1472 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1473 | if (d->data && d->data->controlSocket) {
| 0-1008 | ||||||||||||
1474 | if (d->data->controlSocket->state() == QAbstractSocket::ConnectedState) {
| 298-710 | ||||||||||||
1475 | int msecs = 100; | - | ||||||||||||
1476 | QElapsedTimer stopWatch; | - | ||||||||||||
1477 | stopWatch.start(); | - | ||||||||||||
1478 | while (!d->data->controlSocket->bytesToWrite()) {
| 2-296 | ||||||||||||
1479 | if (!d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())))
| 0-296 | ||||||||||||
1480 | break; executed 296 times by 8 tests: break; Executed by:
| 296 | ||||||||||||
1481 | } never executed: end of block | 0 | ||||||||||||
1482 | } executed 298 times by 8 tests: end of block Executed by:
| 298 | ||||||||||||
1483 | d->data->controlSocket->close(); | - | ||||||||||||
1484 | } executed 1008 times by 8 tests: end of block Executed by:
| 1008 | ||||||||||||
1485 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1486 | if (d->udpData && d->udpData->udpSocket)
| 0-1013 | ||||||||||||
1487 | d->udpData->udpSocket->close(); never executed: d->udpData->udpSocket->close(); | 0 | ||||||||||||
1488 | #endif | - | ||||||||||||
1489 | } executed 1013 times by 8 tests: end of block Executed by:
| 1013 | ||||||||||||
1490 | - | |||||||||||||
1491 | qint64 QSocks5SocketEngine::bytesAvailable() const | - | ||||||||||||
1492 | { | - | ||||||||||||
1493 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1494 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode)
| 1-3287 | ||||||||||||
1495 | return d->connectData->readBuffer.size(); executed 3287 times by 6 tests: return d->connectData->readBuffer.size(); Executed by:
| 3287 | ||||||||||||
1496 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1497 | else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode
| 0-1 | ||||||||||||
1498 | && !d->udpData->pendingDatagrams.isEmpty())
| 0 | ||||||||||||
1499 | return d->udpData->pendingDatagrams.first().data.size(); never executed: return d->udpData->pendingDatagrams.first().data.size(); | 0 | ||||||||||||
1500 | #endif | - | ||||||||||||
1501 | return 0; executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||
1502 | } | - | ||||||||||||
1503 | - | |||||||||||||
1504 | qint64 QSocks5SocketEngine::read(char *data, qint64 maxlen) | - | ||||||||||||
1505 | { | - | ||||||||||||
1506 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1507 | QSOCKS5_Q_DEBUG << "read( , maxlen = " << maxlen << ')'; dead code: QMessageLogger(__FILE__, 1507, __PRETTY_FUNCTION__).debug() << "read( , maxlen = " << maxlen << ')'; | - | ||||||||||||
1508 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode) {
| 0-3290 | ||||||||||||
1509 | if (d->connectData->readBuffer.size() == 0) {
| 355-2935 | ||||||||||||
1510 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) {
| 2-353 | ||||||||||||
1511 | //imitate remote closed | - | ||||||||||||
1512 | close(); | - | ||||||||||||
1513 | setError(QAbstractSocket::RemoteHostClosedError, | - | ||||||||||||
1514 | QLatin1String("Remote host closed connection###")); | - | ||||||||||||
1515 | setState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
1516 | return -1; executed 353 times by 3 tests: return -1; Executed by:
| 353 | ||||||||||||
1517 | } else { | - | ||||||||||||
1518 | return 0; // nothing to be read executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||
1519 | } | - | ||||||||||||
1520 | } | - | ||||||||||||
1521 | qint64 copy = qMin<qint64>(d->connectData->readBuffer.size(), maxlen); | - | ||||||||||||
1522 | memcpy(data, d->connectData->readBuffer.constData(), copy); | - | ||||||||||||
1523 | d->connectData->readBuffer.remove(0, copy); | - | ||||||||||||
1524 | QSOCKS5_DEBUG << "read" << dump(QByteArray(data, copy)); dead code: QMessageLogger(__FILE__, 1524, __PRETTY_FUNCTION__).debug() << "read" << dump(QByteArray(data, copy)); | - | ||||||||||||
1525 | return copy; executed 2935 times by 6 tests: return copy; Executed by:
| 2935 | ||||||||||||
1526 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1527 | } else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) {
| 0 | ||||||||||||
1528 | return readDatagram(data, maxlen); never executed: return readDatagram(data, maxlen); | 0 | ||||||||||||
1529 | #endif | - | ||||||||||||
1530 | } | - | ||||||||||||
1531 | return 0; never executed: return 0; | 0 | ||||||||||||
1532 | } | - | ||||||||||||
1533 | - | |||||||||||||
1534 | qint64 QSocks5SocketEngine::write(const char *data, qint64 len) | - | ||||||||||||
1535 | { | - | ||||||||||||
1536 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1537 | QSOCKS5_Q_DEBUG << "write" << dump(QByteArray(data, len)); dead code: QMessageLogger(__FILE__, 1537, __PRETTY_FUNCTION__).debug() << "write" << dump(QByteArray(data, len)); | - | ||||||||||||
1538 | - | |||||||||||||
1539 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode) {
| 1-1887 | ||||||||||||
1540 | // clamp down the amount of bytes to transfer at once | - | ||||||||||||
1541 | len = qMin<qint64>(len, MaxWriteBufferSize) - d->data->controlSocket->bytesToWrite(); | - | ||||||||||||
1542 | if (len <= 0)
| 0-1887 | ||||||||||||
1543 | return 0; never executed: return 0; | 0 | ||||||||||||
1544 | - | |||||||||||||
1545 | QByteArray buf = QByteArray::fromRawData(data, len); | - | ||||||||||||
1546 | QByteArray sealedBuf; | - | ||||||||||||
1547 | if (!d->data->authenticator->seal(buf, &sealedBuf)) {
| 0-1887 | ||||||||||||
1548 | // ### Handle this error. | - | ||||||||||||
1549 | } never executed: end of block | 0 | ||||||||||||
1550 | - | |||||||||||||
1551 | qint64 written = d->data->controlSocket->write(sealedBuf); | - | ||||||||||||
1552 | if (written <= 0) {
| 0-1887 | ||||||||||||
1553 | QSOCKS5_Q_DEBUG << "native write returned" << written; dead code: QMessageLogger(__FILE__, 1553, __PRETTY_FUNCTION__).debug() << "native write returned" << written; | - | ||||||||||||
1554 | return written; never executed: return written; | 0 | ||||||||||||
1555 | } | - | ||||||||||||
1556 | d->data->controlSocket->waitForBytesWritten(0); | - | ||||||||||||
1557 | //NB: returning len rather than written for the OK case, because the "sealing" may increase the length | - | ||||||||||||
1558 | return len; executed 1887 times by 6 tests: return len; Executed by:
| 1887 | ||||||||||||
1559 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1560 | } else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) {
| 0-1 | ||||||||||||
1561 | // send to connected address | - | ||||||||||||
1562 | return writeDatagram(data, len, QIpPacketHeader(d->peerAddress, d->peerPort)); executed 1 time by 1 test: return writeDatagram(data, len, QIpPacketHeader(d->peerAddress, d->peerPort)); Executed by:
| 1 | ||||||||||||
1563 | #endif | - | ||||||||||||
1564 | } | - | ||||||||||||
1565 | //### set an error ??? | - | ||||||||||||
1566 | return -1; never executed: return -1; | 0 | ||||||||||||
1567 | } | - | ||||||||||||
1568 | - | |||||||||||||
1569 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1570 | #ifndef QT_NO_NETWORKINTERFACE | - | ||||||||||||
1571 | bool QSocks5SocketEngine::joinMulticastGroup(const QHostAddress &, | - | ||||||||||||
1572 | const QNetworkInterface &) | - | ||||||||||||
1573 | { | - | ||||||||||||
1574 | setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
1575 | QLatin1String("Operation on socket is not supported")); | - | ||||||||||||
1576 | return false; never executed: return false; | 0 | ||||||||||||
1577 | } | - | ||||||||||||
1578 | - | |||||||||||||
1579 | bool QSocks5SocketEngine::leaveMulticastGroup(const QHostAddress &, | - | ||||||||||||
1580 | const QNetworkInterface &) | - | ||||||||||||
1581 | { | - | ||||||||||||
1582 | setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
1583 | QLatin1String("Operation on socket is not supported")); | - | ||||||||||||
1584 | return false; never executed: return false; | 0 | ||||||||||||
1585 | } | - | ||||||||||||
1586 | - | |||||||||||||
1587 | - | |||||||||||||
1588 | QNetworkInterface QSocks5SocketEngine::multicastInterface() const | - | ||||||||||||
1589 | { | - | ||||||||||||
1590 | return QNetworkInterface(); never executed: return QNetworkInterface(); | 0 | ||||||||||||
1591 | } | - | ||||||||||||
1592 | - | |||||||||||||
1593 | bool QSocks5SocketEngine::setMulticastInterface(const QNetworkInterface &) | - | ||||||||||||
1594 | { | - | ||||||||||||
1595 | setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
1596 | QLatin1String("Operation on socket is not supported")); | - | ||||||||||||
1597 | return false; never executed: return false; | 0 | ||||||||||||
1598 | } | - | ||||||||||||
1599 | #endif // QT_NO_NETWORKINTERFACE | - | ||||||||||||
1600 | - | |||||||||||||
1601 | qint64 QSocks5SocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header, PacketHeaderOptions) | - | ||||||||||||
1602 | { | - | ||||||||||||
1603 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1604 | - | |||||||||||||
1605 | d->checkForDatagrams(); | - | ||||||||||||
1606 | - | |||||||||||||
1607 | if (d->udpData->pendingDatagrams.isEmpty())
| 0 | ||||||||||||
1608 | return 0; never executed: return 0; | 0 | ||||||||||||
1609 | - | |||||||||||||
1610 | QSocks5RevivedDatagram datagram = d->udpData->pendingDatagrams.dequeue(); | - | ||||||||||||
1611 | int copyLen = qMin<int>(maxlen, datagram.data.size()); | - | ||||||||||||
1612 | memcpy(data, datagram.data.constData(), copyLen); | - | ||||||||||||
1613 | header->senderAddress = datagram.address; | - | ||||||||||||
1614 | header->senderPort = datagram.port; | - | ||||||||||||
1615 | return copyLen; never executed: return copyLen; | 0 | ||||||||||||
1616 | } | - | ||||||||||||
1617 | - | |||||||||||||
1618 | qint64 QSocks5SocketEngine::writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header) | - | ||||||||||||
1619 | { | - | ||||||||||||
1620 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1621 | - | |||||||||||||
1622 | // it is possible to send with out first binding with udp, but socks5 requires a bind. | - | ||||||||||||
1623 | if (!d->data) {
| 0-3 | ||||||||||||
1624 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
1625 | // all udp needs to be bound | - | ||||||||||||
1626 | if (!bind(QHostAddress(QLatin1String("0.0.0.0")), 0)) {
| 0 | ||||||||||||
1627 | //### set error | - | ||||||||||||
1628 | return -1; never executed: return -1; | 0 | ||||||||||||
1629 | } | - | ||||||||||||
1630 | } never executed: end of block | 0 | ||||||||||||
1631 | - | |||||||||||||
1632 | QByteArray outBuf; | - | ||||||||||||
1633 | outBuf.reserve(270 + len); | - | ||||||||||||
1634 | outBuf[0] = 0x00; | - | ||||||||||||
1635 | outBuf[1] = 0x00; | - | ||||||||||||
1636 | outBuf[2] = 0x00; | - | ||||||||||||
1637 | if (!qt_socks5_set_host_address_and_port(header.destinationAddress, header.destinationPort, &outBuf)) {
| 0-3 | ||||||||||||
1638 | } never executed: end of block | 0 | ||||||||||||
1639 | outBuf += QByteArray(data, len); | - | ||||||||||||
1640 | QSOCKS5_DEBUG << "sending" << dump(outBuf); dead code: QMessageLogger(__FILE__, 1640, __PRETTY_FUNCTION__).debug() << "sending" << dump(outBuf); | - | ||||||||||||
1641 | QByteArray sealedBuf; | - | ||||||||||||
1642 | if (!d->data->authenticator->seal(outBuf, &sealedBuf)) {
| 0-3 | ||||||||||||
1643 | QSOCKS5_DEBUG << "sealing data failed"; dead code: QMessageLogger(__FILE__, 1643, __PRETTY_FUNCTION__).debug() << "sealing data failed"; | - | ||||||||||||
1644 | setError(QAbstractSocket::SocketAccessError, d->data->authenticator->errorString()); | - | ||||||||||||
1645 | return -1; never executed: return -1; | 0 | ||||||||||||
1646 | } | - | ||||||||||||
1647 | if (d->udpData->udpSocket->writeDatagram(sealedBuf, d->udpData->associateAddress, d->udpData->associatePort) != sealedBuf.size()) {
| 0-3 | ||||||||||||
1648 | //### try frgamenting | - | ||||||||||||
1649 | if (d->udpData->udpSocket->error() == QAbstractSocket::DatagramTooLargeError)
| 0 | ||||||||||||
1650 | setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); never executed: setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); | 0 | ||||||||||||
1651 | //### else maybe more serious error | - | ||||||||||||
1652 | return -1; never executed: return -1; | 0 | ||||||||||||
1653 | } | - | ||||||||||||
1654 | - | |||||||||||||
1655 | return len; executed 3 times by 1 test: return len; Executed by:
| 3 | ||||||||||||
1656 | } | - | ||||||||||||
1657 | - | |||||||||||||
1658 | bool QSocks5SocketEngine::hasPendingDatagrams() const | - | ||||||||||||
1659 | { | - | ||||||||||||
1660 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1661 | Q_INIT_CHECK(false); executed 1 time by 1 test: return false; Executed by:
| 0-1 | ||||||||||||
1662 | - | |||||||||||||
1663 | d->checkForDatagrams(); | - | ||||||||||||
1664 | - | |||||||||||||
1665 | return !d->udpData->pendingDatagrams.isEmpty(); never executed: return !d->udpData->pendingDatagrams.isEmpty(); | 0 | ||||||||||||
1666 | } | - | ||||||||||||
1667 | - | |||||||||||||
1668 | qint64 QSocks5SocketEngine::pendingDatagramSize() const | - | ||||||||||||
1669 | { | - | ||||||||||||
1670 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1671 | - | |||||||||||||
1672 | d->checkForDatagrams(); | - | ||||||||||||
1673 | - | |||||||||||||
1674 | if (!d->udpData->pendingDatagrams.isEmpty())
| 0 | ||||||||||||
1675 | return d->udpData->pendingDatagrams.head().data.size(); never executed: return d->udpData->pendingDatagrams.head().data.size(); | 0 | ||||||||||||
1676 | return 0; never executed: return 0; | 0 | ||||||||||||
1677 | } | - | ||||||||||||
1678 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
1679 | - | |||||||||||||
1680 | qint64 QSocks5SocketEngine::bytesToWrite() const | - | ||||||||||||
1681 | { | - | ||||||||||||
1682 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1683 | if (d->data && d->data->controlSocket) {
| 0-4689 | ||||||||||||
1684 | return d->data->controlSocket->bytesToWrite(); executed 4689 times by 5 tests: return d->data->controlSocket->bytesToWrite(); Executed by:
| 4689 | ||||||||||||
1685 | } else { | - | ||||||||||||
1686 | return 0; never executed: return 0; | 0 | ||||||||||||
1687 | } | - | ||||||||||||
1688 | } | - | ||||||||||||
1689 | - | |||||||||||||
1690 | int QSocks5SocketEngine::option(SocketOption option) const | - | ||||||||||||
1691 | { | - | ||||||||||||
1692 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1693 | if (d->data && d->data->controlSocket) {
| 0 | ||||||||||||
1694 | // convert the enum and call the real socket | - | ||||||||||||
1695 | if (option == QAbstractSocketEngine::LowDelayOption)
| 0 | ||||||||||||
1696 | return d->data->controlSocket->socketOption(QAbstractSocket::LowDelayOption).toInt(); never executed: return d->data->controlSocket->socketOption(QAbstractSocket::LowDelayOption).toInt(); | 0 | ||||||||||||
1697 | if (option == QAbstractSocketEngine::KeepAliveOption)
| 0 | ||||||||||||
1698 | return d->data->controlSocket->socketOption(QAbstractSocket::KeepAliveOption).toInt(); never executed: return d->data->controlSocket->socketOption(QAbstractSocket::KeepAliveOption).toInt(); | 0 | ||||||||||||
1699 | } never executed: end of block | 0 | ||||||||||||
1700 | return -1; never executed: return -1; | 0 | ||||||||||||
1701 | } | - | ||||||||||||
1702 | - | |||||||||||||
1703 | bool QSocks5SocketEngine::setOption(SocketOption option, int value) | - | ||||||||||||
1704 | { | - | ||||||||||||
1705 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1706 | if (d->data && d->data->controlSocket) {
| 0-345 | ||||||||||||
1707 | // convert the enum and call the real socket | - | ||||||||||||
1708 | if (option == QAbstractSocketEngine::LowDelayOption)
| 51-294 | ||||||||||||
1709 | d->data->controlSocket->setSocketOption(QAbstractSocket::LowDelayOption, value); executed 294 times by 1 test: d->data->controlSocket->setSocketOption(QAbstractSocket::LowDelayOption, value); Executed by:
| 294 | ||||||||||||
1710 | if (option == QAbstractSocketEngine::KeepAliveOption)
| 51-294 | ||||||||||||
1711 | d->data->controlSocket->setSocketOption(QAbstractSocket::KeepAliveOption, value); executed 51 times by 2 tests: d->data->controlSocket->setSocketOption(QAbstractSocket::KeepAliveOption, value); Executed by:
| 51 | ||||||||||||
1712 | return true; executed 345 times by 3 tests: return true; Executed by:
| 345 | ||||||||||||
1713 | } | - | ||||||||||||
1714 | return false; executed 12 times by 2 tests: return false; Executed by:
| 12 | ||||||||||||
1715 | } | - | ||||||||||||
1716 | - | |||||||||||||
1717 | bool QSocks5SocketEnginePrivate::waitForConnected(int msecs, bool *timedOut) | - | ||||||||||||
1718 | { | - | ||||||||||||
1719 | if (data->controlSocket->state() == QAbstractSocket::UnconnectedState)
| 0-494 | ||||||||||||
1720 | return false; never executed: return false; | 0 | ||||||||||||
1721 | - | |||||||||||||
1722 | const Socks5State wantedState = | - | ||||||||||||
1723 | mode == ConnectMode ? Connected :
| 18-476 | ||||||||||||
1724 | mode == BindMode ? BindSuccess :
| 3-15 | ||||||||||||
1725 | UdpAssociateSuccess; | - | ||||||||||||
1726 | - | |||||||||||||
1727 | QElapsedTimer stopWatch; | - | ||||||||||||
1728 | stopWatch.start(); | - | ||||||||||||
1729 | - | |||||||||||||
1730 | while (socks5State != wantedState) {
| 352-479 | ||||||||||||
1731 | if (!data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
| 15-337 | ||||||||||||
1732 | if (data->controlSocket->state() == QAbstractSocket::UnconnectedState)
| 7-8 | ||||||||||||
1733 | return true; executed 8 times by 4 tests: return true; Executed by:
| 8 | ||||||||||||
1734 | - | |||||||||||||
1735 | setErrorState(QSocks5SocketEnginePrivate::ControlSocketError); | - | ||||||||||||
1736 | if (timedOut && data->controlSocket->error() == QAbstractSocket::SocketTimeoutError)
| 0-6 | ||||||||||||
1737 | *timedOut = true; executed 6 times by 2 tests: *timedOut = true; Executed by:
| 6 | ||||||||||||
1738 | return false; executed 7 times by 3 tests: return false; Executed by:
| 7 | ||||||||||||
1739 | } | - | ||||||||||||
1740 | } executed 337 times by 7 tests: end of block Executed by:
| 337 | ||||||||||||
1741 | - | |||||||||||||
1742 | return true; executed 479 times by 5 tests: return true; Executed by:
| 479 | ||||||||||||
1743 | } | - | ||||||||||||
1744 | - | |||||||||||||
1745 | bool QSocks5SocketEngine::waitForRead(int msecs, bool *timedOut) | - | ||||||||||||
1746 | { | - | ||||||||||||
1747 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1748 | QSOCKS5_DEBUG << "waitForRead" << msecs; dead code: QMessageLogger(__FILE__, 1748, __PRETTY_FUNCTION__).debug() << "waitForRead" << msecs; | - | ||||||||||||
1749 | - | |||||||||||||
1750 | d->readNotificationActivated = false; | - | ||||||||||||
1751 | - | |||||||||||||
1752 | QElapsedTimer stopWatch; | - | ||||||||||||
1753 | stopWatch.start(); | - | ||||||||||||
1754 | - | |||||||||||||
1755 | // are we connected yet? | - | ||||||||||||
1756 | if (!d->waitForConnected(msecs, timedOut))
| 0-187 | ||||||||||||
1757 | return false; never executed: return false; | 0 | ||||||||||||
1758 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState)
| 0-187 | ||||||||||||
1759 | return true; never executed: return true; | 0 | ||||||||||||
1760 | - | |||||||||||||
1761 | // we're connected | - | ||||||||||||
1762 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode ||
| 3-184 | ||||||||||||
1763 | d->mode == QSocks5SocketEnginePrivate::BindMode) {
| 1-2 | ||||||||||||
1764 | while (!d->readNotificationActivated) {
| 179-186 | ||||||||||||
1765 | if (!d->data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
| 7-179 | ||||||||||||
1766 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState)
| 2-5 | ||||||||||||
1767 | return true; executed 5 times by 2 tests: return true; Executed by:
| 5 | ||||||||||||
1768 | - | |||||||||||||
1769 | setError(d->data->controlSocket->error(), d->data->controlSocket->errorString()); | - | ||||||||||||
1770 | if (timedOut && d->data->controlSocket->error() == QAbstractSocket::SocketTimeoutError)
| 0-2 | ||||||||||||
1771 | *timedOut = true; never executed: *timedOut = true; | 0 | ||||||||||||
1772 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||
1773 | } | - | ||||||||||||
1774 | } executed 179 times by 3 tests: end of block Executed by:
| 179 | ||||||||||||
1775 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1776 | } else { executed 179 times by 3 tests: end of block Executed by:
| 179 | ||||||||||||
1777 | while (!d->readNotificationActivated) {
| 0-1 | ||||||||||||
1778 | if (!d->udpData->udpSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
| 0-1 | ||||||||||||
1779 | setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); | - | ||||||||||||
1780 | if (timedOut && d->udpData->udpSocket->error() == QAbstractSocket::SocketTimeoutError)
| 0-1 | ||||||||||||
1781 | *timedOut = true; never executed: *timedOut = true; | 0 | ||||||||||||
1782 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
1783 | } | - | ||||||||||||
1784 | } never executed: end of block | 0 | ||||||||||||
1785 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
1786 | } never executed: end of block | 0 | ||||||||||||
1787 | - | |||||||||||||
1788 | - | |||||||||||||
1789 | bool ret = d->readNotificationActivated; | - | ||||||||||||
1790 | d->readNotificationActivated = false; | - | ||||||||||||
1791 | - | |||||||||||||
1792 | QSOCKS5_DEBUG << "waitForRead returned" << ret; dead code: QMessageLogger(__FILE__, 1792, __PRETTY_FUNCTION__).debug() << "waitForRead returned" << ret; | - | ||||||||||||
1793 | return ret; executed 179 times by 3 tests: return ret; Executed by:
| 179 | ||||||||||||
1794 | } | - | ||||||||||||
1795 | - | |||||||||||||
1796 | - | |||||||||||||
1797 | bool QSocks5SocketEngine::waitForWrite(int msecs, bool *timedOut) | - | ||||||||||||
1798 | { | - | ||||||||||||
1799 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1800 | QSOCKS5_DEBUG << "waitForWrite" << msecs; dead code: QMessageLogger(__FILE__, 1800, __PRETTY_FUNCTION__).debug() << "waitForWrite" << msecs; | - | ||||||||||||
1801 | - | |||||||||||||
1802 | QElapsedTimer stopWatch; | - | ||||||||||||
1803 | stopWatch.start(); | - | ||||||||||||
1804 | - | |||||||||||||
1805 | // are we connected yet? | - | ||||||||||||
1806 | if (!d->waitForConnected(msecs, timedOut))
| 6-286 | ||||||||||||
1807 | return false; executed 6 times by 2 tests: return false; Executed by:
| 6 | ||||||||||||
1808 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState)
| 7-279 | ||||||||||||
1809 | return true; executed 7 times by 3 tests: return true; Executed by:
| 7 | ||||||||||||
1810 | - | |||||||||||||
1811 | // we're connected | - | ||||||||||||
1812 | - | |||||||||||||
1813 | // flush any bytes we may still have buffered in the time that we have left | - | ||||||||||||
1814 | if (d->data->controlSocket->bytesToWrite())
| 0-279 | ||||||||||||
1815 | d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); never executed: d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); | 0 | ||||||||||||
1816 | while ((msecs == -1 || stopWatch.elapsed() < msecs)
| 0-273 | ||||||||||||
1817 | && d->data->controlSocket->state() == QAbstractSocket::ConnectedState
| 0-279 | ||||||||||||
1818 | && d->data->controlSocket->bytesToWrite() >= MaxWriteBufferSize)
| 0-279 | ||||||||||||
1819 | d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); never executed: d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); | 0 | ||||||||||||
1820 | return d->data->controlSocket->bytesToWrite() < MaxWriteBufferSize; executed 279 times by 4 tests: return d->data->controlSocket->bytesToWrite() < MaxWriteBufferSize; Executed by:
| 279 | ||||||||||||
1821 | } | - | ||||||||||||
1822 | - | |||||||||||||
1823 | bool QSocks5SocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, | - | ||||||||||||
1824 | bool checkRead, bool checkWrite, | - | ||||||||||||
1825 | int msecs, bool *timedOut) | - | ||||||||||||
1826 | { | - | ||||||||||||
1827 | Q_UNUSED(checkRead); | - | ||||||||||||
1828 | if (!checkWrite) {
| 174-184 | ||||||||||||
1829 | bool canRead = waitForRead(msecs, timedOut); | - | ||||||||||||
1830 | if (readyToRead)
| 0-174 | ||||||||||||
1831 | *readyToRead = canRead; executed 174 times by 2 tests: *readyToRead = canRead; Executed by:
| 174 | ||||||||||||
1832 | return canRead; executed 174 times by 2 tests: return canRead; Executed by:
| 174 | ||||||||||||
1833 | } | - | ||||||||||||
1834 | - | |||||||||||||
1835 | bool canWrite = waitForWrite(msecs, timedOut); | - | ||||||||||||
1836 | if (readyToWrite)
| 0-184 | ||||||||||||
1837 | *readyToWrite = canWrite; executed 184 times by 1 test: *readyToWrite = canWrite; Executed by:
| 184 | ||||||||||||
1838 | return canWrite; executed 184 times by 1 test: return canWrite; Executed by:
| 184 | ||||||||||||
1839 | } | - | ||||||||||||
1840 | - | |||||||||||||
1841 | bool QSocks5SocketEngine::isReadNotificationEnabled() const | - | ||||||||||||
1842 | { | - | ||||||||||||
1843 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1844 | return d->readNotificationEnabled; executed 3047 times by 5 tests: return d->readNotificationEnabled; Executed by:
| 3047 | ||||||||||||
1845 | } | - | ||||||||||||
1846 | - | |||||||||||||
1847 | void QSocks5SocketEngine::setReadNotificationEnabled(bool enable) | - | ||||||||||||
1848 | { | - | ||||||||||||
1849 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1850 | - | |||||||||||||
1851 | QSOCKS5_Q_DEBUG << "setReadNotificationEnabled(" << enable << ')'; dead code: QMessageLogger(__FILE__, 1851, __PRETTY_FUNCTION__).debug() << "setReadNotificationEnabled(" << enable << ')'; | - | ||||||||||||
1852 | - | |||||||||||||
1853 | bool emitSignal = false; | - | ||||||||||||
1854 | if (!d->readNotificationEnabled
| 786-3238 | ||||||||||||
1855 | && enable) {
| 90-696 | ||||||||||||
1856 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode)
| 10-686 | ||||||||||||
1857 | emitSignal = !d->connectData->readBuffer.isEmpty(); executed 686 times by 7 tests: emitSignal = !d->connectData->readBuffer.isEmpty(); Executed by:
| 686 | ||||||||||||
1858 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
1859 | else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode)
| 0-10 | ||||||||||||
1860 | emitSignal = !d->udpData->pendingDatagrams.isEmpty(); never executed: emitSignal = !d->udpData->pendingDatagrams.isEmpty(); | 0 | ||||||||||||
1861 | #endif | - | ||||||||||||
1862 | else if (d->mode == QSocks5SocketEnginePrivate::BindMode
| 0-10 | ||||||||||||
1863 | && d->socketState == QAbstractSocket::ListeningState
| 0-10 | ||||||||||||
1864 | && d->socks5State == QSocks5SocketEnginePrivate::BindSuccess)
| 0-10 | ||||||||||||
1865 | emitSignal = true; executed 10 times by 2 tests: emitSignal = true; Executed by:
| 10 | ||||||||||||
1866 | } executed 696 times by 7 tests: end of block Executed by:
| 696 | ||||||||||||
1867 | - | |||||||||||||
1868 | d->readNotificationEnabled = enable; | - | ||||||||||||
1869 | - | |||||||||||||
1870 | if (emitSignal)
| 10-4014 | ||||||||||||
1871 | d->emitReadNotification(); executed 10 times by 2 tests: d->emitReadNotification(); Executed by:
| 10 | ||||||||||||
1872 | } executed 4024 times by 7 tests: end of block Executed by:
| 4024 | ||||||||||||
1873 | - | |||||||||||||
1874 | bool QSocks5SocketEngine::isWriteNotificationEnabled() const | - | ||||||||||||
1875 | { | - | ||||||||||||
1876 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1877 | return d->writeNotificationEnabled; executed 1924 times by 5 tests: return d->writeNotificationEnabled; Executed by:
| 1924 | ||||||||||||
1878 | } | - | ||||||||||||
1879 | - | |||||||||||||
1880 | void QSocks5SocketEngine::setWriteNotificationEnabled(bool enable) | - | ||||||||||||
1881 | { | - | ||||||||||||
1882 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1883 | d->writeNotificationEnabled = enable; | - | ||||||||||||
1884 | if (enable && d->socketState == QAbstractSocket::ConnectedState) {
| 241-4258 | ||||||||||||
1885 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode && d->data->controlSocket->bytesToWrite())
| 0-2553 | ||||||||||||
1886 | return; // will be emitted as a result of bytes written executed 33 times by 2 tests: return; Executed by:
| 33 | ||||||||||||
1887 | d->emitWriteNotification(); | - | ||||||||||||
1888 | d->writeNotificationActivated = false; | - | ||||||||||||
1889 | } executed 2520 times by 7 tests: end of block Executed by:
| 2520 | ||||||||||||
1890 | } executed 7019 times by 7 tests: end of block Executed by:
| 7019 | ||||||||||||
1891 | - | |||||||||||||
1892 | bool QSocks5SocketEngine::isExceptionNotificationEnabled() const | - | ||||||||||||
1893 | { | - | ||||||||||||
1894 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
1895 | return d->exceptNotificationEnabled; executed 81 times by 3 tests: return d->exceptNotificationEnabled; Executed by:
| 81 | ||||||||||||
1896 | } | - | ||||||||||||
1897 | - | |||||||||||||
1898 | void QSocks5SocketEngine::setExceptionNotificationEnabled(bool enable) | - | ||||||||||||
1899 | { | - | ||||||||||||
1900 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
1901 | d->exceptNotificationEnabled = enable; | - | ||||||||||||
1902 | } executed 162 times by 3 tests: end of block Executed by:
| 162 | ||||||||||||
1903 | - | |||||||||||||
1904 | QAbstractSocketEngine * | - | ||||||||||||
1905 | QSocks5SocketEngineHandler::createSocketEngine(QAbstractSocket::SocketType socketType, | - | ||||||||||||
1906 | const QNetworkProxy &proxy, QObject *parent) | - | ||||||||||||
1907 | { | - | ||||||||||||
1908 | Q_UNUSED(socketType); | - | ||||||||||||
1909 | - | |||||||||||||
1910 | // proxy type must have been resolved by now | - | ||||||||||||
1911 | if (proxy.type() != QNetworkProxy::Socks5Proxy) {
| 711-4949 | ||||||||||||
1912 | QSOCKS5_DEBUG << "not proxying"; dead code: QMessageLogger(__FILE__, 1912, __PRETTY_FUNCTION__).debug() << "not proxying"; | - | ||||||||||||
1913 | return 0; executed 4949 times by 29 tests: return 0; Executed by:
| 4949 | ||||||||||||
1914 | } | - | ||||||||||||
1915 | QScopedPointer<QSocks5SocketEngine> engine(new QSocks5SocketEngine(parent)); | - | ||||||||||||
1916 | engine->setProxy(proxy); | - | ||||||||||||
1917 | return engine.take(); executed 711 times by 9 tests: return engine.take(); Executed by:
| 711 | ||||||||||||
1918 | } | - | ||||||||||||
1919 | - | |||||||||||||
1920 | QAbstractSocketEngine *QSocks5SocketEngineHandler::createSocketEngine(qintptr socketDescriptor, QObject *parent) | - | ||||||||||||
1921 | { | - | ||||||||||||
1922 | QSOCKS5_DEBUG << "createSocketEngine" << socketDescriptor; dead code: QMessageLogger(__FILE__, 1922, __PRETTY_FUNCTION__).debug() << "createSocketEngine" << socketDescriptor; | - | ||||||||||||
1923 | if (socks5BindStore()->contains(socketDescriptor)) {
| 5-988 | ||||||||||||
1924 | QSOCKS5_DEBUG << "bind store contains" << socketDescriptor; dead code: QMessageLogger(__FILE__, 1924, __PRETTY_FUNCTION__).debug() << "bind store contains" << socketDescriptor; | - | ||||||||||||
1925 | return new QSocks5SocketEngine(parent); executed 5 times by 2 tests: return new QSocks5SocketEngine(parent); Executed by:
| 5 | ||||||||||||
1926 | } | - | ||||||||||||
1927 | return 0; executed 988 times by 18 tests: return 0; Executed by:
| 988 | ||||||||||||
1928 | } | - | ||||||||||||
1929 | - | |||||||||||||
1930 | #endif // QT_NO_SOCKS5 | - | ||||||||||||
1931 | - | |||||||||||||
1932 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |