diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-03-05 07:37:50 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-03-05 07:37:50 +0000 |
| commit | 278a85bfeb034b7b8bf9dbabadb51e0f95057446 (patch) | |
| tree | 5942948ef010c77b7b04c6a8db4d46f1ea198108 /source/QXmppSocks.cpp | |
| parent | f4e87e678cb5d0d87ca87b39a864f5fc9d2fab53 (diff) | |
| download | qxmpp-278a85bfeb034b7b8bf9dbabadb51e0f95057446.tar.gz | |
use QDataStream instead of htons/ntohs to avoid having to link against winsock on win32
Diffstat (limited to 'source/QXmppSocks.cpp')
| -rw-r--r-- | source/QXmppSocks.cpp | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/source/QXmppSocks.cpp b/source/QXmppSocks.cpp index 691563a7..079e2a4b 100644 --- a/source/QXmppSocks.cpp +++ b/source/QXmppSocks.cpp @@ -21,17 +21,12 @@ * */ +#include <QDataStream> #include <QEventLoop> #include <QTcpServer> #include <QTcpSocket> #include <QTimer> -#ifdef Q_OS_WIN -#include <winsock2.h> -#else -#include <arpa/inet.h> -#endif - #include "QXmppSocks.h" const static char SocksVersion = 5; @@ -72,47 +67,39 @@ enum State { ReadyState = 2, }; -QByteArray encodeHostAndPort(quint8 type, const QByteArray &host, quint16 port) +static QByteArray encodeHostAndPort(quint8 type, const QByteArray &host, quint16 port) { QByteArray buffer; - buffer.resize(2); + QDataStream stream(&buffer, QIODevice::WriteOnly); // set host name - buffer[0] = type; - buffer[1] = host.size(); - buffer.append(host); + quint8 hostLength = host.size(); + stream << type; + stream << hostLength; + stream.writeRawData(host.constData(), hostLength); // set port - int pos = buffer.size(); - buffer.resize(pos + 2); - quint16 p = htons(port); - memcpy(buffer.data() + pos, &p, 2); + stream << port; return buffer; } -bool parseHostAndPort(const QByteArray buffer, quint8 &type, QByteArray &host, quint16 &port) +static bool parseHostAndPort(const QByteArray buffer, quint8 &type, QByteArray &host, quint16 &port) { if (buffer.size() < 4) return false; - // parse host type - int pos = 0; - type = buffer.at(pos); - pos++; - - // parse host name - quint8 hostLength = buffer.at(pos); - pos++; + QDataStream stream(buffer); + // get host name + quint8 hostLength; + stream >> type; + stream >> hostLength; if (buffer.size() < hostLength + 4) { qWarning("Invalid host length"); return false; } - host = buffer.mid(pos, hostLength); - pos += hostLength; - - // parse host port - quint16 p; - memcpy(&p, buffer.data() + pos, 2); - port = ntohs(p); + host.resize(hostLength); + stream.readRawData(host.data(), hostLength); + // get port + stream >> port; return true; } |
