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 void proto_register_banana(void);
40 void proto_reg_handoff_banana(void);
42 /* Initialize the protocol and registered fields */
43 static int proto_banana
= -1;
44 static int hf_banana_list
= -1;
45 static int hf_banana_int
= -1;
46 static int hf_banana_string
= -1;
47 static int hf_banana_neg_int
= -1;
48 static int hf_banana_float
= -1;
49 static int hf_banana_lg_int
= -1;
50 static int hf_banana_lg_neg_int
= -1;
51 static int hf_banana_pb
= -1;
53 /* Initialize the subtree pointers */
54 static gint ett_banana
= -1;
55 static gint ett_list
= -1;
57 static expert_field ei_banana_unknown_type
= EI_INIT
;
58 static expert_field ei_banana_too_many_value_bytes
= EI_INIT
;
59 static expert_field ei_banana_length_too_long
= EI_INIT
;
60 static expert_field ei_banana_value_too_large
= EI_INIT
;
61 static expert_field ei_banana_pb_error
= EI_INIT
;
63 static dissector_handle_t banana_handle
;
67 #define BE_STRING 0x82
68 #define BE_NEG_INT 0x83
70 #define BE_LG_INT 0x85
71 #define BE_LG_NEG_INT 0x86
74 #define is_element(b) (b >= BE_LIST && b <= BE_PB)
76 static const value_string type_vals
[] = {
78 { BE_INT
, "Integer" },
79 { BE_STRING
, "String" },
80 { BE_NEG_INT
, "Negative Integer" },
81 { BE_FLOAT
, "Float" },
82 { BE_LG_INT
, "Large Integer" },
83 { BE_LG_NEG_INT
, "Large Negative Integer" },
84 { BE_PB
, "pb Profile"},
88 static const value_string pb_vals
[] = {
91 { 0x03, "dereference" },
92 { 0x04, "reference" },
93 { 0x05, "dictionary" },
98 { 0x0a, "persistent" },
100 { 0x0c, "unpersistable" },
109 { 0x15, "password" },
110 { 0x16, "challenge" },
111 { 0x17, "logged_in" },
112 { 0x18, "not_logged_in" },
113 { 0x19, "cachemessage" },
123 #define MAX_ELEMENT_VAL 2147483647 /* Max TE value */
124 #define MAX_ELEMENT_INT_LEN 4
125 #define MAX_ELEMENT_VAL_LEN 8
127 static range_t
*global_banana_tcp_range
= NULL
;
128 static range_t
*banana_tcp_range
= NULL
;
130 /* Dissect the packets */
133 dissect_banana_element(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, int offset
) {
135 proto_tree
*list_tree
;
139 int start_offset
= offset
;
143 /* Accumulate our value/length 'til we hit a valid type */
144 while (tvb_length_remaining(tvb
, offset
) > 0) {
145 byte
= tvb_get_guint8(tvb
, offset
);
149 if (is_element(byte
)) {
152 expert_add_info_format(pinfo
, NULL
, &ei_banana_unknown_type
, "Unknown type %u", byte
);
156 if (val_len
> MAX_ELEMENT_VAL_LEN
) {
157 expert_add_info(pinfo
, NULL
, &ei_banana_too_many_value_bytes
);
159 val
+= byte
+ (val
<< 7);
166 if (val
> MAX_ELEMENT_VAL
) {
167 expert_add_info_format(pinfo
, NULL
, &ei_banana_length_too_long
, "List length %" G_GINT64_MODIFIER
"d longer than we can handle", val
);
169 ti
= proto_tree_add_uint_format_value(tree
, hf_banana_list
, tvb
, start_offset
, offset
- start_offset
- 1, (guint32
) val
, "(%d items)", (gint
) val
);
170 list_tree
= proto_item_add_subtree(ti
, ett_list
);
171 for (i
= 0; i
< val
; i
++) {
173 offset
+= dissect_banana_element(tvb
, pinfo
, list_tree
, offset
);
174 if (offset
<= old_offset
) {
175 return offset
- start_offset
;
180 if (val
> MAX_ELEMENT_VAL
) {
181 expert_add_info_format(pinfo
, NULL
, &ei_banana_value_too_large
, "Integer value %" G_GINT64_MODIFIER
"d too large", val
);
183 proto_tree_add_uint(tree
, hf_banana_int
, tvb
, start_offset
, offset
- start_offset
, (guint32
) val
);
186 if (val
> MAX_ELEMENT_VAL
) {
187 expert_add_info_format(pinfo
, NULL
, &ei_banana_length_too_long
, "String length %" G_GINT64_MODIFIER
"d longer than we can handle", val
);
189 proto_tree_add_item(tree
, hf_banana_string
, tvb
, offset
, (guint32
) val
, ENC_ASCII
|ENC_NA
);
190 offset
+= (gint
) val
;
193 if (val
> MAX_ELEMENT_VAL
) {
194 expert_add_info_format(pinfo
, NULL
, &ei_banana_value_too_large
, "Integer value -%" G_GINT64_MODIFIER
"d too large", val
);
196 proto_tree_add_int(tree
, hf_banana_neg_int
, tvb
, start_offset
, offset
- start_offset
, (gint32
) val
* -1);
199 proto_tree_add_item(tree
, hf_banana_float
, tvb
, offset
, 8, ENC_BIG_ENDIAN
);
203 proto_tree_add_item(tree
, hf_banana_lg_int
, tvb
, start_offset
, offset
- start_offset
, ENC_NA
);
206 proto_tree_add_item(tree
, hf_banana_lg_neg_int
, tvb
, start_offset
, offset
- start_offset
, ENC_NA
);
210 expert_add_info(pinfo
, NULL
, &ei_banana_pb_error
);
213 * The spec says the pb dictionary value comes after the tag.
214 * In real-world captures it comes before.
216 proto_tree_add_item(tree
, hf_banana_pb
, tvb
, offset
- 2, 1, ENC_BIG_ENDIAN
);
222 return offset
- start_offset
;
226 dissect_banana(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
) {
228 gint offset
= 0, old_offset
;
230 proto_tree
*banana_tree
;
232 /* Check that there's enough data */
233 if (tvb_length(tvb
) < 2)
236 /* Fill in our protocol and info columns */
237 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Banana");
239 while (tvb_length_remaining(tvb
, offset
) > 0 && offset
< MAX_ELEMENT_VAL_LEN
) {
240 byte
= tvb_get_guint8(tvb
, offset
);
241 if (is_element(byte
))
245 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "First element: %s",
246 val_to_str(byte
, type_vals
, "Unknown type: %u"));
248 /* Create display subtree for the protocol */
249 ti
= proto_tree_add_item(tree
, proto_banana
, tvb
, 0, -1, ENC_NA
);
250 banana_tree
= proto_item_add_subtree(ti
, ett_banana
);
254 while (offset
> old_offset
) {
256 offset
+= dissect_banana_element(tvb
, pinfo
, banana_tree
, offset
);
259 /* Return the amount of data this dissector was able to dissect */
260 return tvb_length(tvb
);
265 dissector_delete_uint_range("tcp.port", banana_tcp_range
, banana_handle
);
266 g_free(banana_tcp_range
);
267 banana_tcp_range
= range_copy(global_banana_tcp_range
);
268 dissector_add_uint_range("tcp.port", banana_tcp_range
, banana_handle
);
271 /* Register the protocol with Wireshark */
274 proto_register_banana(void)
276 static hf_register_info hf
[] = {
278 { "List Length", "banana.list",
279 FT_UINT32
, BASE_DEC
, NULL
, 0,
280 "Banana list", HFILL
}
283 { "Integer", "banana.int",
284 FT_UINT32
, BASE_DEC
, NULL
, 0,
285 "Banana integer", HFILL
}
288 { "String", "banana.string",
289 FT_STRING
, BASE_NONE
, NULL
, 0,
290 "Banana string", HFILL
}
292 { &hf_banana_neg_int
,
293 { "Negative Integer", "banana.neg_int",
294 FT_INT32
, BASE_DEC
, NULL
, 0,
295 "Banana negative integer", HFILL
}
298 { "Float", "banana.float",
299 FT_DOUBLE
, BASE_NONE
, NULL
, 0,
300 "Banana float", HFILL
}
303 { "Float", "banana.lg_int",
304 FT_BYTES
, BASE_NONE
, NULL
, 0,
305 "Banana large integer", HFILL
}
307 { &hf_banana_lg_neg_int
,
308 { "Float", "banana.lg_neg_int",
309 FT_BYTES
, BASE_NONE
, NULL
, 0,
310 "Banana large negative integer", HFILL
}
313 { "pb Profile Value", "banana.pb",
314 FT_UINT8
, BASE_HEX
, VALS(pb_vals
), 0,
315 "Banana Perspective Broker Profile Value", HFILL
}
319 module_t
*banana_module
;
320 expert_module_t
* expert_banana
;
322 /* Setup protocol subtree array */
323 static gint
*ett
[] = {
328 static ei_register_info ei
[] = {
329 { &ei_banana_unknown_type
, { "banana.unknown_type", PI_UNDECODED
, PI_ERROR
, "Unknown type", EXPFILL
}},
330 { &ei_banana_too_many_value_bytes
, { "banana.too_many_value_bytes", PI_UNDECODED
, PI_ERROR
, "Too many value/length bytes", EXPFILL
}},
331 { &ei_banana_length_too_long
, { "banana.length_too_long", PI_UNDECODED
, PI_ERROR
, "Length too long", EXPFILL
}},
332 { &ei_banana_value_too_large
, { "banana.value_too_large", PI_MALFORMED
, PI_ERROR
, "Value too large", EXPFILL
}},
333 { &ei_banana_pb_error
, { "banana.pb_error", PI_MALFORMED
, PI_ERROR
, "More than 1 byte before pb", EXPFILL
}},
336 /* Register the protocol name and description */
337 proto_banana
= proto_register_protocol("Twisted Banana", "Banana", "banana");
339 /* Required function calls to register the header fields and subtrees used */
340 proto_register_field_array(proto_banana
, hf
, array_length(hf
));
341 proto_register_subtree_array(ett
, array_length(ett
));
342 expert_banana
= expert_register_protocol(proto_banana
);
343 expert_register_field_array(expert_banana
, ei
, array_length(ei
));
345 /* Initialize dissector preferences */
346 banana_module
= prefs_register_protocol(proto_banana
, banana_prefs
);
347 banana_tcp_range
= range_empty();
348 prefs_register_range_preference(banana_module
, "tcp.port", "TCP Ports", "Banana TCP Port range", &global_banana_tcp_range
, 65535);
352 proto_reg_handoff_banana(void)
354 banana_handle
= new_create_dissector_handle(dissect_banana
, proto_banana
);
358 * Editor modelines - http://www.wireshark.org/tools/modelines.html
363 * indent-tabs-mode: nil
366 * ex: set shiftwidth=4 tabstop=8 expandtab:
367 * :indentSize=4:tabSize=8:noTabs=true: