File: protocol_message_formatter.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 (897 lines) | stat: -rw-r--r-- 36,555 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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2016.

  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 "protocol_message_formatter.h"
#include <time.h>
#ifndef WIN32
#include "arpa/inet.h"
#endif
#include "../util/jsonwrapper.h"
#include "node.h"
#include "connection.h"
#include "../log.h"
#include "../datamodel/const.h"
#include "../datamodel/searchmodel.h"
#include "../datamodel/voicecall.h"
#include "../datamodel/model.h"
#include "../datamodel/contentencryptionmodel.h"
#include "../datamodel/cadbrecord.h"

QByteArray ProtocolMessageFormatter::nodeGreeting(const Node& aNode) {
    QByteArray retval ;
    unsigned char ch ;
    ch = KProtocolVersion ;// (unsigned char) PROTOCOL_VERSION ;
    retval.append((const char *)&ch, 1) ;
    QVariant v ( aNode.asQVariant()) ;

    QByteArray nodeRefAsJson ( JSonWrapper::serialize(v) ) ;
    QLOG_STR(nodeRefAsJson) ;
    QByteArray nodeRefAsCompressedJson (qCompress( nodeRefAsJson )) ;
    nodeRefAsJson.clear() ;
    quint32 nodeRefSize = nodeRefAsCompressedJson.size() ;
    quint32 nodeRefSizeNetworkByteorder = htonl(nodeRefSize + 1 + sizeof(quint32));
    retval.append((const char *)(&nodeRefSizeNetworkByteorder), sizeof(nodeRefSize)) ;
    retval.append(nodeRefAsCompressedJson) ;

    return retval ;
}

QByteArray ProtocolMessageFormatter::randomNumbers(void) {
    QByteArray retval ;
    unsigned char ch ;
    ch = KRandomNumbersPacket ;
    retval.append((const char *)&ch, 1) ;
    ch = 0 ;
    retval.append((const char *)&ch, 1) ; // placeholder for message length
    unsigned char numberOfBytesInMessage = 2 + ( rand() % 15 ) ;

    for ( unsigned char i = 0 ; i < numberOfBytesInMessage ; i++ ) {
        ch = ( rand() % 255 ) ;
        retval.append((const char *)&ch, 1) ;
    }
    // then count message len
    unsigned char messagelen = (unsigned char)(retval.length()) ;
    retval.replace(1,1, (const char *) &messagelen, 1) ;
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForBinaryBlob(const Hash& aHash) {
    QByteArray retval ;
    unsigned char ch ( KBinaryFileAtHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForPrivateMessages(const Hash& aHash) {
    QByteArray retval ;
    unsigned char ch ( KPrivMessagesAtHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForPrivateMessagesForProfile(const Hash& aHash,const quint32 aTimeOfOldestMsgToSend) {
    QByteArray retval ;
    unsigned char ch ( KPrivMessagesForProfile  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    quint32 tsInNetworkBO = htonl(aTimeOfOldestMsgToSend) ;
    retval.append((const char *)(&tsInNetworkBO), sizeof(quint32)) ;
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForProfileComments(const Hash& aCommentedProfileHash,
        const quint32 aTimeOfOldestMsgToSend) {
    QByteArray retval ;
    unsigned char ch ( KProfileCommentAtHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aCommentedProfileHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    quint32 tsInNetworkBO = htonl(aTimeOfOldestMsgToSend) ;
    retval.append((const char *)(&tsInNetworkBO), sizeof(quint32)) ;
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForUserProfileComment(const Hash& aIndividualCommentHash) {
    QByteArray retval ;
    unsigned char ch ( KSingleProfileCommentAtHash ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aIndividualCommentHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}



QByteArray ProtocolMessageFormatter::requestForNodesAroundHash(const Hash& aHash) {
    QByteArray retval ;
    unsigned char ch ( KNodesAroundHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForUserProfile(const Hash& aHash) {
    QByteArray retval ;
    unsigned char ch ( KProfileAtHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForClassifiedAd(const Hash& aHash) {
    QByteArray retval ;
    unsigned char ch ( KClassifiedAdAtHash  ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHash.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::requestForAdsClassified(const Hash& aHashOfClassification,
        const quint32  aStartingTimestamp  ,
        const quint32  aEndingTimestamp ) {
    QByteArray retval ;
    unsigned char ch ( KAdsClassifiedAtHash ) ;
    retval.append((const char *)&ch, 1) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashNumberNetworkByteOrder (htonl(aHashOfClassification.iHash160bits[i])) ;
        retval.append((const char *)(&hashNumberNetworkByteOrder), sizeof(quint32)) ;
    }
    const quint32  startingTimestampNetworkBO(htonl(aStartingTimestamp)) ;
    const quint32  endingTimestampNetworkBO(htonl(aEndingTimestamp)) ;
    retval.append((const char *)(&startingTimestampNetworkBO), sizeof(quint32)) ;
    retval.append((const char *)(&endingTimestampNetworkBO), sizeof(quint32)) ;
    return retval ;
}

QByteArray ProtocolMessageFormatter::contentPublish(const unsigned char aContentMagicNumber,
        const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QList<quint32>& aBangPath,
        const QByteArray& aSigningKey,
        bool aIsContentEncrypted,
        bool aIsContentCompressed,
        quint32 aTimeStamp) {
    QByteArray retval ;
    return doContentSendOrPublish(retval,
                                  aContentMagicNumber,
                                  aContentHash,
                                  aContent,
                                  aSignature,
                                  aBangPath,
                                  aSigningKey,
                                  aIsContentEncrypted,
                                  aIsContentCompressed,
                                  aTimeStamp,
                                  true) ;
}

QByteArray ProtocolMessageFormatter::contentSend(const unsigned char aContentMagicNumber,
        const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QByteArray& aSigningKey,
        bool aIsContentEncrypted,
        bool aIsContentCompressed,
        quint32 aTimeStamp) {
    QList<quint32> aDummyBangPath ;
    QByteArray retval ;
    return doContentSendOrPublish(retval,
                                  aContentMagicNumber,
                                  aContentHash,
                                  aContent,
                                  aSignature,
                                  aDummyBangPath,
                                  aSigningKey,
                                  aIsContentEncrypted,
                                  aIsContentCompressed,
                                  aTimeStamp,
                                  false) ;
}

QByteArray ProtocolMessageFormatter::doContentSendOrPublish(QByteArray& retval,
        const unsigned char aContentMagicNumber,
        const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QList<quint32>& aBangPath,
        const QByteArray& aSigningKey,
        bool aIsContentEncrypted,
        bool aIsContentCompressed,
        quint32 aTimeStamp,
        bool aIsPublish) {

    // ok, lets decide here the layout of publish..
    // first magick number telling what it is.
    // then bangpath 5 items and if not all 5 are filled,
    // the remaining ones are filled with zeroes.

    // then content hash, 5*32 bytes

    // then length of the actual data, followed by the actual data
    // then length of the signature, followeds by the signature.
    // then length of the signingkey, followed by the key
    // then flags, one unsigned char.
    //  * if bit 1 is up, it means that content is encrypted.
    //  * if bit 2 is up, it means that content is compressed.
    // then time when content was published. For most of the content
    // the time of publish may be deduced from content itself but in some
    // case like the private profile the date of publish is hidden from
    // us -> this time practically acts as a version-number for the
    // content.
    LOG_STR2("content len = %d", (int)aContent.length()) ;
    LOG_STR2("signature len = %d", (int)aSignature.length()) ;
    if ( aIsPublish ) {
        for ( int i = 0 ; i < aBangPath.size() ; i++ ) {
            LOG_STR2("Bangpath contains %u", aBangPath[i]) ;
        }
    }
    unsigned char ch ;
    ch = aContentMagicNumber ;
    retval.append((const char *)&ch, 1) ;

    quint32 bPathNumberNetworkByteorder ;
    if ( aIsPublish ) {
        if ( aBangPath.size() >= 1 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[0]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 2 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[1]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 3 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[2]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 4 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[3]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 5 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[4]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    }
    // now has 5 numbers as bangpath, then put hash
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aContentHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // after hash, the size of content:
    quint32 lengthNetworkByteorder ( htonl( (quint32)(aContent.size()) ) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    // followed by content:
    retval.append(aContent) ;
    lengthNetworkByteorder = htonl( (quint32)(aSignature.size()) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    retval.append(aSignature) ;
    // followed by signing key

    lengthNetworkByteorder = htonl( (quint32)(aSigningKey.size()) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    retval.append(aSigningKey)  ;

    unsigned char flags ( 0 ) ;
    if ( aIsContentEncrypted ) {
        flags = flags | 1 ;
    }
    if ( aIsContentCompressed ) {
        flags = flags | 2 ;
    }
    retval.append((const char *)&flags, sizeof(unsigned char))  ;
    quint32 timestampNetworkBO ( htonl( aTimeStamp ) ) ;
#ifndef WIN32
#ifdef DEBUG
    time_t contentTime_T ( aTimeStamp ) ;
    char timeBuf[40] ;
    LOG_STR2("Content time of publish at send: %s", ctime_r(&contentTime_T,timeBuf)) ;
#endif
#endif
    retval.append((const char *)&timestampNetworkBO, sizeof(quint32))  ;
    // and that's all folks
    return retval ;
}


QByteArray ProtocolMessageFormatter::privMsgPublish(const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QList<quint32>& aBangPath,
        const Hash& aDestination,
        const Hash& aRecipient,
        quint32 aTimeStamp ) {


    for ( int i = 0 ; i < aBangPath.size() ; i++ ) {
        LOG_STR2("Bangpath if privmsg contains %u", aBangPath[i]) ;
    }

    unsigned char ch ( KPrivMessagePublish ) ;
    QByteArray retval ;
    retval.append((const char *)&ch, 1) ;

    quint32 bPathNumberNetworkByteorder ;

    if ( aBangPath.size() >= 1 ) {
        bPathNumberNetworkByteorder = htonl(aBangPath[0]);
    } else {
        bPathNumberNetworkByteorder = 0 ;
    }
    retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    if ( aBangPath.size() >= 2 ) {
        bPathNumberNetworkByteorder = htonl(aBangPath[1]);
    } else {
        bPathNumberNetworkByteorder = 0 ;
    }
    retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    if ( aBangPath.size() >= 3 ) {
        bPathNumberNetworkByteorder = htonl(aBangPath[2]);
    } else {
        bPathNumberNetworkByteorder = 0 ;
    }
    retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    if ( aBangPath.size() >= 4 ) {
        bPathNumberNetworkByteorder = htonl(aBangPath[3]);
    } else {
        bPathNumberNetworkByteorder = 0 ;
    }
    retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    if ( aBangPath.size() >= 5 ) {
        bPathNumberNetworkByteorder = htonl(aBangPath[4]);
    } else {
        bPathNumberNetworkByteorder = 0 ;
    }
    retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;

    // now has 5 numbers as bangpath, then put hash
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aContentHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // after hash, the size of content:
    quint32 lengthNetworkByteorder ( htonl( (quint32)(aContent.size()) ) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    // followed by content:
    retval.append(aContent) ;
    lengthNetworkByteorder = htonl( (quint32)(aSignature.size()) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    retval.append(aSignature) ;

    quint32 timestampNetworkBO ( htonl( aTimeStamp ) ) ;
#ifndef WIN32
#ifdef DEBUG
    time_t contentTime_T ( aTimeStamp ) ;
    char timebuf[40] ;
    LOG_STR2("Private message time of publish at send: %s", ctime_r(&contentTime_T,timebuf)) ;
#endif
#endif
    retval.append((const char *)&timestampNetworkBO, sizeof(quint32))  ;

    // then destination node:
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aDestination.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // then recipient:
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aRecipient.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::privMsgSend(const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const Hash& aDestination,
        const Hash& aRecipient,
        quint32 aTimeStamp ) {


    unsigned char ch ( KPrivMessageSend ) ;
    QByteArray retval ;
    retval.append((const char *)&ch, 1) ;

    //first thing in send is hash of the message
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aContentHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // after hash, the size of content:
    quint32 lengthNetworkByteorder ( htonl( (quint32)(aContent.size()) ) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    // followed by content:
    retval.append(aContent) ;
    lengthNetworkByteorder = htonl( (quint32)(aSignature.size()) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    retval.append(aSignature) ;

    quint32 timestampNetworkBO ( htonl( aTimeStamp ) ) ;
#ifndef WIN32
#ifdef DEBUG
    time_t contentTime_T ( aTimeStamp ) ;
    char timebuf[40] ;
    LOG_STR2("Private message time of publish at send: %s", ctime_r(&contentTime_T,timebuf)) ;
#endif
#endif
    retval.append((const char *)&timestampNetworkBO, sizeof(quint32))  ;

    // then destination node:
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aDestination.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // then recipient:
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aRecipient.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::replyToAdsClassified(const Hash& aClassificationHash,
        const QList<QPair<Hash,quint32> >& aListOfAds	) {
    QByteArray retval ;
    retval.append((const char *)&KListOfAdsClassifiedAtHash, sizeof(unsigned char)) ;

    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aClassificationHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    quint32 numberArticles ( htonl( aListOfAds.size() ) ) ;
    retval.append((const char *)(&numberArticles), sizeof(quint32)) ;
    for ( int k = 0 ; k < aListOfAds.size() ; k++ ) {
        for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
            quint32 hashBitsNetworkBO ( htonl( aListOfAds[k].first.iHash160bits[i] ) ) ;
            retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
        }
        quint32 ts (  htonl( aListOfAds[k].second ) ) ;
        retval.append((const char *)(&ts), sizeof(quint32)) ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::searchSend(const QString& aSearch,
        bool aSearchAds,
        bool aSearchProfiles,
        bool aSearchComments,
        const Hash& aSearchIdentifier) {
    QByteArray retval ;
    retval.append((const char *)&KSearchRequest, sizeof(unsigned char)) ;
    quint32 searchflags ( 0x0 ) ;
    if ( aSearchAds ) {
        searchflags = searchflags | 0x01 ;
    }
    if ( aSearchProfiles ) {
        searchflags = searchflags | 0x01 << 1 ;
    }
    if ( aSearchComments ) {
        searchflags = searchflags | 0x01 << 2 ;
    }
    quint32 flagBitsNetworkBO ( htonl( searchflags ) ) ;
    retval.append((const char *)(&flagBitsNetworkBO), sizeof(quint32)) ;

    quint32 searchIdNetworkBO ( htonl( aSearchIdentifier.iHash160bits[0] ) );
    retval.append((const char *)(&searchIdNetworkBO), sizeof(quint32)) ;

    QByteArray searchStringUtf8 ;
    searchStringUtf8.append(reinterpret_cast<const char *>(aSearch.toUtf8().constData())) ;
    if ( searchStringUtf8.size() > 20*1024 ) {
        // this is ridiculous
        searchStringUtf8 = QString("bananas").toUtf8() ;
    }
    quint32 sizeNetworkBO ( htonl(searchStringUtf8.size()) ) ;
    retval.append((const char *)(&sizeNetworkBO), sizeof(quint32)) ;
    retval.append(searchStringUtf8) ;
    return retval ;
}

QByteArray ProtocolMessageFormatter::searchResultsSend(const QList<SearchModel::SearchResultItem>& aResults,
        quint32 aSearchId) {
    // search results are actually plain json text.
    // there is magic, length of the json and then
    // the actual json.
    QByteArray retval ;
    retval.append((const char *)&KSearchResults, sizeof(unsigned char)) ;
    const QVariant toplevel
    ( SearchModel::serializeSearchResults(aResults,
                                          aSearchId) ) ;
    const QByteArray resultBytes ( JSonWrapper::serialize(toplevel) ) ;
    if ( resultBytes.size() > 0 ) {
        quint32 sizeNetworkBO ( htonl(resultBytes.size()) ) ;
        retval.append((const char *)(&sizeNetworkBO), sizeof(quint32)) ;
        retval.append(resultBytes) ;
    } else {
        retval.clear() ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::dbSearchTerms(const CaDbRecord::SearchTerms aSearchTerms) {
    QByteArray retval ;
    retval.append((const char *)&KDbRecordSearch, sizeof(unsigned char)) ;
    const QByteArray resultBytes ( aSearchTerms.asJSon() ) ;
    if ( resultBytes.size() > 0 ) {
        quint32 sizeNetworkBO ( htonl(resultBytes.size()) ) ;
        retval.append((const char *)(&sizeNetworkBO), sizeof(quint32)) ;
        retval.append(resultBytes) ;
    } else {
        retval.clear() ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::voiceCall(const VoiceCall& aCall,
        MController& aController,
        const Hash& aSelectedProfile,
        bool aDoSign ) {
    QByteArray retval ;
    if ( aCall.iOkToProceed ) {
        retval.append((const char *)&KVoiceCallStart, sizeof(unsigned char)) ;
    } else {
        retval.append((const char *)&KVoiceCallEnd, sizeof(unsigned char)) ;
    }
    const QByteArray resultBytes ( aCall.asJSon() ) ;
    if ( resultBytes.size() > 0 ) {
        quint32 sizeNetworkBO ( htonl(resultBytes.size()) ) ;
        retval.append((const char *)(&sizeNetworkBO), sizeof(quint32)) ;
        retval.append(resultBytes) ;
        QByteArray signature ;
        if ( aDoSign ) {
            if ( aController.model().contentEncryptionModel().sign(
                        aSelectedProfile,
                        resultBytes,
                        signature ) == 0 ) {
                // 0 is success
                quint32 signatureSizeNetworkBO ( htonl(signature.size()) ) ;
                retval.append((const char *)(&signatureSizeNetworkBO), sizeof(quint32)) ;
                retval.append(signature) ;
            } else {
                QLOG_STR("ERROR: Voice call signing failed") ;
                retval.clear() ;
            }
        } else {
            // put in a zero-size signature
            quint32 signatureSizeNetworkBO ( 0 ) ;
            retval.append((const char *)(&signatureSizeNetworkBO), sizeof(quint32)) ;
        }
    } else {
        retval.clear() ;
    }
    return retval ;
}

QByteArray ProtocolMessageFormatter::voiceCallRtData(quint32 aCallId,
                                                     quint32 aSeqNo,
                                                     VoiceCallEngine::PayloadType aPayloadType,
                                                     const QByteArray& aPayload ) {
    QByteArray retval ;

    switch ( aPayloadType ) {
    case VoiceCallEngine::Audio: {
        retval.append((const char *)&KRealtimeData, sizeof(unsigned char)) ;
        retval.append((const char *)&KRTDataAudioSubtype, sizeof(unsigned char)) ;
        quint32 callIdNetworkBO ( htonl(aCallId ) ) ; 
        retval.append((const char *)(&callIdNetworkBO), sizeof(quint32)) ; 
        quint32 seqNoNetworkBO ( htonl(aSeqNo ) ) ; 
        retval.append((const char *)(&seqNoNetworkBO), sizeof(quint32)) ; 
        quint32 payloadSizeNetworkBO ( htonl(aPayload.size() ) ) ; 
        retval.append((const char *)(&payloadSizeNetworkBO), sizeof(quint32)) ;
        retval.append(aPayload) ; 
        }
        break ; 
    default:
        // do not append anything, zero-size requests are not processed
        break ; 
    }
    return retval ; 
}

QByteArray ProtocolMessageFormatter::profileCommentPublish(const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QList<quint32>& aBangPath,
        const Hash& aProfileCommented,
        quint32 aTimeStamp,
        quint32 aFlags ) {
    return doCommentSendOrPublish(aContentHash,
                                  aContent,
                                  aSignature,
                                  aBangPath,
                                  aProfileCommented,
                                  aTimeStamp,
                                  aFlags,
                                  true );
}

QByteArray ProtocolMessageFormatter::profileCommentSend(const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const Hash& aProfileCommented,
        quint32 aTimeStamp,
        quint32 aFlags ) {
    QList<quint32>  dummyBangPath;
    return doCommentSendOrPublish(aContentHash,
                                  aContent,
                                  aSignature,
                                  dummyBangPath,
                                  aProfileCommented,
                                  aTimeStamp,
                                  aFlags,
                                  false );

}

QByteArray ProtocolMessageFormatter::doCommentSendOrPublish(const Hash& aContentHash,
        const QByteArray& aContent,
        const QByteArray& aSignature,
        const QList<quint32>& aBangPath,
        const Hash& aProfileCommented,
        quint32 aTimeStamp,
        quint32 aFlags,
        bool aIsPublish) {
    // ok, lets decide here the layout of comment publish..
    // first magick number telling what it is.
    // then bangpath 5 items and if not all 5 are filled,
    // the remaining ones are filled with zeroes.

    // then content hash, 5*32 bytes
    // then commented profile hash, 5*32 bytes

    // then length of the actual data, followed by the actual data
    // then length of the signature, followeds by the signature.
    // then flags, one unsigned 32-bit.
    //  * if bit 1 is up, it means that content is encrypted.
    // then time when content was published.


    unsigned char ch ( aIsPublish ? KProfileCommentPublish : KProfileCommentSend ) ;
    QByteArray retval ;
    retval.append((const char *)&ch, 1) ;

    if ( aIsPublish ) { // if in send-mode, just omit bangpath
        quint32 bPathNumberNetworkByteorder ;

        if ( aBangPath.size() >= 1 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[0]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 2 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[1]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 3 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[2]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 4 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[3]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 5 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[4]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    }
    // now has 5 numbers as bangpath, then put hash of content
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aContentHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }

    // now has 5 numbers as bangpath+content hash, then fingerprint of commented profile
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aProfileCommented.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }

    // now has bangpath+content hash+profile hash, follow the content+signature:
    // after hash, the size of content:
    quint32 lengthNetworkByteorder ( htonl( (quint32)(aContent.size()) ) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    // followed by content:
    retval.append(aContent) ;
    lengthNetworkByteorder = htonl( (quint32)(aSignature.size()) ) ;
    retval.append((const char *)(&lengthNetworkByteorder), sizeof(quint32)) ;
    retval.append(aSignature) ;
    // then flags:
    quint32 flagsNetworkByteOrder ( htonl(aFlags) ) ;
    retval.append((const char *)(&flagsNetworkByteOrder), sizeof(quint32)) ;
    // and finally timestamp of publish
    quint32 timestampNetworkBO ( htonl( aTimeStamp ) ) ;
    retval.append((const char *)&timestampNetworkBO, sizeof(quint32))  ;
    return retval ;
}


QByteArray ProtocolMessageFormatter::dbRecordPublish(const CaDbRecord& aRecord,
                                                     const QList<quint32>& aBangPath) {
    return doDbRecordPublishOrSend(aRecord,aBangPath, true) ; 
}
QByteArray ProtocolMessageFormatter::dbRecordSend(const CaDbRecord& aRecord) {
    QList<quint32> dummyBand ; 
    return doDbRecordPublishOrSend(aRecord,dummyBand, false) ; 
}

QByteArray ProtocolMessageFormatter::doDbRecordPublishOrSend(const CaDbRecord& aRecord,
                                                             const QList<quint32>& aBangPath,
                                                             bool aIsPublish ) {


    unsigned char ch ( aIsPublish ? KDbRecordPublish : KDbRecordSend ) ;
    QByteArray retval ;
    retval.append((const char *)&ch, 1) ;

    if ( aIsPublish ) { // if in send-mode, just omit bangpath
        quint32 bPathNumberNetworkByteorder ;

        if ( aBangPath.size() >= 1 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[0]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 2 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[1]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 3 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[2]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 4 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[3]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
        if ( aBangPath.size() >= 5 ) {
            bPathNumberNetworkByteorder = htonl(aBangPath[4]);
        } else {
            bPathNumberNetworkByteorder = 0 ;
        }
        retval.append((const char *)(&bPathNumberNetworkByteorder), sizeof(quint32)) ;
    }
    // now has 5 numbers as bangpath, then put hash of content
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aRecord.iRecordId.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // then put hash of collection
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aRecord.iCollectionId.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // then put hash of sender
    for ( int i = 0 ; i < Hash::KNumberOfIntsInHash ; i++ ) {
        quint32 hashBitsNetworkBO ( htonl( aRecord.iSenderHash.iHash160bits[i] ) ) ;
        retval.append((const char *)(&hashBitsNetworkBO), sizeof(quint32)) ;
    }
    // then len of searchphrase.. lets first have the phrase in Utf8
    const QByteArray searchPhraseUtf8 ( aRecord.iSearchPhrase.toUtf8() ) ;
    const quint32 searchPhraseUtf8Len(searchPhraseUtf8.length()) ;
    const quint32 searchPhraseUtf8LenNetworkBO(htonl(searchPhraseUtf8Len)) ;
    retval.append((const char *)(&searchPhraseUtf8LenNetworkBO), sizeof(quint32)) ;
    if ( searchPhraseUtf8Len > 0 ) {
        retval.append(searchPhraseUtf8) ; 
    }
    // searchnumber is a bit problematic, as it is 64 bit signed. 
    // there is not only byte-order issue, in theory there could 
    // be platforms where the sign-bit is some funny location. 
    // lets do performance-sub-optimal way by printing the number
    // into a string and putting that into byte-stream. 
    const QByteArray searchNumberUtf8 ( QString::number(aRecord.iSearchNumber ).toUtf8() ) ;
    const quint32 searchNumberUtf8Len(searchNumberUtf8.length()) ;
    const quint32 searchNumberUtf8LenNetworkBO(htonl(searchNumberUtf8Len)) ;
    retval.append((const char *)(&searchNumberUtf8LenNetworkBO), sizeof(quint32)) ;
    if ( searchNumberUtf8Len > 0 ) {
        retval.append(searchNumberUtf8) ; 
    }

    // then actual record data. it may have zero len
    const quint32 dataLen(aRecord.iData.length()) ;
    const quint32 dataLenNetworkBO(htonl(dataLen)) ;
    retval.append((const char *)(&dataLenNetworkBO), sizeof(quint32)) ;
    if ( dataLen > 0 ) {
        retval.append(aRecord.iData) ; 
    }
    // then signature data. should not have zero len
    const quint32 signatureLen(aRecord.iSignature.length()) ;
    if ( signatureLen == 0 ) {
        QLOG_STR("Trying to publish record with no signature!!") ; 
        return QByteArray() ; 
    }
    const quint32 signatureLenNetworkBO(htonl(signatureLen)) ;
    retval.append((const char *)(&signatureLenNetworkBO), sizeof(quint32)) ;
    if ( signatureLen > 0 ) {
        retval.append(aRecord.iSignature) ; 
    }

    // time of publish
    quint32 timestampNetworkBO ( htonl( aRecord.iTimeOfPublish ) ) ;
    retval.append((const char *)&timestampNetworkBO, sizeof(quint32))  ;

    // flags. currently includes only encryption bit. 
    quint32 flags ( htonl( aRecord.iIsEncrypted ? 1 : 0 ) ) ;
    retval.append((const char *)&flags, sizeof(quint32))  ;

    return retval ; 
}