2011-12-21 14:21:17 +00:00
|
|
|
/* packet-bzr.c
|
|
|
|
* Routines for Bazaar packet dissection
|
2014-02-03 00:36:50 +00:00
|
|
|
* Copyright 2011,2013 Jelmer Vernooij <jelmer@samba.org>
|
2011-12-21 14:21:17 +00:00
|
|
|
*
|
|
|
|
* http://doc.bazaar.canonical.com/developers/network-protocol.html
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* Copied from packet-pop.c
|
|
|
|
*
|
2018-02-12 11:23:27 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2011-12-21 14:21:17 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2011-12-21 14:21:17 +00:00
|
|
|
|
|
|
|
#include <epan/packet.h>
|
|
|
|
#include <epan/prefs.h>
|
|
|
|
|
2013-12-14 12:45:57 +00:00
|
|
|
void proto_register_bzr(void);
|
|
|
|
void proto_reg_handoff_bzr(void);
|
|
|
|
|
2011-12-21 14:21:17 +00:00
|
|
|
static int proto_bzr = -1;
|
|
|
|
|
|
|
|
static gint ett_bzr = -1;
|
|
|
|
static gint ett_prefixed_bencode = -1;
|
|
|
|
static gint ett_prefixed_bytes = -1;
|
|
|
|
|
|
|
|
static gint hf_bzr_prefixed_bencode = -1;
|
|
|
|
static gint hf_bzr_prefixed_bencode_len = -1;
|
|
|
|
static gint hf_bzr_bytes = -1;
|
|
|
|
static gint hf_bzr_bytes_data = -1;
|
|
|
|
static gint hf_bzr_bytes_length = -1;
|
|
|
|
static gint hf_bzr_result = -1;
|
|
|
|
static gint hf_bzr_packet_protocol_version = -1;
|
|
|
|
static gint hf_bzr_packet_kind = -1;
|
|
|
|
|
2014-02-03 00:36:50 +00:00
|
|
|
static dissector_handle_t bencode_handle;
|
2016-12-17 01:06:11 +00:00
|
|
|
static dissector_handle_t bzr_handle = NULL;
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2012-02-20 22:20:30 +00:00
|
|
|
#define REQUEST_VERSION_TWO "bzr request 2\n"
|
|
|
|
#define RESPONSE_VERSION_TWO "bzr response 2\n"
|
2011-12-21 14:21:17 +00:00
|
|
|
#define MESSAGE_VERSION_THREE "bzr message 3 (bzr 1.6)\n"
|
|
|
|
|
2012-01-14 17:18:26 +00:00
|
|
|
#define TCP_PORT_BZR 4155
|
2011-12-21 14:21:17 +00:00
|
|
|
|
|
|
|
static const value_string message_part_kind[] = {
|
2012-01-14 17:18:26 +00:00
|
|
|
{'s', "Structure"},
|
|
|
|
{'b', "Bytes"},
|
|
|
|
{'o', "Single byte"},
|
|
|
|
{'e', "End"},
|
|
|
|
{0, NULL}
|
2011-12-21 14:21:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const value_string message_results[] = {
|
2012-01-14 17:18:26 +00:00
|
|
|
{'S', "Success"},
|
|
|
|
{'E', "Error"},
|
|
|
|
{0, NULL}
|
2011-12-21 14:21:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* desegmentation of Bazaar over TCP */
|
|
|
|
static gboolean bzr_desegment = TRUE;
|
|
|
|
|
|
|
|
static guint get_bzr_prefixed_len(tvbuff_t *tvb, int offset)
|
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
guint header_len;
|
|
|
|
header_len = tvb_get_ntohl(tvb, offset);
|
|
|
|
return 4 + header_len;
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
get_bzr_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
|
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
int next_offset;
|
2017-04-29 14:30:23 +00:00
|
|
|
gint len = 0, current_len;
|
2012-01-14 17:18:26 +00:00
|
|
|
gint protocol_version_len;
|
|
|
|
guint8 cmd = 0;
|
|
|
|
|
|
|
|
/* Protocol version */
|
|
|
|
protocol_version_len = tvb_find_line_end(tvb, offset, -1, &next_offset,
|
|
|
|
TRUE);
|
|
|
|
if (protocol_version_len == -1) /* End of the packet not seen yet */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
len += protocol_version_len + 1;
|
|
|
|
|
|
|
|
/* Headers */
|
2017-04-29 14:30:23 +00:00
|
|
|
current_len = len;
|
2012-01-14 17:18:26 +00:00
|
|
|
len += get_bzr_prefixed_len(tvb, next_offset);
|
2017-04-29 14:30:23 +00:00
|
|
|
if (current_len > len) /* Make sure we're not going backwards */
|
|
|
|
return -1;
|
2012-01-14 17:18:26 +00:00
|
|
|
|
2012-02-20 22:22:48 +00:00
|
|
|
while (tvb_reported_length_remaining(tvb, offset + len) > 0) {
|
2012-01-14 17:18:26 +00:00
|
|
|
cmd = tvb_get_guint8(tvb, offset + len);
|
|
|
|
len += 1;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case 's':
|
|
|
|
case 'b':
|
2017-04-29 14:30:23 +00:00
|
|
|
current_len = len;
|
2012-01-14 17:18:26 +00:00
|
|
|
len += get_bzr_prefixed_len(tvb, offset + len);
|
2017-04-29 14:30:23 +00:00
|
|
|
if (current_len > len) /* Make sure we're not going backwards */
|
|
|
|
return -1;
|
2012-01-14 17:18:26 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2014-02-03 00:36:50 +00:00
|
|
|
dissect_prefixed_bencode(tvbuff_t *tvb, gint offset, packet_info *pinfo,
|
2012-01-14 17:18:26 +00:00
|
|
|
proto_tree *tree)
|
2011-12-21 14:21:17 +00:00
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
guint32 plen;
|
|
|
|
proto_tree *prefixed_bencode_tree;
|
|
|
|
proto_item *ti;
|
2014-06-01 18:38:24 +00:00
|
|
|
tvbuff_t *subtvb;
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2012-01-14 17:18:26 +00:00
|
|
|
plen = tvb_get_ntohl(tvb, offset);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2018-06-12 17:47:36 +00:00
|
|
|
ti = proto_tree_add_item(tree, hf_bzr_prefixed_bencode, tvb, offset, -1,
|
|
|
|
ENC_NA);
|
2012-01-14 17:18:26 +00:00
|
|
|
prefixed_bencode_tree = proto_item_add_subtree(ti, ett_prefixed_bencode);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
proto_tree_add_item(prefixed_bencode_tree, hf_bzr_prefixed_bencode_len,
|
2018-06-12 17:47:36 +00:00
|
|
|
tvb, offset, 4, ENC_BIG_ENDIAN);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2014-06-01 18:38:24 +00:00
|
|
|
subtvb = tvb_new_subset_length(tvb, offset+4, plen);
|
|
|
|
call_dissector(bencode_handle, subtvb, pinfo, prefixed_bencode_tree);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2018-06-12 17:47:36 +00:00
|
|
|
proto_item_set_len(ti, 4 + plen);
|
|
|
|
|
2012-01-14 17:18:26 +00:00
|
|
|
return 4 + plen;
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
dissect_prefixed_bytes(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
|
2012-01-14 17:18:26 +00:00
|
|
|
proto_tree *tree)
|
2011-12-21 14:21:17 +00:00
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
guint32 plen;
|
|
|
|
proto_tree *prefixed_bytes_tree;
|
|
|
|
proto_item *ti;
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2012-01-14 17:18:26 +00:00
|
|
|
plen = tvb_get_ntohl(tvb, offset);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2018-06-12 17:47:36 +00:00
|
|
|
ti = proto_tree_add_item(tree, hf_bzr_bytes, tvb, offset, -1, ENC_NA);
|
2012-01-14 17:18:26 +00:00
|
|
|
prefixed_bytes_tree = proto_item_add_subtree(ti, ett_prefixed_bytes);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2018-06-12 17:47:36 +00:00
|
|
|
proto_tree_add_item(prefixed_bytes_tree, hf_bzr_bytes_length,
|
|
|
|
tvb, offset, 4, ENC_BIG_ENDIAN);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2018-06-12 17:47:36 +00:00
|
|
|
proto_tree_add_item(prefixed_bytes_tree, hf_bzr_bytes_data,
|
|
|
|
tvb, offset+4, plen, ENC_NA);
|
|
|
|
|
|
|
|
proto_item_set_len(ti, 4 + plen);
|
2011-12-21 14:21:17 +00:00
|
|
|
|
2012-01-14 17:18:26 +00:00
|
|
|
return 4 + plen;
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
dissect_body(tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree)
|
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
guint8 cmd = 0;
|
|
|
|
|
|
|
|
while (tvb_reported_length_remaining(tvb, offset) > 0) {
|
|
|
|
cmd = tvb_get_guint8(tvb, offset);
|
2016-09-19 01:48:50 +00:00
|
|
|
proto_tree_add_item(tree, hf_bzr_packet_kind, tvb, offset, 1, ENC_ASCII|ENC_NA);
|
2012-01-14 17:18:26 +00:00
|
|
|
offset += 1;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case 's':
|
|
|
|
offset += dissect_prefixed_bencode(tvb, offset, pinfo, tree);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
offset += dissect_prefixed_bytes(tvb, offset, pinfo, tree);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
proto_tree_add_item(tree, hf_bzr_result, tvb, offset, 1,
|
2016-09-19 01:48:50 +00:00
|
|
|
ENC_ASCII|ENC_NA);
|
2012-01-14 17:18:26 +00:00
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dissect_bzr_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
|
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
proto_tree *bzr_tree;
|
|
|
|
proto_item *ti;
|
|
|
|
int offset = 0;
|
|
|
|
gint protocol_version_len;
|
|
|
|
|
|
|
|
ti = proto_tree_add_item(tree, proto_bzr, tvb, offset, -1, ENC_NA);
|
|
|
|
bzr_tree = proto_item_add_subtree(ti, ett_bzr);
|
|
|
|
|
|
|
|
protocol_version_len = tvb_find_line_end(tvb, offset, -1, &offset, TRUE);
|
|
|
|
if (protocol_version_len == -1) /* Invalid packet */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (bzr_tree)
|
|
|
|
{
|
|
|
|
proto_tree_add_item(bzr_tree, hf_bzr_packet_protocol_version, tvb, 0,
|
2012-05-05 00:03:59 +00:00
|
|
|
protocol_version_len+1, ENC_ASCII|ENC_NA);
|
2012-01-14 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += dissect_prefixed_bencode(tvb, offset, pinfo, bzr_tree);
|
2012-02-20 22:20:30 +00:00
|
|
|
/*offset +=*/ dissect_body(tvb, offset, pinfo, bzr_tree);
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 00:30:18 +00:00
|
|
|
static int
|
|
|
|
dissect_bzr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
|
2011-12-21 14:21:17 +00:00
|
|
|
{
|
2012-02-20 22:20:30 +00:00
|
|
|
gint offset = 0, pdu_len;
|
2012-01-14 17:18:26 +00:00
|
|
|
tvbuff_t *next_tvb;
|
|
|
|
|
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "BZR");
|
|
|
|
|
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Bazaar Smart Protocol");
|
|
|
|
|
|
|
|
while (tvb_reported_length_remaining(tvb, offset) > 0) {
|
|
|
|
pdu_len = get_bzr_pdu_len(pinfo, tvb, offset);
|
|
|
|
if (pdu_len == -1) {
|
|
|
|
if (pinfo->can_desegment && bzr_desegment) {
|
|
|
|
pinfo->desegment_offset = offset;
|
|
|
|
pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
|
2015-11-09 00:30:18 +00:00
|
|
|
return tvb_captured_length(tvb);
|
2012-01-14 17:18:26 +00:00
|
|
|
} else {
|
|
|
|
pdu_len = tvb_reported_length_remaining(tvb, offset);
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 14:35:50 +00:00
|
|
|
next_tvb = tvb_new_subset_length(tvb, offset, pdu_len);
|
2012-01-14 17:18:26 +00:00
|
|
|
dissect_bzr_pdu(next_tvb, pinfo, tree);
|
|
|
|
offset += pdu_len;
|
|
|
|
}
|
2015-11-09 00:30:18 +00:00
|
|
|
|
|
|
|
return tvb_captured_length(tvb);
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_bzr(void)
|
|
|
|
{
|
2012-01-14 17:18:26 +00:00
|
|
|
static hf_register_info hf[] = {
|
|
|
|
{ &hf_bzr_packet_kind,
|
2016-09-19 01:48:50 +00:00
|
|
|
{ "Packet kind", "bzr.kind", FT_CHAR, BASE_HEX,
|
2012-01-14 17:18:26 +00:00
|
|
|
VALS(message_part_kind), 0x0, NULL, HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_packet_protocol_version,
|
|
|
|
{ "Protocol version", "bzr.protocol_version", FT_STRING, BASE_NONE,
|
|
|
|
NULL, 0x0, NULL, HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_prefixed_bencode,
|
2018-06-12 17:47:36 +00:00
|
|
|
{ "Bencode packet", "bzr.bencode", FT_NONE, BASE_NONE, NULL, 0x0,
|
2012-01-14 17:18:26 +00:00
|
|
|
"Serialized structure of integers, dictionaries, strings and "
|
|
|
|
"lists.", HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_prefixed_bencode_len,
|
|
|
|
{ "Bencode packet length", "bzr.bencode.length", FT_UINT32,
|
|
|
|
BASE_HEX, NULL, 0x0, NULL, HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_bytes,
|
2018-06-12 17:47:36 +00:00
|
|
|
{ "Prefixed bytes", "bzr.bytes", FT_NONE, BASE_NONE, NULL, 0x0,
|
2012-01-14 17:18:26 +00:00
|
|
|
"Bytes field with prefixed 32-bit length", HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_bytes_data,
|
|
|
|
{ "Prefixed bytes data", "bzr.bytes.data", FT_BYTES, BASE_NONE,
|
|
|
|
NULL, 0x0, NULL, HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_bytes_length,
|
|
|
|
{ "Prefixed bytes length", "bzr.bytes.length", FT_UINT32, BASE_HEX,
|
|
|
|
NULL, 0x0, NULL, HFILL },
|
|
|
|
},
|
|
|
|
{ &hf_bzr_result,
|
2016-09-19 01:48:50 +00:00
|
|
|
{ "Result", "bzr.result", FT_CHAR, BASE_HEX,
|
2012-01-14 17:18:26 +00:00
|
|
|
VALS(message_results), 0x0,
|
|
|
|
"Command result (success or failure with error message)", HFILL
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_bzr,
|
|
|
|
&ett_prefixed_bencode,
|
|
|
|
&ett_prefixed_bytes,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_t *bzr_module;
|
|
|
|
proto_bzr = proto_register_protocol("Bazaar Smart Protocol", "Bazaar", "bzr");
|
2016-12-17 01:06:11 +00:00
|
|
|
bzr_handle = register_dissector("bzr", dissect_bzr, proto_bzr);
|
2012-01-14 17:18:26 +00:00
|
|
|
proto_register_field_array(proto_bzr, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
|
|
|
bzr_module = prefs_register_protocol(proto_bzr, NULL);
|
|
|
|
|
|
|
|
prefs_register_bool_preference(bzr_module, "desegment",
|
|
|
|
"Reassemble Bazaar messages spanning multiple TCP segments",
|
|
|
|
"Whether the Bazaar dissector should reassemble messages spanning multiple TCP segments."
|
|
|
|
" To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\""
|
|
|
|
" in the TCP protocol settings.",
|
|
|
|
&bzr_desegment);
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_reg_handoff_bzr(void)
|
|
|
|
{
|
2016-03-16 13:02:52 +00:00
|
|
|
bencode_handle = find_dissector_add_dependency("bencode", proto_bzr);
|
2014-02-03 00:36:50 +00:00
|
|
|
|
2016-10-07 20:25:01 +00:00
|
|
|
dissector_add_uint_with_preference("tcp.port", TCP_PORT_BZR, bzr_handle);
|
2011-12-21 14:21:17 +00:00
|
|
|
}
|
2014-09-29 18:42:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|