2 * Routines for the Twisted Banana serialization protocol dissection
3 * Copyright 2009, Gerald Combs <gerald@wireshark.org>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
13 * Based on "Banana Protocol Specifications"
14 * https://twisted.org/documents/16.1.1/core/specifications/banana.html
19 #include <epan/packet.h>
20 #include <epan/expert.h>
22 void proto_register_banana(void);
23 void proto_reg_handoff_banana(void);
25 /* Initialize the protocol and registered fields */
26 static int proto_banana
;
27 static int hf_banana_list
;
28 static int hf_banana_int
;
29 static int hf_banana_string
;
30 static int hf_banana_neg_int
;
31 static int hf_banana_float
;
32 static int hf_banana_lg_int
;
33 static int hf_banana_lg_neg_int
;
34 static int hf_banana_pb
;
36 /* Initialize the subtree pointers */
37 static int ett_banana
;
40 static expert_field ei_banana_unknown_type
;
41 static expert_field ei_banana_too_many_value_bytes
;
42 static expert_field ei_banana_length_too_long
;
43 static expert_field ei_banana_value_too_large
;
44 static expert_field ei_banana_pb_error
;
46 static dissector_handle_t banana_handle
;
50 #define BE_STRING 0x82
51 #define BE_NEG_INT 0x83
53 #define BE_LG_INT 0x85
54 #define BE_LG_NEG_INT 0x86
57 #define is_element(b) (b >= BE_LIST && b <= BE_PB)
59 static const value_string type_vals
[] = {
61 { BE_INT
, "Integer" },
62 { BE_STRING
, "String" },
63 { BE_NEG_INT
, "Negative Integer" },
64 { BE_FLOAT
, "Float" },
65 { BE_LG_INT
, "Large Integer" },
66 { BE_LG_NEG_INT
, "Large Negative Integer" },
67 { BE_PB
, "pb Profile"},
71 static const value_string pb_vals
[] = {
74 { 0x03, "dereference" },
75 { 0x04, "reference" },
76 { 0x05, "dictionary" },
81 { 0x0a, "persistent" },
83 { 0x0c, "unpersistable" },
93 { 0x16, "challenge" },
94 { 0x17, "logged_in" },
95 { 0x18, "not_logged_in" },
96 { 0x19, "cachemessage" },
106 #define MAX_ELEMENT_VAL 2147483647 /* Max TE value */
107 #define MAX_ELEMENT_INT_LEN 4
108 #define MAX_ELEMENT_VAL_LEN 8
110 /* Dissect the packets */
113 // NOLINTNEXTLINE(misc-no-recursion)
114 dissect_banana_element(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, int offset
) {
116 proto_tree
*list_tree
;
120 int start_offset
= offset
;
124 /* Accumulate our value/length 'til we hit a valid type */
125 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
126 byte
= tvb_get_uint8(tvb
, offset
);
130 if (is_element(byte
)) {
133 expert_add_info_format(pinfo
, NULL
, &ei_banana_unknown_type
, "Unknown type %u", byte
);
137 if (val_len
> MAX_ELEMENT_VAL_LEN
) {
138 expert_add_info(pinfo
, NULL
, &ei_banana_too_many_value_bytes
);
140 val
+= byte
+ (val
<< 7);
147 if (val
> MAX_ELEMENT_VAL
) {
148 expert_add_info_format(pinfo
, NULL
, &ei_banana_length_too_long
, "List length %" PRId64
" longer than we can handle", val
);
150 ti
= proto_tree_add_uint_format_value(tree
, hf_banana_list
, tvb
, start_offset
, offset
- start_offset
- 1, (uint32_t) val
, "(%d items)", (int) val
);
151 list_tree
= proto_item_add_subtree(ti
, ett_list
);
152 for (i
= 0; i
< val
; i
++) {
154 increment_dissection_depth(pinfo
);
155 offset
+= dissect_banana_element(tvb
, pinfo
, list_tree
, offset
);
156 decrement_dissection_depth(pinfo
);
157 if (offset
<= old_offset
) {
158 return offset
- start_offset
;
163 if (val
> MAX_ELEMENT_VAL
) {
164 expert_add_info_format(pinfo
, NULL
, &ei_banana_value_too_large
, "Integer value %" PRId64
" too large", val
);
166 proto_tree_add_uint(tree
, hf_banana_int
, tvb
, start_offset
, offset
- start_offset
, (uint32_t) val
);
169 if (val
> MAX_ELEMENT_VAL
) {
170 expert_add_info_format(pinfo
, NULL
, &ei_banana_length_too_long
, "String length %" PRId64
" longer than we can handle", val
);
172 proto_tree_add_item(tree
, hf_banana_string
, tvb
, offset
, (uint32_t) val
, ENC_ASCII
);
176 if (val
> MAX_ELEMENT_VAL
) {
177 expert_add_info_format(pinfo
, NULL
, &ei_banana_value_too_large
, "Integer value -%" PRId64
" too large", val
);
179 proto_tree_add_int(tree
, hf_banana_neg_int
, tvb
, start_offset
, offset
- start_offset
, (int32_t) val
* -1);
182 proto_tree_add_item(tree
, hf_banana_float
, tvb
, offset
, 8, ENC_BIG_ENDIAN
);
186 proto_tree_add_item(tree
, hf_banana_lg_int
, tvb
, start_offset
, offset
- start_offset
, ENC_NA
);
189 proto_tree_add_item(tree
, hf_banana_lg_neg_int
, tvb
, start_offset
, offset
- start_offset
, ENC_NA
);
193 expert_add_info(pinfo
, NULL
, &ei_banana_pb_error
);
196 * The spec says the pb dictionary value comes after the tag.
197 * In real-world captures it comes before.
199 proto_tree_add_item(tree
, hf_banana_pb
, tvb
, offset
- 2, 1, ENC_BIG_ENDIAN
);
204 return offset
- start_offset
;
208 dissect_banana(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
) {
210 int offset
= 0, old_offset
;
212 proto_tree
*banana_tree
;
214 /* Check that there's enough data */
215 if (tvb_reported_length(tvb
) < 2)
218 /* Fill in our protocol and info columns */
219 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Banana");
221 while (tvb_reported_length_remaining(tvb
, offset
) > 0 && offset
< MAX_ELEMENT_VAL_LEN
) {
222 byte
= tvb_get_uint8(tvb
, offset
);
223 if (is_element(byte
))
227 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "First element: %s",
228 val_to_str(byte
, type_vals
, "Unknown type: %u"));
230 /* Create display subtree for the protocol */
231 ti
= proto_tree_add_item(tree
, proto_banana
, tvb
, 0, -1, ENC_NA
);
232 banana_tree
= proto_item_add_subtree(ti
, ett_banana
);
236 while (offset
> old_offset
) {
238 offset
+= dissect_banana_element(tvb
, pinfo
, banana_tree
, offset
);
241 /* Return the amount of data this dissector was able to dissect */
242 return tvb_reported_length(tvb
);
245 /* Register the protocol with Wireshark */
248 proto_register_banana(void)
250 static hf_register_info hf
[] = {
252 { "List Length", "banana.list",
253 FT_UINT32
, BASE_DEC
, NULL
, 0,
254 "Banana list", HFILL
}
257 { "Integer", "banana.int",
258 FT_UINT32
, BASE_DEC
, NULL
, 0,
259 "Banana integer", HFILL
}
262 { "String", "banana.string",
263 FT_STRING
, BASE_NONE
, NULL
, 0,
264 "Banana string", HFILL
}
266 { &hf_banana_neg_int
,
267 { "Negative Integer", "banana.neg_int",
268 FT_INT32
, BASE_DEC
, NULL
, 0,
269 "Banana negative integer", HFILL
}
272 { "Float", "banana.float",
273 FT_DOUBLE
, BASE_NONE
, NULL
, 0,
274 "Banana float", HFILL
}
277 { "Float", "banana.lg_int",
278 FT_BYTES
, BASE_NONE
, NULL
, 0,
279 "Banana large integer", HFILL
}
281 { &hf_banana_lg_neg_int
,
282 { "Float", "banana.lg_neg_int",
283 FT_BYTES
, BASE_NONE
, NULL
, 0,
284 "Banana large negative integer", HFILL
}
287 { "pb Profile Value", "banana.pb",
288 FT_UINT8
, BASE_HEX
, VALS(pb_vals
), 0,
289 "Banana Perspective Broker Profile Value", HFILL
}
293 expert_module_t
* expert_banana
;
295 /* Setup protocol subtree array */
296 static int *ett
[] = {
301 static ei_register_info ei
[] = {
302 { &ei_banana_unknown_type
, { "banana.unknown_type", PI_UNDECODED
, PI_ERROR
, "Unknown type", EXPFILL
}},
303 { &ei_banana_too_many_value_bytes
, { "banana.too_many_value_bytes", PI_UNDECODED
, PI_ERROR
, "Too many value/length bytes", EXPFILL
}},
304 { &ei_banana_length_too_long
, { "banana.length_too_long", PI_UNDECODED
, PI_ERROR
, "Length too long", EXPFILL
}},
305 { &ei_banana_value_too_large
, { "banana.value_too_large", PI_MALFORMED
, PI_ERROR
, "Value too large", EXPFILL
}},
306 { &ei_banana_pb_error
, { "banana.pb_error", PI_MALFORMED
, PI_ERROR
, "More than 1 byte before pb", EXPFILL
}},
309 /* Register the protocol name and description */
310 proto_banana
= proto_register_protocol("Twisted Banana", "Banana", "banana");
312 /* Required function calls to register the header fields and subtrees used */
313 proto_register_field_array(proto_banana
, hf
, array_length(hf
));
314 proto_register_subtree_array(ett
, array_length(ett
));
315 expert_banana
= expert_register_protocol(proto_banana
);
316 expert_register_field_array(expert_banana
, ei
, array_length(ei
));
318 banana_handle
= register_dissector("banana", dissect_banana
, proto_banana
);
322 proto_reg_handoff_banana(void)
324 dissector_add_uint_range_with_preference("tcp.port", "", banana_handle
);
328 * Editor modelines - https://www.wireshark.org/tools/modelines.html
333 * indent-tabs-mode: nil
336 * ex: set shiftwidth=4 tabstop=8 expandtab:
337 * :indentSize=4:tabSize=8:noTabs=true: