aboutsummaryrefslogtreecommitdiff
path: root/source/QXmppSocks.cpp
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-03-03 15:01:31 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-03-03 15:01:31 +0000
commitd84a3a8af6333f6a82f9c2840791a1a6cc3d058f (patch)
tree1df07046631c409176b36ea2b3601e79e9852e17 /source/QXmppSocks.cpp
parent6b3dc7faad567f732764e7e5ff3397d945ef998f (diff)
downloadqxmpp-d84a3a8af6333f6a82f9c2840791a1a6cc3d058f.tar.gz
make it possible to read and write to SOCKS5 client/server
Diffstat (limited to 'source/QXmppSocks.cpp')
-rw-r--r--source/QXmppSocks.cpp55
1 files changed, 46 insertions, 9 deletions
diff --git a/source/QXmppSocks.cpp b/source/QXmppSocks.cpp
index 5ba9b593..c11c6584 100644
--- a/source/QXmppSocks.cpp
+++ b/source/QXmppSocks.cpp
@@ -214,10 +214,14 @@ void QXmppSocksClient::slotReadyRead()
}
// FIXME : what do we do with the resulting name / port?
+ // from now on, forward signals
+ connect(m_socket, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
+ disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
+ connect(m_socket, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
+
+ // notify of connection
emit connected();
- } else {
- emit readyRead();
}
}
@@ -235,6 +239,11 @@ bool QXmppSocksClient::waitForConnected(int msecs)
return false;
}
+qint64 QXmppSocksClient::write(const QByteArray &data)
+{
+ return m_socket->write(data);
+}
+
QXmppSocksServer::QXmppSocksServer(QObject *parent)
: QObject(parent),
m_hostPort(0),
@@ -257,6 +266,11 @@ bool QXmppSocksServer::listen(const QHostAddress &address, quint16 port)
return m_server->listen(address, port);
}
+QByteArray QXmppSocksServer::readAll()
+{
+ return m_socket->readAll();
+}
+
QHostAddress QXmppSocksServer::serverAddress() const
{
return m_server->serverAddress();
@@ -289,14 +303,30 @@ void QXmppSocksServer::slotReadyRead()
QByteArray buffer = m_socket->readAll();
if (buffer.size() < 3 ||
buffer.at(0) != SocksVersion ||
- buffer.at(1) != 0x01 ||
- buffer.at(2) != NoAuthentication)
+ buffer.at(1) + 2 != buffer.size())
{
qWarning("QXmppSocksServer received invalid handshake");
m_socket->close();
return;
}
+ // check authentication method
+ bool foundMethod = false;
+ for (int i = 2; i < buffer.size(); i++)
+ {
+ if (buffer.at(i) == NoAuthentication)
+ {
+ foundMethod = true;
+ break;
+ }
+ }
+ if (!foundMethod)
+ {
+ qWarning("QXmppSocksServer received bad authentication method");
+ m_socket->close();
+ return;
+ }
+
// send connect to server response
buffer.resize(2);
buffer[0] = SocksVersion;
@@ -342,12 +372,17 @@ void QXmppSocksServer::slotReadyRead()
buffer[2] = 0x00;
buffer.append(encodeHostAndPort(
DomainName,
- m_server->serverAddress().toString().toAscii(),
- m_server->serverPort()));
+ m_hostName.toAscii(),
+ m_hostPort));
m_socket->write(buffer);
- // connect signals
+ // from now on, forward signals
connect(m_socket, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64)));
+ disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
+ connect(m_socket, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
+
+ // notify of connection
+ emit connected();
}
}
@@ -361,9 +396,11 @@ void QXmppSocksServer::setHostPort(quint16 hostPort)
m_hostPort = hostPort;
}
-void QXmppSocksServer::write(const QByteArray &data)
+qint64 QXmppSocksServer::write(const QByteArray &data)
{
if (m_socket)
- m_socket->write(data);
+ return m_socket->write(data);
+ else
+ return -1;
}