Move 3 ASN1 dissectors to 'clean' group; move 1 PIDL dissector to 'dirty' group.
[metze/wireshark/wip.git] / epan / dissectors / packet-banana.c
1 /* packet-bananna.c
2  * Routines for the Twisted Banana serialization protocol dissection
3  * Copyright 2009, Gerald Combs <gerald@wireshark.org>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 /*
27  * Based on "Banana Protocol Specifications"
28  * http://twistedmatrix.com/projects/core/documentation/specifications/banana.html
29  */
30
31 #include "config.h"
32
33 #include <glib.h>
34
35 #include <epan/packet.h>
36 #include <epan/prefs.h>
37 #include <epan/expert.h>
38
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;
49
50 /* Initialize the subtree pointers */
51 static gint ett_banana = -1;
52 static gint ett_list = -1;
53
54 static dissector_handle_t banana_handle;
55
56 #define BE_LIST         0x80
57 #define BE_INT          0x81
58 #define BE_STRING       0x82
59 #define BE_NEG_INT      0x83
60 #define BE_FLOAT        0x84
61 #define BE_LG_INT       0x85
62 #define BE_LG_NEG_INT   0x86
63 #define BE_PB           0x87
64
65 #define is_element(b) (b >= BE_LIST && b <= BE_PB)
66
67 static const value_string type_vals[] = {
68     { BE_LIST,          "List" },
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"},
76     { 0, NULL }
77 };
78
79 static const value_string pb_vals[] = {
80     { 0x01, "None" },
81     { 0x02, "class" },
82     { 0x03, "dereference" },
83     { 0x04, "reference" },
84     { 0x05, "dictionary" },
85     { 0x06, "function" },
86     { 0x07, "instance" },
87     { 0x08, "list" },
88     { 0x09, "module" },
89     { 0x0a, "persistent" },
90     { 0x0b, "tuple" },
91     { 0x0c, "unpersistable" },
92     { 0x0d, "copy" },
93     { 0x0e, "cache" },
94     { 0x0f, "cached" },
95     { 0x10, "remote" },
96     { 0x11, "local" },
97     { 0x12, "lcache" },
98     { 0x13, "version" },
99     { 0x14, "login" },
100     { 0x15, "password" },
101     { 0x16, "challenge" },
102     { 0x17, "logged_in" },
103     { 0x18, "not_logged_in" },
104     { 0x19, "cachemessage" },
105     { 0x1a, "message" },
106     { 0x1b, "answer" },
107     { 0x1c, "error" },
108     { 0x1d, "decref" },
109     { 0x1e, "decache" },
110     { 0x1f, "uncache" },
111     { 0,    NULL }
112 };
113
114 #define MAX_ELEMENT_VAL 2147483647 /* Max TE value */
115 #define MAX_ELEMENT_INT_LEN 4
116 #define MAX_ELEMENT_VAL_LEN 8
117
118 static range_t *global_banana_tcp_range = NULL;
119 static range_t *banana_tcp_range = NULL;
120
121 /* Dissect the packets */
122
123 static int
124 dissect_banana_element(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset) {
125     proto_item *ti;
126     proto_tree *list_tree;
127     guint8 byte = 0;
128     gint64 val = 0;
129     gint val_len = 0;
130     int start_offset = offset;
131     int old_offset;
132     int i;
133
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);
137         offset++;
138
139         if (byte & 0x80) {
140             if (is_element(byte)) {
141                 break;
142             } else {
143                 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "Unknown type %u", byte);
144             }
145         } else {
146             val_len++;
147             if (val_len > MAX_ELEMENT_VAL_LEN) {
148                 expert_add_info_format(pinfo, NULL, PI_UNDECODED, PI_ERROR, "Too many value/length bytes");
149             }
150             val += byte + (val << 7);
151         }
152     }
153
154     /* Type */
155     switch (byte) {
156         case BE_LIST:
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);
159             }
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++) {
163                 old_offset = offset;
164                 offset += dissect_banana_element(tvb, pinfo, list_tree, offset);
165                 if (offset <= old_offset) {
166                     return offset - start_offset;
167                 }
168             }
169             break;
170         case BE_INT:
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);
173             }
174             proto_tree_add_uint(tree, hf_banana_int, tvb, start_offset, offset - start_offset, (guint32) val);
175             break;
176         case BE_STRING:
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);
179             }
180             proto_tree_add_item(tree, hf_banana_string, tvb, offset, (guint32) val, ENC_ASCII|ENC_NA);
181             offset += (gint) val;
182             break;
183         case BE_NEG_INT:
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);
186             }
187             proto_tree_add_int(tree, hf_banana_neg_int, tvb, start_offset, offset - start_offset, (gint32) val * -1);
188             break;
189         case BE_FLOAT:
190             proto_tree_add_item(tree, hf_banana_float, tvb, offset, 8, ENC_BIG_ENDIAN);
191             offset += 8;
192             break;
193         case BE_LG_INT:
194             proto_tree_add_item(tree, hf_banana_lg_int, tvb, start_offset, offset - start_offset, ENC_NA);
195             break;
196         case BE_LG_NEG_INT:
197             proto_tree_add_item(tree, hf_banana_lg_neg_int, tvb, start_offset, offset - start_offset, ENC_NA);
198             break;
199         case BE_PB:
200             if (val_len > 1) {
201                 expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "More than 1 byte before pb");
202             }
203             /*
204              * The spec says the pb dictionary value comes after the tag.
205              * In real-world captures it comes before.
206              */
207             proto_tree_add_item(tree, hf_banana_pb, tvb, offset - 2, 1, ENC_BIG_ENDIAN);
208             break;
209         default:
210             return 0;
211             break;
212     }
213     return offset - start_offset;
214 }
215
216 static int
217 dissect_banana(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
218     guint8 byte = 0;
219     gint offset = 0, old_offset;
220     proto_item *ti;
221     proto_tree *banana_tree;
222
223     /* Check that there's enough data */
224     if (tvb_length(tvb) < 2)
225         return 0;
226
227     /* Fill in our protocol and info columns */
228     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Banana");
229
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))
233             break;
234         offset++;
235     }
236     col_add_fstr(pinfo->cinfo, COL_INFO, "First element: %s",
237         val_to_str(byte, type_vals, "Unknown type: %u"));
238
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);
242
243     offset = 0;
244     old_offset = -1;
245     while (offset > old_offset) {
246         old_offset = offset;
247         offset += dissect_banana_element(tvb, pinfo, banana_tree, offset);
248     }
249
250     /* Return the amount of data this dissector was able to dissect */
251     return tvb_length(tvb);
252 }
253
254 static void
255 range_delete_banana_tcp_callback(guint32 port) {
256       dissector_delete_uint("tcp.port", port, banana_handle);
257 }
258
259 static void
260 range_add_banana_tcp_callback(guint32 port) {
261     dissector_add_uint("tcp.port", port, banana_handle);
262 }
263
264 static void
265 banana_prefs(void) {
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);
270 }
271
272 /* Register the protocol with Wireshark */
273
274 void
275 proto_register_banana(void)
276 {
277     static hf_register_info hf[] = {
278         { &hf_banana_list,
279             { "List Length", "banana.list",
280                 FT_UINT32, BASE_DEC, NULL, 0,
281                 "Banana list", HFILL }
282         },
283         { &hf_banana_int,
284             { "Integer", "banana.int",
285                 FT_UINT32, BASE_DEC, NULL, 0,
286                 "Banana integer", HFILL }
287         },
288         { &hf_banana_string,
289             { "String", "banana.string",
290                 FT_STRING, BASE_NONE, NULL, 0,
291                 "Banana string", HFILL }
292         },
293         { &hf_banana_neg_int,
294             { "Negative Integer", "banana.neg_int",
295                 FT_INT32, BASE_DEC, NULL, 0,
296                 "Banana negative integer", HFILL }
297         },
298         { &hf_banana_float,
299             { "Float", "banana.float",
300                 FT_DOUBLE, BASE_NONE, NULL, 0,
301                 "Banana float", HFILL }
302         },
303         { &hf_banana_lg_int,
304             { "Float", "banana.lg_int",
305                 FT_BYTES, BASE_NONE, NULL, 0,
306                 "Banana large integer", HFILL }
307         },
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 }
312         },
313         { &hf_banana_pb,
314             { "pb Profile Value", "banana.pb",
315                 FT_UINT8, BASE_HEX, VALS(pb_vals), 0,
316                 "Banana Perspective Broker Profile Value", HFILL }
317         }
318     };
319
320     module_t *banana_module;
321
322     /* Setup protocol subtree array */
323     static gint *ett[] = {
324         &ett_banana,
325         &ett_list
326     };
327
328     /* Register the protocol name and description */
329     proto_banana = proto_register_protocol("Twisted Banana",
330         "Banana", "banana");
331
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));
335
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);
340 }
341
342 void
343 proto_reg_handoff_banana(void)
344 {
345     banana_handle = new_create_dissector_handle(dissect_banana, proto_banana);
346 }
347
348 /*
349  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
350  *
351  * Local variables:
352  * c-basic-offset: 4
353  * tab-width: 8
354  * indent-tabs-mode: nil
355  * End:
356  *
357  * ex: set shiftwidth=4 tabstop=8 expandtab:
358  * :indentSize=4:tabSize=8:noTabs=true:
359  */
360
361