File: connection.cpp

package info (click to toggle)
classified-ads 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,772 kB
  • sloc: cpp: 34,291; tcl: 1,175; xml: 64; makefile: 40
file content (856 lines) | stat: -rw-r--r-- 36,355 bytes parent folder | download
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2018.

  This file is part of Classified Ads.

  Classified Ads 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 2.1 of the License, or (at your option) any later version.

  Classified Ads 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef WIN32
#define NOMINMAX
#include <WinSock2.h>
#endif
#include "connection.h"
#include "../log.h"
#include <QSslSocket>
#include "../datamodel/model.h"
#ifndef WIN32
#include "arpa/inet.h"
#endif
#include <QDateTime>
#include <QCoreApplication>
#include <QThread>
#include "../datamodel/profilemodel.h"
#include "../datamodel/camodel.h"
#include "../datamodel/binaryfilemodel.h"
#include "../datamodel/privmsgmodel.h"
#include "../datamodel/profilecommentmodel.h"
#include "../datamodel/cadbrecordmodel.h"
#include <QWaitCondition>
#include <QMutex>
#include <QSslCipher>

/** nat subnet. see similar variable in node.cpp */
static const QPair<QHostAddress, int> normalNats1 ( QHostAddress::parseSubnet("10.0.0.0/8") );
static const QPair<QHostAddress, int> normalNats2 ( QHostAddress::parseSubnet("172.16.0.0/20"));
static const QPair<QHostAddress, int> normalNats3 ( QHostAddress::parseSubnet("192.168.0.0/16") );
/** non-routable address space that some ISP's seem to offer */
static const QPair<QHostAddress, int> rfc6598 ( QHostAddress::parseSubnet("100.64.0.0/10") );


Connection::Connection (const QHostAddress& aAddr,
                        const int aPort,
                        ConnectionObserver& aObserver,
                        Model& aModel,
                        MController& aController,
                        const Hash& aFpOfNodeToTry)
    : iNeedsToRun(true),
      iTimeOfLastActivity(QDateTime::currentDateTimeUtc().toTime_t()),
      iNumberOfPacketsReceived(0),
      iConnectionState(Initial),
      iObserver(aObserver) ,
      iModel(aModel),
      iBytesExpectedInPacketBeingRead (0),
      iBytesInPacketBeingRead (0),
      iBytesRead(NULL),
      iInvocationsSinceLastByteReceived(0),
      iNodeOfConnectedPeer(NULL),
      iAddrToConnect(aAddr),
      iPortToConnect(aPort),
      iSocketDescriptor(-1) ,
      iSocketIsIncoming(false),
      iController(aController),
      iFpOfNodeWeTrying( aFpOfNodeToTry),
      iStageOfBucketFill( OwnNodeGreeting ),
      iTimeOfBucketFill(QDateTime::currentDateTimeUtc().toTime_t()),
      iSocketOpenTime(QDateTime::currentDateTimeUtc().toTime_t()),
      iBytesIn(0),
      iBytesOut(0),
      iPeerAddress(aAddr),
      iSleepBetweenSendOperations(500),
      iReadMutex(QMutex::NonRecursive),
      iIsAlreadyReading(false) {
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::Connection outgoing") ;
}

Connection::Connection(int aSocketDescriptor,
                       ConnectionObserver& aObserver,
                       Model &aModel,
                       MController& aController)
    : iNeedsToRun(true),
      iTimeOfLastActivity(QDateTime::currentDateTimeUtc().toTime_t()),
      iNumberOfPacketsReceived(0),
      iConnectionState(Initial),
      iObserver(aObserver) ,
      iModel(aModel),
      iBytesExpectedInPacketBeingRead (0),
      iBytesInPacketBeingRead (0),      
      iBytesRead(NULL),
      iInvocationsSinceLastByteReceived(0),
      iNodeOfConnectedPeer(NULL),
      iPortToConnect(-1),
      iSocketDescriptor(aSocketDescriptor),
      iSocketIsIncoming(true),
      iController(aController),
      iStageOfBucketFill( OwnNodeGreeting ),
      iTimeOfBucketFill(QDateTime::currentDateTimeUtc().toTime_t() ),
      iSocketOpenTime(QDateTime::currentDateTimeUtc().toTime_t()),
      iBytesIn(0),
      iBytesOut(0),
      iSleepBetweenSendOperations(500),
      iReadMutex(QMutex::NonRecursive),
      iIsAlreadyReading(false)  {
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::Connection incoming") ;
}


Connection::~Connection () {
    iModel.lock() ;
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::~Connection") ;
    // here note that we have member variable iSocket that is a pointer.
    // iSocket is a automatic variable of the run() method and
    // iSocket just points to that ; iSocket naturally will be
    // deleted when context of run() goes out of scope.
    if ( iNextProtocolItemToSend.size() ) {
        for ( int i = iNextProtocolItemToSend.size()-1 ; i >= 0 ; i-- ) {
            QByteArray* item = iNextProtocolItemToSend.value(i) ;
            delete item ;
            iNextProtocolItemToSend.removeAt(i) ;
        }
    }
    if ( iNodeOfConnectedPeer ) {
        delete iNodeOfConnectedPeer ;
        iNodeOfConnectedPeer = NULL ;
    }
    if ( iBytesRead ) {
        delete iBytesRead ;
        iBytesRead = NULL ;
    }
    iSocket = NULL ;
    iModel.unlock() ;
}

void Connection::run() {
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run") ;

    connect(this, SIGNAL(finished()),
	    this, SLOT(aboutToFinish())) ; 
    
    iBytesRead = new QByteArray() ;
    iBytesRead->clear() ;

    if ( iSocketIsIncoming ) {
        runForIncomingConnections() ;
    } else {
        runForOutgoingConnections() ;
    }
    if ( iConnectionState == Initial ||
            iConnectionState == Open ) {
        iConnectionState = Closing ;
    }
    iObserver.connectionClosed(this) ; // will call datamodel.lock() inside
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run out " + QString::number((qulonglong)(QThread::currentThreadId()))) ;
    emit finished() ;
    delete iNodeOfConnectedPeer ;
    iNodeOfConnectedPeer = NULL ;
    delete iBytesRead ;
    iBytesRead = NULL ;
    iSocket = NULL ; // iSocket was automatic variable inside scope
    // of runForIncomingConnections/runForOutgoingConnections
    // and it is no more -> set to NULL to prevent accidental access
}

void Connection::runForIncomingConnections() {
    QSslSocket sock ;
    iSocket = &sock ; // when this method goes out of
    // scope, iSocket must be set to NULL

    if (iSocket->setSocketDescriptor(iSocketDescriptor)) {
        setupSocketSignals() ;
        // the idea here with model.lock is that
        // this class instance here is owned by datamodel
        // and datamodel may frobnicate our internals so
        // if we touch any part of this class that is somehow
        // visible to outside, we must first lock(), then
        // release(). Frobnication of private parts that
        // are guaranteed to not have a get()ter method
        // or anything should be ok, and of course the
        // automatic variables.
        if ( iModel.lock() == false ) {
            iSocket->close() ;
            return ;
        }
        iSocket->setLocalCertificate(iModel.nodeModel().nodeCert());
        iSocket->setPrivateKey(iModel.nodeModel().nodeKey());
        iModel.unlock() ;
        iSocket->startServerEncryption();
    } else {
        LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"iSocket->setSocketDescriptor failed?????") ;
        iSocket->close() ;
        return ;
    }
    iSocket->setSocketOption(QAbstractSocket::LowDelayOption,1) ; // immediate send
    for ( int i = 0 ; iConnectionState == Initial ; i++ ) {
        msleep(1000) ;
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;
        if ( i > 10 ) { // 10 seconds passed and connection state not changed
            iSocket->abort() ;
            iNeedsToRun = false ;
            return ;
        }
    }

    // ok, obviously we can receive, mark that down for future
    // advertisement:
    iModel.lock() ;
    iController.getNode().setIpAddrWithChecks(iSocket->localAddress()) ;
    iModel.unlock() ;
    // ok, we came here, we have SSL handshake done ; lets
    // instantiate some data structures..
    Hash fingerPrintOfPeer (iSocket->peerCertificate()) ;
    iPeerHash = fingerPrintOfPeer ;
    // it also may be so that connection was attempted to a particular
    // node - is it possible that we managed to connect other node
    // than that?
    if (  iFpOfNodeWeTrying != KNullHash  ) { // if other than null hash
        if (  iFpOfNodeWeTrying != fingerPrintOfPeer)  {
            iSocket->disconnectFromHost() ;
            iNeedsToRun = false ; // this causes readloop to return immediately
        }
    }
    if ( iController.getNode().nodeFingerPrint() == fingerPrintOfPeer ) {
        // damn, connected to self..
        emit blackListNetworkAddr(iSocket->peerAddress()) ;
        if ( iSocketIsIncoming == false ) {
            emit blackListNetworkAddr(iAddrToConnect) ;
        }
        iSocket->disconnectFromHost() ;
        iNeedsToRun = false ; // this causes readloop to return immediately
        LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run incoming connected to self") ;
    }
    iNodeOfConnectedPeer= new Node(fingerPrintOfPeer, -1) ;

    // lets here do so that lets not frobnicate any instance
    // variables ; let the iSocket be only one and that in
    // turn will be frobnicated only from here.
    // if we need to send something, we ask datamodel for
    // stuff to send, take it into local variable here
    // and do our sending. this way we can manage with
    // single mutex in model and things will be in sync.
    readLoop() ;
    iModel.lock() ;
    iConnectionState = Connection::Closing ;
    iModel.connectionStateChanged(Connection::Closing, this) ;
    iModel.unlock() ;
    if ( iNeedsToRun == false && iSocket &&  iSocket->state() == QAbstractSocket::ConnectedState ) {
        iSocket->close() ;
    }
    if ( iSocket ) {
        iSocket->waitForDisconnected(10*1000) ;
    }
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run(incoming) out") ;
}

void Connection::socketReady() {
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +
            "Connection::socketReady auth " +
            iSocket->sessionCipher().authenticationMethod() +
            " keyEx " +
            iSocket->sessionCipher().keyExchangeMethod() +
            " encryption " +
            iSocket->sessionCipher().encryptionMethod() +
            " bits " +
            QString::number(iSocket->sessionCipher().usedBits())) ;
    iModel.lock() ;
    iConnectionState = Open ;
    iModel.connectionStateChanged(iConnectionState, this) ;
    iPeerAddress = iSocket->peerAddress() ;
    iPeerHash = Hash(   iSocket->peerCertificate() );
    if ( iPeerAddress == iSocket->localAddress() ) {
        QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) + "Connected to self, addr = " + iSocket->peerAddress().toString()) ;
        // oh great, broadcast?
        // connected to self
        iNeedsToRun = false ;
        iSocket->close() ;
        return ;
    }
    iModel.unlock() ;
}

void Connection::runForOutgoingConnections () {
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::runForOutgoingConnections in") ;
    iModel.lock() ;
    iModel.connectionStateChanged(Connection::Initial ,
                                  this) ;
    iModel.unlock() ;
    QSslSocket sock ;
    iSocket = &sock ;

    setupSocketSignals() ;
    // the idea here with model.lock is that
    // this class instance here is owned by datamodel
    // and datamodel may frobnicate our internals so
    // if we touch any part of this class that is somehow
    // visible to outside, we must first lock(), then
    // release(). Frobnication of private parts that
    // are guaranteed to not have a get()ter method
    // or anything should be ok, and of course the
    // automatic variables.
    if ( iModel.lock() == false ) {
        return ;
    }
    iSocket->setSocketOption(QAbstractSocket::LowDelayOption,1) ; // immediate send
    iSocket->setLocalCertificate(iModel.nodeModel().nodeCert());
    iSocket->setPrivateKey(iModel.nodeModel().nodeKey());
    iModel.unlock() ;
    iSocket->setPeerVerifyMode(QSslSocket::QueryPeer) ;
    QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connecting to " + iAddrToConnect.toString() + " port " + QString::number(iPortToConnect) ) ;
    iSocket->connectToHostEncrypted(iAddrToConnect.toString(), iPortToConnect);

    for ( int i = 0 ; iConnectionState == Initial ; i++ ) {
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;
        msleep(1000) ;
        if ( i > 10 ) { // 10 seconds passed and connection state not changed
            iSocket->abort() ;
            iNeedsToRun = false ;
            QLOG_STR("Connection attempt to " + iAddrToConnect.toString() + " timed out") ;
            QLOG_STR("Connection attempt to " + iFpOfNodeWeTrying.toString() + " timed out") ;
            emit connectionAttemptFailed(iFpOfNodeWeTrying) ; // then don't try again immediately
            return ;
        }
    }

    if ( iSocket->peerAddress() == iSocket->localAddress() ) {
        QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connected to self, addr = " + iSocket->peerAddress().toString()) ;
        // oh great, broadcast?
        // connected to self
        iNeedsToRun = false ;
        iSocket->close() ;
        return ;
    }

    Hash fingerPrintOfPeer (iSocket->peerCertificate()) ;
    iPeerHash = fingerPrintOfPeer ;
    // it also may be so that connection was attempted to a particular
    // node - is it possible that we managed to connect other node
    // than that?
    if (  iFpOfNodeWeTrying != KNullHash  ) { // if other than null hash
        if (  iFpOfNodeWeTrying != fingerPrintOfPeer ) {
            iSocket->abort() ;
            iNeedsToRun = false ; // this causes readloop to return immediately
        }
    }
    if ( iController.getNode().nodeFingerPrint() == fingerPrintOfPeer ) {
        // damn, connected to self..
        emit blackListNetworkAddr(iSocket->peerAddress()) ;
        if ( iSocketIsIncoming == false ) {
            emit blackListNetworkAddr(iAddrToConnect) ;
        }
        iSocket->disconnectFromHost() ;
        iNeedsToRun = false ; // this causes readloop to return immediately
        LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run outgoing connected to self") ;
    }
    iNodeOfConnectedPeer= new Node(fingerPrintOfPeer, -1) ;

    // we just managed to connect, mark that thing down for
    // future advertisement
    iModel.lock() ;
    iNodeOfConnectedPeer->setCanReceiveIncoming(true) ;
    iController.getNode().setIpAddrWithChecks(iSocket->localAddress()) ;
    iModel.unlock() ;

    readLoop() ;
    if ( iNeedsToRun == false && iSocket &&  iSocket->state() == QAbstractSocket::ConnectedState ) {
        iSocket->close() ;
    }
    if ( iSocket ) {
        iSocket->waitForDisconnected(30*1000) ;
    }
    iSocket = NULL ;
    LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Connection::run  (outgoing )out") ;
}


// this is called from run() method when there is data available
void Connection::performRead() {
    iReadMutex.lock() ;
    if ( iIsAlreadyReading ) {
      iReadMutex.unlock() ;
      QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +" was already reading") ;
      return ; 
    }
    iIsAlreadyReading = true ;
    iReadMutex.unlock() ;
    
    quint64 bytesAvail (0) ;

    while ( ( bytesAvail = iSocket->bytesAvailable() ) > 0 ) {
        if ( iBytesExpectedInPacketBeingRead == 0 &&
                bytesAvail < sizeof(quint32) ) {
            msleep(50) ; // size not yet received, wait for 50 ms
        }
        if ( iBytesExpectedInPacketBeingRead == 0 &&
	     iBytesRead->size() == 0 && // only if not in middle of packet
                bytesAvail >= (quint64) sizeof(quint32) ) {
            // we're not reading a packet yet, read packet
            quint32 bytesExpectedNetworkByteOrder = 0 ;
            if ( iSocket->read((char *)&bytesExpectedNetworkByteOrder,
                               sizeof(quint32)) != sizeof(quint32)) {
                iNeedsToRun = false ;
                iModel.lock() ;
                iModel.connectionStateChanged(Connection::Closing, this) ;
                iModel.unlock() ;
                iSocket->abort() ;
		QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +" performread out abort") ;
		iReadMutex.lock() ;
		iIsAlreadyReading = false ;
		iReadMutex.unlock() ;
                return ;
            } else {
                iBytesIn += sizeof(quint32) ;
                bytesAvail = iSocket->bytesAvailable() ;
            }
            iBytesExpectedInPacketBeingRead = ntohl ( bytesExpectedNetworkByteOrder);
            iBytesInPacketBeingRead = 0 ; 
            QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +" Connection::performRead expecting bytes= "+ QString::number( iBytesExpectedInPacketBeingRead)) ;
	    QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +" Connection::performRead bytes alread = "+ QString::number( iBytesRead->size() )) ;
        }

        if ( iBytesExpectedInPacketBeingRead > 0 &&
	     iBytesExpectedInPacketBeingRead > iBytesInPacketBeingRead &&
                bytesAvail > 0 ) {
            if ( bytesAvail > iBytesExpectedInPacketBeingRead ) {
                bytesAvail = ( iBytesExpectedInPacketBeingRead -
                               iBytesInPacketBeingRead );
            }
            QByteArray bytesReadThisTime ( iSocket->read(bytesAvail) ) ;
            if ( bytesReadThisTime.size() > 0 ) {
                iBytesRead->append(bytesReadThisTime) ;
                iBytesIn += bytesReadThisTime.size() ;
		iBytesInPacketBeingRead += bytesReadThisTime.size() ;
                iInvocationsSinceLastByteReceived = 0 ;
                // seems like we received something:
                iTimeOfLastActivity = QDateTime::currentDateTimeUtc().toTime_t() ;
            }
        }
        if ( iBytesExpectedInPacketBeingRead == iBytesInPacketBeingRead)  {
            // if we came here, we have one total packet..
            if ( iObserver.dataReceived(*iBytesRead,
                                        *this) == false ) {
                QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"### Closing due to unparseable data, peer = " + peerAddress().toString()) ;
                iSocket->abort() ;
                iNeedsToRun = false ;
            } else {
                QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;
            }
            iBytesExpectedInPacketBeingRead = 0 ;
            iBytesRead->clear() ;
            iInvocationsSinceLastByteReceived = 0 ;
            iNumberOfPacketsReceived++ ;
        }
    }
    iReadMutex.lock() ;
    iIsAlreadyReading = false ;
    iReadMutex.unlock() ;    
    QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +" performread out") ;
}

bool Connection::Ipv6AddressesEqual(const Q_IPV6ADDR& aAddr1,
                                    const Q_IPV6ADDR& aAddr2) {
    for ( int i = 0 ; i < 16 ; i++ ) {
        if ( aAddr1.c[i] != aAddr2.c[i]) return false ;
    }
    return true ;
}

// practically this is more like write-loop because actual
// reading and writing is done in SLOT()s that are connected
// to read+write events.
void Connection::readLoop() {
    connect(iSocket, SIGNAL(readyRead ()),
            this, SLOT(readyRead ()));
    iObserver.connectionReady(this) ; // before starting to read, inform the observer
    QByteArray *itemToSend = NULL ;
    while ( iNeedsToRun && iSocket->state() == QAbstractSocket::ConnectedState ) {
        iModel.lock() ;
        checkForBucketFill() ;
        int numberOfQueueItems = iSendQueue.size() ;
        iModel.unlock() ;

        if ( numberOfQueueItems > 0 ) {
            msleep( 10 ) ;
        } else {
            msleep( iSleepBetweenSendOperations ) ;
        }
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;

        // we came here because we have no bytes to read from
        // sock. first check if we do have read operation pending
        if ( iBytesExpectedInPacketBeingRead > 0 &&
                iBytesExpectedInPacketBeingRead > (quint64)(iBytesRead->size())) {
            iInvocationsSinceLastByteReceived++ ; // gets incremented every 500ms
        }
        if ( iInvocationsSinceLastByteReceived > 1000 ) {
            // we have had read operation pending for 500 seconds and
            // not a single byte has got through -> give up
            if ( iNeedsToRun ) {
                iModel.lock() ;
                LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Read operation was pending too long, closing")  ;
                iConnectionState = Connection::Closing ;
                iModel.connectionStateChanged(Connection::Closing, this) ;
                iModel.unlock() ;
                iSocket->abort() ;
                iNeedsToRun = false ;
                LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Giving up pending read operation after 500 seconds");
            }
            break ;
        }
        // ok, then try write operation, if applicable
        if ( iNeedsToRun && iSocket->bytesToWrite() == 0 ) {
            iModel.lock() ;
            itemToSend = iModel.nodeModel().getNextItemToSend(*this) ;
            if ( itemToSend != NULL ) {
                QLOG_STR("Got " + QString::number(itemToSend->size()) +
                         " bytes to send, items in q = " +
                         QString::number(iSendQueue.size())) ;
                iSleepBetweenSendOperations = 30 ; // we got item to send, next time sleep only 30ms
            } else {
                iSleepBetweenSendOperations = 500 ; // queue empty, sleep 500 ms
            }
            iModel.unlock() ;
        }
        // now we own itemToSend
        if ( itemToSend != NULL &&
                iSocket != NULL &&
                iSocket->state() == QAbstractSocket::ConnectedState ) {
            quint32 bytesInPayload = itemToSend->size() ;
            quint32 bytesInPayloadNetworkOrder = htonl(bytesInPayload) ;
            QByteArray messageSizeBytes ;
            messageSizeBytes.append((const char *)&bytesInPayloadNetworkOrder,
                                    sizeof(quint32)) ;
            if ( iSocket->write(messageSizeBytes) != sizeof(quint32) ) {
                LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Write operation did not succeed, closing..")  ;
                if ( iNeedsToRun ) {
                    iModel.lock() ;
                    iConnectionState = Connection::Closing ;
                    iModel.connectionStateChanged(Connection::Closing, this) ;
                    iModel.unlock() ;
                }
                iSocket->abort() ;
                iNeedsToRun = false ;
            } else {
                iBytesOut += sizeof(quint32) ;
            }
            if ( iSocket->write(*itemToSend) != itemToSend->size() ) {
                LOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Write operation did not succeed, closing..")  ;
                if ( iNeedsToRun ) {
                    iModel.lock() ;
                    iConnectionState = Connection::Closing ;
                    iModel.connectionStateChanged(Connection::Closing, this) ;
                    iModel.unlock() ;
                }
                iSocket->abort() ;
                iNeedsToRun = false ;
            } else {
                iBytesOut += itemToSend->size() ;
            }
            // ok ; how long to wait for write.
            // very slow network here. have 100 connected clients.
            // say, upload rate is 20kb/s, divided by 100 clients
            // we get 204 bytes per sec. .. but at least 30 seconds.
            delete itemToSend ;
            itemToSend = NULL ;
            // seems like we sent something:
            iTimeOfLastActivity = QDateTime::currentDateTimeUtc().toTime_t() ;
            flushSocket() ;
        }
    }
    if ( iNeedsToRun && iSocket ) {
        QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"Readloop out, addr = " + peerAddress().toString()) ;
    }
}

// reason for this implementation here is that when connection
// is about to get deleted, there has been SIGSEGV's in calls
// iSocket->peerAddress() ; indication of a locking problem
// or similar, the socket is already partly torn down.
// have the peer address here as normal variable and avoid the
// dangerous call:
QHostAddress Connection::peerAddress() const {
    return iPeerAddress ;
}

const Hash& Connection::fingerPrintOfNodeAttempted() {
    return iFpOfNodeWeTrying ;
}

bool Connection::isInPrivateAddrSpace() const {
    bool retval = false ;
    if ( iSocket ) {
        QHostAddress peer ( iSocket->peerAddress() ) ;
        if ( peer.protocol() == QAbstractSocket::IPv4Protocol ) {

            if ( peer.isInSubnet(normalNats1) ||
                    peer.isInSubnet(normalNats2) ||
                    peer.isInSubnet(normalNats3) ||
                    peer.isInSubnet(rfc6598))
                retval = true ;
        }
    }
    return retval ;
}

void Connection::checkForBucketFill() {
    if ( iTimeOfBucketFill+15 < QDateTime::currentDateTimeUtc().toTime_t() &&
            iNextProtocolItemToSend.size() == 0 &&
            iSendQueue.size() == 0 &&
            iSocket &&
            iNumberOfPacketsReceived > 1 &&
            iNodeOfConnectedPeer ) {
        // we've been connected for 15 seconds now and there is nothing in queue.
        switch ( iStageOfBucketFill ) {
        case OwnNodeGreeting:
            // initial stage. first queue possible private messages
            iEndOfBucket = iModel.nodeModel().bucketEndHash(iNodeOfConnectedPeer->nodeFingerPrint()) ;
            // here put private messages into queue
            iModel.privateMessageModel().fillBucket(iSendQueue,
                                                    iNodeOfConnectedPeer->nodeFingerPrint(),
                                                    iEndOfBucket,
                                                    iNodeOfConnectedPeer->lastMutualConnectTime(),
                                                    iNodeOfConnectedPeer->nodeFingerPrint() ) ;
            iStageOfBucketFill = PrivateMessage ; // and set next stage
            break ;
        case PrivateMessage:
            // Next stage after private messages

            iModel.profileModel().fillBucket(iSendQueue,
                                             iNodeOfConnectedPeer->nodeFingerPrint(),
                                             iEndOfBucket,
                                             iNodeOfConnectedPeer->lastMutualConnectTime(),
                                             iNodeOfConnectedPeer->nodeFingerPrint() ) ;

            iStageOfBucketFill = UserProfile; // and set next stage
            break ;
        case UserProfile:
            // Next stage after user profile changes. Note that
            // last mutual connect time is set in protocol parser
            // as a side-effect of parsing the initial node greeting
            iModel.classifiedAdsModel().fillBucket(iSendQueue,
                                                   iNodeOfConnectedPeer->nodeFingerPrint(),
                                                   iEndOfBucket,
                                                   iNodeOfConnectedPeer->lastMutualConnectTime(),
                                                   iNodeOfConnectedPeer->nodeFingerPrint() ) ;

            iStageOfBucketFill = ClassifiedAd; // and set next stage
            break ;
        case ClassifiedAd:
            // Next stage after classified ads

            iModel.binaryFileModel().fillBucket(iSendQueue,
                                                iNodeOfConnectedPeer->nodeFingerPrint(),
                                                iEndOfBucket,
                                                iNodeOfConnectedPeer->lastMutualConnectTime(),
                                                iNodeOfConnectedPeer->nodeFingerPrint()) ;
            iStageOfBucketFill = BinaryBlob; // and set next stage
            break ;
        case BinaryBlob:
            // Next stage after binary blob
            iModel.profileCommentModel().fillBucket(iSendQueue,
                                                    iNodeOfConnectedPeer->nodeFingerPrint(),
                                                    iEndOfBucket,
                                                    iNodeOfConnectedPeer->lastMutualConnectTime(),
                                                    iNodeOfConnectedPeer->nodeFingerPrint()) ;
            iStageOfBucketFill = DbRecord; // next stage distributed database records
            break ;
        case DbRecord:
            // Next stage after binary blob
            iModel.caDbRecordModel()->fillBucket(iSendQueue,
                                                 iNodeOfConnectedPeer->nodeFingerPrint(),
                                                 iEndOfBucket,
                                                 iNodeOfConnectedPeer->lastMutualConnectTime(),
                                                 iNodeOfConnectedPeer->nodeFingerPrint()) ;
            iStageOfBucketFill = UserProfileComment; // and set next stage
            break ;
        case UserProfileComment:
            // we came here, we've sent profiles, ads and binary files profile comments and
            // distributed database records and the queue is empty: 
            // set mutual connect time of the node
            // so we won't send the same stuff again next time:
            iNodeOfConnectedPeer->setLastMutualConnectTime(QDateTime::currentDateTimeUtc().toTime_t()) ;
            iModel.nodeModel().updateNodeLastMutualConnectTimeInDb(iNodeOfConnectedPeer->nodeFingerPrint(),
                    iNodeOfConnectedPeer->lastMutualConnectTime()) ;
            iStageOfBucketFill = ClassifiedAd2NdAddr; // and set final stage
            break ;

        case ClassifiedAd2NdAddr: // this is our final stage
            if ( ( iTimeOfBucketFill + (60*iMinutesBetweenBucketFill ))  < QDateTime::currentDateTimeUtc().toTime_t() ) {
                // iMinutesBetweenBucketFill minutes passed since last bucket fill: re-do
                iTimeOfBucketFill = QDateTime::currentDateTimeUtc().toTime_t() ;
                iStageOfBucketFill = OwnNodeGreeting;
            }
            break ;
        default:
            // finally do no thing
            break ;
        }
    }
}

Hash Connection::getPeerHash() const {
    if ( iPeerHash != KNullHash ) {
        return iPeerHash ;
    } else {
        if ( iConnectionState == Connection::Open && iSocket ) {
            return Hash(   iSocket->peerCertificate() ) ;
        } else {
            // this may be KNullHash but for some outgoing connections
            // this is anyway known already when socket is not
            // yet in connected state.
            return iFpOfNodeWeTrying ;
        }
    }
}

bool Connection::forciblyCloseSocket() {
    iNeedsToRun = false ;
    iFpOfNodeWeTrying = KNullHash ;
    return true ;
}


void Connection::flushSocket() {
    // setting TCP_NODELAY has side-effect of flushing the socket.
    // at least in linux.
    if ( iSocket ) {
        iSocket->flush() ;
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;
        iSocket->setSocketOption(QAbstractSocket::LowDelayOption,0) ;
        iSocket->setSocketOption(QAbstractSocket::LowDelayOption,1) ;
        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents) ;
    }
}


// Connected from QSslSocket. Called when bytes have been written
void Connection::encryptedBytesWritten ( qint64
#ifdef DEBUG
        written
#endif
                                       ) {
    QLOG_STR("Connection::encryptedBytesWritten " + QString::number(written));
}

// Connected from QSslSocket. Called on handshake errors
void Connection::sslErrors ( const QList<QSslError> & errors  ) {
    QLOG_STR("Connection::sslErrors") ;
    foreach ( const QSslError& e , errors ) {
        switch ( e.error() ) {
        case QSslError::NoError:
        case QSslError::CertificateExpired:
        case QSslError::InvalidNotBeforeField:
        case QSslError::InvalidNotAfterField:
        case QSslError::SelfSignedCertificate:
        case QSslError::SelfSignedCertificateInChain:
        case QSslError::UnableToVerifyFirstCertificate:
        case QSslError::CertificateRevoked:
        case QSslError::InvalidCaCertificate:
        case QSslError::CertificateUntrusted:
        case QSslError::SubjectIssuerMismatch:
        case QSslError::AuthorityIssuerSerialNumberMismatch:
        case QSslError::HostNameMismatch:
        case QSslError::InvalidPurpose:
            QLOG_STR("Ssl error ignored: " + e.errorString()) ;
            break ;
        case QSslError::UnableToGetIssuerCertificate:
        case QSslError::UnableToDecryptCertificateSignature:
        case QSslError::UnableToDecodeIssuerPublicKey:
        case QSslError::CertificateSignatureFailed:
        case QSslError::CertificateNotYetValid:
        case QSslError::UnableToGetLocalIssuerCertificate:
        case QSslError::PathLengthExceeded:
        case QSslError::CertificateRejected:
        case QSslError::NoPeerCertificate:
        case QSslError::UnspecifiedError:
        case QSslError::NoSslSupport:
        case QSslError::CertificateBlacklisted:
            QLOG_STR("Ssl error detected: " + e.errorString()) ;
            iNeedsToRun = false ;
            break ;
        }
    }
}
// Connected from QSslSocket::error. Called on error
void Connection::socketError ( QAbstractSocket::SocketError
#ifdef DEBUG
                               socketError
#endif
                             ) {
    iNeedsToRun = false ;
    QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +
             "Connection::socketError "+ QString::number(socketError)) ;

    if ( iSocketIsIncoming == false && // this is outgoing
            iFpOfNodeWeTrying != KNullHash && // and we know who we tried to reach
            iConnectionState == Connection::Initial // and didn't get past initial stage
       ) {
        emit connectionAttemptFailed(iFpOfNodeWeTrying) ; // then don't try again immediately

    } else {
        QLOG_STR("No emit") ;
    }
}

// Connected from QSslSocket. Called on close
void Connection::disconnected () {
    iNeedsToRun = false ;
    QLOG_STR(QString("0x%1 ").arg((qulonglong)this, 8) +"disconnected") ;
}
// Connected from QSslSocket. Called when bytes are available
void Connection::readyRead () {
    performRead() ;
}

void Connection::setupSocketSignals() {
    connect(iSocket, SIGNAL(encrypted()), this, SLOT(socketReady()));
    connect(iSocket, SIGNAL(encryptedBytesWritten ( qint64 )),
            this, SLOT(encryptedBytesWritten ( qint64 )));
    connect(iSocket, SIGNAL(sslErrors ( const QList<QSslError> & )),
            this, SLOT(sslErrors ( const QList<QSslError> & )));
    connect(iSocket, SIGNAL(error ( QAbstractSocket::SocketError )),
            this, SLOT(socketError ( QAbstractSocket::SocketError )));
    connect(iSocket, SIGNAL(disconnected ()),
            this, SLOT(disconnected ()));
    // note that readyread signal is connected when
    // readloop is entered
}

void Connection::msleep(int aMilliSeconds) {
    QWaitCondition waitCondition;
    QMutex mutex;
    QThread::yieldCurrentThread ();
    mutex.lock() ; // waitCondition needs a mutex initially locked
    waitCondition.wait(&mutex, aMilliSeconds);// give other threads a chance..
    mutex.unlock() ;
}

// slot that contains cleanup related to thread termination
void Connection::aboutToFinish() {
  QLOG_STR("Connection cleanup for thread " + QString::number((qulonglong)(QThread::currentThreadId())) ) ; 
  iModel.lock() ; 
  iModel.threadTerminationCleanup( QThread::currentThread() ) ; 
  iModel.unlock() ; 
  QLOG_STR("Connection cleanup out for thread " + QString::number((qulonglong)(QThread::currentThreadId()))  ) ; 
}