1 /******************************************************************************************************/
3 * Routines for BT-DHT dissection
4 * Copyright 2011, Xiao Xiangquan <xiaoxiangquan@gmail.com>
6 * A plugin for BT-DHT packet:
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1999 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
19 #include <epan/packet.h>
20 #include <epan/conversation.h>
21 #include <epan/prefs.h>
22 #include <epan/to_str.h>
23 #include <epan/expert.h>
25 #include <wsutil/strtoi.h>
29 void proto_register_bt_dht(void);
30 void proto_reg_handoff_bt_dht(void);
33 * https://www.bittorrent.org/beps/bep_0005.html BEP 5 DHT Protocol
34 * https://www.bittorrent.org/beps/bep_0042.html BEP 42 DHT Security extension
37 static int proto_bt_dht
;
38 static dissector_handle_t bt_dht_handle
;
41 static int hf_bencoded_int
;
42 static int hf_bencoded_string
;
43 static int hf_bencoded_list
;
44 static int hf_bencoded_dict
;
45 static int hf_bencoded_dict_entry
;
46 static int hf_bencoded_list_terminator
;
48 static int hf_bt_dht_error
;
49 static int hf_bt_dht_peers
;
50 static int hf_bt_dht_peer
;
51 static int hf_bt_dht_nodes
;
52 static int hf_bt_dht_node
;
53 static int hf_bt_dht_id
;
58 static int hf_truncated_data
;
60 static expert_field ei_int_string
;
61 static expert_field ei_invalid_len
;
62 static expert_field ei_duplicate_dict_keys
;
63 static expert_field ei_unsorted_dict_keys
;
66 static int ett_bt_dht
;
67 static int ett_bencoded_list
;
68 static int ett_bencoded_dict
;
69 static int ett_bencoded_dict_entry
;
70 static int ett_bt_dht_error
;
71 static int ett_bt_dht_peers
;
72 static int ett_bt_dht_nodes
;
74 /* some keys use short name in packet */
75 static const value_string short_key_name_value_string
[] = {
76 { 'a', "Request arguments" },
78 { 'q', "Request type" },
79 { 'r', "Response values" },
80 { 't', "Transaction ID" },
82 { 'y', "Message type" },
86 /* some values use short name in packet */
87 static const value_string short_val_name_value_string
[] = {
94 static const char dict_str
[] = "Dictionary...";
95 static const char list_str
[] = "List...";
99 bencoded_string_length(packet_info
*pinfo
, tvbuff_t
*tvb
, unsigned *offset_ptr
, unsigned *length
)
101 unsigned offset
, start
;
102 unsigned remaining
= tvb_captured_length_remaining(tvb
, *offset_ptr
);
106 offset
= *offset_ptr
;
109 while(tvb_get_uint8(tvb
, offset
) != ':' && --remaining
)
112 if (remaining
&& ws_strtou32(tvb_get_string_enc(pinfo
->pool
, tvb
, start
, offset
-start
, ENC_ASCII
),
114 ++offset
; /* skip the ':' */
115 *offset_ptr
= offset
;
123 * dissect a bencoded string from tvb, start at offset. it's like "5:abcde"
124 * *result will be the decoded value
128 dissect_bencoded_string(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **result
, bool tohex
, const char *label
)
131 if (!bencoded_string_length(pinfo
, tvb
, &offset
, &string_len
))
134 const unsigned remaining
= tvb_captured_length_remaining(tvb
, offset
);
135 if (remaining
< string_len
)
138 /* fill the return data */
142 *result
= tvb_bytes_to_str(pinfo
->pool
, tvb
, offset
, string_len
);
144 *result
= tvb_get_string_enc( pinfo
->pool
, tvb
, offset
, string_len
, ENC_ASCII
);
146 proto_tree_add_string_format( tree
, hf_bencoded_string
, tvb
, offset
, string_len
, *result
, "%s: %s", label
, *result
);
147 offset
+= string_len
;
152 * dissect a bencoded integer from tvb, start at offset. it's like "i5673e"
153 * *result will be the decoded value
156 dissect_bencoded_int(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **result
, const char *label
)
158 unsigned start_offset
;
159 unsigned remaining
= tvb_captured_length_remaining(tvb
, offset
);
161 /* the shortest valid integer is i0e, so we need at least 3 bytes */
165 if (tvb_get_uint8(tvb
, offset
) != 'i')
170 start_offset
= offset
;
171 while (tvb_get_uint8(tvb
, offset
) != 'e' && --remaining
)
177 proto_tree_add_item(tree
, hf_bencoded_list_terminator
, tvb
, offset
, 1, ENC_ASCII
);
179 *result
= tvb_get_string_enc( pinfo
->pool
, tvb
, start_offset
, offset
-start_offset
, ENC_ASCII
);
180 proto_tree_add_string_format( tree
, hf_bencoded_int
, tvb
, start_offset
, offset
-start_offset
, *result
,
181 "%s: %s", label
, *result
);
187 /* pre definition of dissect_bencoded_dict(), which is needed by dissect_bencoded_list() */
188 static int dissect_bencoded_dict(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char *label
);
190 /* dissect a bencoded list from tvb, start at offset. it's like "lXXXe", "X" is any bencoded thing */
192 // NOLINTNEXTLINE(misc-no-recursion)
193 dissect_bencoded_list(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char *label
)
196 proto_tree
*sub_tree
;
200 /* the shortest valid list is "le", so we need at least 2 bytes */
201 if (tvb_captured_length_remaining(tvb
, offset
) < 2)
204 ti
= proto_tree_add_none_format( tree
, hf_bencoded_list
, tvb
, offset
, 0, "%s: list...", label
);
205 sub_tree
= proto_item_add_subtree( ti
, ett_bencoded_list
);
207 if (tvb_get_uint8(tvb
, offset
) != 'l')
211 while (tvb_captured_length_remaining(tvb
, offset
) > 0)
213 one_byte
= tvb_get_uint8(tvb
, offset
);
217 unsigned start_offset
= offset
;
222 offset
= dissect_bencoded_int( tvb
, pinfo
, sub_tree
, offset
, &result
, "Integer" );
226 offset
= dissect_bencoded_list( tvb
, pinfo
, sub_tree
, offset
, "Sub-list" );
230 offset
= dissect_bencoded_dict( tvb
, pinfo
, sub_tree
, offset
, "Sub-dict" );
234 offset
= dissect_bencoded_string( tvb
, pinfo
, sub_tree
, offset
, &result
, false, "String" );
237 if (offset
<= start_offset
)
239 proto_tree_add_expert(sub_tree
, pinfo
, &ei_int_string
, tvb
, offset
, -1);
240 /* if offset is not going on, there is no chance to exit the loop, then return*/
245 if (tvb_captured_length_remaining(tvb
, offset
) == 0)
248 proto_tree_add_item(sub_tree
, hf_bencoded_list_terminator
, tvb
, offset
, 1, ENC_ASCII
);
253 /* dissect a bt dht error from tvb, start at offset. it's like "li201e9:error msge" */
255 dissect_bt_dht_error(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **result
, const char *label
)
258 proto_tree
*sub_tree
;
259 const char *error_no
, *error_msg
;
264 ti
= proto_tree_add_item( tree
, hf_bt_dht_error
, tvb
, offset
, 0, ENC_NA
);
265 sub_tree
= proto_item_add_subtree( ti
, ett_bt_dht_error
);
267 /* we have confirmed that the first byte is 'l' */
270 /* dissect bt-dht error number and message */
271 offset
= dissect_bencoded_int( tvb
, pinfo
, sub_tree
, offset
, &error_no
, "Error ID" );
275 offset
= dissect_bencoded_string( tvb
, pinfo
, sub_tree
, offset
, &error_msg
, false, "Error Message" );
280 proto_item_set_text( ti
, "%s: error %s, %s", label
, error_no
, error_msg
);
281 col_append_fstr( pinfo
->cinfo
, COL_INFO
, " No=%s Msg=%s", error_no
, error_msg
);
282 *result
= wmem_strdup_printf(pinfo
->pool
, "error %s, %s", error_no
, error_msg
);
287 /* dissect a bt dht values list from tvb, start at offset. it's like "l6:....6:....e" */
289 dissect_bt_dht_values(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **result
, const char *label
)
292 proto_tree
*sub_tree
;
293 proto_item
*value_ti
;
294 proto_tree
*value_tree
;
299 ti
= proto_tree_add_item( tree
, hf_bt_dht_peers
, tvb
, offset
, 0, ENC_NA
);
300 sub_tree
= proto_item_add_subtree( ti
, ett_bt_dht_peers
);
303 /* we has confirmed that the first byte is 'l' */
306 /* dissect bt-dht values */
307 while( tvb_get_uint8(tvb
,offset
)!='e' )
309 if (!bencoded_string_length(pinfo
, tvb
, &offset
, &string_len
))
311 expert_add_info(pinfo
, ti
, &ei_invalid_len
);
312 // Fail hard here rather than potentially looping excessively.
315 else if (string_len
== 6)
317 /* 4 bytes ip, 2 bytes port */
320 value_ti
= proto_tree_add_item( sub_tree
, hf_bt_dht_peer
, tvb
, offset
, 6, ENC_NA
);
321 proto_item_append_text(value_ti
, " %d", peer_index
);
322 value_tree
= proto_item_add_subtree( value_ti
, ett_bt_dht_peers
);
324 proto_tree_add_item( value_tree
, hf_ip
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
325 proto_item_append_text(value_ti
, " (IP/Port: %s", tvb_ip_to_str(pinfo
->pool
, tvb
, offset
));
326 proto_tree_add_item( value_tree
, hf_port
, tvb
, offset
+4, 2, ENC_BIG_ENDIAN
);
327 proto_item_append_text(value_ti
, ":%u)", tvb_get_ntohs( tvb
, offset
+4 ));
329 else if (string_len
== 18)
331 /* 16 bytes ip, 2 bytes port */
334 value_ti
= proto_tree_add_item( sub_tree
, hf_bt_dht_peer
, tvb
, offset
, 18, ENC_NA
);
335 proto_item_append_text(value_ti
, " %d", peer_index
);
336 value_tree
= proto_item_add_subtree( value_ti
, ett_bt_dht_peers
);
338 proto_tree_add_item( value_tree
, hf_ip6
, tvb
, offset
, 16, ENC_NA
);
339 proto_item_append_text(value_ti
, " (IPv6/Port: [%s]", tvb_ip6_to_str(pinfo
->pool
, tvb
, offset
));
340 proto_tree_add_item( value_tree
, hf_port
, tvb
, offset
+16, 2, ENC_BIG_ENDIAN
);
341 proto_item_append_text(value_ti
, ":%u)", tvb_get_ntohs( tvb
, offset
+16 ));
346 proto_tree_add_item( tree
, hf_truncated_data
, tvb
, offset
, string_len
, ENC_NA
);
349 offset
+= string_len
;
352 if (tvb_get_uint8(tvb
,offset
)=='e') { /* list ending delimiter */
353 proto_tree_add_item(sub_tree
, hf_bencoded_list_terminator
, tvb
, offset
, 1, ENC_ASCII
);
357 proto_item_set_text( ti
, "%s: %d peers", label
, peer_index
);
358 col_append_fstr( pinfo
->cinfo
, COL_INFO
, " Peers=%d", peer_index
);
359 *result
= wmem_strdup_printf(pinfo
->pool
, "%d peers", peer_index
);
365 dissect_bt_dht_nodes(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **result
, const char *label
, bool is_ipv6
)
368 proto_tree
*sub_tree
;
370 proto_tree
*node_tree
;
374 unsigned node_byte_length
;
376 if (!bencoded_string_length(pinfo
, tvb
, &offset
, &string_len
))
379 ti
= proto_tree_add_item( tree
, hf_bt_dht_nodes
, tvb
, offset
, string_len
, ENC_NA
);
380 sub_tree
= proto_item_add_subtree( ti
, ett_bt_dht_nodes
);
383 /* 26 bytes = 20 bytes id + 4 bytes ipv4 address + 2 bytes port */
384 node_byte_length
= 26;
388 /* 38 bytes = 20 bytes id + 16 bytes ipv6 address + 2 bytes port */
389 node_byte_length
= 38;
392 for( ; string_len
>=node_byte_length
; string_len
-=node_byte_length
, offset
+=node_byte_length
)
396 node_ti
= proto_tree_add_item( sub_tree
, hf_bt_dht_node
, tvb
, offset
, node_byte_length
, ENC_NA
);
397 proto_item_append_text(node_ti
, " %d", node_index
);
398 node_tree
= proto_item_add_subtree( node_ti
, ett_bt_dht_peers
);
400 proto_tree_add_item( node_tree
, hf_bt_dht_id
, tvb
, offset
, 20, ENC_NA
);
401 proto_item_append_text(node_ti
, " (id: %s", tvb_bytes_to_str(pinfo
->pool
, tvb
, offset
, 20));
405 proto_tree_add_item( node_tree
, hf_ip6
, tvb
, offset
+20, 16, ENC_NA
);
406 proto_item_append_text(node_ti
, ", IPv6/Port: [%s]", tvb_ip6_to_str(pinfo
->pool
, tvb
, offset
+20));
408 proto_tree_add_item( node_tree
, hf_port
, tvb
, offset
+36, 2, ENC_BIG_ENDIAN
);
409 proto_item_append_text(node_ti
, ":%u)", tvb_get_ntohs( tvb
, offset
+36 ));
413 proto_tree_add_item( node_tree
, hf_ip
, tvb
, offset
+20, 4, ENC_BIG_ENDIAN
);
414 proto_item_append_text(node_ti
, ", IPv4/Port: %s", tvb_ip_to_str(pinfo
->pool
, tvb
, offset
+20));
416 proto_tree_add_item( node_tree
, hf_port
, tvb
, offset
+24, 2, ENC_BIG_ENDIAN
);
417 proto_item_append_text(node_ti
, ":%u)", tvb_get_ntohs( tvb
, offset
+24 ));
423 proto_tree_add_item( tree
, hf_truncated_data
, tvb
, offset
, string_len
, ENC_NA
);
424 offset
+= string_len
;
426 proto_item_set_text( ti
, "%s: %d nodes", label
, node_index
);
427 col_append_fstr( pinfo
->cinfo
, COL_INFO
, " Nodes=%d", node_index
);
428 *result
= wmem_strdup_printf(pinfo
->pool
, "%d", node_index
);
434 // NOLINTNEXTLINE(misc-no-recursion)
435 dissect_bencoded_dict_entry(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char **key
)
438 proto_tree
*sub_tree
;
441 unsigned orig_offset
= offset
;
445 ti
= proto_tree_add_item( tree
, hf_bencoded_dict_entry
, tvb
, offset
, 0, ENC_NA
);
446 sub_tree
= proto_item_add_subtree( ti
, ett_bencoded_dict_entry
);
448 /* dissect the key, it must be a string */
449 offset
= dissect_bencoded_string( tvb
, pinfo
, sub_tree
, offset
, key
, false, "Key" );
452 proto_tree_add_expert_format(sub_tree
, pinfo
, &ei_int_string
, tvb
, offset
, -1, "Invalid string for Key");
456 if (tvb_captured_length_remaining(tvb
, offset
) == 0)
459 /* If it is a dict, then just do recursion */
460 switch( tvb_get_uint8(tvb
,offset
) )
463 offset
= dissect_bencoded_dict( tvb
, pinfo
, sub_tree
, offset
, "Value" );
467 if( strcmp(*key
,"e")==0 )
468 offset
= dissect_bt_dht_error( tvb
, pinfo
, sub_tree
, offset
, &val
, "Value" );
469 else if( strcmp(*key
,"values")==0 )
470 offset
= dissect_bt_dht_values( tvb
, pinfo
, sub_tree
, offset
, &val
, "Value" );
471 /* other unfamiliar lists */
474 offset
= dissect_bencoded_list( tvb
, pinfo
, sub_tree
, offset
, "Value" );
479 offset
= dissect_bencoded_int( tvb
, pinfo
, sub_tree
, offset
, &val
, "Value" );
483 /* special process */
484 if( strcmp(*key
,"nodes")==0 )
486 offset
= dissect_bt_dht_nodes( tvb
, pinfo
, sub_tree
, offset
, &val
, "Value", 0 );
488 else if( strcmp(*key
,"nodes6")==0 )
490 offset
= dissect_bt_dht_nodes( tvb
, pinfo
, sub_tree
, offset
, &val
, "Value", 1 );
492 else if( strcmp(*key
,"ip")==0 )
495 * BEP 42 DHT Security extension
496 * https://www.bittorrent.org/beps/bep_0042.html
497 * https://www.rasterbar.com/products/libtorrent/dht_sec.html
501 int old_offset
= offset
;
502 if (!bencoded_string_length(pinfo
, tvb
, &offset
, &len
)) {
503 proto_tree_add_expert_format(sub_tree
, pinfo
, &ei_int_string
, tvb
, offset
, -1, "Invalid string for value");
508 proto_tree_add_item(sub_tree
, hf_ip
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
509 val
= tvb_ip_to_str(pinfo
->pool
, tvb
, offset
);
511 proto_tree_add_item(sub_tree
, hf_port
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
516 proto_tree_add_item(sub_tree
, hf_ip6
, tvb
, offset
, 16, ENC_NA
);
517 val
= tvb_ip6_to_str(pinfo
->pool
, tvb
, offset
);
520 proto_tree_add_item(sub_tree
, hf_port
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
524 offset
= dissect_bencoded_string( tvb
, pinfo
, sub_tree
, old_offset
, &val
, true, "Value" );
529 /* some need to return hex string */
530 tohex
= strcmp(*key
,"id")==0 || strcmp(*key
,"target")==0
531 || strcmp(*key
,"info_hash")==0 || strcmp(*key
,"t")==0
532 || strcmp(*key
,"v")==0 || strcmp(*key
,"token")==0;
533 offset
= dissect_bencoded_string( tvb
, pinfo
, sub_tree
, offset
, &val
, tohex
, "Value" );
539 proto_tree_add_expert_format(sub_tree
, pinfo
, &ei_int_string
, tvb
, offset
, -1, "Invalid string for value");
543 if(*key
&& strcmp(*key
,"q")==0 && strlen(val
)>1 )
544 col_prepend_fstr(pinfo
->cinfo
, COL_INFO
, "%c%s", g_ascii_toupper(val
[0]), val
+ 1);
545 if(*key
&& strcmp(*key
,"r")==0 )
546 col_prepend_fstr(pinfo
->cinfo
, COL_INFO
, "Response");
547 if(*key
&& strcmp(*key
,"e")==0 )
548 col_prepend_fstr(pinfo
->cinfo
, COL_INFO
, "Error");
549 if(*key
&& (strcmp(*key
,"info_hash")==0 || strcmp(*key
,"target")==0) )
550 col_append_fstr(pinfo
->cinfo
, COL_INFO
, " %c%s=%s", g_ascii_toupper((*key
)[0]), *key
+ 1, val
);
552 const char * printable_key
= *key
;
553 if(key
&& strlen(*key
)==1 )
554 printable_key
= val_to_str_const( (*key
)[0], short_key_name_value_string
, *key
);
555 if(val
&& strlen(val
)==1 )
556 val
= val_to_str_const( val
[0], short_val_name_value_string
, val
);
558 proto_item_set_text( ti
, "%s: %s", printable_key
, val
);
559 proto_item_set_len( ti
, offset
-orig_offset
);
566 // NOLINTNEXTLINE(misc-no-recursion)
567 dissect_bencoded_dict(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, const char *label
)
570 proto_tree
*sub_tree
;
571 unsigned orig_offset
= offset
;
573 /* the shortest valid dictionary is "de", so we need at least 2 bytes */
574 if (tvb_captured_length_remaining(tvb
, offset
) < 2)
579 ti
= proto_tree_add_item(tree
, proto_bt_dht
, tvb
, 0, -1, ENC_NA
);
580 sub_tree
= proto_item_add_subtree(ti
, ett_bt_dht
);
584 ti
= proto_tree_add_none_format( tree
, hf_bencoded_dict
, tvb
, offset
, -1, "%s: Dictionary...", label
);
585 sub_tree
= proto_item_add_subtree( ti
, ett_bencoded_dict
);
588 if (tvb_get_uint8(tvb
, offset
) != 'd')
592 const char * prev_key
= NULL
;
593 unsigned prev_key_offset
= 0;
594 while (tvb_captured_length_remaining(tvb
, offset
) > 0) {
595 if (tvb_get_uint8(tvb
, offset
) == 'e')
598 const char * key
= NULL
;
599 const unsigned entry_start
= offset
;
600 offset
= dissect_bencoded_dict_entry( tvb
, pinfo
, sub_tree
, offset
, &key
);
603 proto_tree_add_expert(sub_tree
, pinfo
, &ei_int_string
, tvb
, offset
, -1);
607 if (prev_key
!= NULL
&& key
!= NULL
) {
608 const int ordering
= strcmp(key
, prev_key
);
610 proto_tree_add_expert(
611 sub_tree
, pinfo
, &ei_unsorted_dict_keys
, tvb
, prev_key_offset
, offset
- prev_key_offset
);
612 } else if (ordering
== 0) {
613 proto_tree_add_expert(
614 sub_tree
, pinfo
, &ei_duplicate_dict_keys
, tvb
, prev_key_offset
, offset
- prev_key_offset
);
619 prev_key_offset
= entry_start
;
622 if (tvb_captured_length_remaining(tvb
, offset
) == 0)
625 proto_tree_add_item(sub_tree
, hf_bencoded_list_terminator
, tvb
, offset
, 1, ENC_ASCII
);
627 proto_item_set_len( ti
, offset
-orig_offset
);
633 test_bt_dht(packet_info
*pinfo _U_
, tvbuff_t
*tvb
, int offset
, void *data _U_
)
636 /* The DHT KRPC protocol sends packets that are bencoded dictionaries.
637 * Bencoded dictionaries always have the keys in sorted (raw string)
638 * order. There are three possible message types, query, which has "a" and
639 * "q" keys that map to dictionaries, response, which has an "r" key
640 * that maps to a dictionary, and error, which has an "e" key that maps
643 * Conveniently, those keys appear in sort order before all other possible
644 * top level keys, with the exception of the "ip" key added in BEP-0042.
646 * Thus, there are only four possible initial sets of bytes, corresponding
647 * to beginning with an "a" dictionary, "r" dictionary, "ip" string, or an
651 if (tvb_captured_length_remaining(tvb
, offset
) < DHT_MIN_LEN
)
654 if (tvb_memeql(tvb
, offset
, (const uint8_t*)"d1:ad", 5) == 0) {
656 } else if (tvb_memeql(tvb
, offset
, (const uint8_t*)"d1:rd", 5) == 0) {
658 } else if (tvb_memeql(tvb
, offset
, (const uint8_t*)"d2:ip", 5) == 0) {
660 } else if (tvb_memeql(tvb
, offset
, (const uint8_t*)"d1:el", 5) == 0) {
668 dissect_bt_dht(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
)
670 /* BitTorrent clients use the same UDP connection for DHT as for uTP.
671 * So even if this has been set as the dissector for this conversation
672 * or port, test it and reject it if not BT-DHT in order to give other
673 * dissectors, especially BT-uTP, a chance.
675 if (!test_bt_dht(pinfo
, tvb
, 0, data
)) {
679 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "BT-DHT");
680 col_clear(pinfo
->cinfo
, COL_INFO
);
682 /* XXX: There is a separate "bencode" dissector. Would it be possible
683 * to use it, at least to move some functions into a shared header?
684 * DHT has some keys with special meanings, and some values that
685 * are supposed to be interpreted specially (e.g., IP/port combinations),
686 * so maybe it's more trouble than it's worth.
688 return dissect_bencoded_dict(tvb
, pinfo
, tree
, 0, "BitTorrent DHT Protocol");
692 bool dissect_bt_dht_heur (tvbuff_t
*tvb
, packet_info
*pinfo
,
693 proto_tree
*tree
, void *data
)
695 conversation_t
*conversation
;
697 if (!test_bt_dht(pinfo
, tvb
, 0, data
)) {
701 conversation
= find_or_create_conversation(pinfo
);
702 conversation_set_dissector_from_frame_number(conversation
, pinfo
->num
, bt_dht_handle
);
704 dissect_bt_dht(tvb
, pinfo
, tree
, NULL
);
709 proto_register_bt_dht(void)
711 expert_module_t
* expert_bt_dht
;
713 static hf_register_info hf
[] = {
714 { &hf_bencoded_string
,
715 { "String", "bt-dht.bencoded.string",
716 FT_STRING
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
719 { "List", "bt-dht.bencoded.list",
720 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
723 { "Int", "bt-dht.bencoded.int",
724 FT_STRING
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
727 { "Dictionary", "bt-dht.bencoded.dict",
728 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
730 { &hf_bencoded_dict_entry
,
731 { "Dictionary Entry", "bt-dht.bencoded.dict_entry",
732 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
734 { &hf_bencoded_list_terminator
,
735 { "Terminator", "bt-dht.bencoded.list.terminator",
736 FT_STRING
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
739 { "Error", "bt-dht.error",
740 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
743 { "Peer", "bt-dht.peer",
744 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
747 { "Peers", "bt-dht.peers",
748 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
751 { "Node", "bt-dht.node",
752 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
755 { "Nodes", "bt-dht.nodes",
756 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
760 FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
764 FT_IPv4
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
767 { "IP", "bt-dht.ip6",
768 FT_IPv6
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
771 { "Port", "bt-dht.port",
772 FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
}
774 { &hf_truncated_data
,
775 { "Truncated data", "bt-dht.truncated_data",
776 FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
780 static ei_register_info ei
[] = {
781 { &ei_int_string
, { "bt-dht.invalid_string", PI_MALFORMED
, PI_ERROR
,
782 "String must contain an integer", EXPFILL
}},
783 { &ei_invalid_len
, { "bt-dht.invalid_length", PI_MALFORMED
, PI_ERROR
,
784 "Invalid length", EXPFILL
}},
785 { &ei_duplicate_dict_keys
, { "bt-dht.bencoding.dict_duplicate_key", PI_PROTOCOL
, PI_WARN
,
786 "Dictionary has duplicate keys", EXPFILL
}},
787 { &ei_unsorted_dict_keys
, { "bt-dht.bencoding.dict_out_of_order", PI_PROTOCOL
, PI_CHAT
,
788 "Dictionary keys are not in sorted order", EXPFILL
}},
791 /* Setup protocol subtree array */
792 static int *ett
[] = {
799 &ett_bencoded_dict_entry
802 module_t
*bt_dht_module
;
804 proto_bt_dht
= proto_register_protocol ("BitTorrent DHT Protocol", "BT-DHT", "bt-dht");
806 bt_dht_module
= prefs_register_protocol(proto_bt_dht
, NULL
);
807 prefs_register_obsolete_preference(bt_dht_module
, "enable");
809 proto_register_field_array(proto_bt_dht
, hf
, array_length(hf
));
810 proto_register_subtree_array(ett
, array_length(ett
));
812 expert_bt_dht
= expert_register_protocol(proto_bt_dht
);
813 expert_register_field_array(expert_bt_dht
, ei
, array_length(ei
));
815 bt_dht_handle
= register_dissector("bt-dht", dissect_bt_dht
, proto_bt_dht
);
819 proto_reg_handoff_bt_dht(void)
821 heur_dissector_add("udp", dissect_bt_dht_heur
, "BitTorrent DHT over UDP", "bittorrent_dht_udp", proto_bt_dht
, HEURISTIC_ENABLE
);
823 // If this is ever streamed (transported over TCP) we need to add recursion checks.
824 dissector_add_for_decode_as_with_preference("udp.port", bt_dht_handle
);
833 * indent-tabs-mode: nil
836 * ex: set shiftwidth=2 tabstop=8 expandtab:
837 * :indentSize=2:tabSize=8:noTabs=true: