src/network/socket/qsocks5socketengine.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    47 #include "qudpsocket.h"
    47 #include "qudpsocket.h"
    48 #include "qtcpserver.h"
    48 #include "qtcpserver.h"
    49 #include "qdebug.h"
    49 #include "qdebug.h"
    50 #include "qhash.h"
    50 #include "qhash.h"
    51 #include "qqueue.h"
    51 #include "qqueue.h"
    52 #include "qdatetime.h"
    52 #include "qelapsedtimer.h"
    53 #include "qmutex.h"
    53 #include "qmutex.h"
    54 #include "qthread.h"
    54 #include "qthread.h"
    55 #include "qcoreapplication.h"
    55 #include "qcoreapplication.h"
    56 #include "qurl.h"
    56 #include "qurl.h"
    57 #include "qauthenticator.h"
    57 #include "qauthenticator.h"
   306 {
   306 {
   307     QHostAddress localAddress;
   307     QHostAddress localAddress;
   308     quint16 localPort;
   308     quint16 localPort;
   309     QHostAddress peerAddress;
   309     QHostAddress peerAddress;
   310     quint16 peerPort;
   310     quint16 peerPort;
   311     QDateTime timeStamp;
   311     QElapsedTimer timeStamp;
   312 };
   312 };
   313 
   313 
   314 struct QSocks5RevivedDatagram
   314 struct QSocks5RevivedDatagram
   315 {
   315 {
   316     QByteArray data;
   316     QByteArray data;
   367 {
   367 {
   368     QMutexLocker lock(&mutex);
   368     QMutexLocker lock(&mutex);
   369     if (store.contains(socketDescriptor)) {
   369     if (store.contains(socketDescriptor)) {
   370         // qDebug() << "delete it";
   370         // qDebug() << "delete it";
   371     }
   371     }
   372     bindData->timeStamp = QDateTime::currentDateTime();
   372     bindData->timeStamp.start();
   373     store.insert(socketDescriptor, bindData);
   373     store.insert(socketDescriptor, bindData);
   374     // start sweep timer if not started
   374     // start sweep timer if not started
   375     if (sweepTimerId == -1)
   375     if (sweepTimerId == -1)
   376         sweepTimerId = startTimer(60000);
   376         sweepTimerId = startTimer(60000);
   377 }
   377 }
   410     if (event->timerId() == sweepTimerId) {
   410     if (event->timerId() == sweepTimerId) {
   411         QSOCKS5_DEBUG << "QSocks5BindStore performing sweep";
   411         QSOCKS5_DEBUG << "QSocks5BindStore performing sweep";
   412         QMutableHashIterator<int, QSocks5BindData *> it(store);
   412         QMutableHashIterator<int, QSocks5BindData *> it(store);
   413         while (it.hasNext()) {
   413         while (it.hasNext()) {
   414             it.next();
   414             it.next();
   415             if (it.value()->timeStamp.secsTo(QDateTime::currentDateTime()) > 350) {
   415             if (it.value()->timeStamp.hasExpired(350000)) {
   416                 QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ";
   416                 QSOCKS5_DEBUG << "QSocks5BindStore removing JJJJ";
   417                 it.remove();
   417                 it.remove();
   418             }
   418             }
   419         }
   419         }
   420     }
   420     }
  1353         //### something invalid
  1353         //### something invalid
  1354         return false;
  1354         return false;
  1355     }
  1355     }
  1356 
  1356 
  1357     int msecs = SOCKS5_BLOCKING_BIND_TIMEOUT;
  1357     int msecs = SOCKS5_BLOCKING_BIND_TIMEOUT;
  1358     QTime stopWatch;
  1358     QElapsedTimer stopWatch;
  1359     stopWatch.start();
  1359     stopWatch.start();
  1360     d->data->controlSocket->connectToHost(d->proxyInfo.hostName(), d->proxyInfo.port());
  1360     d->data->controlSocket->connectToHost(d->proxyInfo.hostName(), d->proxyInfo.port());
  1361     if (!d->waitForConnected(msecs, 0) ||
  1361     if (!d->waitForConnected(msecs, 0) ||
  1362         d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) {
  1362         d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) {
  1363         // waitForConnected sets the error state and closes the socket
  1363         // waitForConnected sets the error state and closes the socket
  1453     QSOCKS5_Q_DEBUG << "close()";
  1453     QSOCKS5_Q_DEBUG << "close()";
  1454     Q_D(QSocks5SocketEngine);
  1454     Q_D(QSocks5SocketEngine);
  1455     if (d->data && d->data->controlSocket) {
  1455     if (d->data && d->data->controlSocket) {
  1456         if (d->data->controlSocket->state() == QAbstractSocket::ConnectedState) {
  1456         if (d->data->controlSocket->state() == QAbstractSocket::ConnectedState) {
  1457             int msecs = 100;
  1457             int msecs = 100;
  1458             QTime stopWatch;
  1458             QElapsedTimer stopWatch;
  1459             stopWatch.start();
  1459             stopWatch.start();
  1460             while (!d->data->controlSocket->bytesToWrite()) {
  1460             while (!d->data->controlSocket->bytesToWrite()) {
  1461                if (!d->data->controlSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed())))
  1461                if (!d->data->controlSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed())))
  1462                    break;
  1462                    break;
  1463             }
  1463             }
  1672     const Socks5State wantedState =
  1672     const Socks5State wantedState =
  1673         mode == ConnectMode ? Connected :
  1673         mode == ConnectMode ? Connected :
  1674         mode == BindMode ? BindSuccess :
  1674         mode == BindMode ? BindSuccess :
  1675         UdpAssociateSuccess;
  1675         UdpAssociateSuccess;
  1676 
  1676 
  1677     QTime stopWatch;
  1677     QElapsedTimer stopWatch;
  1678     stopWatch.start();
  1678     stopWatch.start();
  1679 
  1679 
  1680     while (socks5State != wantedState) {
  1680     while (socks5State != wantedState) {
  1681         if (!data->controlSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) {
  1681         if (!data->controlSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) {
  1682             if (data->controlSocket->state() == QAbstractSocket::UnconnectedState)
  1682             if (data->controlSocket->state() == QAbstractSocket::UnconnectedState)
  1697     Q_D(QSocks5SocketEngine);
  1697     Q_D(QSocks5SocketEngine);
  1698     QSOCKS5_DEBUG << "waitForRead" << msecs;
  1698     QSOCKS5_DEBUG << "waitForRead" << msecs;
  1699 
  1699 
  1700     d->readNotificationActivated = false;
  1700     d->readNotificationActivated = false;
  1701 
  1701 
  1702     QTime stopWatch;
  1702     QElapsedTimer stopWatch;
  1703     stopWatch.start();
  1703     stopWatch.start();
  1704 
  1704 
  1705     // are we connected yet?
  1705     // are we connected yet?
  1706     if (!d->waitForConnected(msecs, timedOut))
  1706     if (!d->waitForConnected(msecs, timedOut))
  1707         return false;
  1707         return false;
  1747 bool QSocks5SocketEngine::waitForWrite(int msecs, bool *timedOut)
  1747 bool QSocks5SocketEngine::waitForWrite(int msecs, bool *timedOut)
  1748 {
  1748 {
  1749     Q_D(QSocks5SocketEngine);
  1749     Q_D(QSocks5SocketEngine);
  1750     QSOCKS5_DEBUG << "waitForWrite" << msecs;
  1750     QSOCKS5_DEBUG << "waitForWrite" << msecs;
  1751 
  1751 
  1752     QTime stopWatch;
  1752     QElapsedTimer stopWatch;
  1753     stopWatch.start();
  1753     stopWatch.start();
  1754 
  1754 
  1755     // are we connected yet?
  1755     // are we connected yet?
  1756     if (!d->waitForConnected(msecs, timedOut))
  1756     if (!d->waitForConnected(msecs, timedOut))
  1757         return false;
  1757         return false;