Qt 4.8
Classes | Macros | Functions | Variables
qsocks5socketengine.cpp File Reference
#include "qsocks5socketengine_p.h"
#include "qtcpsocket.h"
#include "qudpsocket.h"
#include "qtcpserver.h"
#include "qdebug.h"
#include "qhash.h"
#include "qqueue.h"
#include "qelapsedtimer.h"
#include "qmutex.h"
#include "qthread.h"
#include "qcoreapplication.h"
#include "qurl.h"
#include "qauthenticator.h"
#include <qendian.h>
#include <qnetworkinterface.h>

Go to the source code of this file.

Classes

struct  QSocks5BindData
 
class  QSocks5BindStore
 
struct  QSocks5ConnectData
 
struct  QSocks5Data
 
struct  QSocks5RevivedDatagram
 
struct  QSocks5UdpAssociateData
 

Macros

#define MAX_DATA_DUMP   256
 
#define Q_INIT_CHECK(returnValue)
 
#define QSOCKS5_D_DEBUG   if (0) qDebug()
 
#define QSOCKS5_DEBUG   if (0) qDebug()
 
#define QSOCKS5_Q_DEBUG   if (0) qDebug()
 
#define S5_AUTHMETHOD_NONE   0x00
 
#define S5_AUTHMETHOD_NOTACCEPTABLE   0xFF
 
#define S5_AUTHMETHOD_PASSWORD   0x02
 
#define S5_BIND   0x02
 
#define S5_CONNECT   0x01
 
#define S5_DOMAINNAME   0x03
 
#define S5_IP_V4   0x01
 
#define S5_IP_V6   0x04
 
#define S5_PASSWORDAUTH_VERSION   0x01
 
#define S5_R_ERROR_ADD_TYPE_NOT_SUPORTED   0x08
 
#define S5_R_ERROR_CMD_NOT_SUPPORTED   0x07
 
#define S5_R_ERROR_CON_NOT_ALLOWED   0x02
 
#define S5_R_ERROR_CONN_REFUSED   0x05
 
#define S5_R_ERROR_HOST_UNREACH   0x04
 
#define S5_R_ERROR_NET_UNREACH   0x03
 
#define S5_R_ERROR_SOCKS_FAILURE   0x01
 
#define S5_R_ERROR_TTL   0x06
 
#define S5_SUCCESS   0x00
 
#define S5_UDP_ASSOCIATE   0x03
 
#define S5_VERSION_5   0x05
 
#define SOCKS5_BLOCKING_BIND_TIMEOUT   10000
 

Functions

static QString dump (const QByteArray &)
 
static bool qt_socks5_get_host_address_and_port (const QByteArray &buf, QHostAddress *pAddress, quint16 *pPort, int *pPos)
 
static bool qt_socks5_set_host_address_and_port (const QHostAddress &address, quint16 port, QByteArray *pBuf)
 
static bool qt_socks5_set_host_name_and_port (const QString &hostname, quint16 port, QByteArray *pBuf)
 
static int qt_timeout_value (int msecs, int elapsed)
 

Variables

static QBasicAtomicInt descriptorCounter = Q_BASIC_ATOMIC_INITIALIZER(1)
 
static const int MaxWriteBufferSize = 128*1024
 
static QString s5StateToString (QSocks5SocketEnginePrivate::Socks5State)
 

Macro Definition Documentation

◆ MAX_DATA_DUMP

#define MAX_DATA_DUMP   256

Definition at line 71 of file qsocks5socketengine.cpp.

◆ Q_INIT_CHECK

#define Q_INIT_CHECK (   returnValue)
Value:
do { \
if (!d->data) { \
return returnValue; \
} } while (0)
double d
Definition: qnumeric_p.h:62

Definition at line 78 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEngine::hasPendingDatagrams().

◆ QSOCKS5_D_DEBUG

#define QSOCKS5_D_DEBUG   if (0) qDebug()

◆ QSOCKS5_DEBUG

#define QSOCKS5_DEBUG   if (0) qDebug()

◆ QSOCKS5_Q_DEBUG

#define QSOCKS5_Q_DEBUG   if (0) qDebug()

◆ S5_AUTHMETHOD_NONE

#define S5_AUTHMETHOD_NONE   0x00

◆ S5_AUTHMETHOD_NOTACCEPTABLE

#define S5_AUTHMETHOD_NOTACCEPTABLE   0xFF

◆ S5_AUTHMETHOD_PASSWORD

#define S5_AUTHMETHOD_PASSWORD   0x02

Definition at line 101 of file qsocks5socketengine.cpp.

◆ S5_BIND

#define S5_BIND   0x02

◆ S5_CONNECT

#define S5_CONNECT   0x01

◆ S5_DOMAINNAME

#define S5_DOMAINNAME   0x03

◆ S5_IP_V4

#define S5_IP_V4   0x01

◆ S5_IP_V6

#define S5_IP_V6   0x04

◆ S5_PASSWORDAUTH_VERSION

#define S5_PASSWORDAUTH_VERSION   0x01

◆ S5_R_ERROR_ADD_TYPE_NOT_SUPORTED

#define S5_R_ERROR_ADD_TYPE_NOT_SUPORTED   0x08

Definition at line 98 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_CMD_NOT_SUPPORTED

#define S5_R_ERROR_CMD_NOT_SUPPORTED   0x07

Definition at line 97 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_CON_NOT_ALLOWED

#define S5_R_ERROR_CON_NOT_ALLOWED   0x02

Definition at line 92 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_CONN_REFUSED

#define S5_R_ERROR_CONN_REFUSED   0x05

Definition at line 95 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_HOST_UNREACH

#define S5_R_ERROR_HOST_UNREACH   0x04

Definition at line 94 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_NET_UNREACH

#define S5_R_ERROR_NET_UNREACH   0x03

Definition at line 93 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_SOCKS_FAILURE

#define S5_R_ERROR_SOCKS_FAILURE   0x01

Definition at line 91 of file qsocks5socketengine.cpp.

◆ S5_R_ERROR_TTL

#define S5_R_ERROR_TTL   0x06

Definition at line 96 of file qsocks5socketengine.cpp.

◆ S5_SUCCESS

#define S5_SUCCESS   0x00

◆ S5_UDP_ASSOCIATE

#define S5_UDP_ASSOCIATE   0x03

◆ S5_VERSION_5

#define S5_VERSION_5   0x05

◆ SOCKS5_BLOCKING_BIND_TIMEOUT

#define SOCKS5_BLOCKING_BIND_TIMEOUT   10000

Definition at line 75 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEngine::bind().

Function Documentation

◆ dump()

static QString dump ( const QByteArray )
inlinestatic

◆ qt_socks5_get_host_address_and_port()

static bool qt_socks5_get_host_address_and_port ( const QByteArray buf,
QHostAddress pAddress,
quint16 pPort,
int *  pPos 
)
static

Definition at line 222 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEnginePrivate::_q_udpSocketReadNotification(), and QSocks5SocketEnginePrivate::parseRequestMethodReply().

223 {
224  bool ret = false;
225  int pos = *pPos;
226  const unsigned char *pBuf = reinterpret_cast<const unsigned char*>(buf.constData());
227  QHostAddress address;
228  quint16 port = 0;
229 
230  if (buf.size() - pos < 1) {
231  QSOCKS5_DEBUG << "need more data address/port";
232  return false;
233  }
234  if (pBuf[pos] == S5_IP_V4) {
235  pos++;
236  if (buf.size() - pos < 4) {
237  QSOCKS5_DEBUG << "need more data for ip4 address";
238  return false;
239  }
240  address.setAddress(qFromBigEndian<quint32>(&pBuf[pos]));
241  pos += 4;
242  ret = true;
243  } else if (pBuf[pos] == S5_IP_V6) {
244  pos++;
245  if (buf.size() - pos < 16) {
246  QSOCKS5_DEBUG << "need more data for ip6 address";
247  return false;
248  }
250  for (int i = 0; i < 16; ++i)
251  add[i] = buf[pos++];
252  ret = true;
253  } else if (pBuf[pos] == S5_DOMAINNAME){
254  // just skip it
255  pos++;
256  qDebug() << "skipping hostname of len" << uint(pBuf[pos]);
257  pos += uchar(pBuf[pos]);
258  } else {
259  QSOCKS5_DEBUG << "invalid address type" << (int)pBuf[pos];
260  ret = false;
261  }
262 
263  if (ret) {
264  if (buf.size() - pos < 2) {
265  QSOCKS5_DEBUG << "need more data for port";
266  return false;
267  }
268  port = qFromBigEndian<quint16>(&pBuf[pos]);
269  pos += 2;
270  }
271 
272  if (ret) {
273  QSOCKS5_DEBUG << "got [" << address << ':' << port << ']';
274  *pAddress = address;
275  *pPort = port;
276  *pPos = pos;
277  }
278 
279  return ret;
280 }
#define add(aName)
#define S5_IP_V4
quint32 qFromBigEndian< quint32 >(const uchar *src)
Definition: qendian.h:239
#define S5_DOMAINNAME
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
unsigned short quint16
Definition: qglobal.h:936
unsigned int uint
Definition: qglobal.h:996
quint16 qFromBigEndian< quint16 >(const uchar *src)
Definition: qendian.h:249
#define S5_IP_V6
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
#define QSOCKS5_DEBUG

◆ qt_socks5_set_host_address_and_port()

static bool qt_socks5_set_host_address_and_port ( const QHostAddress address,
quint16  port,
QByteArray pBuf 
)
static

Definition at line 159 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEnginePrivate::sendRequestMethod(), and QSocks5SocketEngine::writeDatagram().

160 {
161  QSOCKS5_DEBUG << "setting [" << address << ':' << port << ']';
162 
163  union {
164  quint16 port;
165  quint32 ipv4;
166  QIPv6Address ipv6;
167  char ptr;
168  } data;
169 
170  // add address
171  if (address.protocol() == QAbstractSocket::IPv4Protocol) {
172  data.ipv4 = qToBigEndian<quint32>(address.toIPv4Address());
173  pBuf->append(S5_IP_V4);
174  pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.ipv4));
175  } else if (address.protocol() == QAbstractSocket::IPv6Protocol) {
176  data.ipv6 = address.toIPv6Address();
177  pBuf->append(S5_IP_V6);
178  pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.ipv6));
179  } else {
180  return false;
181  }
182 
183  // add port
184  data.port = qToBigEndian<quint16>(port);
185  pBuf->append(QByteArray::fromRawData(&data.ptr, sizeof data.port));
186  return true;
187 }
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
QByteArray & append(char c)
Appends the character ch to this byte array.
#define S5_IP_V4
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
const T * ptr(const T &t)
#define S5_IP_V6
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
unsigned int quint32
Definition: qglobal.h:938
#define QSOCKS5_DEBUG

◆ qt_socks5_set_host_name_and_port()

static bool qt_socks5_set_host_name_and_port ( const QString hostname,
quint16  port,
QByteArray pBuf 
)
static

Definition at line 192 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEnginePrivate::sendRequestMethod().

193 {
194  QSOCKS5_DEBUG << "setting [" << hostname << ':' << port << ']';
195 
196  QByteArray encodedHostName = QUrl::toAce(hostname);
197  QByteArray &buf = *pBuf;
198 
199  if (encodedHostName.length() > 255)
200  return false;
201 
202  buf.append(S5_DOMAINNAME);
203  buf.append(uchar(encodedHostName.length()));
204  buf.append(encodedHostName);
205 
206  // add port
207  union {
208  quint16 port;
209  char ptr;
210  } data;
211  data.port = qToBigEndian<quint16>(port);
212  buf.append(QByteArray::fromRawData(&data.ptr, sizeof data.port));
213 
214  return true;
215 }
QByteArray & append(char c)
Appends the character ch to this byte array.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define S5_DOMAINNAME
unsigned char uchar
Definition: qglobal.h:994
static QByteArray toAce(const QString &)
Returns the ASCII Compatible Encoding of the given domain name domain.
Definition: qurl.cpp:6158
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
const T * ptr(const T &t)
int length() const
Same as size().
Definition: qbytearray.h:356
#define QSOCKS5_DEBUG

◆ qt_timeout_value()

static int qt_timeout_value ( int  msecs,
int  elapsed 
)
static

Definition at line 286 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEngine::bind(), QSocks5SocketEngine::close(), QSocks5SocketEnginePrivate::waitForConnected(), QSocks5SocketEngine::waitForRead(), and QSocks5SocketEngine::waitForWrite().

287 {
288  if (msecs == -1)
289  return -1;
290 
291  int timeout = msecs - elapsed;
292  return timeout < 0 ? 0 : timeout;
293 }
static double elapsed(qint64 after, qint64 before)

Variable Documentation

◆ descriptorCounter

QBasicAtomicInt descriptorCounter = Q_BASIC_ATOMIC_INITIALIZER(1)
static

Definition at line 1011 of file qsocks5socketengine.cpp.

◆ MaxWriteBufferSize

const int MaxWriteBufferSize = 128*1024
static

◆ s5StateToString

QString s5StateToString(QSocks5SocketEnginePrivate::Socks5State)
inlinestatic

Definition at line 151 of file qsocks5socketengine.cpp.

Referenced by QSocks5SocketEnginePrivate::_q_controlSocketReadNotification().

151 { return QString(); }
The QString class provides a Unicode character string.
Definition: qstring.h:83