/
packet-bitcoin.c
2581 lines (2292 loc) · 78.7 KB
/
packet-bitcoin.c
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* packet-bitcoin.c
* Routines for bitcoin dissection
* Copyright 2011, Christian Svensson <blue@cmd.nu>
* Bitcoin address: 15Y2EN5mLnsTt3CZBfgpnZR5SeLwu7WEHz
*
* See https://en.bitcoin.it/wiki/Protocol_specification
*
* Updated 2015, Laurenz Kamp <laurenz.kamp@gmx.de>
* Changes made:
* Updated dissectors:
* -> ping: ping packets now have a nonce.
* -> version: If version >= 70002, version messages have a relay flag.
* -> Messages with no payload: Added mempool and filterclear messages.
* Added dissectors:
* -> pong message
* -> notfound message
* -> reject message
* -> filterload
* -> filteradd
* -> merkleblock
* -> headers
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/exceptions.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include "packet-tcp.h"
#define BITCOIN_MAIN_MAGIC_NUMBER 0xD9B4BEF9
#define BITCOIN_TESTNET_MAGIC_NUMBER 0xDAB5BFFA
#define BITCOIN_TESTNET3_MAGIC_NUMBER 0x0709110B
static const value_string inv_types[] =
{
{ 0, "ERROR" },
{ 1, "MSG_TX" },
{ 2, "MSG_BLOCK" },
{ 3, "MSG_FILTERED_BLOCK" },
{ 4, "MSG_CMPCT_BLOCK" },
{ 5, "MSG_WTX" },
{ 0x40000001, "MSG_WITNESS_TX" },
{ 0x40000002, "MSG_WITNESS_BLOCK" },
{ 0, NULL }
};
static const value_string network_ids[] =
{
{ 0x01, "IPv4" },
{ 0x02, "IPv6" },
{ 0x03, "Tor v2" },
{ 0x04, "Tor v3" },
{ 0x05, "I2P" },
{ 0x06, "Cjdns" },
{ 0, NULL }
};
static const value_string reject_ccode[] =
{
{ 0x01, "REJECT_MALFORMED" },
{ 0x10, "REJECT_INVALID" },
{ 0x11, "REJECT_OBSOLETE" },
{ 0x12, "REJECT_DUPLICATE" },
{ 0x40, "REJECT_NONSTANDARD" },
{ 0x41, "REJECT_DUST" },
{ 0x42, "REJECT_INSUFFICIENTFEE" },
{ 0x43, "REJECT_CHECKPOINT" },
{ 0, NULL }
};
static const value_string filterload_nflags[] =
{
{ 0, "BLOOM_UPDATE_NONE" },
{ 1, "BLOOM_UPDATE_ALL" },
{ 2, "BLOOM_UPDATE_P2PUBKEY_ONLY" },
{ 0, NULL }
};
/*
* Minimum bitcoin identification header.
* - Magic - 4 bytes
* - Command - 12 bytes
* - Payload length - 4 bytes
* - Checksum - 4 bytes
*/
#define BITCOIN_HEADER_LENGTH 4+12+4+4
void proto_register_bitcoin(void);
void proto_reg_handoff_bitcoin(void);
static dissector_handle_t bitcoin_handle;
static dissector_table_t bitcoin_command_table;
static int proto_bitcoin;
static int hf_address_address;
static int hf_address_port;
static int hf_address_services;
static int hf_bitcoin_checksum;
static int hf_bitcoin_command;
static int hf_bitcoin_length;
static int hf_bitcoin_magic;
static int hf_bitcoin_msg_addr;
static int hf_bitcoin_msg_addrv2;
static int hf_bitcoin_msg_block;
static int hf_bitcoin_msg_feefilter;
static int hf_bitcoin_msg_filteradd;
static int hf_bitcoin_msg_filterload;
static int hf_bitcoin_msg_getblocks;
static int hf_bitcoin_msg_getdata;
static int hf_bitcoin_msg_getheaders;
static int hf_bitcoin_msg_headers;
static int hf_bitcoin_msg_inv;
static int hf_bitcoin_msg_merkleblock;
static int hf_bitcoin_msg_notfound;
static int hf_bitcoin_msg_ping;
static int hf_bitcoin_msg_pong;
static int hf_bitcoin_msg_reject;
static int hf_bitcoin_msg_sendcmpct;
static int hf_bitcoin_msg_tx;
static int hf_bitcoin_msg_version;
static int hf_data_value;
static int hf_data_varint_count16;
static int hf_data_varint_count32;
static int hf_data_varint_count64;
static int hf_data_varint_count8;
static int hf_msg_addr_address;
static int hf_msg_addr_count16;
static int hf_msg_addr_count32;
static int hf_msg_addr_count64;
static int hf_msg_addr_count8;
static int hf_msg_addr_timestamp;
static int hf_msg_addrv2_count16;
static int hf_msg_addrv2_count32;
static int hf_msg_addrv2_count64;
static int hf_msg_addrv2_count8;
static int hf_msg_addrv2_item;
static int hf_msg_addrv2_timestamp;
static int hf_msg_addrv2_services;
static int hf_msg_addrv2_network;
static int hf_msg_addrv2_address_ipv4;
static int hf_msg_addrv2_address_ipv6;
static int hf_msg_addrv2_address_other;
static int hf_msg_addrv2_port;
static int hf_msg_block_bits;
static int hf_msg_block_merkle_root;
static int hf_msg_block_nonce;
static int hf_msg_block_prev_block;
static int hf_msg_block_time;
static int hf_msg_block_transactions16;
static int hf_msg_block_transactions32;
static int hf_msg_block_transactions64;
static int hf_msg_block_transactions8;
static int hf_msg_block_version;
static int hf_msg_feefilter_value;
static int hf_msg_filteradd_data;
static int hf_msg_filterload_filter;
static int hf_msg_filterload_nflags;
static int hf_msg_filterload_nhashfunc;
static int hf_msg_filterload_ntweak;
static int hf_msg_getblocks_count16;
static int hf_msg_getblocks_count32;
static int hf_msg_getblocks_count64;
static int hf_msg_getblocks_count8;
static int hf_msg_getblocks_start;
static int hf_msg_getblocks_stop;
static int hf_msg_getdata_count16;
static int hf_msg_getdata_count32;
static int hf_msg_getdata_count64;
static int hf_msg_getdata_count8;
static int hf_msg_getdata_hash;
static int hf_msg_getdata_type;
static int hf_msg_getheaders_count16;
static int hf_msg_getheaders_count32;
static int hf_msg_getheaders_count64;
static int hf_msg_getheaders_count8;
static int hf_msg_getheaders_start;
static int hf_msg_getheaders_stop;
static int hf_msg_getheaders_version;
static int hf_msg_headers_bits;
static int hf_msg_headers_count16;
static int hf_msg_headers_count32;
static int hf_msg_headers_count64;
static int hf_msg_headers_count8;
static int hf_msg_headers_merkle_root;
static int hf_msg_headers_nonce;
static int hf_msg_headers_prev_block;
static int hf_msg_headers_time;
static int hf_msg_headers_version;
static int hf_msg_inv_count16;
static int hf_msg_inv_count32;
static int hf_msg_inv_count64;
static int hf_msg_inv_count8;
static int hf_msg_inv_hash;
static int hf_msg_inv_type;
static int hf_msg_merkleblock_bits;
static int hf_msg_merkleblock_flags_data;
static int hf_msg_merkleblock_flags_size16;
static int hf_msg_merkleblock_flags_size32;
static int hf_msg_merkleblock_flags_size64;
static int hf_msg_merkleblock_flags_size8;
static int hf_msg_merkleblock_hashes_count16;
static int hf_msg_merkleblock_hashes_count32;
static int hf_msg_merkleblock_hashes_count64;
static int hf_msg_merkleblock_hashes_count8;
static int hf_msg_merkleblock_hashes_hash;
static int hf_msg_merkleblock_merkle_root;
static int hf_msg_merkleblock_nonce;
static int hf_msg_merkleblock_prev_block;
static int hf_msg_merkleblock_time;
static int hf_msg_merkleblock_transactions;
static int hf_msg_merkleblock_version;
static int hf_msg_notfound_count16;
static int hf_msg_notfound_count32;
static int hf_msg_notfound_count64;
static int hf_msg_notfound_count8;
static int hf_msg_notfound_hash;
static int hf_msg_notfound_type;
static int hf_msg_ping_nonce;
static int hf_msg_pong_nonce;
static int hf_msg_reject_ccode;
static int hf_msg_reject_data;
static int hf_msg_reject_message;
static int hf_msg_reject_reason;
static int hf_msg_sendcmpct_announce;
static int hf_msg_sendcmpct_version;
static int hf_msg_tx_in;
static int hf_msg_tx_in_count16;
static int hf_msg_tx_in_count32;
static int hf_msg_tx_in_count64;
static int hf_msg_tx_in_count8;
static int hf_msg_tx_in_prev_outp_hash;
static int hf_msg_tx_in_prev_outp_index;
static int hf_msg_tx_in_prev_output;
static int hf_msg_tx_in_script16;
static int hf_msg_tx_in_script32;
static int hf_msg_tx_in_script64;
static int hf_msg_tx_in_script8;
static int hf_msg_tx_in_seq;
static int hf_msg_tx_in_sig_script;
static int hf_msg_tx_lock_time;
static int hf_msg_tx_out;
static int hf_msg_tx_out_count16;
static int hf_msg_tx_out_count32;
static int hf_msg_tx_out_count64;
static int hf_msg_tx_out_count8;
static int hf_msg_tx_out_script;
static int hf_msg_tx_out_script16;
static int hf_msg_tx_out_script32;
static int hf_msg_tx_out_script64;
static int hf_msg_tx_out_script8;
static int hf_msg_tx_out_value;
static int hf_msg_tx_witness;
static int hf_msg_tx_witness_components16;
static int hf_msg_tx_witness_components32;
static int hf_msg_tx_witness_components64;
static int hf_msg_tx_witness_components8;
static int hf_msg_tx_witness_component;
static int hf_msg_tx_witness_component_length16;
static int hf_msg_tx_witness_component_length32;
static int hf_msg_tx_witness_component_length64;
static int hf_msg_tx_witness_component_length8;
static int hf_msg_tx_witness_component_data;
static int hf_msg_tx_version;
static int hf_msg_tx_flag;
static int hf_msg_version_addr_me;
static int hf_msg_version_addr_you;
static int hf_msg_version_nonce;
static int hf_msg_version_relay;
static int hf_msg_version_services;
static int hf_msg_version_start_height;
static int hf_msg_version_timestamp;
static int hf_msg_version_user_agent;
static int hf_msg_version_version;
static int hf_services_network;
static int hf_services_getutxo;
static int hf_services_bloom;
static int hf_services_witness;
static int hf_services_xthin;
static int hf_services_compactfilters;
static int hf_services_networklimited;
static int hf_services_p2pv2;
static int hf_string_value;
static int hf_string_varint_count16;
static int hf_string_varint_count32;
static int hf_string_varint_count64;
static int hf_string_varint_count8;
static int * const services_hf_flags[] = {
&hf_services_network,
&hf_services_getutxo,
&hf_services_bloom,
&hf_services_witness,
&hf_services_xthin,
&hf_services_compactfilters,
&hf_services_networklimited,
&hf_services_p2pv2,
NULL
};
static gint ett_bitcoin;
static gint ett_bitcoin_msg;
static gint ett_services;
static gint ett_address;
static gint ett_string;
static gint ett_addr_list;
static gint ett_inv_list;
static gint ett_getdata_list;
static gint ett_notfound_list;
static gint ett_getblocks_list;
static gint ett_getheaders_list;
static gint ett_tx_in_list;
static gint ett_tx_in_outp;
static gint ett_tx_out_list;
static gint ett_tx_witness_list;
static gint ett_tx_witness_component_list;
static expert_field ei_bitcoin_command_unknown;
static expert_field ei_bitcoin_address_length;
static expert_field ei_bitcoin_script_len;
static bool bitcoin_desegment = true;
static guint
get_bitcoin_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb,
int offset, void *data _U_)
{
guint32 length;
length = BITCOIN_HEADER_LENGTH;
/* add payload length */
length += tvb_get_letohl(tvb, offset+16);
return length;
}
static void
format_feefilter_value(gchar *buf, gint64 value) {
snprintf(buf, ITEM_LABEL_LENGTH, "%.3f sat/B", ((gdouble) value) / 1000);
}
/**
* Create a sub-tree and fill it with a net_addr structure
*/
static proto_tree *
create_address_tree(tvbuff_t *tvb, proto_item *ti, guint32 offset)
{
proto_tree *tree;
tree = proto_item_add_subtree(ti, ett_address);
/* services */
proto_tree_add_bitmask(tree, tvb, offset, hf_address_services,
ett_services, services_hf_flags, ENC_LITTLE_ENDIAN);
offset += 8;
/* IPv6 address */
proto_tree_add_item(tree, hf_address_address, tvb, offset, 16, ENC_NA);
offset += 16;
/* port */
proto_tree_add_item(tree, hf_address_port, tvb, offset, 2, ENC_BIG_ENDIAN);
return tree;
}
/**
* Extract a variable length integer from a tvbuff
*/
static void
get_varint(tvbuff_t *tvb, const gint offset, gint *length, guint64 *ret)
{
guint value;
/* Note: just throw an exception if not enough bytes are available in the tvbuff */
/* calculate variable length */
value = tvb_get_guint8(tvb, offset);
if (value < 0xfd)
{
*length = 1;
*ret = value;
return;
}
if (value == 0xfd)
{
*length = 3;
*ret = tvb_get_letohs(tvb, offset+1);
return;
}
if (value == 0xfe)
{
*length = 5;
*ret = tvb_get_letohl(tvb, offset+1);
return;
}
*length = 9;
*ret = tvb_get_letoh64(tvb, offset+1);
return;
}
static void add_varint_item(proto_tree *tree, tvbuff_t *tvb, const gint offset, gint length,
int hfi8, int hfi16, int hfi32, int hfi64)
{
switch (length)
{
case 1:
proto_tree_add_item(tree, hfi8, tvb, offset, 1, ENC_LITTLE_ENDIAN);
break;
case 3:
proto_tree_add_item(tree, hfi16, tvb, offset+1, 2, ENC_LITTLE_ENDIAN);
break;
case 5:
proto_tree_add_item(tree, hfi32, tvb, offset+1, 4, ENC_LITTLE_ENDIAN);
break;
case 9:
proto_tree_add_item(tree, hfi64, tvb, offset+1, 8, ENC_LITTLE_ENDIAN);
break;
}
}
static proto_tree *
create_string_tree(proto_tree *tree, int hfindex, tvbuff_t *tvb, guint32* offset)
{
proto_tree *subtree;
proto_item *ti;
gint varint_length;
guint64 varint;
gint string_length;
/* First is the length of the following string as a varint */
get_varint(tvb, *offset, &varint_length, &varint);
string_length = (gint) varint;
ti = proto_tree_add_item(tree, hfindex, tvb, *offset, varint_length + string_length, ENC_NA);
subtree = proto_item_add_subtree(ti, ett_string);
/* length */
add_varint_item(subtree, tvb, *offset, varint_length, hf_string_varint_count8,
hf_string_varint_count16, hf_string_varint_count32,
hf_string_varint_count64);
*offset += varint_length;
/* string */
proto_tree_add_item(subtree, hf_string_value, tvb, *offset, string_length,
ENC_ASCII);
*offset += string_length;
return subtree;
}
static proto_tree *
create_data_tree(proto_tree *tree, int hfindex, tvbuff_t *tvb, guint32* offset)
{
proto_tree *subtree;
proto_item *ti;
gint varint_length;
guint64 varint;
gint data_length;
/* First is the length of the following string as a varint */
get_varint(tvb, *offset, &varint_length, &varint);
data_length = (gint) varint;
ti = proto_tree_add_item(tree, hfindex, tvb, *offset, varint_length + data_length, ENC_NA);
subtree = proto_item_add_subtree(ti, ett_string);
/* length */
add_varint_item(subtree, tvb, *offset, varint_length, hf_data_varint_count8,
hf_data_varint_count16, hf_data_varint_count32,
hf_data_varint_count64);
*offset += varint_length;
/* data */
proto_tree_add_item(subtree, hf_data_value, tvb, *offset, data_length,
BASE_SHOW_UTF_8_PRINTABLE);
*offset += data_length;
return subtree;
}
/* Note: A number of the following message handlers include code of the form:
* ...
* guint64 count;
* ...
* for (; count > 0; count--)
* {
* proto_tree_add_item9...);
* offset += ...;
* proto_tree_add_item9...);
* offset += ...;
* ...
* }
* ...
*
* Issue if 'count' is a very large number:
* If 'tree' is NULL, then the result will be effectively (but not really)
* an infinite loop. This is true because if 'tree' is NULL then
* proto_tree_add_item(tree, ...) is effectively a no-op and will not throw
* an exception.
* So: the loop should be executed only when 'tree' is defined so that the
* proto_ calls will throw an exception when the tvb is used up;
* This should only take a few-hundred loops at most.
* https://gitlab.com/wireshark/wireshark/-/issues/8312
*/
/**
* Handler for version messages
*/
static int
dissect_bitcoin_msg_version(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
guint32 version;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_version, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
version = tvb_get_letohl(tvb, offset);
proto_tree_add_item(tree, hf_msg_version_version, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
proto_tree_add_bitmask(tree, tvb, offset, hf_msg_version_services,
ett_services, services_hf_flags, ENC_LITTLE_ENDIAN);
offset += 8;
proto_tree_add_item(tree, hf_msg_version_timestamp, tvb, offset, 8, ENC_TIME_SECS_NSECS|ENC_LITTLE_ENDIAN);
offset += 8;
ti = proto_tree_add_item(tree, hf_msg_version_addr_you, tvb, offset, 26, ENC_NA);
create_address_tree(tvb, ti, offset);
offset += 26;
if (version >= 106)
{
ti = proto_tree_add_item(tree, hf_msg_version_addr_me, tvb, offset, 26, ENC_NA);
create_address_tree(tvb, ti, offset);
offset += 26;
proto_tree_add_item(tree, hf_msg_version_nonce, tvb, offset, 8, ENC_LITTLE_ENDIAN);
offset += 8;
create_string_tree(tree, hf_msg_version_user_agent, tvb, &offset);
}
if (version >= 209)
{
proto_tree_add_item(tree, hf_msg_version_start_height, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
}
if (version >= 70002)
{
proto_tree_add_item(tree, hf_msg_version_relay, tvb, offset, 1, ENC_LITTLE_ENDIAN);
offset += 1;
}
return offset;
}
/**
* Handler for address messages
*/
static int
dissect_bitcoin_msg_addr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_addr, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_addr_count8, hf_msg_addr_count16,
hf_msg_addr_count32, hf_msg_addr_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree *subtree;
ti = proto_tree_add_item(tree, hf_msg_addr_address, tvb, offset, 30, ENC_NA);
subtree = create_address_tree(tvb, ti, offset+4);
proto_tree_add_item(subtree, hf_msg_addr_timestamp, tvb, offset, 4, ENC_TIME_SECS|ENC_LITTLE_ENDIAN);
offset += 26;
offset += 4;
}
return offset;
}
/**
* Handler for addrv2 messages
*/
static int
dissect_bitcoin_msg_addrv2(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_addrv2, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_addrv2_count8, hf_msg_addrv2_count16,
hf_msg_addrv2_count32, hf_msg_addrv2_count64);
offset += length;
for (; count > 0; count--)
{
proto_item *sti;
proto_item *sti_services;
proto_tree *subtree;
guint64 services;
guint8 network;
guint64 address_length;
sti = proto_tree_add_item(tree, hf_msg_addrv2_item, tvb, offset, -1, ENC_NA);
subtree = proto_item_add_subtree(sti, ett_addr_list);
proto_tree_add_item(subtree, hf_msg_addrv2_timestamp, tvb, offset, 4, ENC_TIME_SECS|ENC_LITTLE_ENDIAN);
offset += 4;
get_varint(tvb, offset, &length, &services);
sti_services = proto_tree_add_bitmask_value(subtree, tvb, offset, hf_msg_addrv2_services,
ett_services, services_hf_flags, services);
proto_item_set_len(sti_services, length);
offset += length;
network = tvb_get_guint8(tvb, offset);
proto_tree_add_item(subtree, hf_msg_addrv2_network, tvb, offset, 1, ENC_LITTLE_ENDIAN);
offset += 1;
get_varint(tvb, offset, &length, &address_length);
offset += length;
switch (network)
{
case 1:
proto_tree_add_item(subtree, hf_msg_addrv2_address_ipv4, tvb, offset, (guint) address_length, ENC_NA);
if (address_length != 4) {
proto_tree_add_expert(subtree, pinfo, &ei_bitcoin_address_length,
tvb, offset, (guint) address_length);
}
break;
case 2:
proto_tree_add_item(subtree, hf_msg_addrv2_address_ipv6, tvb, offset, (guint) address_length, ENC_NA);
if (address_length != 16) {
proto_tree_add_expert(subtree, pinfo, &ei_bitcoin_address_length,
tvb, offset, (guint) address_length);
}
break;
default:
proto_tree_add_item(subtree, hf_msg_addrv2_address_other, tvb, offset, (guint) address_length, ENC_NA);
break;
}
offset += address_length;
proto_tree_add_item(subtree, hf_msg_addrv2_port, tvb, offset, 2, ENC_BIG_ENDIAN);
offset += 2;
proto_item_set_end(sti, tvb, offset);
}
return offset;
}
/**
* Handler for inventory messages
*/
static int
dissect_bitcoin_msg_inv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_inv, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_inv_count8, hf_msg_inv_count16,
hf_msg_inv_count32, hf_msg_inv_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree *subtree;
subtree = proto_tree_add_subtree(tree, tvb, offset, 36, ett_inv_list, NULL, "Inventory vector");
proto_tree_add_item(subtree, hf_msg_inv_type, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
proto_tree_add_item(subtree, hf_msg_inv_hash, tvb, offset, 32, ENC_NA);
offset += 32;
}
return offset;
}
/**
* Handler for getdata messages
*/
static int
dissect_bitcoin_msg_getdata(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_getdata, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_getdata_count8, hf_msg_getdata_count16,
hf_msg_getdata_count32, hf_msg_getdata_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree *subtree;
subtree = proto_tree_add_subtree(tree, tvb, offset, 36, ett_getdata_list, NULL, "Inventory vector");
proto_tree_add_item(subtree, hf_msg_getdata_type, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
proto_tree_add_item(subtree, hf_msg_getdata_hash, tvb, offset, 32, ENC_NA);
offset += 32;
}
return offset;
}
/**
* Handler for notfound messages
*/
static int
dissect_bitcoin_msg_notfound(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_notfound, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_notfound_count8, hf_msg_notfound_count16,
hf_msg_notfound_count32, hf_msg_notfound_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree *subtree;
subtree = proto_tree_add_subtree(tree, tvb, offset, 36, ett_notfound_list, NULL, "Inventory vector");
proto_tree_add_item(subtree, hf_msg_notfound_type, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
proto_tree_add_item(subtree, hf_msg_notfound_hash, tvb, offset, 32, ENC_NA);
offset += 32;
}
return offset;
}
/**
* Handler for getblocks messages
*/
static int
dissect_bitcoin_msg_getblocks(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_getblocks, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
/* why the protocol version is sent here nobody knows */
proto_tree_add_item(tree, hf_msg_version_version, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_getblocks_count8, hf_msg_getblocks_count16,
hf_msg_getblocks_count32, hf_msg_getblocks_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree_add_item(tree, hf_msg_getblocks_start, tvb, offset, 32, ENC_NA);
offset += 32;
}
proto_tree_add_item(tree, hf_msg_getblocks_stop, tvb, offset, 32, ENC_NA);
offset += 32;
return offset;
}
/**
* Handler for getheaders messages
* UNTESTED
*/
static int
dissect_bitcoin_msg_getheaders(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
proto_item *ti;
gint length;
guint64 count;
guint32 offset = 0;
ti = proto_tree_add_item(tree, hf_bitcoin_msg_getheaders, tvb, offset, -1, ENC_NA);
tree = proto_item_add_subtree(ti, ett_bitcoin_msg);
proto_tree_add_item(tree, hf_msg_headers_version, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
get_varint(tvb, offset, &length, &count);
add_varint_item(tree, tvb, offset, length, hf_msg_getheaders_count8, hf_msg_getheaders_count16,
hf_msg_getheaders_count32, hf_msg_getheaders_count64);
offset += length;
for (; count > 0; count--)
{
proto_tree_add_item(tree, hf_msg_getheaders_start, tvb, offset, 32, ENC_NA);
offset += 32;
}
proto_tree_add_item(tree, hf_msg_getheaders_stop, tvb, offset, 32, ENC_NA);
offset += 32;
return offset;
}
/**
* Handler for tx message body
*/
static guint32
dissect_bitcoin_msg_tx_common(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, guint msgnum)
{
proto_item *rti;
gint count_length;
guint64 in_count;
guint64 out_count;
if (msgnum == 0) {
rti = proto_tree_add_item(tree, hf_bitcoin_msg_tx, tvb, offset, -1, ENC_NA);
} else {
rti = proto_tree_add_none_format(tree, hf_bitcoin_msg_tx, tvb, offset, -1, "Tx message [ %4d ]", msgnum);
}
tree = proto_item_add_subtree(rti, ett_bitcoin_msg);
proto_tree_add_item(tree, hf_msg_tx_version, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
/* If present, "flag" always starts with 0x00. */
/* Otherwise we proceed straight to "in_count". */
guint8 flag = tvb_get_guint8(tvb, offset);
if (flag == 0) {
proto_tree_add_item(tree, hf_msg_tx_flag, tvb, offset, 2, ENC_NA);
offset += 2;
}
/* TxIn[] */
get_varint(tvb, offset, &count_length, &in_count);
add_varint_item(tree, tvb, offset, count_length, hf_msg_tx_in_count8, hf_msg_tx_in_count16,
hf_msg_tx_in_count32, hf_msg_tx_in_count64);
offset += count_length;
/* TxIn
* [36] previous_output outpoint
* [1+] script length var_int
* [ ?] signature script uchar[]
* [ 4] sequence uint32_t
*
* outpoint (aka previous output)
* [32] hash char[32
* [ 4] index uint32_t
*
*/
for (guint64 idx = 0; idx < in_count; idx++)
{
proto_tree *subtree;
proto_tree *prevtree;
proto_item *ti;
proto_item *pti;
guint64 script_length;
guint32 scr_len_offset;
scr_len_offset = offset+36;
get_varint(tvb, scr_len_offset, &count_length, &script_length);
/* A funny script_length won't cause an exception since the field type is FT_NONE */
ti = proto_tree_add_item(tree, hf_msg_tx_in, tvb, offset,
36 + count_length + (guint)script_length + 4, ENC_NA);
subtree = proto_item_add_subtree(ti, ett_tx_in_list);
/* previous output */
pti = proto_tree_add_item(subtree, hf_msg_tx_in_prev_output, tvb, offset, 36, ENC_NA);
prevtree = proto_item_add_subtree(pti, ett_tx_in_outp);
proto_tree_add_item(prevtree, hf_msg_tx_in_prev_outp_hash, tvb, offset, 32, ENC_NA);
offset += 32;
proto_tree_add_item(prevtree, hf_msg_tx_in_prev_outp_index, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
/* end previous output */
add_varint_item(subtree, tvb, offset, count_length, hf_msg_tx_in_script8, hf_msg_tx_in_script16,
hf_msg_tx_in_script32, hf_msg_tx_in_script64);
offset += count_length;
if ((offset + script_length) > G_MAXINT) {
proto_tree_add_expert(tree, pinfo, &ei_bitcoin_script_len,
tvb, scr_len_offset, count_length);
return G_MAXINT;
}
proto_tree_add_item(subtree, hf_msg_tx_in_sig_script, tvb, offset, (guint)script_length, ENC_NA);
offset += (guint)script_length;
proto_tree_add_item(subtree, hf_msg_tx_in_seq, tvb, offset, 4, ENC_LITTLE_ENDIAN);
offset += 4;
}
/* TxOut[] */
get_varint(tvb, offset, &count_length, &out_count);
add_varint_item(tree, tvb, offset, count_length, hf_msg_tx_out_count8, hf_msg_tx_out_count16,
hf_msg_tx_out_count32, hf_msg_tx_out_count64);
offset += count_length;
/* TxOut
* [ 8] value
* [1+] script length [var_int]
* [ ?] script
*/
for (; out_count > 0; out_count--)
{
proto_item *ti;
proto_tree *subtree;
guint64 script_length;
guint32 scr_len_offset;
scr_len_offset = offset+8;
get_varint(tvb, scr_len_offset, &count_length, &script_length);
/* A funny script_length won't cause an exception since the field type is FT_NONE */
ti = proto_tree_add_item(tree, hf_msg_tx_out, tvb, offset,
8 + count_length + (guint)script_length , ENC_NA);
subtree = proto_item_add_subtree(ti, ett_tx_out_list);
proto_tree_add_item(subtree, hf_msg_tx_out_value, tvb, offset, 8, ENC_LITTLE_ENDIAN);
offset += 8;
add_varint_item(subtree, tvb, offset, count_length, hf_msg_tx_out_script8, hf_msg_tx_out_script16,
hf_msg_tx_out_script32, hf_msg_tx_out_script64);
offset += count_length;
if ((offset + script_length) > G_MAXINT) {
proto_tree_add_expert(tree, pinfo, &ei_bitcoin_script_len,
tvb, scr_len_offset, count_length);