2 * Routines for the Twisted Banana serialization protocol dissection
3 * Copyright 2009, Gerald Combs <gerald@wireshark.org>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * Based on "Banana Protocol Specifications"
28 * http://twistedmatrix.com/projects/core/documentation/specifications/banana.html
35 #include <epan/packet.h>
36 #include <epan/prefs.h>
37 #include <epan/expert.h>
39 /* Initialize the protocol and registered fields */
40 static int proto_banana = -1;
41 static int hf_banana_list = -1;
42 static int hf_banana_int = -1;
43 static int hf_banana_string = -1;
44 static int hf_banana_neg_int = -1;
45 static int hf_banana_float = -1;
46 static int hf_banana_lg_int = -1;
47 static int hf_banana_lg_neg_int = -1;
48 static int hf_banana_pb = -1;
50 /* Initialize the subtree pointers */
51 static gint ett_banana = -1;
52 static gint ett_list = -1;
54 static dissector_handle_t banana_handle;
58 #define BE_STRING 0x82
59 #define BE_NEG_INT 0x83
61 #define BE_LG_INT 0x85
62 #define BE_LG_NEG_INT 0x86
65 #define is_element(b) (b >= BE_LIST && b <= BE_PB)
67 static const value_string type_vals[] = {
69 { BE_INT, "Integer" },
70 { BE_STRING, "String" },
71 { BE_NEG_INT, "Negative Integer" },
72 { BE_FLOAT, "Float" },
73 { BE_LG_INT, "Large Integer" },
74 { BE_LG_NEG_INT, "Large Negative Integer" },
75 { BE_PB, "pb Profile"},
79 static const value_string pb_vals[] = {
82 { 0x03, "dereference" },
83 { 0x04, "reference" },
84 { 0x05, "dictionary" },
89 { 0x0a, "persistent" },
91 { 0x0c, "unpersistable" },
100 { 0x15, "password" },
101 { 0x16, "challenge" },
102 { 0x17, "logged_in" },
103 { 0x18, "not_logged_in" },
104 { 0x19, "cachemessage" },
114 #define MAX_ELEMENT_VAL 2147483647 /* Max TE value */
115 #define MAX_ELEMENT_INT_LEN 4
116 #define MAX_ELEMENT_VAL_LEN 8
118 static range_t *global_banana_tcp_range = NULL;
119 static range_t *banana_tcp_range = NULL;
121 /* Dissect the packets */
124 dissect_banana_element(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset) {
126 proto_tree *list_tree;
130 int start_offset = offset;
134 /* Accumulate our value/length 'til we hit a valid type */
135 while (tvb_length_remaining(tvb, offset) > 0) {
136 byte = tvb_get_guint8(tvb, offset);
140 if (is_element(byte)) {
143 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "Unknown type %u", byte);
147 if (val_len > MAX_ELEMENT_VAL_LEN) {
148 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "Too many value/length bytes");
150 val += byte + (val << 7);
157 if (val > MAX_ELEMENT_VAL) {
158 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "List length %" G_GINT64_MODIFIER "d longer than we can handle", val);
160 ti = proto_tree_add_uint_format_value(tree, hf_banana_list, tvb, start_offset, offset - start_offset - 1, (guint32) val, "(%d items)", (gint) val);
161 list_tree = proto_item_add_subtree(ti, ett_list);
162 for (i = 0; i < val; i++) {
164 offset += dissect_banana_element(tvb, pinfo, list_tree, offset);
165 if (offset <= old_offset) {
166 return offset - start_offset;
171 if (val > MAX_ELEMENT_VAL) {
172 expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "Integer value %" G_GINT64_MODIFIER "d too large", val);
174 proto_tree_add_uint(tree, hf_banana_int, tvb, start_offset, offset - start_offset, (guint32) val);
177 if (val > MAX_ELEMENT_VAL) {
178 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "String length %" G_GINT64_MODIFIER "d longer than we can handle", val);
180 proto_tree_add_item(tree, hf_banana_string, tvb, offset, (guint32) val, ENC_ASCII|ENC_NA);
181 offset += (gint) val;
184 if (val > MAX_ELEMENT_VAL) {
185 expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "Integer value -%" G_GINT64_MODIFIER "d too large", val);
187 proto_tree_add_int(tree, hf_banana_neg_int, tvb, start_offset, offset - start_offset, (gint32) val * -1);
190 proto_tree_add_item(tree, hf_banana_float, tvb, offset, 8, ENC_BIG_ENDIAN);
194 proto_tree_add_item(tree, hf_banana_lg_int, tvb, start_offset, offset - start_offset, ENC_NA);
197 proto_tree_add_item(tree, hf_banana_lg_neg_int, tvb, start_offset, offset - start_offset, ENC_NA);
201 expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "More than 1 byte before pb");
204 * The spec says the pb dictionary value comes after the tag.
205 * In real-world captures it comes before.
207 proto_tree_add_item(tree, hf_banana_pb, tvb, offset - 2, 1, ENC_BIG_ENDIAN);
213 return offset - start_offset;
217 dissect_banana(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
219 gint offset = 0, old_offset;
221 proto_tree *banana_tree;
223 /* Check that there's enough data */
224 if (tvb_length(tvb) < 2)
227 /* Fill in our protocol and info columns */
228 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Banana");
230 while (tvb_length_remaining(tvb, offset) > 0 && offset < MAX_ELEMENT_VAL_LEN) {
231 byte = tvb_get_guint8(tvb, offset);
232 if (is_element(byte))
236 col_add_fstr(pinfo->cinfo, COL_INFO, "First element: %s",
237 val_to_str(byte, type_vals, "Unknown type: %u"));
239 /* Create display subtree for the protocol */
240 ti = proto_tree_add_item(tree, proto_banana, tvb, 0, -1, ENC_NA);
241 banana_tree = proto_item_add_subtree(ti, ett_banana);
245 while (offset > old_offset) {
247 offset += dissect_banana_element(tvb, pinfo, banana_tree, offset);
250 /* Return the amount of data this dissector was able to dissect */
251 return tvb_length(tvb);
255 range_delete_banana_tcp_callback(guint32 port) {
256 dissector_delete_uint("tcp.port", port, banana_handle);
260 range_add_banana_tcp_callback(guint32 port) {
261 dissector_add_uint("tcp.port", port, banana_handle);
266 range_foreach(banana_tcp_range, range_delete_banana_tcp_callback);
267 g_free(banana_tcp_range);
268 banana_tcp_range = range_copy(global_banana_tcp_range);
269 range_foreach(banana_tcp_range, range_add_banana_tcp_callback);
272 /* Register the protocol with Wireshark */
275 proto_register_banana(void)
277 static hf_register_info hf[] = {
279 { "List Length", "banana.list",
280 FT_UINT32, BASE_DEC, NULL, 0,
281 "Banana list", HFILL }
284 { "Integer", "banana.int",
285 FT_UINT32, BASE_DEC, NULL, 0,
286 "Banana integer", HFILL }
289 { "String", "banana.string",
290 FT_STRING, BASE_NONE, NULL, 0,
291 "Banana string", HFILL }
293 { &hf_banana_neg_int,
294 { "Negative Integer", "banana.neg_int",
295 FT_INT32, BASE_DEC, NULL, 0,
296 "Banana negative integer", HFILL }
299 { "Float", "banana.float",
300 FT_DOUBLE, BASE_NONE, NULL, 0,
301 "Banana float", HFILL }
304 { "Float", "banana.lg_int",
305 FT_BYTES, BASE_NONE, NULL, 0,
306 "Banana large integer", HFILL }
308 { &hf_banana_lg_neg_int,
309 { "Float", "banana.lg_neg_int",
310 FT_BYTES, BASE_NONE, NULL, 0,
311 "Banana large negative integer", HFILL }
314 { "pb Profile Value", "banana.pb",
315 FT_UINT8, BASE_HEX, VALS(pb_vals), 0,
316 "Banana Perspective Broker Profile Value", HFILL }
320 module_t *banana_module;
322 /* Setup protocol subtree array */
323 static gint *ett[] = {
328 /* Register the protocol name and description */
329 proto_banana = proto_register_protocol("Twisted Banana",
332 /* Required function calls to register the header fields and subtrees used */
333 proto_register_field_array(proto_banana, hf, array_length(hf));
334 proto_register_subtree_array(ett, array_length(ett));
336 /* Initialize dissector preferences */
337 banana_module = prefs_register_protocol(proto_banana, banana_prefs);
338 banana_tcp_range = range_empty();
339 prefs_register_range_preference(banana_module, "tcp.port", "TCP Ports", "Banana TCP Port range", &global_banana_tcp_range, 65535);
343 proto_reg_handoff_banana(void)
345 banana_handle = new_create_dissector_handle(dissect_banana, proto_banana);
349 * Editor modelines - http://www.wireshark.org/tools/modelines.html
354 * indent-tabs-mode: nil
357 * ex: set shiftwidth=4 tabstop=8 expandtab:
358 * :indentSize=4:tabSize=8:noTabs=true: