-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstractconnection.cpp
755 lines (697 loc) · 22.9 KB
/
abstractconnection.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/****************************************************************************
**
** Ireen — cross-platform OSCAR protocol library
**
** Copyright © 2012 Ruslan Nigmatullin <[email protected]>
** Alexey Prokhin <[email protected]>
**
*****************************************************************************
**
** $IREEN_BEGIN_LICENSE$
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as
** published by the Free Software Foundation, either version 3
** of the License, or (at your option) any later version.
**
** 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 Lesser General Public License
** along with this program. If not, see http://www.gnu.org/licenses/.
** $IREEN_END_LICENSE$
**
****************************************************************************/
#include "abstractconnection_p.h"
#include <QHostInfo>
#include <QBuffer>
#include <QCoreApplication>
#include <QNetworkProxy>
namespace Ireen {
ProtocolError::ProtocolError(const SNAC &snac)
{
m_code = snac.read<qint16>();
m_subcode = 0;
m_tlvs = snac.read<TLVMap>();
if (m_tlvs.contains(0x08)) {
DataUnit data(m_tlvs.value(0x08));
m_subcode = data.read<qint16>();
}
}
QString ProtocolError::errorString()
{
switch (m_code) {
case (0x01):
return QT_TRANSLATE_NOOP("ProtocolError", "Invalid SNAC header");
case (0x02):
return QT_TRANSLATE_NOOP("ProtocolError", "Server rate limit exceeded");
case (0x03):
return QT_TRANSLATE_NOOP("ProtocolError", "Client rate limit exceeded");
case (0x04):
return QT_TRANSLATE_NOOP("ProtocolError", "Recipient is not logged in");
case (0x05):
return QT_TRANSLATE_NOOP("ProtocolError", "Requested service unavailable");
case (0x06):
return QT_TRANSLATE_NOOP("ProtocolError", "Requested service not defined");
case (0x07):
return QT_TRANSLATE_NOOP("ProtocolError", "You sent obsolete SNAC");
case (0x08):
return QT_TRANSLATE_NOOP("ProtocolError", "Not supported by server");
case (0x09):
return QT_TRANSLATE_NOOP("ProtocolError", "Not supported by client");
case (0x0A):
return QT_TRANSLATE_NOOP("ProtocolError", "Refused by client");
case (0x0B):
return QT_TRANSLATE_NOOP("ProtocolError", "Reply too big");
case (0x0C):
return QT_TRANSLATE_NOOP("ProtocolError", "Responses lost");
case (0x0D):
return QT_TRANSLATE_NOOP("ProtocolError", "Request denied");
case (0x0E):
return QT_TRANSLATE_NOOP("ProtocolError", "Incorrect SNAC format");
case (0x0F):
return QT_TRANSLATE_NOOP("ProtocolError", "Insufficient rights");
case (0x10):
return QT_TRANSLATE_NOOP("ProtocolError", "In local permit/deny (recipient blocked)");
case (0x11):
return QT_TRANSLATE_NOOP("ProtocolError", "Sender too evil");
case (0x12):
return QT_TRANSLATE_NOOP("ProtocolError", "Receiver too evil");
case (0x13):
return QT_TRANSLATE_NOOP("ProtocolError", "User temporarily unavailable");
case (0x14):
return QT_TRANSLATE_NOOP("ProtocolError", "No match");
case (0x15):
return QT_TRANSLATE_NOOP("ProtocolError", "List overflow");
case (0x16):
return QT_TRANSLATE_NOOP("ProtocolError", "Request ambiguous");
case (0x17):
return QT_TRANSLATE_NOOP("ProtocolError", "Server queue full");
case (0x18):
return QT_TRANSLATE_NOOP("ProtocolError", "Not while on AOL");
default:
return QT_TRANSLATE_NOOP("ProtocolError", "Unknown error");
}
}
ConnectionRate::ConnectionRate(const SNAC &sn, AbstractConnection *conn) :
m_conn(conn)
{
m_groupId = sn.read<quint16>();
update(sn);
}
void ConnectionRate::update(const SNAC &sn)
{
m_windowSize = sn.read<quint32>();
m_clearLevel = sn.read<quint32>();
#if !MINIMIZE_RATE_MEMORY_USAGE
m_alertLevel = sn.read<quint32>();
m_limitLevel = sn.read<quint32>();
m_disconnectLevel = sn.read<quint32>();
#else
sn.skipData(12);
#endif
m_currentLevel = sn.read<quint32>();
m_maxLevel = sn.read<quint32>();
m_lastTimeDiff = sn.read<quint32>();
#if !MINIMIZE_RATE_MEMORY_USAGE
m_currentState = sn.read<quint8>();
#else
sn.skipData(1);
#endif
m_time = QDateTime::currentDateTime().addMSecs(-qint32(m_lastTimeDiff));
m_defaultPriority = (m_clearLevel + m_maxLevel) / 2;
}
void ConnectionRate::send(const SNAC &snac, bool priority)
{
QQueue<SNAC> &queue = priority ? m_highPriorityQueue : m_lowPriorityQueue;
queue.enqueue(snac);
if (!m_timer.isActive()) {
sendNextPackets();
if (!queue.isEmpty())
m_timer.start(500, this);
}
}
bool ConnectionRate::testRate(bool priority)
{
quint32 timeDiff = getTimeDiff(QDateTime::currentDateTime());
quint32 newLevel = (m_currentLevel * (m_windowSize - 1) + timeDiff) / m_windowSize;
return newLevel > (priority ? m_clearLevel : m_defaultPriority);
}
void ConnectionRate::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_timer.timerId())
sendNextPackets();
}
void ConnectionRate::sendNextPackets()
{
Q_ASSERT(!m_highPriorityQueue.isEmpty() || !m_lowPriorityQueue.isEmpty());
QDateTime dateTime = QDateTime::currentDateTime();
quint32 timeDiff = getTimeDiff(dateTime);
quint32 newLevel;
forever {
bool priority = !m_highPriorityQueue.isEmpty();
if (!priority && m_lowPriorityQueue.isEmpty()) {
m_timer.stop();
break;
}
newLevel = (m_currentLevel * (m_windowSize - 1) + timeDiff) / m_windowSize;
if (newLevel < (priority ? m_clearLevel : m_defaultPriority))
break;
SNAC snac = priority ? m_highPriorityQueue.dequeue() : m_lowPriorityQueue.dequeue();
m_lastTimeDiff = timeDiff;
m_time = dateTime;
timeDiff = 0;
m_currentLevel = qMin(newLevel, m_maxLevel);
m_conn->sendSnac(snac);
}
}
quint32 ConnectionRate::getTimeDiff(const QDateTime &dateTime)
{
if (dateTime.date() == m_time.date())
return m_time.time().msecsTo(dateTime.time());
else if (m_time.daysTo(dateTime) == 1)
return 86400000 - m_time.time().msec() + dateTime.time().msec();
else // That should never happen
return 86400000;
}
void AbstractConnectionPrivate::init(AbstractConnection *q)
{
aliveTimer.setInterval(180000);
q->connect(&aliveTimer, SIGNAL(timeout()), SLOT(sendAlivePacket()));
socket = new Socket(q);
#if IREEN_SSL_SUPPORT
socket->setProtocol(QSsl::TlsV1);
socket->setPeerVerifyMode(QSslSocket::VerifyNone); // TODO:
#endif
q->connect(socket, SIGNAL(readyRead()), SLOT(readData()));
q->connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
SLOT(stateChanged(QAbstractSocket::SocketState)));
q->connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(error(QAbstractSocket::SocketError)));
{
ClientInfo info =
{ "ICQ Client", 266, 20, 52, 1, 3916, 85, "en", "us" };
clientInfo = info;
}
id = (quint32) qrand();
error = AbstractConnection::NoError;
q->m_infos << SNACInfo(ServiceFamily, ServiceServerReady)
<< SNACInfo(ServiceFamily, ServiceServerNameInfo)
<< SNACInfo(ServiceFamily, ServiceServerFamilies2)
<< SNACInfo(ServiceFamily, ServiceServerAsksServices)
<< SNACInfo(ServiceFamily, ServiceServerRateChange)
<< SNACInfo(ServiceFamily, ServiceError);
QList<SNACInfo> initSnacs;
initSnacs << SNACInfo(ServiceFamily, ServiceClientFamilies)
<< SNACInfo(ServiceFamily, ServiceClientReqRateInfo)
<< SNACInfo(ServiceFamily, ServiceClientRateAck)
<< SNACInfo(ServiceFamily, ServiceClientReady)
<< SNACInfo(ServiceFamily, ServiceClientNewService);
q->registerInitializationSnacs(initSnacs);
}
AbstractConnection::AbstractConnection(QObject *parent) :
QObject(parent), d_ptr(new AbstractConnectionPrivate)
{
d_func()->init(this);
}
AbstractConnection::~AbstractConnection()
{
Q_D(AbstractConnection);
foreach(const ConnectionRate *rate, d->rates)
delete rate;
}
void AbstractConnection::registerHandler(SNACHandler *handler)
{
Q_D(AbstractConnection);
QList<SNACInfo> infos = handler->infos();
foreach(const SNACInfo &info, infos)
d->handlers.insertMulti((info.first << 16) | info.second, handler);
}
void AbstractConnection::disconnectFromHost(bool force)
{
Q_D(AbstractConnection);
if (d->socket->state() == QTcpSocket::UnconnectedState)
return;
if (!force) {
FLAP flap(0x04);
flap.append<quint32>(0x00000001);
send(flap);
}
d->socket->disconnectFromHost();
}
const QHostAddress &AbstractConnection::externalIP() const
{
return d_func()->ext_ip;
}
const QList<quint16> &AbstractConnection::servicesList()
{
return d_func()->services;
}
Socket *AbstractConnection::socket()
{
return d_func()->socket;
}
const Socket *AbstractConnection::socket() const
{
return d_func()->socket;
}
QNetworkProxy AbstractConnection::proxy() const
{
return d_func()->socket->proxy();
}
AbstractConnection::ConnectionError AbstractConnection::error()
{
return d_func()->error;
}
void AbstractConnection::setProxy(const QNetworkProxy &oldProxy)
{
QNetworkProxy proxy = oldProxy;
proxy.setCapabilities(proxy.capabilities() &=~ QNetworkProxy::HostNameLookupCapability);
debug() << Q_FUNC_INFO << proxy.type() << proxy.hostName() << proxy.port() << proxy.capabilities();
d_func()->socket->setProxy(proxy);
emit proxyUpdated(proxy);
}
QString AbstractConnection::errorString()
{
Q_D(AbstractConnection);
if (d->error == NoError)
return QString();
if (!d->errorStr.isEmpty())
return d->errorStr;
switch (d->error) {
case InvalidNickOrPassword:
return QCoreApplication::translate("ConnectionError", "Invalid nick or password");
case ServiceUnaivalable:
return QCoreApplication::translate("ConnectionError", "Service temporarily unavailable");
case IncorrectNickOrPassword:
return QCoreApplication::translate("ConnectionError", "Incorrect nick or password");
case MismatchNickOrPassword:
return QCoreApplication::translate("ConnectionError", "Mismatch nick or password");
case InternalClientError:
return QCoreApplication::translate("ConnectionError", "Internal client error (bad input to authorizer)");
case InvalidAccount:
return QCoreApplication::translate("ConnectionError", "Invalid account");
case DeletedAccount:
return QCoreApplication::translate("ConnectionError", "Deleted account");
case ExpiredAccount:
return QCoreApplication::translate("ConnectionError", "Expired account");
case NoAccessToDatabase:
return QCoreApplication::translate("ConnectionError", "No access to database");
case NoAccessToResolver:
return QCoreApplication::translate("ConnectionError", "No access to resolver");
case InvalidDatabaseFields:
return QCoreApplication::translate("ConnectionError", "Invalid database fields");
case BadDatabaseStatus:
return QCoreApplication::translate("ConnectionError", "Bad database status");
case BadResolverStatus:
return QCoreApplication::translate("ConnectionError", "Bad resolver status");
case InternalError:
return QCoreApplication::translate("ConnectionError", "Internal error");
case ServiceOffline:
return QCoreApplication::translate("ConnectionError", "Service temporarily offline");
case SuspendedAccount:
return QCoreApplication::translate("ConnectionError", "Suspended account");
case DBSendError:
return QCoreApplication::translate("ConnectionError", "DB send error");
case DBLinkError:
return QCoreApplication::translate("ConnectionError", "DB link error");
case ReservationMapError:
return QCoreApplication::translate("ConnectionError", "Reservation map error");
case ReservationLinkError:
return QCoreApplication::translate("ConnectionError", "Reservation link error");
case ConnectionLimitExceeded :
return QCoreApplication::translate("ConnectionError", "The users num connected from this IP has reached the maximum");
case ConnectionLimitExceededReservation:
return QCoreApplication::translate("ConnectionError", "The number of users connected from this IP has reached the maximum (reservation)");
case RateLimitExceededReservation:
return QCoreApplication::translate("ConnectionError", "Rate limit exceeded (reservation). Please try to reconnect in a few minutes");
case UserHeavilyWarned:
return QCoreApplication::translate("ConnectionError", "User too heavily warned");
case ReservationTimeout:
return QCoreApplication::translate("ConnectionError", "Reservation timeout");
case ClientUpgradeRequired:
return QCoreApplication::translate("ConnectionError", "You are using an older version of ICQ. Upgrade required");
case ClientUpgradeRecommended:
return QCoreApplication::translate("ConnectionError", "You are using an older version of ICQ. Upgrade recommended");
case RateLimitExceeded:
return QCoreApplication::translate("ConnectionError", "Rate limit exceeded. Please try to reconnect in a few minutes");
case IcqNetworkError:
return QCoreApplication::translate("ConnectionError", "Can't register on the ICQ network. Reconnect in a few minutes");
case InvalidSecirID:
return QCoreApplication::translate("ConnectionError", "Invalid SecurID");
case AgeLimit:
return QCoreApplication::translate("ConnectionError", "Account suspended because of your age (age < 13)");
case AnotherClientLogined:
return QCoreApplication::translate("ConnectionError", "Another client is logging with this UIN");
case HostNotFound:
return QCoreApplication::translate("ConnectionError", "No IP addresses were found for the host");
case SocketError:
return d_func()->socket->errorString();
default:
return QCoreApplication::translate("ConnectionError", "Unknown error");
}
}
const ClientInfo &AbstractConnection::clientInfo()
{
return d_func()->clientInfo;
}
AbstractConnection::State AbstractConnection::state() const
{
return d_func()->state;
}
void AbstractConnection::registerInitializationSnacs(const QList<SNACInfo> &snacs, bool append)
{
Q_D(AbstractConnection);
if (append)
d->initSnacs += snacs.toSet();
else
d->initSnacs = snacs.toSet();
}
void AbstractConnection::registerInitializationSnac(quint16 family, quint16 subtype)
{
d_func()->initSnacs.insert(SNACInfo(family, subtype));
}
AbstractConnection::AbstractConnection(AbstractConnectionPrivate *d, QObject *parent):
QObject(parent), d_ptr(d)
{
d->init(this);
}
const FLAP &AbstractConnection::flap()
{
return d_func()->flap;
}
void AbstractConnection::send(SNAC &snac, bool priority)
{
Q_D(AbstractConnection);
ConnectionRate *rate = d->ratesHash.value(snac.family() << 16 | snac.subtype());
if (!rate)
// The first rate class is used by default.
rate = d->rates.value(1);
if (rate)
rate->send(snac, priority);
else
sendSnac(snac);
}
void AbstractConnection::sendSnac(quint16 family, quint16 subtype, bool priority)
{
SNAC snac(family, subtype);
send(snac, priority);
}
void AbstractConnection::send(FLAP &flap)
{
Q_D(AbstractConnection);
flap.setSeqNum(d->seqNum());
//debug(VeryVerbose) << "FLAP:" << flap.toByteArray().toHex().constData();
d->socket->write(flap);
//d->socket->flush();
}
bool AbstractConnection::testRate(quint16 family, quint16 subtype, bool priority)
{
ConnectionRate *rate = d_func()->ratesHash.value(family << 16 | subtype);
return rate ? rate->testRate(priority) : true;
}
quint32 AbstractConnection::sendSnac(SNAC &snac)
{
Q_D(AbstractConnection);
QString dbgStr;
quint32 id = 0;
// Not allow any snacs in unconnected state
if (d->state == Unconnected) {
dbgStr = "Trying to send SNAC(0x%1, 0x%2) to %3 which is in unconnected state";
}
// In connecting state, allow only snacs from whitelist
else if (d->state == Connecting && !d->initSnacs.contains(SNACInfo(snac.family(), snac.subtype()))) {
dbgStr = "Trying to send SNAC(0x%1, 0x%2) to %3 which is in connecting state";
} else {
dbgStr = "SNAC(0x%1, 0x%2) is sent to %3";
// Send this snac
FLAP flap(0x02);
id = d->nextId();
snac.setId(id);
flap.append(snac.toByteArray());
snac.lock();
send(flap);
}
debug(DebugVerbose) << dbgStr
.arg(snac.family(), 4, 16, QChar('0'))
.arg(snac.subtype(), 4, 16, QChar('0'))
.arg(metaObject()->className());
return id;
}
void AbstractConnection::setSeqNum(quint16 seqnum)
{
d_func()->seqnum = seqnum;
}
void AbstractConnection::processNewConnection()
{
debug(DebugVerbose) << QString("processNewConnection: %1 %2 %3")
.arg(flap().channel(), 2, 16, QChar('0'))
.arg(flap().seqNum())
.arg(flap().data().toHex().constData());
setState(Connecting);
}
void AbstractConnection::processCloseConnection()
{
Q_D(AbstractConnection);
debug(DebugVerbose) << QString("processCloseConnection: %1 %2 %3")
.arg(d->flap.channel(), 2, 16, QChar('0'))
.arg(d->flap.seqNum())
.arg(d->flap.data().toHex().constData());
FLAP flap(0x04);
flap.append<quint32>(0x00000001);
send(flap);
socket()->disconnectFromHost();
}
void AbstractConnection::onDisconnect()
{
setState(Unconnected);
d_func()->aliveTimer.stop();
emit disconnected();
}
void AbstractConnection::onError(ConnectionError error)
{
if (error != NoError)
d_func()->socket->close();
}
void AbstractConnection::setError(ConnectionError e, const QString &errorStr)
{
Q_D(AbstractConnection);
d->error = e;
d->errorStr = errorStr;
if (d->error != NoError) {
onError(e);
emit error(e);
}
}
void AbstractConnection::handleSNAC(AbstractConnection *conn, const SNAC &sn)
{
Q_UNUSED(conn);
Q_ASSERT(this == conn);
Q_D(AbstractConnection);
switch ((sn.family() << 16) | sn.subtype()) {
// Server sends supported services list
case ServiceFamily << 16 | ServiceServerReady: {
QList<quint16> &services = d->services;
while (sn.dataSize() != 0)
services << sn.read<quint16>();
SNAC snac(ServiceFamily, ServiceClientFamilies);
// Sending the same as ICQ 6
snac.append<quint32>(0x00220001);
snac.append<quint32>(0x00010004);
snac.append<quint32>(0x00130004);
snac.append<quint32>(0x00020001);
snac.append<quint32>(0x00030001);
snac.append<quint32>(0x00150001);
snac.append<quint32>(0x00040001);
snac.append<quint32>(0x00060001);
snac.append<quint32>(0x00090001);
snac.append<quint32>(0x000a0001);
snac.append<quint32>(0x000b0001);
send(snac);
break;
}
// This is the reply to CLI_REQINFO
case ServiceFamily << 16 | ServiceServerNameInfo: {
// Skip uin
sn.read<QByteArray, quint8>();
sn.skipData(4);
// TLV(x01) User type?
// TLV(x0C) Empty CLI2CLI Direct connection info
// TLV(x0A) External IP
// TLV(x0F) Number of seconds that user has been online
// TLV(x03) The online since time.
// TLV(x0A) External IP again
// TLV(x22) Unknown
// TLV(x1E) Unknown: empty.
// TLV(x05) Member of ICQ since.
// TLV(x14) Unknown
TLVMap tlvs = sn.read<TLVMap>();
quint32 ip = tlvs.value(0x0a).read<quint32>();
d->ext_ip = QHostAddress(ip);
//debug() << conn->externalIP();
break;
}
// Server sends its services version numbers
case ServiceFamily << 16 | ServiceServerFamilies2: {
SNAC snac(ServiceFamily, ServiceClientReqRateInfo);
send(snac);
break;
}
// Server sends rate limits information
case ServiceFamily << 16 | ServiceServerAsksServices: {
foreach(const ConnectionRate *rate, d->rates)
delete rate;
d->rates.clear();
d->ratesHash.clear();
// Rate classes
quint16 groupCount = sn.read<quint16>();
for (int i = 0; i < groupCount; ++i) {
ConnectionRate *rate = new ConnectionRate(sn, this);
if (rate->isEmpty())
continue;
if (rate->groupId() == 1) // the first rate class will be used by default anyway
d->rates.insert(rate->groupId(), rate);
}
// Rate groups
while (sn.dataSize() >= 4) {
quint16 groupId = sn.read<quint16>();
quint16 count = sn.read<quint16>();
QHash<quint16, ConnectionRate*>::iterator rateItr = d->rates.find(groupId);
if (rateItr == d->rates.end()) {
sn.skipData(count * 4);
continue;
}
for (int j = 0; j < count; ++j) {
quint32 snacType = sn.read<quint32>();
d->ratesHash.insert(snacType, *rateItr);
}
}
// Accepting rates
SNAC snac(ServiceFamily, ServiceClientRateAck);
for (int i = 1; i <= groupCount; i++)
snac.append<quint16>(i);
send(snac);
break;
}
case ServiceFamily << 16 | ServiceServerRateChange: {
sn.read<QByteArray, quint16>(); // Unknown
quint16 code = sn.read<quint16>();
if (code == 2)
debug() << "Rate limits warning";
if (code == 3)
debug() << "Rate limits hit";
if (code == 4)
debug() << "Rate limits clear";
quint32 groupId = sn.read<quint16>();
if (d->rates.contains(groupId))
d->rates.value(groupId)->update(sn);
break;
}
case ServiceFamily << 16 | ServiceError: {
ProtocolError error(sn);
debug() << QString("Error (%1, %2): %3")
.arg(error.code(), 2, 16)
.arg(error.subcode(), 2, 16)
.arg(error.errorString());
break;
}
}
}
void AbstractConnection::setState(AbstractConnection::State state)
{
Q_D(AbstractConnection);
d->state = state;
if (state == Connected)
d->aliveTimer.start();
}
quint16 AbstractConnection::generateFlapSequence()
{
quint32 n = qrand(), s = 0;
for (quint32 i = n; i >>= 3; s += i);
return ((((0 - s) ^ (quint8) n) & 7) ^ n) + 2;
}
void AbstractConnection::processSnac()
{
Q_D(AbstractConnection);
SNAC snac = SNAC::fromByteArray(d->flap.data());
debug(DebugVerbose) << QString("SNAC(0x%1, 0x%2) is received from %3")
.arg(snac.family(), 4, 16, QChar('0'))
.arg(snac.subtype(), 4, 16, QChar('0'))
.arg(metaObject()->className());
bool found = false;
foreach(SNACHandler *handler, d->handlers.values((snac.family() << 16)| snac.subtype())) {
found = true;
snac.resetState();
handler->handleSNAC(this, snac);
}
if (!found) {
warning() << QString("No handlers for SNAC(0x%1, 0x%2) in %3")
.arg(snac.family(), 4, 16, QChar('0'))
.arg(snac.subtype(), 4, 16, QChar('0'))
.arg(metaObject()->className());
}
}
void AbstractConnection::readData()
{
Q_D(AbstractConnection);
if (d->socket->bytesAvailable() <= 0) {
debug() << "readyRead emmited but the socket is empty";
return;
}
if (d->flap.readData(d->socket)) {
if (d->flap.isFinished()) {
switch (d->flap.channel()) {
case 0x01:
processNewConnection();
break;
case 0x02:
processSnac();
break;
case 0x04:
processCloseConnection();
break;
default:
debug() << "Unknown shac channel" << hex << d->flap.channel();
case 0x03:
break;
case 0x05:
debug() << "Connection alive!";
break;
}
d->flap.clear();
}
// Just give a chance to other parts of qutIM to do something if needed
if (d->socket->bytesAvailable())
QTimer::singleShot(0, this, SLOT(readData()));
} else {
critical() << "Strange situation at" << Q_FUNC_INFO << ":" << __LINE__;
d->socket->close();
}
}
void AbstractConnection::stateChanged(QAbstractSocket::SocketState state)
{
debug(DebugVerbose) << "New connection state" << state << this->metaObject()->className();
if (state == QAbstractSocket::UnconnectedState)
onDisconnect();
}
void AbstractConnection::error(QAbstractSocket::SocketError error)
{
QString str = errorString();
#if IREEN_SSL_SUPPORT
foreach (const QSslError &error, socket()->sslErrors()) {
str += '\n';
str += error.errorString();
}
#endif
setError(SocketError, str);
debug() << "Connection error:" << error << errorString();
}
void AbstractConnection::sendAlivePacket()
{
FLAP flap(0x05);
flap.append<quint16>(0);
send(flap);
debug() << "Alive packet has been sent";
}
} // namespace Ireen