-
Notifications
You must be signed in to change notification settings - Fork 218
/
datastreampeer.cpp
431 lines (341 loc) · 12.8 KB
/
datastreampeer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/***************************************************************************
* Copyright (C) 2005-2018 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) version 3. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QtEndian>
#include <QDataStream>
#include <QHostAddress>
#include <QTcpSocket>
#include "datastreampeer.h"
#include "quassel.h"
#include "serializers/serializers.h"
using namespace Protocol;
DataStreamPeer::DataStreamPeer(::AuthHandler *authHandler, QTcpSocket *socket, quint16 features, Compressor::CompressionLevel level, QObject *parent)
: RemotePeer(authHandler, socket, level, parent)
{
Q_UNUSED(features);
}
quint16 DataStreamPeer::supportedFeatures()
{
return 0;
}
bool DataStreamPeer::acceptsFeatures(quint16 peerFeatures)
{
Q_UNUSED(peerFeatures);
return true;
}
quint16 DataStreamPeer::enabledFeatures() const
{
return 0;
}
void DataStreamPeer::processMessage(const QByteArray &msg)
{
QDataStream stream(msg);
stream.setVersion(QDataStream::Qt_4_2);
QVariantList list;
if (!Serializers::deserialize(stream, features(), list))
close("Peer sent corrupt data, closing down!");
if (stream.status() != QDataStream::Ok) {
close("Peer sent corrupt data, closing down!");
return;
}
// if no sigproxy is set, we're in handshake mode
if (!signalProxy())
handleHandshakeMessage(list);
else
handlePackedFunc(list);
}
void DataStreamPeer::writeMessage(const QVariantMap &handshakeMsg)
{
QVariantList list;
QVariantMap::const_iterator it = handshakeMsg.begin();
while (it != handshakeMsg.end()) {
list << it.key().toUtf8() << it.value();
++it;
}
writeMessage(list);
}
void DataStreamPeer::writeMessage(const QVariantList &sigProxyMsg)
{
QByteArray data;
QDataStream msgStream(&data, QIODevice::WriteOnly);
msgStream.setVersion(QDataStream::Qt_4_2);
msgStream << sigProxyMsg;
writeMessage(data);
}
/*** Handshake messages ***/
/* These messages are transmitted during handshake phase, which in case of the legacy protocol means they have
* a structure different from those being used after the handshake.
* Also, the legacy handshake does not fully match the redesigned one, so we'll have to do various mappings here.
*/
void DataStreamPeer::handleHandshakeMessage(const QVariantList &mapData)
{
QVariantMap m;
for (int i = 0; i < mapData.count()/2; ++i)
m[QString::fromUtf8(mapData[2*i].toByteArray())] = mapData[2*i+1];
QString msgType = m["MsgType"].toString();
if (msgType.isEmpty()) {
emit protocolError(tr("Invalid handshake message!"));
return;
}
if (msgType == "ClientInit") {
handle(RegisterClient{Quassel::Features{m["FeatureList"].toStringList(), Quassel::LegacyFeatures(m["Features"].toUInt())},
m["ClientVersion"].toString(),
m["ClientDate"].toString(),
false // UseSsl obsolete
});
}
else if (msgType == "ClientInitReject") {
handle(ClientDenied(m["Error"].toString()));
}
else if (msgType == "ClientInitAck") {
handle(ClientRegistered{Quassel::Features{m["FeatureList"].toStringList(), Quassel::LegacyFeatures(m["CoreFeatures"].toUInt())},
m["Configured"].toBool(),
m["StorageBackends"].toList(),
m["Authenticators"].toList(),
false // SupportsSsl obsolete
});
}
else if (msgType == "CoreSetupData") {
QVariantMap map = m["SetupData"].toMap();
handle(SetupData(map["AdminUser"].toString(), map["AdminPasswd"].toString(), map["Backend"].toString(), map["ConnectionProperties"].toMap(), map["Authenticator"].toString(), map["AuthProperties"].toMap()));
}
else if (msgType == "CoreSetupReject") {
handle(SetupFailed(m["Error"].toString()));
}
else if (msgType == "CoreSetupAck") {
handle(SetupDone());
}
else if (msgType == "ClientLogin") {
handle(Login(m["User"].toString(), m["Password"].toString()));
}
else if (msgType == "ClientLoginReject") {
handle(LoginFailed(m["Error"].toString()));
}
else if (msgType == "ClientLoginAck") {
handle(LoginSuccess());
}
else if (msgType == "SessionInit") {
QVariantMap map = m["SessionState"].toMap();
handle(SessionState(map["Identities"].toList(), map["BufferInfos"].toList(), map["NetworkIds"].toList()));
}
else {
emit protocolError(tr("Unknown protocol message of type %1").arg(msgType));
}
}
void DataStreamPeer::dispatch(const RegisterClient &msg) {
QVariantMap m;
m["MsgType"] = "ClientInit";
m["Features"] = static_cast<quint32>(msg.features.toLegacyFeatures());
m["FeatureList"] = msg.features.toStringList();
m["ClientVersion"] = msg.clientVersion;
m["ClientDate"] = msg.buildDate;
writeMessage(m);
}
void DataStreamPeer::dispatch(const ClientDenied &msg) {
QVariantMap m;
m["MsgType"] = "ClientInitReject";
m["Error"] = msg.errorString;
writeMessage(m);
}
void DataStreamPeer::dispatch(const ClientRegistered &msg) {
QVariantMap m;
m["MsgType"] = "ClientInitAck";
if (hasFeature(Quassel::Feature::ExtendedFeatures)) {
m["FeatureList"] = msg.features.toStringList();
}
else {
m["CoreFeatures"] = static_cast<quint32>(msg.features.toLegacyFeatures());
}
m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
m["StorageBackends"] = msg.backendInfo;
if (hasFeature(Quassel::Feature::Authenticators)) {
m["Authenticators"] = msg.authenticatorInfo;
}
writeMessage(m);
}
void DataStreamPeer::dispatch(const SetupData &msg)
{
QVariantMap map;
map["AdminUser"] = msg.adminUser;
map["AdminPasswd"] = msg.adminPassword;
map["Backend"] = msg.backend;
map["ConnectionProperties"] = msg.setupData;
// Auth backend properties.
map["Authenticator"] = msg.authenticator;
map["AuthProperties"] = msg.authSetupData;
QVariantMap m;
m["MsgType"] = "CoreSetupData";
m["SetupData"] = map;
writeMessage(m);
}
void DataStreamPeer::dispatch(const SetupFailed &msg)
{
QVariantMap m;
m["MsgType"] = "CoreSetupReject";
m["Error"] = msg.errorString;
writeMessage(m);
}
void DataStreamPeer::dispatch(const SetupDone &msg)
{
Q_UNUSED(msg)
QVariantMap m;
m["MsgType"] = "CoreSetupAck";
writeMessage(m);
}
void DataStreamPeer::dispatch(const Login &msg)
{
QVariantMap m;
m["MsgType"] = "ClientLogin";
m["User"] = msg.user;
m["Password"] = msg.password;
writeMessage(m);
}
void DataStreamPeer::dispatch(const LoginFailed &msg)
{
QVariantMap m;
m["MsgType"] = "ClientLoginReject";
m["Error"] = msg.errorString;
writeMessage(m);
}
void DataStreamPeer::dispatch(const LoginSuccess &msg)
{
Q_UNUSED(msg)
QVariantMap m;
m["MsgType"] = "ClientLoginAck";
writeMessage(m);
}
void DataStreamPeer::dispatch(const SessionState &msg)
{
QVariantMap m;
m["MsgType"] = "SessionInit";
QVariantMap map;
map["BufferInfos"] = msg.bufferInfos;
map["NetworkIds"] = msg.networkIds;
map["Identities"] = msg.identities;
m["SessionState"] = map;
writeMessage(m);
}
/*** Standard messages ***/
void DataStreamPeer::handlePackedFunc(const QVariantList &packedFunc)
{
QVariantList params(packedFunc);
if (params.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Received incompatible data:" << packedFunc;
return;
}
// TODO: make sure that this is a valid request type
RequestType requestType = (RequestType)params.takeFirst().value<qint16>();
switch (requestType) {
case Sync: {
if (params.count() < 3) {
qWarning() << Q_FUNC_INFO << "Received invalid sync call:" << params;
return;
}
QByteArray className = params.takeFirst().toByteArray();
QString objectName = QString::fromUtf8(params.takeFirst().toByteArray());
QByteArray slotName = params.takeFirst().toByteArray();
handle(Protocol::SyncMessage(className, objectName, slotName, params));
break;
}
case RpcCall: {
if (params.empty()) {
qWarning() << Q_FUNC_INFO << "Received empty RPC call!";
return;
}
QByteArray slotName = params.takeFirst().toByteArray();
handle(Protocol::RpcCall(slotName, params));
break;
}
case InitRequest: {
if (params.count() != 2) {
qWarning() << Q_FUNC_INFO << "Received invalid InitRequest:" << params;
return;
}
QByteArray className = params[0].toByteArray();
QString objectName = QString::fromUtf8(params[1].toByteArray());
handle(Protocol::InitRequest(className, objectName));
break;
}
case InitData: {
if (params.count() < 2) {
qWarning() << Q_FUNC_INFO << "Received invalid InitData:" << params;
return;
}
QByteArray className = params.takeFirst().toByteArray();
QString objectName = QString::fromUtf8(params.takeFirst().toByteArray());
QVariantMap initData;
for (int i = 0; i < params.count()/2; ++i)
initData[QString::fromUtf8(params[2*i].toByteArray())] = params[2*i+1];
handle(Protocol::InitData(className, objectName, initData));
break;
}
case HeartBeat: {
if (params.count() != 1) {
qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
return;
}
// Note: QDateTime instead of QTime as in the legacy protocol!
handle(Protocol::HeartBeat(params[0].toDateTime()));
break;
}
case HeartBeatReply: {
if (params.count() != 1) {
qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
return;
}
// Note: QDateTime instead of QTime as in the legacy protocol!
handle(Protocol::HeartBeatReply(params[0].toDateTime()));
break;
}
}
}
void DataStreamPeer::dispatch(const Protocol::SyncMessage &msg)
{
dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className << msg.objectName.toUtf8() << msg.slotName << msg.params);
}
void DataStreamPeer::dispatch(const Protocol::RpcCall &msg)
{
dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.slotName << msg.params);
}
void DataStreamPeer::dispatch(const Protocol::InitRequest &msg)
{
dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className << msg.objectName.toUtf8());
}
void DataStreamPeer::dispatch(const Protocol::InitData &msg)
{
QVariantList initData;
QVariantMap::const_iterator it = msg.initData.begin();
while (it != msg.initData.end()) {
initData << it.key().toUtf8() << it.value();
++it;
}
dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName.toUtf8() << initData);
}
void DataStreamPeer::dispatch(const Protocol::HeartBeat &msg)
{
dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp);
}
void DataStreamPeer::dispatch(const Protocol::HeartBeatReply &msg)
{
dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp);
}
void DataStreamPeer::dispatchPackedFunc(const QVariantList &packedFunc)
{
writeMessage(packedFunc);
}