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