X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fprotocols%2Flegacy%2Flegacypeer.cpp;h=9ceb4607ee831cc68241efdaf6376636859e4431;hp=b292223e34dff3051b81959a0c6db7f036460511;hb=3856cfd6126cdea97c91a22be7ded92ccc0a3577;hpb=208ccb6d91ebb3c26a67c35c11411ba3ab27708a diff --git a/src/common/protocols/legacy/legacypeer.cpp b/src/common/protocols/legacy/legacypeer.cpp index b292223e..9ceb4607 100644 --- a/src/common/protocols/legacy/legacypeer.cpp +++ b/src/common/protocols/legacy/legacypeer.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2014 by the Quassel Project * + * Copyright (C) 2005-2016 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -19,10 +19,10 @@ ***************************************************************************/ #include +#include #include #include "legacypeer.h" -#include "quassel.h" /* version.inc is no longer used for this */ const uint protocolVersion = 10; @@ -31,13 +31,11 @@ const uint clientNeedsProtocol = protocolVersion; using namespace Protocol; -LegacyPeer::LegacyPeer(::AuthHandler *authHandler, QTcpSocket *socket, QObject *parent) - : RemotePeer(authHandler, socket, parent), - _blockSize(0), +LegacyPeer::LegacyPeer(::AuthHandler *authHandler, QTcpSocket *socket, Compressor::CompressionLevel level, QObject *parent) + : RemotePeer(authHandler, socket, level, parent), _useCompression(false) { - _stream.setDevice(socket); - _stream.setVersion(QDataStream::Qt_4_2); + } @@ -56,56 +54,22 @@ void LegacyPeer::setSignalProxy(::SignalProxy *proxy) } -void LegacyPeer::onSocketDataAvailable() -{ - QVariant item; - while (readSocketData(item)) { - // if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere - if (!signalProxy()) - handleHandshakeMessage(item); - else - handlePackedFunc(item); - } -} - - -bool LegacyPeer::readSocketData(QVariant &item) +void LegacyPeer::processMessage(const QByteArray &msg) { - if (_blockSize == 0) { - if (socket()->bytesAvailable() < 4) - return false; - _stream >> _blockSize; - } - - if (_blockSize > 1 << 22) { - close("Peer tried to send package larger than max package size!"); - return false; - } - - if (_blockSize == 0) { - close("Peer tried to send 0 byte package!"); - return false; - } - - if (socket()->bytesAvailable() < _blockSize) { - emit transferProgress(socket()->bytesAvailable(), _blockSize); - return false; - } - - emit transferProgress(_blockSize, _blockSize); - - _blockSize = 0; + QDataStream stream(msg); + stream.setVersion(QDataStream::Qt_4_2); + QVariant item; if (_useCompression) { QByteArray rawItem; - _stream >> rawItem; + stream >> rawItem; int nbytes = rawItem.size(); if (nbytes <= 4) { const char *data = rawItem.constData(); if (nbytes < 4 || (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0)) { close("Peer sent corrupted compressed data!"); - return false; + return; } } @@ -116,25 +80,24 @@ bool LegacyPeer::readSocketData(QVariant &item) itemStream >> item; } else { - _stream >> item; + stream >> item; } - if (!item.isValid()) { + if (stream.status() != QDataStream::Ok || !item.isValid()) { close("Peer sent corrupt data: unable to load QVariant!"); - return false; + return; } - return true; + // if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere + if (!signalProxy()) + handleHandshakeMessage(item); + else + handlePackedFunc(item); } -void LegacyPeer::writeSocketData(const QVariant &item) +void LegacyPeer::writeMessage(const QVariant &item) { - if (!socket()->isOpen()) { - qWarning() << Q_FUNC_INFO << "Can't write to a closed socket!"; - return; - } - QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_2); @@ -153,7 +116,7 @@ void LegacyPeer::writeSocketData(const QVariant &item) out << item; } - _stream << block; // also writes the length as part of the serialization format + writeMessage(block); } @@ -188,7 +151,7 @@ void LegacyPeer::handleHandshakeMessage(const QVariant &msg) socket()->setProperty("UseCompression", true); } #endif - handle(RegisterClient(m["ClientVersion"].toString(), m["UseSsl"].toBool())); + handle(RegisterClient(m["ClientVersion"].toString(), m["ClientDate"].toString(), m["UseSsl"].toBool())); } else if (msgType == "ClientInitReject") { @@ -207,7 +170,7 @@ void LegacyPeer::handleHandshakeMessage(const QVariant &msg) socket()->setProperty("UseCompression", true); #endif - handle(ClientRegistered(m["CoreFeatures"].toUInt(), m["Configured"].toBool(), m["StorageBackends"].toList(), m["SupportSsl"].toBool(), QDateTime())); + handle(ClientRegistered(m["CoreFeatures"].toUInt(), m["Configured"].toBool(), m["StorageBackends"].toList(), m["SupportSsl"].toBool())); } else if (msgType == "CoreSetupData") { @@ -250,7 +213,7 @@ void LegacyPeer::dispatch(const RegisterClient &msg) { QVariantMap m; m["MsgType"] = "ClientInit"; m["ClientVersion"] = msg.clientVersion; - m["ClientDate"] = Quassel::buildInfo().buildDate; + m["ClientDate"] = msg.buildDate; // FIXME only in compat mode m["ProtocolVersion"] = protocolVersion; @@ -261,7 +224,7 @@ void LegacyPeer::dispatch(const RegisterClient &msg) { m["UseCompression"] = false; #endif - writeSocketData(m); + writeMessage(m); } @@ -270,7 +233,7 @@ void LegacyPeer::dispatch(const ClientDenied &msg) { m["MsgType"] = "ClientInitReject"; m["Error"] = msg.errorString; - writeSocketData(m); + writeMessage(m); } @@ -285,20 +248,12 @@ void LegacyPeer::dispatch(const ClientRegistered &msg) { m["SupportSsl"] = msg.sslSupported; m["SupportsCompression"] = socket()->property("UseCompression").toBool(); // this property gets already set in the ClientInit handler - // This is only used for old v10 clients (pre-0.5) - int uptime = msg.coreStartTime.secsTo(QDateTime::currentDateTime().toUTC()); - int updays = uptime / 86400; uptime %= 86400; - int uphours = uptime / 3600; uptime %= 3600; - int upmins = uptime / 60; - m["CoreInfo"] = tr("Quassel Core Version %1
" - "Built: %2
" - "Up %3d%4h%5m (since %6)").arg(Quassel::buildInfo().fancyVersionString) - .arg(Quassel::buildInfo().buildDate) - .arg(updays).arg(uphours, 2, 10, QChar('0')).arg(upmins, 2, 10, QChar('0')).arg(msg.coreStartTime.toString(Qt::TextDate)); + // This is only used for display by really old v10 clients (pre-0.5), and we no longer set this + m["CoreInfo"] = QString(); m["LoginEnabled"] = m["Configured"] = msg.coreConfigured; - writeSocketData(m); + writeMessage(m); } @@ -313,7 +268,7 @@ void LegacyPeer::dispatch(const SetupData &msg) QVariantMap m; m["MsgType"] = "CoreSetupData"; m["SetupData"] = map; - writeSocketData(m); + writeMessage(m); } @@ -323,7 +278,7 @@ void LegacyPeer::dispatch(const SetupFailed &msg) m["MsgType"] = "CoreSetupReject"; m["Error"] = msg.errorString; - writeSocketData(m); + writeMessage(m); } @@ -334,7 +289,7 @@ void LegacyPeer::dispatch(const SetupDone &msg) QVariantMap m; m["MsgType"] = "CoreSetupAck"; - writeSocketData(m); + writeMessage(m); } @@ -345,7 +300,7 @@ void LegacyPeer::dispatch(const Login &msg) m["User"] = msg.user; m["Password"] = msg.password; - writeSocketData(m); + writeMessage(m); } @@ -355,7 +310,7 @@ void LegacyPeer::dispatch(const LoginFailed &msg) m["MsgType"] = "ClientLoginReject"; m["Error"] = msg.errorString; - writeSocketData(m); + writeMessage(m); } @@ -366,7 +321,7 @@ void LegacyPeer::dispatch(const LoginSuccess &msg) QVariantMap m; m["MsgType"] = "ClientLoginAck"; - writeSocketData(m); + writeMessage(m); } @@ -381,7 +336,7 @@ void LegacyPeer::dispatch(const SessionState &msg) map["Identities"] = msg.identities; m["SessionState"] = map; - writeSocketData(m); + writeMessage(m); } @@ -518,7 +473,7 @@ void LegacyPeer::dispatch(const Protocol::HeartBeatReply &msg) void LegacyPeer::dispatchPackedFunc(const QVariantList &packedFunc) { - writeSocketData(QVariant(packedFunc)); + writeMessage(QVariant(packedFunc)); }