| 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 inline QString s5StateToString(QSocks5SocketEnginePrivate::Socks5State) { return QString(); } | - | ||||||||||||
| 147 | static inline QString dump(const QByteArray &) { return QString(); } | - | ||||||||||||
| 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) { | - | ||||||||||||
| 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) { | - | ||||||||||||
| 171 | data.ipv6 = address.toIPv6Address(); | - | ||||||||||||
| 172 | pBuf->append(S5_IP_V6); | - | ||||||||||||
| 173 | pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.ipv6)); | - | ||||||||||||
| 174 | } else { | - | ||||||||||||
| 175 | return false; | - | ||||||||||||
| 176 | } | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | // add port | - | ||||||||||||
| 179 | data.port = qToBigEndian<quint16>(port); | - | ||||||||||||
| 180 | pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.port)); | - | ||||||||||||
| 181 | return true; | - | ||||||||||||
| 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) | - | ||||||||||||
| 195 | return false; | - | ||||||||||||
| 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; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 227 | QSOCKS5_DEBUG << "need more data address/port"; dead code: QMessageLogger(__FILE__, 227, __PRETTY_FUNCTION__).debug() << "need more data address/port"; | - | ||||||||||||
| 228 | return 0; | - | ||||||||||||
| 229 | } | - | ||||||||||||
| 230 | if (pBuf[pos] == S5_IP_V4) { | - | ||||||||||||
| 231 | pos++; | - | ||||||||||||
| 232 | if (buf.size() - pos < 4) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 235 | } | - | ||||||||||||
| 236 | address.setAddress(qFromBigEndian<quint32>(&pBuf[pos])); | - | ||||||||||||
| 237 | pos += 4; | - | ||||||||||||
| 238 | ret = 1; | - | ||||||||||||
| 239 | } else if (pBuf[pos] == S5_IP_V6) { | - | ||||||||||||
| 240 | pos++; | - | ||||||||||||
| 241 | if (buf.size() - pos < 16) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 244 | } | - | ||||||||||||
| 245 | QIPv6Address add; | - | ||||||||||||
| 246 | for (int i = 0; i < 16; ++i) | - | ||||||||||||
| 247 | add[i] = buf[pos++]; | - | ||||||||||||
| 248 | address.setAddress(add); | - | ||||||||||||
| 249 | ret = 1; | - | ||||||||||||
| 250 | } else if (pBuf[pos] == S5_DOMAINNAME){ | - | ||||||||||||
| 251 | // just skip it | - | ||||||||||||
| 252 | pos++; | - | ||||||||||||
| 253 | qDebug() << "skipping hostname of len" << uint(pBuf[pos]); | - | ||||||||||||
| 254 | pos += uchar(pBuf[pos]); | - | ||||||||||||
| 255 | } else { | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 259 | - | |||||||||||||
| 260 | if (ret == 1) { | - | ||||||||||||
| 261 | if (buf.size() - pos < 2) { | - | ||||||||||||
| 262 | QSOCKS5_DEBUG << "need more data for port"; dead code: QMessageLogger(__FILE__, 262, __PRETTY_FUNCTION__).debug() << "need more data for port"; | - | ||||||||||||
| 263 | return 0; | - | ||||||||||||
| 264 | } | - | ||||||||||||
| 265 | port = qFromBigEndian<quint16>(&pBuf[pos]); | - | ||||||||||||
| 266 | pos += 2; | - | ||||||||||||
| 267 | } | - | ||||||||||||
| 268 | - | |||||||||||||
| 269 | if (ret == 1) { | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 275 | - | |||||||||||||
| 276 | return ret; | - | ||||||||||||
| 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) | - | ||||||||||||
| 337 | - | |||||||||||||
| 338 | QSocks5BindStore::QSocks5BindStore() | - | ||||||||||||
| 339 | : mutex(QMutex::Recursive) | - | ||||||||||||
| 340 | , sweepTimerId(-1) | - | ||||||||||||
| 341 | { | - | ||||||||||||
| 342 | QCoreApplication *app = QCoreApplication::instance(); | - | ||||||||||||
| 343 | if (app && app->thread() != thread()) | - | ||||||||||||
| 344 | moveToThread(app->thread()); | - | ||||||||||||
| 345 | } | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 355 | // qDebug("delete it"); | - | ||||||||||||
| 356 | } | - | ||||||||||||
| 357 | bindData->timeStamp.start(); | - | ||||||||||||
| 358 | store.insert(socketDescriptor, bindData); | - | ||||||||||||
| 359 | // start sweep timer if not started | - | ||||||||||||
| 360 | if (sweepTimerId == -1) | - | ||||||||||||
| 361 | sweepTimerId = startTimer(60000); | - | ||||||||||||
| 362 | } | - | ||||||||||||
| 363 | - | |||||||||||||
| 364 | bool QSocks5BindStore::contains(qintptr socketDescriptor) | - | ||||||||||||
| 365 | { | - | ||||||||||||
| 366 | QMutexLocker lock(&mutex); | - | ||||||||||||
| 367 | return store.contains(socketDescriptor); | - | ||||||||||||
| 368 | } | - | ||||||||||||
| 369 | - | |||||||||||||
| 370 | QSocks5BindData *QSocks5BindStore::retrieve(qintptr socketDescriptor) | - | ||||||||||||
| 371 | { | - | ||||||||||||
| 372 | QMutexLocker lock(&mutex); | - | ||||||||||||
| 373 | if (!const auto it = store.containsconstFind(socketDescriptor))); | - | ||||||||||||
| 374 | if (it == store.cend())
| 0-6 | ||||||||||||
| 375 | return 0; never executed: return 0; | 0 | ||||||||||||
| 376 | QSocks5BindData *bindData = it.value(); | - | ||||||||||||
| 377 | store.takeerase(socketDescriptorit); | - | ||||||||||||
| 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 blockExecuted 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 blockExecuted 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 | QMutableHashIterator<int, QSocks5BindData *> it(store);
| 0 | ||||||||||||
while
| ||||||||||||||
it
| ||||||||||||||
| 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.remove();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; | - | ||||||||||||
| 421 | } | - | ||||||||||||
| 422 | - | |||||||||||||
| 423 | bool QSocks5Authenticator::beginAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
| 424 | { | - | ||||||||||||
| 425 | Q_UNUSED(socket); | - | ||||||||||||
| 426 | *completed = true; | - | ||||||||||||
| 427 | return true; | - | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | bool QSocks5Authenticator::continueAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
| 431 | { | - | ||||||||||||
| 432 | Q_UNUSED(socket); | - | ||||||||||||
| 433 | *completed = true; | - | ||||||||||||
| 434 | return true; | - | ||||||||||||
| 435 | } | - | ||||||||||||
| 436 | - | |||||||||||||
| 437 | bool QSocks5Authenticator::seal(const QByteArray &buf, QByteArray *sealedBuf) | - | ||||||||||||
| 438 | { | - | ||||||||||||
| 439 | *sealedBuf = buf; | - | ||||||||||||
| 440 | return true; | - | ||||||||||||
| 441 | } | - | ||||||||||||
| 442 | - | |||||||||||||
| 443 | bool QSocks5Authenticator::unSeal(const QByteArray &sealedBuf, QByteArray *buf) | - | ||||||||||||
| 444 | { | - | ||||||||||||
| 445 | *buf = sealedBuf; | - | ||||||||||||
| 446 | return true; | - | ||||||||||||
| 447 | } | - | ||||||||||||
| 448 | - | |||||||||||||
| 449 | bool QSocks5Authenticator::unSeal(QTcpSocket *sealedSocket, QByteArray *buf) | - | ||||||||||||
| 450 | { | - | ||||||||||||
| 451 | return unSeal(sealedSocket->readAll(), buf); | - | ||||||||||||
| 452 | } | - | ||||||||||||
| 453 | - | |||||||||||||
| 454 | QSocks5PasswordAuthenticator::QSocks5PasswordAuthenticator(const QString &userName, const QString &password) | - | ||||||||||||
| 455 | { | - | ||||||||||||
| 456 | this->userName = userName; | - | ||||||||||||
| 457 | this->password = password; | - | ||||||||||||
| 458 | } | - | ||||||||||||
| 459 | - | |||||||||||||
| 460 | char QSocks5PasswordAuthenticator::methodId() | - | ||||||||||||
| 461 | { | - | ||||||||||||
| 462 | return 0x02; | - | ||||||||||||
| 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(); | - | ||||||||||||
| 480 | } | - | ||||||||||||
| 481 | - | |||||||||||||
| 482 | bool QSocks5PasswordAuthenticator::continueAuthenticate(QTcpSocket *socket, bool *completed) | - | ||||||||||||
| 483 | { | - | ||||||||||||
| 484 | *completed = false; | - | ||||||||||||
| 485 | - | |||||||||||||
| 486 | if (socket->bytesAvailable() < 2) | - | ||||||||||||
| 487 | return true; | - | ||||||||||||
| 488 | - | |||||||||||||
| 489 | QByteArray buf = socket->read(2); | - | ||||||||||||
| 490 | if (buf.at(0) == S5_PASSWORDAUTH_VERSION && buf.at(1) == 0x00) { | - | ||||||||||||
| 491 | *completed = true; | - | ||||||||||||
| 492 | return true; | - | ||||||||||||
| 493 | } | - | ||||||||||||
| 494 | - | |||||||||||||
| 495 | // must disconnect | - | ||||||||||||
| 496 | socket->close(); | - | ||||||||||||
| 497 | return false; | - | ||||||||||||
| 498 | } | - | ||||||||||||
| 499 | - | |||||||||||||
| 500 | QString QSocks5PasswordAuthenticator::errorString() | - | ||||||||||||
| 501 | { | - | ||||||||||||
| 502 | return QLatin1String("Socks5 user name or password incorrect"); | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 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) { | - | ||||||||||||
| 538 | connectData = new QSocks5ConnectData; | - | ||||||||||||
| 539 | data = connectData; | - | ||||||||||||
| 540 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 541 | } else if (mode == UdpAssociateMode) { | - | ||||||||||||
| 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) { | - | ||||||||||||
| 554 | bindData = new QSocks5BindData; | - | ||||||||||||
| 555 | data = bindData; | - | ||||||||||||
| 556 | } | - | ||||||||||||
| 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()) { | - | ||||||||||||
| 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 { | - | ||||||||||||
| 582 | QSOCKS5_D_DEBUG << "not using authentication"; dead code: QMessageLogger(__FILE__, 582, __PRETTY_FUNCTION__).debug() << "not using authentication"; | - | ||||||||||||
| 583 | data->authenticator = new QSocks5Authenticator(); | - | ||||||||||||
| 584 | } | - | ||||||||||||
| 585 | } | - | ||||||||||||
| 586 | - | |||||||||||||
| 587 | void QSocks5SocketEnginePrivate::setErrorState(Socks5State state, const QString &extraMessage) | - | ||||||||||||
| 588 | { | - | ||||||||||||
| 589 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 590 | - | |||||||||||||
| 591 | switch (state) { | - | ||||||||||||
| 592 | case Uninitialized: | - | ||||||||||||
| 593 | case Authenticating: | - | ||||||||||||
| 594 | case AuthenticationMethodsSent: | - | ||||||||||||
| 595 | case RequestMethodSent: | - | ||||||||||||
| 596 | case Connected: | - | ||||||||||||
| 597 | case UdpAssociateSuccess: | - | ||||||||||||
| 598 | case BindSuccess: | - | ||||||||||||
| 599 | // these aren't error states | - | ||||||||||||
| 600 | return; | - | ||||||||||||
| 601 | - | |||||||||||||
| 602 | case ConnectError: | - | ||||||||||||
| 603 | case ControlSocketError: { | - | ||||||||||||
| 604 | QAbstractSocket::SocketError controlSocketError = data->controlSocket->error(); | - | ||||||||||||
| 605 | if (socks5State != Connected) { | - | ||||||||||||
| 606 | switch (controlSocketError) { | - | ||||||||||||
| 607 | case QAbstractSocket::ConnectionRefusedError: | - | ||||||||||||
| 608 | q->setError(QAbstractSocket::ProxyConnectionRefusedError, | - | ||||||||||||
| 609 | QSocks5SocketEngine::tr("Connection to proxy refused")); | - | ||||||||||||
| 610 | break; | - | ||||||||||||
| 611 | case QAbstractSocket::RemoteHostClosedError: | - | ||||||||||||
| 612 | q->setError(QAbstractSocket::ProxyConnectionClosedError, | - | ||||||||||||
| 613 | QSocks5SocketEngine::tr("Connection to proxy closed prematurely")); | - | ||||||||||||
| 614 | break; | - | ||||||||||||
| 615 | case QAbstractSocket::HostNotFoundError: | - | ||||||||||||
| 616 | q->setError(QAbstractSocket::ProxyNotFoundError, | - | ||||||||||||
| 617 | QSocks5SocketEngine::tr("Proxy host not found")); | - | ||||||||||||
| 618 | break; | - | ||||||||||||
| 619 | case QAbstractSocket::SocketTimeoutError: | - | ||||||||||||
| 620 | if (state == ConnectError) { | - | ||||||||||||
| 621 | q->setError(QAbstractSocket::ProxyConnectionTimeoutError, | - | ||||||||||||
| 622 | QSocks5SocketEngine::tr("Connection to proxy timed out")); | - | ||||||||||||
| 623 | break; | - | ||||||||||||
| 624 | } | - | ||||||||||||
| 625 | /* fall through */ | - | ||||||||||||
| 626 | default: | - | ||||||||||||
| 627 | q->setError(controlSocketError, data->controlSocket->errorString()); | - | ||||||||||||
| 628 | break; | - | ||||||||||||
| 629 | } | - | ||||||||||||
| 630 | } else { | - | ||||||||||||
| 631 | q->setError(controlSocketError, data->controlSocket->errorString()); | - | ||||||||||||
| 632 | } | - | ||||||||||||
| 633 | break; | - | ||||||||||||
| 634 | } | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | case AuthenticatingError: | - | ||||||||||||
| 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; | - | ||||||||||||
| 642 | - | |||||||||||||
| 643 | case RequestError: | - | ||||||||||||
| 644 | // error code set by caller (overload) | - | ||||||||||||
| 645 | break; | - | ||||||||||||
| 646 | - | |||||||||||||
| 647 | case SocksError: | - | ||||||||||||
| 648 | q->setError(QAbstractSocket::ProxyProtocolError, | - | ||||||||||||
| 649 | QSocks5SocketEngine::tr("SOCKS version 5 protocol error")); | - | ||||||||||||
| 650 | break; | - | ||||||||||||
| 651 | - | |||||||||||||
| 652 | case HostNameLookupError: | - | ||||||||||||
| 653 | q->setError(QAbstractSocket::HostNotFoundError, | - | ||||||||||||
| 654 | QAbstractSocket::tr("Host not found")); | - | ||||||||||||
| 655 | break; | - | ||||||||||||
| 656 | } | - | ||||||||||||
| 657 | - | |||||||||||||
| 658 | q->setState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
| 659 | socks5State = state; | - | ||||||||||||
| 660 | } | - | ||||||||||||
| 661 | - | |||||||||||||
| 662 | void QSocks5SocketEnginePrivate::setErrorState(Socks5State state, Socks5Error socks5error) | - | ||||||||||||
| 663 | { | - | ||||||||||||
| 664 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 665 | switch (socks5error) { | - | ||||||||||||
| 666 | case SocksFailure: | - | ||||||||||||
| 667 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
| 668 | QSocks5SocketEngine::tr("General SOCKSv5 server failure")); | - | ||||||||||||
| 669 | break; | - | ||||||||||||
| 670 | case ConnectionNotAllowed: | - | ||||||||||||
| 671 | q->setError(QAbstractSocket::SocketAccessError, | - | ||||||||||||
| 672 | QSocks5SocketEngine::tr("Connection not allowed by SOCKSv5 server")); | - | ||||||||||||
| 673 | break; | - | ||||||||||||
| 674 | case NetworkUnreachable: | - | ||||||||||||
| 675 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
| 676 | QAbstractSocket::tr("Network unreachable")); | - | ||||||||||||
| 677 | break; | - | ||||||||||||
| 678 | case HostUnreachable: | - | ||||||||||||
| 679 | q->setError(QAbstractSocket::HostNotFoundError, | - | ||||||||||||
| 680 | QAbstractSocket::tr("Host not found")); | - | ||||||||||||
| 681 | break; | - | ||||||||||||
| 682 | case ConnectionRefused: | - | ||||||||||||
| 683 | q->setError(QAbstractSocket::ConnectionRefusedError, | - | ||||||||||||
| 684 | QAbstractSocket::tr("Connection refused")); | - | ||||||||||||
| 685 | break; | - | ||||||||||||
| 686 | case TTLExpired: | - | ||||||||||||
| 687 | q->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
| 688 | QSocks5SocketEngine::tr("TTL expired")); | - | ||||||||||||
| 689 | break; | - | ||||||||||||
| 690 | case CommandNotSupported: | - | ||||||||||||
| 691 | q->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
| 692 | QSocks5SocketEngine::tr("SOCKSv5 command not supported")); | - | ||||||||||||
| 693 | break; | - | ||||||||||||
| 694 | case AddressTypeNotSupported: | - | ||||||||||||
| 695 | q->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
| 696 | QSocks5SocketEngine::tr("Address type not supported")); | - | ||||||||||||
| 697 | break; | - | ||||||||||||
| 698 | - | |||||||||||||
| 699 | default: | - | ||||||||||||
| 700 | q->setError(QAbstractSocket::UnknownSocketError, | - | ||||||||||||
| 701 | QSocks5SocketEngine::tr("Unknown SOCKSv5 proxy error code 0x%1").arg(int(socks5error), 16)); | - | ||||||||||||
| 702 | break; | - | ||||||||||||
| 703 | } | - | ||||||||||||
| 704 | - | |||||||||||||
| 705 | setErrorState(state, QString()); | - | ||||||||||||
| 706 | } | - | ||||||||||||
| 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()) { | - | ||||||||||||
| 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 { | - | ||||||||||||
| 732 | // authentication failure | - | ||||||||||||
| 733 | - | |||||||||||||
| 734 | setErrorState(AuthenticatingError); | - | ||||||||||||
| 735 | data->controlSocket->close(); | - | ||||||||||||
| 736 | emitConnectionNotification(); | - | ||||||||||||
| 737 | } | - | ||||||||||||
| 738 | } | - | ||||||||||||
| 739 | - | |||||||||||||
| 740 | void QSocks5SocketEnginePrivate::parseAuthenticationMethodReply() | - | ||||||||||||
| 741 | { | - | ||||||||||||
| 742 | // not enough data to begin | - | ||||||||||||
| 743 | if (data->controlSocket->bytesAvailable() < 2) | - | ||||||||||||
| 744 | return; | - | ||||||||||||
| 745 | - | |||||||||||||
| 746 | QByteArray buf = data->controlSocket->read(2); | - | ||||||||||||
| 747 | if (buf.at(0) != S5_VERSION_5) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 753 | } | - | ||||||||||||
| 754 | - | |||||||||||||
| 755 | bool authComplete = false; | - | ||||||||||||
| 756 | if (uchar(buf.at(1)) == S5_AUTHMETHOD_NONE) { | - | ||||||||||||
| 757 | authComplete = true; | - | ||||||||||||
| 758 | } else if (uchar(buf.at(1)) == S5_AUTHMETHOD_NOTACCEPTABLE) { | - | ||||||||||||
| 759 | reauthenticate(); | - | ||||||||||||
| 760 | return; | - | ||||||||||||
| 761 | } else if (buf.at(1) != data->authenticator->methodId() | - | ||||||||||||
| 762 | || !data->authenticator->beginAuthenticate(data->controlSocket, &authComplete)) { | - | ||||||||||||
| 763 | setErrorState(AuthenticatingError, QLatin1String("Socks5 host did not support authentication method.")); | - | ||||||||||||
| 764 | socketError = QAbstractSocket::SocketAccessError; // change the socket error | - | ||||||||||||
| 765 | emitConnectionNotification(); | - | ||||||||||||
| 766 | return; | - | ||||||||||||
| 767 | } | - | ||||||||||||
| 768 | - | |||||||||||||
| 769 | if (authComplete) | - | ||||||||||||
| 770 | sendRequestMethod(); | - | ||||||||||||
| 771 | else | - | ||||||||||||
| 772 | socks5State = Authenticating; | - | ||||||||||||
| 773 | } | - | ||||||||||||
| 774 | - | |||||||||||||
| 775 | void QSocks5SocketEnginePrivate::parseAuthenticatingReply() | - | ||||||||||||
| 776 | { | - | ||||||||||||
| 777 | bool authComplete = false; | - | ||||||||||||
| 778 | if (!data->authenticator->continueAuthenticate(data->controlSocket, &authComplete)) { | - | ||||||||||||
| 779 | reauthenticate(); | - | ||||||||||||
| 780 | return; | - | ||||||||||||
| 781 | } | - | ||||||||||||
| 782 | if (authComplete) | - | ||||||||||||
| 783 | sendRequestMethod(); | - | ||||||||||||
| 784 | } | - | ||||||||||||
| 785 | - | |||||||||||||
| 786 | void QSocks5SocketEnginePrivate::sendRequestMethod() | - | ||||||||||||
| 787 | { | - | ||||||||||||
| 788 | QHostAddress address; | - | ||||||||||||
| 789 | quint16 port = 0; | - | ||||||||||||
| 790 | char command = 0; | - | ||||||||||||
| 791 | if (mode == ConnectMode) { | - | ||||||||||||
| 792 | command = S5_CONNECT; | - | ||||||||||||
| 793 | address = peerAddress; | - | ||||||||||||
| 794 | port = peerPort; | - | ||||||||||||
| 795 | } else if (mode == BindMode) { | - | ||||||||||||
| 796 | command = S5_BIND; | - | ||||||||||||
| 797 | address = localAddress; | - | ||||||||||||
| 798 | port = localPort; | - | ||||||||||||
| 799 | } else { | - | ||||||||||||
| 800 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 801 | command = S5_UDP_ASSOCIATE; | - | ||||||||||||
| 802 | address = localAddress; //data->controlSocket->localAddress(); | - | ||||||||||||
| 803 | port = localPort; | - | ||||||||||||
| 804 | #endif | - | ||||||||||||
| 805 | } | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 816 | } else if (!peerName.isEmpty() && !qt_socks5_set_host_name_and_port(peerName, port, &buf)) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 824 | // ### Handle this error. | - | ||||||||||||
| 825 | } | - | ||||||||||||
| 826 | data->controlSocket->write(sealedBuf); | - | ||||||||||||
| 827 | data->controlSocket->flush(); | - | ||||||||||||
| 828 | socks5State = RequestMethodSent; | - | ||||||||||||
| 829 | } | - | ||||||||||||
| 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 blockExecuted 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 blockExecuted by:
| 4 | ||||||||||||
| 866 | setErrorState(RequestError, socks5Error); | - | ||||||||||||
| 867 | } executed 17 times by 3 tests: end of blockExecuted 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 blockExecuted 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 blockExecuted by:
| 2-634 | ||||||||||||
| 898 | socks5State = BindSuccess; | - | ||||||||||||
| 899 | q->setState(QAbstractSocket::ListeningState); | - | ||||||||||||
| 900 | } else { executed 11 times by 3 tests: end of blockExecuted by:
| 11 | ||||||||||||
| 901 | socks5State = UdpAssociateSuccess; | - | ||||||||||||
| 902 | } executed 2 times by 1 test: end of blockExecuted 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 blockExecuted by:
| 2 | ||||||||||||
| 910 | // got an error | - | ||||||||||||
| 911 | data->controlSocket->close(); | - | ||||||||||||
| 912 | emitConnectionNotification(); | - | ||||||||||||
| 913 | } executed 22 times by 4 tests: end of blockExecuted by:
| 22 | ||||||||||||
| 914 | } | - | ||||||||||||
| 915 | - | |||||||||||||
| 916 | void QSocks5SocketEnginePrivate::_q_emitPendingReadNotification() | - | ||||||||||||
| 917 | { | - | ||||||||||||
| 918 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 919 | readNotificationPending = false; | - | ||||||||||||
| 920 | if (readNotificationEnabled) { | - | ||||||||||||
| 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) | - | ||||||||||||
| 925 | return; | - | ||||||||||||
| 926 | // check if there needs to be a new zero read notification | - | ||||||||||||
| 927 | if (data && data->controlSocket->state() == QAbstractSocket::UnconnectedState | - | ||||||||||||
| 928 | && data->controlSocket->error() == QAbstractSocket::RemoteHostClosedError) { | - | ||||||||||||
| 929 | connectData->readBuffer.clear(); | - | ||||||||||||
| 930 | emitReadNotification(); | - | ||||||||||||
| 931 | } | - | ||||||||||||
| 932 | } | - | ||||||||||||
| 933 | } | - | ||||||||||||
| 934 | - | |||||||||||||
| 935 | void QSocks5SocketEnginePrivate::emitReadNotification() | - | ||||||||||||
| 936 | { | - | ||||||||||||
| 937 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 938 | readNotificationActivated = true; | - | ||||||||||||
| 939 | if (readNotificationEnabled && !readNotificationPending) { | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 944 | } | - | ||||||||||||
| 945 | - | |||||||||||||
| 946 | void QSocks5SocketEnginePrivate::_q_emitPendingWriteNotification() | - | ||||||||||||
| 947 | { | - | ||||||||||||
| 948 | writeNotificationPending = false; | - | ||||||||||||
| 949 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 950 | if (writeNotificationEnabled) { | - | ||||||||||||
| 951 | QSOCKS5_D_DEBUG << "emitting writeNotification"; dead code: QMessageLogger(__FILE__, 951, __PRETTY_FUNCTION__).debug() << "emitting writeNotification"; | - | ||||||||||||
| 952 | emit q->writeNotification(); | - | ||||||||||||
| 953 | } | - | ||||||||||||
| 954 | } | - | ||||||||||||
| 955 | - | |||||||||||||
| 956 | void QSocks5SocketEnginePrivate::emitWriteNotification() | - | ||||||||||||
| 957 | { | - | ||||||||||||
| 958 | Q_Q(QSocks5SocketEngine); | - | ||||||||||||
| 959 | writeNotificationActivated = true; | - | ||||||||||||
| 960 | if (writeNotificationEnabled && !writeNotificationPending) { | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 965 | } | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 982 | - | |||||||||||||
| 983 | QSocks5SocketEngine::QSocks5SocketEngine(QObject *parent) | - | ||||||||||||
| 984 | :QAbstractSocketEngine(*new QSocks5SocketEnginePrivate(), parent) | - | ||||||||||||
| 985 | { | - | ||||||||||||
| 986 | } | - | ||||||||||||
| 987 | - | |||||||||||||
| 988 | QSocks5SocketEngine::~QSocks5SocketEngine() | - | ||||||||||||
| 989 | { | - | ||||||||||||
| 990 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
| 991 | - | |||||||||||||
| 992 | if (d->data) { | - | ||||||||||||
| 993 | delete d->data->authenticator; | - | ||||||||||||
| 994 | delete d->data->controlSocket; | - | ||||||||||||
| 995 | } | - | ||||||||||||
| 996 | if (d->connectData) | - | ||||||||||||
| 997 | delete d->connectData; | - | ||||||||||||
| 998 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 999 | if (d->udpData) { | - | ||||||||||||
| 1000 | delete d->udpData->udpSocket; | - | ||||||||||||
| 1001 | delete d->udpData; | - | ||||||||||||
| 1002 | } | - | ||||||||||||
| 1003 | #endif | - | ||||||||||||
| 1004 | if (d->bindData) | - | ||||||||||||
| 1005 | delete d->bindData; | - | ||||||||||||
| 1006 | } | - | ||||||||||||
| 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; | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 1084 | - | |||||||||||||
| 1085 | qintptr QSocks5SocketEngine::socketDescriptor() const | - | ||||||||||||
| 1086 | { | - | ||||||||||||
| 1087 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1088 | return d->socketDescriptor; | - | ||||||||||||
| 1089 | } | - | ||||||||||||
| 1090 | - | |||||||||||||
| 1091 | bool QSocks5SocketEngine::isValid() const | - | ||||||||||||
| 1092 | { | - | ||||||||||||
| 1093 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1094 | return d->socketType != QAbstractSocket::UnknownSocketType | - | ||||||||||||
| 1095 | && d->socks5State != QSocks5SocketEnginePrivate::SocksError | - | ||||||||||||
| 1096 | && (d->socketError == QAbstractSocket::UnknownSocketError | - | ||||||||||||
| 1097 | || d->socketError == QAbstractSocket::SocketTimeoutError | - | ||||||||||||
| 1098 | || d->socketError == QAbstractSocket::UnfinishedSocketOperationError); | - | ||||||||||||
| 1099 | } | - | ||||||||||||
| 1100 | - | |||||||||||||
| 1101 | bool QSocks5SocketEngine::connectInternal() | - | ||||||||||||
| 1102 | { | - | ||||||||||||
| 1103 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
| 1104 | - | |||||||||||||
| 1105 | if (!d->data) { | - | ||||||||||||
| 1106 | if (socketType() == QAbstractSocket::TcpSocket) { | - | ||||||||||||
| 1107 | d->initialize(QSocks5SocketEnginePrivate::ConnectMode); | - | ||||||||||||
| 1108 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1109 | } else if (socketType() == QAbstractSocket::UdpSocket) { | - | ||||||||||||
| 1110 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
| 1111 | // all udp needs to be bound | - | ||||||||||||
| 1112 | if (!bind(QHostAddress(QLatin1String("0.0.0.0")), 0)) | - | ||||||||||||
| 1113 | return false; | - | ||||||||||||
| 1114 | - | |||||||||||||
| 1115 | setState(QAbstractSocket::ConnectedState); | - | ||||||||||||
| 1116 | return true; | - | ||||||||||||
| 1117 | #endif | - | ||||||||||||
| 1118 | } else { | - | ||||||||||||
| 1119 | qFatal("QSocks5SocketEngine::connectToHost: in QTcpServer mode"); | - | ||||||||||||
| 1120 | return false; | - | ||||||||||||
| 1121 | } | - | ||||||||||||
| 1122 | } | - | ||||||||||||
| 1123 | - | |||||||||||||
| 1124 | if (d->socketState != QAbstractSocket::ConnectingState) { | - | ||||||||||||
| 1125 | if (d->socks5State == QSocks5SocketEnginePrivate::Uninitialized | - | ||||||||||||
| 1126 | // We may have new auth credentials since an earlier failure: | - | ||||||||||||
| 1127 | || d->socks5State == QSocks5SocketEnginePrivate::AuthenticatingError) { | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 1132 | - | |||||||||||||
| 1133 | d->data->controlSocket->connectToHost(d->proxyInfo.hostName(), d->proxyInfo.port()); | - | ||||||||||||
| 1134 | } | - | ||||||||||||
| 1135 | - | |||||||||||||
| 1136 | return false; | - | ||||||||||||
| 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(); | - | ||||||||||||
| 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(); | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 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) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 1181 | } | - | ||||||||||||
| 1182 | - | |||||||||||||
| 1183 | switch (socks5State) { | - | ||||||||||||
| 1184 | case AuthenticationMethodsSent: | - | ||||||||||||
| 1185 | parseAuthenticationMethodReply(); | - | ||||||||||||
| 1186 | break; | - | ||||||||||||
| 1187 | case Authenticating: | - | ||||||||||||
| 1188 | parseAuthenticatingReply(); | - | ||||||||||||
| 1189 | break; | - | ||||||||||||
| 1190 | case RequestMethodSent: | - | ||||||||||||
| 1191 | parseRequestMethodReply(); | - | ||||||||||||
| 1192 | break; | - | ||||||||||||
| 1193 | case Connected: { | - | ||||||||||||
| 1194 | QByteArray buf; | - | ||||||||||||
| 1195 | if (!data->authenticator->unSeal(data->controlSocket, &buf)) { | - | ||||||||||||
| 1196 | // qDebug("unseal error maybe need to wait for more data"); | - | ||||||||||||
| 1197 | } | - | ||||||||||||
| 1198 | if (buf.size()) { | - | ||||||||||||
| 1199 | QSOCKS5_DEBUG << dump(buf); dead code: QMessageLogger(__FILE__, 1199, __PRETTY_FUNCTION__).debug() << dump(buf); | - | ||||||||||||
| 1200 | connectData->readBuffer += buf; | - | ||||||||||||
| 1201 | emitReadNotification(); | - | ||||||||||||
| 1202 | } | - | ||||||||||||
| 1203 | break; | - | ||||||||||||
| 1204 | } | - | ||||||||||||
| 1205 | case BindSuccess: | - | ||||||||||||
| 1206 | // only get here if command is bind | - | ||||||||||||
| 1207 | if (mode == BindMode) { | - | ||||||||||||
| 1208 | parseRequestMethodReply(); | - | ||||||||||||
| 1209 | break; | - | ||||||||||||
| 1210 | } | - | ||||||||||||
| 1211 | - | |||||||||||||
| 1212 | // fall through | - | ||||||||||||
| 1213 | default: | - | ||||||||||||
| 1214 | qWarning("QSocks5SocketEnginePrivate::_q_controlSocketReadNotification: " | - | ||||||||||||
| 1215 | "Unexpectedly received data while in state=%d and mode=%d", | - | ||||||||||||
| 1216 | socks5State, mode); | - | ||||||||||||
| 1217 | break; | - | ||||||||||||
| 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 | - | ||||||||||||
| 1226 | || (mode == ConnectMode | - | ||||||||||||
| 1227 | && data->controlSocket->bytesToWrite())) | - | ||||||||||||
| 1228 | return; | - | ||||||||||||
| 1229 | if (data->controlSocket->bytesToWrite() < MaxWriteBufferSize) { | - | ||||||||||||
| 1230 | emitWriteNotification(); | - | ||||||||||||
| 1231 | writeNotificationActivated = false; | - | ||||||||||||
| 1232 | } | - | ||||||||||||
| 1233 | } | - | ||||||||||||
| 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) | - | ||||||||||||
| 1240 | return; // ignore this error -- comes from the waitFor* functions | - | ||||||||||||
| 1241 | - | |||||||||||||
| 1242 | if (error == QAbstractSocket::RemoteHostClosedError | - | ||||||||||||
| 1243 | && socks5State == Connected) { | - | ||||||||||||
| 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) | - | ||||||||||||
| 1247 | connectData->readBuffer.clear(); | - | ||||||||||||
| 1248 | emitReadNotification(); | - | ||||||||||||
| 1249 | data->controlSocket->close(); | - | ||||||||||||
| 1250 | // cause a disconnect in the outer socket | - | ||||||||||||
| 1251 | emitWriteNotification(); | - | ||||||||||||
| 1252 | } else if (socks5State == Uninitialized | - | ||||||||||||
| 1253 | || socks5State == AuthenticationMethodsSent | - | ||||||||||||
| 1254 | || socks5State == Authenticating | - | ||||||||||||
| 1255 | || socks5State == RequestMethodSent) { | - | ||||||||||||
| 1256 | setErrorState(socks5State == Uninitialized ? ConnectError : ControlSocketError); | - | ||||||||||||
| 1257 | data->controlSocket->close(); | - | ||||||||||||
| 1258 | emitConnectionNotification(); | - | ||||||||||||
| 1259 | } else { | - | ||||||||||||
| 1260 | q_func()->setError(data->controlSocket->error(), data->controlSocket->errorString()); | - | ||||||||||||
| 1261 | emitReadNotification(); | - | ||||||||||||
| 1262 | emitWriteNotification(); | - | ||||||||||||
| 1263 | } | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 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 | } | - | ||||||||||||
| 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()) | - | ||||||||||||
| 1281 | const_cast<QSocks5SocketEnginePrivate *>(this)->_q_udpSocketReadNotification(); | - | ||||||||||||
| 1282 | } | - | ||||||||||||
| 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()) { | - | ||||||||||||
| 1290 | QSOCKS5_D_DEBUG << "false read ??"; dead code: QMessageLogger(__FILE__, 1290, __PRETTY_FUNCTION__).debug() << "false read ??"; | - | ||||||||||||
| 1291 | return; | - | ||||||||||||
| 1292 | } | - | ||||||||||||
| 1293 | - | |||||||||||||
| 1294 | while (udpData->udpSocket->hasPendingDatagrams()) { | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 1300 | QSOCKS5_D_DEBUG << "failed unsealing datagram discarding"; dead code: QMessageLogger(__FILE__, 1300, __PRETTY_FUNCTION__).debug() << "failed unsealing datagram discarding"; | - | ||||||||||||
| 1301 | return; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1307 | QSOCKS5_D_DEBUG << "bugus udp data, discarding"; dead code: QMessageLogger(__FILE__, 1307, __PRETTY_FUNCTION__).debug() << "bugus udp data, discarding"; | - | ||||||||||||
| 1308 | return; | - | ||||||||||||
| 1309 | } | - | ||||||||||||
| 1310 | QSocks5RevivedDatagram datagram; | - | ||||||||||||
| 1311 | if (buf[pos++] != 0 || buf[pos++] != 0) { | - | ||||||||||||
| 1312 | QSOCKS5_D_DEBUG << "invalid datagram discarding"; dead code: QMessageLogger(__FILE__, 1312, __PRETTY_FUNCTION__).debug() << "invalid datagram discarding"; | - | ||||||||||||
| 1313 | return; | - | ||||||||||||
| 1314 | } | - | ||||||||||||
| 1315 | if (buf[pos++] != 0) { //### add fragmentation reading support | - | ||||||||||||
| 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; | - | ||||||||||||
| 1318 | } | - | ||||||||||||
| 1319 | if (qt_socks5_get_host_address_and_port(inBuf, &datagram.address, &datagram.port, &pos) != 1) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 1322 | } | - | ||||||||||||
| 1323 | datagram.data = QByteArray(&buf[pos], inBuf.size() - pos); | - | ||||||||||||
| 1324 | udpData->pendingDatagrams.enqueue(datagram); | - | ||||||||||||
| 1325 | } | - | ||||||||||||
| 1326 | emitReadNotification(); | - | ||||||||||||
| 1327 | } | - | ||||||||||||
| 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) | - | ||||||||||||
| 1338 | address = QHostAddress::AnyIPv4; //SOCKS5 doesn't support dual stack, and there isn't any implementation of udp on ipv6 yet | - | ||||||||||||
| 1339 | else | - | ||||||||||||
| 1340 | address = addr; | - | ||||||||||||
| 1341 | - | |||||||||||||
| 1342 | if (!d->data) { | - | ||||||||||||
| 1343 | if (socketType() == QAbstractSocket::TcpSocket) { | - | ||||||||||||
| 1344 | d->initialize(QSocks5SocketEnginePrivate::BindMode); | - | ||||||||||||
| 1345 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1346 | } else if (socketType() == QAbstractSocket::UdpSocket) { | - | ||||||||||||
| 1347 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
| 1348 | #endif | - | ||||||||||||
| 1349 | } else { | - | ||||||||||||
| 1350 | //### something invalid | - | ||||||||||||
| 1351 | return false; | - | ||||||||||||
| 1352 | } | - | ||||||||||||
| 1353 | } | - | ||||||||||||
| 1354 | - | |||||||||||||
| 1355 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1356 | if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) { | - | ||||||||||||
| 1357 | if (!d->udpData->udpSocket->bind(address, port)) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 1361 | } | - | ||||||||||||
| 1362 | d->localAddress = d->udpData->udpSocket->localAddress(); | - | ||||||||||||
| 1363 | d->localPort = d->udpData->udpSocket->localPort(); | - | ||||||||||||
| 1364 | } else | - | ||||||||||||
| 1365 | #endif | - | ||||||||||||
| 1366 | if (d->mode == QSocks5SocketEnginePrivate::BindMode) { | - | ||||||||||||
| 1367 | d->localAddress = address; | - | ||||||||||||
| 1368 | d->localPort = port; | - | ||||||||||||
| 1369 | } else { | - | ||||||||||||
| 1370 | //### something invalid | - | ||||||||||||
| 1371 | return false; | - | ||||||||||||
| 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) || | - | ||||||||||||
| 1379 | d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 1383 | } | - | ||||||||||||
| 1384 | if (d->socks5State == QSocks5SocketEnginePrivate::BindSuccess) { | - | ||||||||||||
| 1385 | setState(QAbstractSocket::BoundState); | - | ||||||||||||
| 1386 | return true; | - | ||||||||||||
| 1387 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1388 | } else if (d->socks5State == QSocks5SocketEnginePrivate::UdpAssociateSuccess) { | - | ||||||||||||
| 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() | - | ||||||||||||
| 1400 | || writeDatagram(0,0, QIpPacketHeader(d->data->controlSocket->localAddress(), dummy.localPort())) != 0 | - | ||||||||||||
| 1401 | || !dummy.waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed())) | - | ||||||||||||
| 1402 | || dummy.readDatagram(0,0, &d->localAddress, &d->localPort) != 0) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 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; | - | ||||||||||||
| 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; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1433 | d->socketState = QAbstractSocket::ListeningState; | - | ||||||||||||
| 1434 | - | |||||||||||||
| 1435 | // check if we already have a connection | - | ||||||||||||
| 1436 | if (d->socks5State == QSocks5SocketEnginePrivate::BindSuccess) | - | ||||||||||||
| 1437 | d->emitReadNotification(); | - | ||||||||||||
| 1438 | - | |||||||||||||
| 1439 | return true; | - | ||||||||||||
| 1440 | } | - | ||||||||||||
| 1441 | return false; | - | ||||||||||||
| 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: | - | ||||||||||||
| 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; | - | ||||||||||||
| 1469 | case QSocks5SocketEnginePrivate::ControlSocketError: | - | ||||||||||||
| 1470 | setError(QAbstractSocket::ProxyProtocolError, QLatin1String("Control socket error")); | - | ||||||||||||
| 1471 | break; | - | ||||||||||||
| 1472 | default: | - | ||||||||||||
| 1473 | setError(QAbstractSocket::ProxyProtocolError, QLatin1String("SOCKS5 proxy error")); | - | ||||||||||||
| 1474 | break; | - | ||||||||||||
| 1475 | } | - | ||||||||||||
| 1476 | return sd; | - | ||||||||||||
| 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 blockExecuted by:
| 298 | ||||||||||||
| 1493 | d->data->controlSocket->close(); | - | ||||||||||||
| 1494 | } executed 1008 times by 8 tests: end of blockExecuted 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 blockExecuted by:
| 1013 | ||||||||||||
| 1501 | - | |||||||||||||
| 1502 | qint64 QSocks5SocketEngine::bytesAvailable() const | - | ||||||||||||
| 1503 | { | - | ||||||||||||
| 1504 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1505 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode) | - | ||||||||||||
| 1506 | return d->connectData->readBuffer.size(); | - | ||||||||||||
| 1507 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1508 | else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode | - | ||||||||||||
| 1509 | && !d->udpData->pendingDatagrams.isEmpty()) | - | ||||||||||||
| 1510 | return d->udpData->pendingDatagrams.first().data.size(); | - | ||||||||||||
| 1511 | #endif | - | ||||||||||||
| 1512 | return 0; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1520 | if (d->connectData->readBuffer.size() == 0) { | - | ||||||||||||
| 1521 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) { | - | ||||||||||||
| 1522 | //imitate remote closed | - | ||||||||||||
| 1523 | close(); | - | ||||||||||||
| 1524 | setError(QAbstractSocket::RemoteHostClosedError, | - | ||||||||||||
| 1525 | QLatin1String("Remote host closed connection###")); | - | ||||||||||||
| 1526 | setState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
| 1527 | return -1; | - | ||||||||||||
| 1528 | } else { | - | ||||||||||||
| 1529 | return 0; // nothing to be read | - | ||||||||||||
| 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; | - | ||||||||||||
| 1537 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1538 | } else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) { | - | ||||||||||||
| 1539 | return readDatagram(data, maxlen); | - | ||||||||||||
| 1540 | #endif | - | ||||||||||||
| 1541 | } | - | ||||||||||||
| 1542 | return 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) { | - | ||||||||||||
| 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) | - | ||||||||||||
| 1554 | return 0; | - | ||||||||||||
| 1555 | - | |||||||||||||
| 1556 | QByteArray buf = QByteArray::fromRawData(data, len); | - | ||||||||||||
| 1557 | QByteArray sealedBuf; | - | ||||||||||||
| 1558 | if (!d->data->authenticator->seal(buf, &sealedBuf)) { | - | ||||||||||||
| 1559 | // ### Handle this error. | - | ||||||||||||
| 1560 | } | - | ||||||||||||
| 1561 | - | |||||||||||||
| 1562 | qint64 written = d->data->controlSocket->write(sealedBuf); | - | ||||||||||||
| 1563 | if (written <= 0) { | - | ||||||||||||
| 1564 | QSOCKS5_Q_DEBUG << "native write returned" << written; dead code: QMessageLogger(__FILE__, 1564, __PRETTY_FUNCTION__).debug() << "native write returned" << written; | - | ||||||||||||
| 1565 | return written; | - | ||||||||||||
| 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; | - | ||||||||||||
| 1570 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1571 | } else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) { | - | ||||||||||||
| 1572 | // send to connected address | - | ||||||||||||
| 1573 | return writeDatagram(data, len, QIpPacketHeader(d->peerAddress, d->peerPort)); | - | ||||||||||||
| 1574 | #endif | - | ||||||||||||
| 1575 | } | - | ||||||||||||
| 1576 | //### set an error ??? | - | ||||||||||||
| 1577 | return -1; | - | ||||||||||||
| 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; | - | ||||||||||||
| 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; | - | ||||||||||||
| 1596 | } | - | ||||||||||||
| 1597 | - | |||||||||||||
| 1598 | - | |||||||||||||
| 1599 | QNetworkInterface QSocks5SocketEngine::multicastInterface() const | - | ||||||||||||
| 1600 | { | - | ||||||||||||
| 1601 | return QNetworkInterface(); | - | ||||||||||||
| 1602 | } | - | ||||||||||||
| 1603 | - | |||||||||||||
| 1604 | bool QSocks5SocketEngine::setMulticastInterface(const QNetworkInterface &) | - | ||||||||||||
| 1605 | { | - | ||||||||||||
| 1606 | setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
| 1607 | QLatin1String("Operation on socket is not supported")); | - | ||||||||||||
| 1608 | return false; | - | ||||||||||||
| 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()) | - | ||||||||||||
| 1619 | return 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; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1635 | d->initialize(QSocks5SocketEnginePrivate::UdpAssociateMode); | - | ||||||||||||
| 1636 | // all udp needs to be bound | - | ||||||||||||
| 1637 | if (!bind(QHostAddress(QLatin1String("0.0.0.0")), 0)) { | - | ||||||||||||
| 1638 | //### set error | - | ||||||||||||
| 1639 | return -1; | - | ||||||||||||
| 1640 | } | - | ||||||||||||
| 1641 | } | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 1649 | } | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 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; | - | ||||||||||||
| 1657 | } | - | ||||||||||||
| 1658 | if (d->udpData->udpSocket->writeDatagram(sealedBuf, d->udpData->associateAddress, d->udpData->associatePort) != sealedBuf.size()) { | - | ||||||||||||
| 1659 | //### try frgamenting | - | ||||||||||||
| 1660 | if (d->udpData->udpSocket->error() == QAbstractSocket::DatagramTooLargeError) | - | ||||||||||||
| 1661 | setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); | - | ||||||||||||
| 1662 | //### else maybe more serious error | - | ||||||||||||
| 1663 | return -1; | - | ||||||||||||
| 1664 | } | - | ||||||||||||
| 1665 | - | |||||||||||||
| 1666 | return len; | - | ||||||||||||
| 1667 | } | - | ||||||||||||
| 1668 | - | |||||||||||||
| 1669 | bool QSocks5SocketEngine::hasPendingDatagrams() const | - | ||||||||||||
| 1670 | { | - | ||||||||||||
| 1671 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1672 | Q_INIT_CHECK(false); | - | ||||||||||||
| 1673 | - | |||||||||||||
| 1674 | d->checkForDatagrams(); | - | ||||||||||||
| 1675 | - | |||||||||||||
| 1676 | return !d->udpData->pendingDatagrams.isEmpty(); | - | ||||||||||||
| 1677 | } | - | ||||||||||||
| 1678 | - | |||||||||||||
| 1679 | qint64 QSocks5SocketEngine::pendingDatagramSize() const | - | ||||||||||||
| 1680 | { | - | ||||||||||||
| 1681 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1682 | - | |||||||||||||
| 1683 | d->checkForDatagrams(); | - | ||||||||||||
| 1684 | - | |||||||||||||
| 1685 | if (!d->udpData->pendingDatagrams.isEmpty()) | - | ||||||||||||
| 1686 | return d->udpData->pendingDatagrams.head().data.size(); | - | ||||||||||||
| 1687 | return 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) { | - | ||||||||||||
| 1695 | return d->data->controlSocket->bytesToWrite(); | - | ||||||||||||
| 1696 | } else { | - | ||||||||||||
| 1697 | return 0; | - | ||||||||||||
| 1698 | } | - | ||||||||||||
| 1699 | } | - | ||||||||||||
| 1700 | - | |||||||||||||
| 1701 | int QSocks5SocketEngine::option(SocketOption option) const | - | ||||||||||||
| 1702 | { | - | ||||||||||||
| 1703 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1704 | if (d->data && d->data->controlSocket) { | - | ||||||||||||
| 1705 | // convert the enum and call the real socket | - | ||||||||||||
| 1706 | if (option == QAbstractSocketEngine::LowDelayOption) | - | ||||||||||||
| 1707 | return d->data->controlSocket->socketOption(QAbstractSocket::LowDelayOption).toInt(); | - | ||||||||||||
| 1708 | if (option == QAbstractSocketEngine::KeepAliveOption) | - | ||||||||||||
| 1709 | return d->data->controlSocket->socketOption(QAbstractSocket::KeepAliveOption).toInt(); | - | ||||||||||||
| 1710 | } | - | ||||||||||||
| 1711 | return -1; | - | ||||||||||||
| 1712 | } | - | ||||||||||||
| 1713 | - | |||||||||||||
| 1714 | bool QSocks5SocketEngine::setOption(SocketOption option, int value) | - | ||||||||||||
| 1715 | { | - | ||||||||||||
| 1716 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
| 1717 | if (d->data && d->data->controlSocket) { | - | ||||||||||||
| 1718 | // convert the enum and call the real socket | - | ||||||||||||
| 1719 | if (option == QAbstractSocketEngine::LowDelayOption) | - | ||||||||||||
| 1720 | d->data->controlSocket->setSocketOption(QAbstractSocket::LowDelayOption, value); | - | ||||||||||||
| 1721 | if (option == QAbstractSocketEngine::KeepAliveOption) | - | ||||||||||||
| 1722 | d->data->controlSocket->setSocketOption(QAbstractSocket::KeepAliveOption, value); | - | ||||||||||||
| 1723 | return true; | - | ||||||||||||
| 1724 | } | - | ||||||||||||
| 1725 | return false; | - | ||||||||||||
| 1726 | } | - | ||||||||||||
| 1727 | - | |||||||||||||
| 1728 | bool QSocks5SocketEnginePrivate::waitForConnected(int msecs, bool *timedOut) | - | ||||||||||||
| 1729 | { | - | ||||||||||||
| 1730 | if (data->controlSocket->state() == QAbstractSocket::UnconnectedState) | - | ||||||||||||
| 1731 | return false; | - | ||||||||||||
| 1732 | - | |||||||||||||
| 1733 | const Socks5State wantedState = | - | ||||||||||||
| 1734 | mode == ConnectMode ? Connected : | - | ||||||||||||
| 1735 | mode == BindMode ? BindSuccess : | - | ||||||||||||
| 1736 | UdpAssociateSuccess; | - | ||||||||||||
| 1737 | - | |||||||||||||
| 1738 | QElapsedTimer stopWatch; | - | ||||||||||||
| 1739 | stopWatch.start(); | - | ||||||||||||
| 1740 | - | |||||||||||||
| 1741 | while (socks5State != wantedState) { | - | ||||||||||||
| 1742 | if (!data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { | - | ||||||||||||
| 1743 | if (data->controlSocket->state() == QAbstractSocket::UnconnectedState) | - | ||||||||||||
| 1744 | return true; | - | ||||||||||||
| 1745 | - | |||||||||||||
| 1746 | setErrorState(QSocks5SocketEnginePrivate::ControlSocketError); | - | ||||||||||||
| 1747 | if (timedOut && data->controlSocket->error() == QAbstractSocket::SocketTimeoutError) | - | ||||||||||||
| 1748 | *timedOut = true; | - | ||||||||||||
| 1749 | return false; | - | ||||||||||||
| 1750 | } | - | ||||||||||||
| 1751 | } | - | ||||||||||||
| 1752 | - | |||||||||||||
| 1753 | return true; | - | ||||||||||||
| 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)) | - | ||||||||||||
| 1768 | return false; | - | ||||||||||||
| 1769 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) | - | ||||||||||||
| 1770 | return true; | - | ||||||||||||
| 1771 | - | |||||||||||||
| 1772 | // we're connected | - | ||||||||||||
| 1773 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode || | - | ||||||||||||
| 1774 | d->mode == QSocks5SocketEnginePrivate::BindMode) { | - | ||||||||||||
| 1775 | while (!d->readNotificationActivated) { | - | ||||||||||||
| 1776 | if (!d->data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { | - | ||||||||||||
| 1777 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) | - | ||||||||||||
| 1778 | return true; | - | ||||||||||||
| 1779 | - | |||||||||||||
| 1780 | setError(d->data->controlSocket->error(), d->data->controlSocket->errorString()); | - | ||||||||||||
| 1781 | if (timedOut && d->data->controlSocket->error() == QAbstractSocket::SocketTimeoutError) | - | ||||||||||||
| 1782 | *timedOut = true; | - | ||||||||||||
| 1783 | return false; | - | ||||||||||||
| 1784 | } | - | ||||||||||||
| 1785 | } | - | ||||||||||||
| 1786 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1787 | } else { | - | ||||||||||||
| 1788 | while (!d->readNotificationActivated) { | - | ||||||||||||
| 1789 | if (!d->udpData->udpSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { | - | ||||||||||||
| 1790 | setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); | - | ||||||||||||
| 1791 | if (timedOut && d->udpData->udpSocket->error() == QAbstractSocket::SocketTimeoutError) | - | ||||||||||||
| 1792 | *timedOut = true; | - | ||||||||||||
| 1793 | return false; | - | ||||||||||||
| 1794 | } | - | ||||||||||||
| 1795 | } | - | ||||||||||||
| 1796 | #endif // QT_NO_UDPSOCKET | - | ||||||||||||
| 1797 | } | - | ||||||||||||
| 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; | - | ||||||||||||
| 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)) | - | ||||||||||||
| 1818 | return false; | - | ||||||||||||
| 1819 | if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) | - | ||||||||||||
| 1820 | return true; | - | ||||||||||||
| 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()) | - | ||||||||||||
| 1826 | d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); | - | ||||||||||||
| 1827 | while ((msecs == -1 || stopWatch.elapsed() < msecs) | - | ||||||||||||
| 1828 | && d->data->controlSocket->state() == QAbstractSocket::ConnectedState | - | ||||||||||||
| 1829 | && d->data->controlSocket->bytesToWrite() >= MaxWriteBufferSize) | - | ||||||||||||
| 1830 | d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); | - | ||||||||||||
| 1831 | return d->data->controlSocket->bytesToWrite() < MaxWriteBufferSize; | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1840 | bool canRead = waitForRead(msecs, timedOut); | - | ||||||||||||
| 1841 | if (readyToRead) | - | ||||||||||||
| 1842 | *readyToRead = canRead; | - | ||||||||||||
| 1843 | return canRead; | - | ||||||||||||
| 1844 | } | - | ||||||||||||
| 1845 | - | |||||||||||||
| 1846 | bool canWrite = waitForWrite(msecs, timedOut); | - | ||||||||||||
| 1847 | if (readyToWrite) | - | ||||||||||||
| 1848 | *readyToWrite = canWrite; | - | ||||||||||||
| 1849 | return canWrite; | - | ||||||||||||
| 1850 | } | - | ||||||||||||
| 1851 | - | |||||||||||||
| 1852 | bool QSocks5SocketEngine::isReadNotificationEnabled() const | - | ||||||||||||
| 1853 | { | - | ||||||||||||
| 1854 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1855 | return d->readNotificationEnabled; | - | ||||||||||||
| 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 | - | ||||||||||||
| 1866 | && enable) { | - | ||||||||||||
| 1867 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode) | - | ||||||||||||
| 1868 | emitSignal = !d->connectData->readBuffer.isEmpty(); | - | ||||||||||||
| 1869 | #ifndef QT_NO_UDPSOCKET | - | ||||||||||||
| 1870 | else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) | - | ||||||||||||
| 1871 | emitSignal = !d->udpData->pendingDatagrams.isEmpty(); | - | ||||||||||||
| 1872 | #endif | - | ||||||||||||
| 1873 | else if (d->mode == QSocks5SocketEnginePrivate::BindMode | - | ||||||||||||
| 1874 | && d->socketState == QAbstractSocket::ListeningState | - | ||||||||||||
| 1875 | && d->socks5State == QSocks5SocketEnginePrivate::BindSuccess) | - | ||||||||||||
| 1876 | emitSignal = true; | - | ||||||||||||
| 1877 | } | - | ||||||||||||
| 1878 | - | |||||||||||||
| 1879 | d->readNotificationEnabled = enable; | - | ||||||||||||
| 1880 | - | |||||||||||||
| 1881 | if (emitSignal) | - | ||||||||||||
| 1882 | d->emitReadNotification(); | - | ||||||||||||
| 1883 | } | - | ||||||||||||
| 1884 | - | |||||||||||||
| 1885 | bool QSocks5SocketEngine::isWriteNotificationEnabled() const | - | ||||||||||||
| 1886 | { | - | ||||||||||||
| 1887 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1888 | return d->writeNotificationEnabled; | - | ||||||||||||
| 1889 | } | - | ||||||||||||
| 1890 | - | |||||||||||||
| 1891 | void QSocks5SocketEngine::setWriteNotificationEnabled(bool enable) | - | ||||||||||||
| 1892 | { | - | ||||||||||||
| 1893 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
| 1894 | d->writeNotificationEnabled = enable; | - | ||||||||||||
| 1895 | if (enable && d->socketState == QAbstractSocket::ConnectedState) { | - | ||||||||||||
| 1896 | if (d->mode == QSocks5SocketEnginePrivate::ConnectMode && d->data->controlSocket->bytesToWrite()) | - | ||||||||||||
| 1897 | return; // will be emitted as a result of bytes written | - | ||||||||||||
| 1898 | d->emitWriteNotification(); | - | ||||||||||||
| 1899 | d->writeNotificationActivated = false; | - | ||||||||||||
| 1900 | } | - | ||||||||||||
| 1901 | } | - | ||||||||||||
| 1902 | - | |||||||||||||
| 1903 | bool QSocks5SocketEngine::isExceptionNotificationEnabled() const | - | ||||||||||||
| 1904 | { | - | ||||||||||||
| 1905 | Q_D(const QSocks5SocketEngine); | - | ||||||||||||
| 1906 | return d->exceptNotificationEnabled; | - | ||||||||||||
| 1907 | } | - | ||||||||||||
| 1908 | - | |||||||||||||
| 1909 | void QSocks5SocketEngine::setExceptionNotificationEnabled(bool enable) | - | ||||||||||||
| 1910 | { | - | ||||||||||||
| 1911 | Q_D(QSocks5SocketEngine); | - | ||||||||||||
| 1912 | d->exceptNotificationEnabled = enable; | - | ||||||||||||
| 1913 | } | - | ||||||||||||
| 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) { | - | ||||||||||||
| 1923 | QSOCKS5_DEBUG << "not proxying"; dead code: QMessageLogger(__FILE__, 1923, __PRETTY_FUNCTION__).debug() << "not proxying"; | - | ||||||||||||
| 1924 | return 0; | - | ||||||||||||
| 1925 | } | - | ||||||||||||
| 1926 | QScopedPointer<QSocks5SocketEngine> engine(new QSocks5SocketEngine(parent)); | - | ||||||||||||
| 1927 | engine->setProxy(proxy); | - | ||||||||||||
| 1928 | return engine.take(); | - | ||||||||||||
| 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)) { | - | ||||||||||||
| 1935 | QSOCKS5_DEBUG << "bind store contains" << socketDescriptor; dead code: QMessageLogger(__FILE__, 1935, __PRETTY_FUNCTION__).debug() << "bind store contains" << socketDescriptor; | - | ||||||||||||
| 1936 | return new QSocks5SocketEngine(parent); | - | ||||||||||||
| 1937 | } | - | ||||||||||||
| 1938 | return 0; | - | ||||||||||||
| 1939 | } | - | ||||||||||||
| 1940 | - | |||||||||||||
| 1941 | #endif // QT_NO_SOCKS5 | - | ||||||||||||
| 1942 | - | |||||||||||||
| 1943 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |