1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QHostAddress>
24 #include "datastreampeer.h"
27 using namespace Protocol;
29 DataStreamPeer::DataStreamPeer(::AuthHandler *authHandler, QTcpSocket *socket, quint16 features, QObject *parent)
30 : RemotePeer(authHandler, socket, parent),
35 _stream.setDevice(socket);
36 _stream.setVersion(QDataStream::Qt_4_2);
40 quint16 DataStreamPeer::supportedFeatures()
46 bool DataStreamPeer::acceptsFeatures(quint16 peerFeatures)
48 Q_UNUSED(peerFeatures);
53 quint16 DataStreamPeer::enabledFeatures() const
59 void DataStreamPeer::onSocketDataAvailable()
62 while (readSocketData(item)) {
63 // if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere
65 handleHandshakeMessage(item);
67 handlePackedFunc(item);
72 bool DataStreamPeer::readSocketData(QVariant &item)
74 if (_blockSize == 0) {
75 if (socket()->bytesAvailable() < 4)
77 _stream >> _blockSize;
80 if (_blockSize > 1 << 22) {
81 close("Peer tried to send package larger than max package size!");
85 if (_blockSize == 0) {
86 close("Peer tried to send 0 byte package!");
90 if (socket()->bytesAvailable() < _blockSize) {
91 emit transferProgress(socket()->bytesAvailable(), _blockSize);
95 emit transferProgress(_blockSize, _blockSize);
100 if (!item.isValid()) {
101 close("Peer sent corrupt data: unable to load QVariant!");
109 void DataStreamPeer::writeSocketData(const QVariant &item)
111 if (!socket()->isOpen()) {
112 qWarning() << Q_FUNC_INFO << "Can't write to a closed socket!";
117 QDataStream out(&block, QIODevice::WriteOnly);
118 out.setVersion(QDataStream::Qt_4_2);
122 _stream << block; // also writes the length as part of the serialization format
126 /*** Handshake messages ***/
128 /* These messages are transmitted during handshake phase, which in case of the legacy protocol means they have
129 * a structure different from those being used after the handshake.
130 * Also, the legacy handshake does not fully match the redesigned one, so we'll have to do various mappings here.
133 void DataStreamPeer::handleHandshakeMessage(const QVariant &msg)
135 QVariantMap m = msg.toMap();
137 QString msgType = m["MsgType"].toString();
138 if (msgType.isEmpty()) {
139 emit protocolError(tr("Invalid handshake message!"));
143 if (msgType == "ClientInit") {
144 handle(RegisterClient(m["ClientVersion"].toString(), false)); // UseSsl obsolete
147 else if (msgType == "ClientInitReject") {
148 handle(ClientDenied(m["Error"].toString()));
151 else if (msgType == "ClientInitAck") {
152 handle(ClientRegistered(m["CoreFeatures"].toUInt(), m["Configured"].toBool(), m["StorageBackends"].toList(), false, QDateTime())); // SupportsSsl and coreStartTime obsolete
155 else if (msgType == "CoreSetupData") {
156 QVariantMap map = m["SetupData"].toMap();
157 handle(SetupData(map["AdminUser"].toString(), map["AdminPasswd"].toString(), map["Backend"].toString(), map["ConnectionProperties"].toMap()));
160 else if (msgType == "CoreSetupReject") {
161 handle(SetupFailed(m["Error"].toString()));
164 else if (msgType == "CoreSetupAck") {
168 else if (msgType == "ClientLogin") {
169 handle(Login(m["User"].toString(), m["Password"].toString()));
172 else if (msgType == "ClientLoginReject") {
173 handle(LoginFailed(m["Error"].toString()));
176 else if (msgType == "ClientLoginAck") {
177 handle(LoginSuccess());
180 else if (msgType == "SessionInit") {
181 QVariantMap map = m["SessionState"].toMap();
182 handle(SessionState(map["Identities"].toList(), map["BufferInfos"].toList(), map["NetworkIds"].toList()));
186 emit protocolError(tr("Unknown protocol message of type %1").arg(msgType));
191 void DataStreamPeer::dispatch(const RegisterClient &msg) {
193 m["MsgType"] = "ClientInit";
194 m["ClientVersion"] = msg.clientVersion;
195 m["ClientDate"] = Quassel::buildInfo().buildDate;
201 void DataStreamPeer::dispatch(const ClientDenied &msg) {
203 m["MsgType"] = "ClientInitReject";
204 m["Error"] = msg.errorString;
210 void DataStreamPeer::dispatch(const ClientRegistered &msg) {
212 m["MsgType"] = "ClientInitAck";
213 m["CoreFeatures"] = msg.coreFeatures;
214 m["StorageBackends"] = msg.backendInfo;
215 m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
221 void DataStreamPeer::dispatch(const SetupData &msg)
224 map["AdminUser"] = msg.adminUser;
225 map["AdminPasswd"] = msg.adminPassword;
226 map["Backend"] = msg.backend;
227 map["ConnectionProperties"] = msg.setupData;
230 m["MsgType"] = "CoreSetupData";
231 m["SetupData"] = map;
236 void DataStreamPeer::dispatch(const SetupFailed &msg)
239 m["MsgType"] = "CoreSetupReject";
240 m["Error"] = msg.errorString;
246 void DataStreamPeer::dispatch(const SetupDone &msg)
251 m["MsgType"] = "CoreSetupAck";
257 void DataStreamPeer::dispatch(const Login &msg)
260 m["MsgType"] = "ClientLogin";
261 m["User"] = msg.user;
262 m["Password"] = msg.password;
268 void DataStreamPeer::dispatch(const LoginFailed &msg)
271 m["MsgType"] = "ClientLoginReject";
272 m["Error"] = msg.errorString;
278 void DataStreamPeer::dispatch(const LoginSuccess &msg)
283 m["MsgType"] = "ClientLoginAck";
289 void DataStreamPeer::dispatch(const SessionState &msg)
292 m["MsgType"] = "SessionInit";
295 map["BufferInfos"] = msg.bufferInfos;
296 map["NetworkIds"] = msg.networkIds;
297 map["Identities"] = msg.identities;
298 m["SessionState"] = map;
304 /*** Standard messages ***/
306 void DataStreamPeer::handlePackedFunc(const QVariant &packedFunc)
308 QVariantList params(packedFunc.toList());
310 if (params.isEmpty()) {
311 qWarning() << Q_FUNC_INFO << "Received incompatible data:" << packedFunc;
315 // TODO: make sure that this is a valid request type
316 RequestType requestType = (RequestType)params.takeFirst().value<int>();
317 switch (requestType) {
319 if (params.count() < 3) {
320 qWarning() << Q_FUNC_INFO << "Received invalid sync call:" << params;
323 QByteArray className = params.takeFirst().toByteArray();
324 QString objectName = QString::fromUtf8(params.takeFirst().toByteArray());
325 QByteArray slotName = params.takeFirst().toByteArray();
326 handle(Protocol::SyncMessage(className, objectName, slotName, params));
330 if (params.empty()) {
331 qWarning() << Q_FUNC_INFO << "Received empty RPC call!";
334 QByteArray slotName = params.takeFirst().toByteArray();
335 handle(Protocol::RpcCall(slotName, params));
339 if (params.count() != 2) {
340 qWarning() << Q_FUNC_INFO << "Received invalid InitRequest:" << params;
343 QByteArray className = params[0].toByteArray();
344 QString objectName = QString::fromUtf8(params[1].toByteArray());
345 handle(Protocol::InitRequest(className, objectName));
349 if (params.count() != 3) {
350 qWarning() << Q_FUNC_INFO << "Received invalid InitData:" << params;
353 QByteArray className = params[0].toByteArray();
354 QString objectName = QString::fromUtf8(params[1].toByteArray());
355 QVariantMap initData = params[2].toMap();
356 handle(Protocol::InitData(className, objectName, initData));
360 if (params.count() != 1) {
361 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
364 // The legacy protocol would only send a QTime, no QDateTime
365 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
366 QDateTime dateTime = QDateTime::currentDateTime().toUTC();
367 dateTime.setTime(params[0].toTime());
368 handle(Protocol::HeartBeat(dateTime));
371 case HeartBeatReply: {
372 if (params.count() != 1) {
373 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
376 // The legacy protocol would only send a QTime, no QDateTime
377 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
378 QDateTime dateTime = QDateTime::currentDateTime().toUTC();
379 dateTime.setTime(params[0].toTime());
380 handle(Protocol::HeartBeatReply(dateTime));
388 void DataStreamPeer::dispatch(const Protocol::SyncMessage &msg)
390 dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className << msg.objectName.toUtf8() << msg.slotName << msg.params);
394 void DataStreamPeer::dispatch(const Protocol::RpcCall &msg)
396 dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.slotName << msg.params);
400 void DataStreamPeer::dispatch(const Protocol::InitRequest &msg)
402 dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className << msg.objectName.toUtf8());
406 void DataStreamPeer::dispatch(const Protocol::InitData &msg)
408 dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName.toUtf8() << msg.initData);
412 void DataStreamPeer::dispatch(const Protocol::HeartBeat &msg)
414 dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp.time());
418 void DataStreamPeer::dispatch(const Protocol::HeartBeatReply &msg)
420 dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp.time());
424 void DataStreamPeer::dispatchPackedFunc(const QVariantList &packedFunc)
426 writeSocketData(QVariant(packedFunc));