diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2014-02-03 00:36:50 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2014-02-04 14:24:13 +0000 |
commit | 53404776be11d66574c4849ddfb7dd10f63f8854 (patch) | |
tree | 5b022220ec7b4715fabacd4b1461ae4fa08acf78 /epan/dissectors/packet-bittorrent.c | |
parent | 19873060150648c91fcc19b099f332f7d9f119e3 (diff) |
Split out bencode dissector and use it in the bzr dissector.
Change-Id: I5b2ef173faf7670fad64298d67f2aaf055b8d6d8
Signed-Off-By: Jelmer Vernooij <jelmer@samba.org>
Reviewed-on: https://code.wireshark.org/review/82
Reviewed-by: Evan Huus <eapache@gmail.com>
Tested-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'epan/dissectors/packet-bittorrent.c')
-rw-r--r-- | epan/dissectors/packet-bittorrent.c | 300 |
1 files changed, 6 insertions, 294 deletions
diff --git a/epan/dissectors/packet-bittorrent.c b/epan/dissectors/packet-bittorrent.c index f1999d2c81..251bc2f6df 100644 --- a/epan/dissectors/packet-bittorrent.c +++ b/epan/dissectors/packet-bittorrent.c @@ -130,6 +130,7 @@ static const struct amp_message amp_messages[] = { }; static dissector_handle_t dissector_handle; +static dissector_handle_t bencode_handle; static int proto_bittorrent = -1; /* static gint hf_bittorrent_field_length = -1; */ @@ -150,12 +151,6 @@ static gint hf_bittorrent_piece_index = -1; static gint hf_bittorrent_piece_begin = -1; static gint hf_bittorrent_piece_length = -1; static gint hf_bittorrent_piece_data = -1; -static gint hf_bittorrent_bstr_length = -1; -static gint hf_bittorrent_bstr = -1; -static gint hf_bittorrent_bint = -1; -static gint hf_bittorrent_bdict = -1; -static gint hf_bittorrent_bdict_entry = -1; -static gint hf_bittorrent_blist = -1; static gint hf_azureus_jpc_addrlen = -1; static gint hf_azureus_jpc_addr = -1; static gint hf_azureus_jpc_port = -1; @@ -166,9 +161,6 @@ static gint hf_bittorrent_extended = -1; static gint ett_bittorrent = -1; static gint ett_bittorrent_msg = -1; static gint ett_peer_id = -1; -static gint ett_bittorrent_bdict = -1; -static gint ett_bittorrent_bdict_entry = -1; -static gint ett_bittorrent_blist = -1; static gboolean bittorrent_desegment = TRUE; static gboolean decode_client_information = FALSE; @@ -314,269 +306,6 @@ get_bittorrent_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb, int offset) } } -static int -dissect_bencoding_str(tvbuff_t *tvb, packet_info *pinfo _U_, - int offset, int length, proto_tree *tree, proto_item *ti, int treeadd) -{ - guint8 ch; - int stringlen = 0, nextstringlen; - int used; - int izero = 0; - - if (length<2) { - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Decode Aborted: Invalid String"); - } - return -1; - } - - used = 0; - - while (length>=1) { - ch = tvb_get_guint8(tvb, offset+used); - length--; - used++; - - if (ch==':' && used>1) { - if (stringlen>length || stringlen<0) { - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Decode Aborted: Invalid String Length"); - } - return -1; - } - if (tree) { - proto_tree_add_uint(tree, hf_bittorrent_bstr_length, tvb, offset, used, stringlen); - proto_tree_add_item(tree, hf_bittorrent_bstr, tvb, offset+used, stringlen, ENC_ASCII|ENC_NA); - - if (treeadd==1) { - proto_item_append_text(ti, " Key: %s", format_text((guchar *)tvb_memdup(wmem_packet_scope(), tvb, offset+used, stringlen), stringlen)); - } - if (treeadd==2) { - proto_item_append_text(ti, " Value: %s", format_text((guchar *)tvb_memdup(wmem_packet_scope(), tvb, offset+used, stringlen), stringlen)); - } - } - return used+stringlen; - } - - if (!izero && ch>='0' && ch<='9') { - if (ch=='0' && used==1) { - izero = 1; - } - - nextstringlen = (stringlen * 10) + (ch - '0'); - if (nextstringlen>=stringlen) { - stringlen = nextstringlen; - continue; - } - } - - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Decode Aborted: Invalid String"); - } - return -1; - } - - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Truncated Data"); - } - return -1; -} - -static int -dissect_bencoding_int(tvbuff_t *tvb, packet_info *pinfo _U_, - int offset, int length, proto_tree *tree, proto_item *ti, int treeadd) -{ - gint32 ival=0; - int neg = 0; - int izero = 0; - int used; - guint8 ch; - - if (length<3) { - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Decode Aborted: Invalid Integer"); - } - return -1; - } - - length--; - used = 1; - - while (length>=1) { - ch = tvb_get_guint8(tvb, offset+used); - length--; - used++; - - switch (ch) { - case 'e': - if (tree) { - if (neg) ival = -ival; - proto_tree_add_int(tree, hf_bittorrent_bint, tvb, offset, used, ival); - if (treeadd==2) { - proto_item_append_text(ti, " Value: %d", ival); - } - } - return used; - - case '-': - if (used==2) { - neg = 1; - break; - } - /* Fall through */ - - default: - if (!(ch=='0' && used==3 && neg)) { /* -0 is invalid */ - if (ch=='0' && used==2) { /* as is 0[0-9]+ */ - izero = 1; - break; - } - if (!izero && ch>='0' && ch<='9') { - ival = (ival * 10) + (ch - '0'); - break; - } - } - - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Decode Aborted: Invalid Integer"); - } - return -1; - } - } - - if (tree) { - proto_tree_add_text(tree, tvb, offset, length, "Truncated Data"); - } - return -1; -} - -static int -dissect_bencoding_rec(tvbuff_t *tvb, packet_info *pinfo _U_, - int offset, int length, proto_tree *tree, int level, proto_item *treei, int treeadd) -{ - guint8 op; - int oplen = 0, op1len, op2len; - int used; - - proto_item *ti = NULL, *td = NULL; - proto_tree *itree = NULL, *dtree = NULL; - - if (level>10) { - proto_tree_add_text(tree, tvb, offset, -1, "Decode Aborted: Nested Too Deep"); - return -1; - } - if (length<1) { - proto_tree_add_text(tree, tvb, offset, -1, "Truncated Data"); - return length; - } - - op = tvb_get_guint8(tvb, offset); - if (tree) { - oplen = dissect_bencoding_rec(tvb, pinfo, offset, length, NULL, level, NULL, 0); - if (oplen<0) oplen = length; - } - - switch (op) { - case 'd': - if (tree) { - td = proto_tree_add_item(tree, hf_bittorrent_bdict, tvb, offset, oplen, ENC_NA); - dtree = proto_item_add_subtree(td, ett_bittorrent_bdict); - } - - used = 1; - length--; - - while (length>=1) { - op = tvb_get_guint8(tvb, offset+used); - - if (op=='e') { - return used+1; - } - - op1len = dissect_bencoding_str(tvb, pinfo, offset+used, length, NULL, NULL, 0); - if (op1len<0) { - if (dtree) { - proto_tree_add_text(dtree, tvb, offset+used, -1, "Decode Aborted: Invalid Dictionary Key"); - } - return op1len; - } - - op2len = -1; - if (length-op1len>2) - op2len = dissect_bencoding_rec(tvb, pinfo, offset+used+op1len, length-op1len, NULL, level+1, NULL, 0); - if (op2len<0) { - if (dtree) { - proto_tree_add_text(dtree, tvb, offset+used+op1len, -1, "Decode Aborted: Invalid Dictionary Value"); - } - return op2len; - } - - if (dtree) { - ti = proto_tree_add_item(dtree, hf_bittorrent_bdict_entry, tvb, offset+used, op1len+op2len, ENC_NA); - itree = proto_item_add_subtree(ti, ett_bittorrent_bdict_entry); - - dissect_bencoding_str(tvb, pinfo, offset+used, length, itree, ti, 1); - dissect_bencoding_rec(tvb, pinfo, offset+used+op1len, length-op1len, itree, level+1, ti, 2); - } - - used += op1len+op2len; - length -= op1len+op2len; - } - if (dtree) { - proto_tree_add_text(dtree, tvb, offset+used, -1, "Truncated Data"); - } - return -1; - - case 'l': - if (tree) { - ti = proto_tree_add_item(tree, hf_bittorrent_blist, tvb, offset, oplen, ENC_NA); - itree = proto_item_add_subtree(ti, ett_bittorrent_blist); - } - - used = 1; - length--; - - while (length>=1) { - op = tvb_get_guint8(tvb, offset+used); - - if (op=='e') { - return used+1; - } - - oplen = dissect_bencoding_rec(tvb, pinfo, offset+used, length, itree, level+1, ti, 0); - if (oplen<1) return oplen; - - used += oplen; - length -= oplen; - } - if (itree) { - proto_tree_add_text(itree, tvb, offset+used, -1, "Truncated Data"); - } - return -1; - - case 'i': - return dissect_bencoding_int(tvb, pinfo, offset, length, tree, treei, treeadd); - - default: - if (op>='1' && op<='9') { - return dissect_bencoding_str(tvb, pinfo, offset, length, tree, treei, treeadd); - } - - if (tree) { - proto_tree_add_text(tree, tvb, offset, -1, "Decode Aborted: Invalid Bencoding"); - } - } - - return -1; -} - -static void -dissect_bencoding(tvbuff_t *tvb, packet_info *pinfo _U_, - int offset, int length, proto_tree *tree) -{ - dissect_bencoding_rec(tvb, pinfo, offset, length, tree, 0, NULL, 0); -} - static void dissect_bittorrent_message (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) { @@ -592,6 +321,7 @@ dissect_bittorrent_message (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) proto_item *ti; guint32 piece_index, piece_begin, piece_length; guint32 stringlen; + tvbuff_t *subtvb; if (tvb_bytes_exist(tvb, offset + BITTORRENT_HEADER_LENGTH, 1)) { /* Check for data from the middle of a message. */ @@ -742,7 +472,8 @@ dissect_bittorrent_message (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) case AZUREUS_MESSAGE_HANDSHAKE: case AZUREUS_MESSAGE_PEER_EXCHANGE: - dissect_bencoding(tvb, pinfo, offset, length, mtree); + subtvb = tvb_new_subset(tvb, offset, length, length); + call_dissector(bencode_handle, subtvb, pinfo, mtree); break; case AZUREUS_MESSAGE_JPC_HELLO: @@ -909,24 +640,6 @@ proto_register_bittorrent(void) { &hf_bittorrent_piece_length, { "Piece Length", "bittorrent.piece.length", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL } }, - { &hf_bittorrent_bstr_length, - { "String Length", "bittorrent.bstr.length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL } - }, - { &hf_bittorrent_bstr, - { "String", "bittorrent.bstr", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } - }, - { &hf_bittorrent_bint, - { "Integer", "bittorrent.bint", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } - }, - { &hf_bittorrent_bdict, - { "Dictionary", "bittorrent.bdict", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL } - }, - { &hf_bittorrent_bdict_entry, - { "Entry", "bittorrent.bdict.entry", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL } - }, - { &hf_bittorrent_blist, - { "List", "bittorrent.blist", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL } - }, { &hf_azureus_jpc_addrlen, { "Cache Address Length", "bittorrent.jpc.addr.length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL } }, @@ -951,9 +664,6 @@ proto_register_bittorrent(void) &ett_bittorrent, &ett_bittorrent_msg, &ett_peer_id, - &ett_bittorrent_bdict, - &ett_bittorrent_bdict_entry, - &ett_bittorrent_blist }; module_t *bittorrent_module; @@ -980,6 +690,8 @@ proto_register_bittorrent(void) void proto_reg_handoff_bittorrent(void) { + bencode_handle = find_dissector("bencode"); + dissector_handle = find_dissector("bittorrent.tcp"); #if 0 dissector_add_uint("tcp.port", 6881, dissector_handle); |