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