Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-rmt-alc.c
1 /* packet-rmt-alc.c
2  * Reliable Multicast Transport (RMT)
3  * ALC Protocol Instantiation dissector
4  * Copyright 2005, Stefano Pettini <spettini@users.sourceforge.net>
5  *
6  * Asynchronous Layered Coding (ALC):
7  * ----------------------------------
8  *
9  * A massively scalable reliable content delivery protocol.
10  * Asynchronous Layered Coding combines the Layered Coding Transport
11  * (LCT) building block, a multiple rate congestion control building
12  * block and the Forward Error Correction (FEC) building block to
13  * provide congestion controlled reliable asynchronous delivery of
14  * content to an unlimited number of concurrent receivers from a single
15  * sender.
16  *
17  * References:
18  *     RFC 3450, Asynchronous Layered Coding protocol instantiation
19  *
20  * $Id$
21  *
22  * Wireshark - Network traffic analyzer
23  * By Gerald Combs <gerald@wireshark.org>
24  * Copyright 1998 Gerald Combs
25  *
26  * This program is free software; you can redistribute it and/or
27  * modify it under the terms of the GNU General Public License
28  * as published by the Free Software Foundation; either version 2
29  * of the License, or (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39  */
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include <glib.h>
49
50 #include <epan/packet.h>
51 #include <epan/prefs.h>
52
53 #include "packet-rmt-alc.h"
54
55 /* Initialize the protocol and registered fields */
56 /* ============================================= */
57
58 static int proto = -1;
59
60 static struct _alc_hf hf;
61 static struct _alc_ett ett;
62
63 static struct _alc_prefs preferences;
64 static dissector_handle_t xml_handle;
65
66
67 /* Preferences */
68 /* =========== */
69
70 /* Set/Reset preferences to default values */
71 static void alc_prefs_set_default(struct _alc_prefs *alc_prefs)
72 {
73         alc_prefs->use_default_udp_port = FALSE;
74         alc_prefs->default_udp_port = 4001;
75
76         lct_prefs_set_default(&alc_prefs->lct);
77         fec_prefs_set_default(&alc_prefs->fec);
78 }
79
80 /* Register preferences */
81 static void alc_prefs_register(struct _alc_prefs *alc_prefs, module_t *module)
82 {
83         prefs_register_bool_preference(module,
84                 "default.udp_port.enabled",
85                 "Use default UDP port",
86                 "Whether that payload of UDP packets with a specific destination port should be automatically dissected as ALC packets",
87                  &alc_prefs->use_default_udp_port);
88
89         prefs_register_uint_preference(module,
90                 "default.udp_port",
91                 "Default UDP destination port",
92                 "Specifies the UDP destination port for automatic dissection of ALC packets",
93                  10, &alc_prefs->default_udp_port);
94
95         lct_prefs_register(&alc_prefs->lct, module);
96         fec_prefs_register(&alc_prefs->fec, module);
97 }
98
99 /* Save preferences to alc_prefs_old */
100 static void alc_prefs_save(struct _alc_prefs *p, struct _alc_prefs *p_old)
101 {
102         *p_old = *p;
103 }
104
105 /* Code to actually dissect the packets */
106 /* ==================================== */
107
108 static void dissect_alc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
109 {
110         /* Logical packet representation */
111         struct _alc alc;
112
113         /* Offset for subpacket dissection */
114         guint offset;
115
116         /* Set up structures needed to add the protocol subtree and manage it */
117         proto_item *ti;
118         proto_tree *alc_tree;
119
120         /* Flute or not */
121         tvbuff_t *new_tvb;
122         gboolean is_flute = FALSE;
123
124         /* Structures and variables initialization */
125         offset = 0;
126         memset(&alc, 0, sizeof(struct _alc));
127
128         /* Update packet info */
129         pinfo->current_proto = "ALC";
130
131         /* Make entries in Protocol column and Info column on summary display */
132         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ALC");
133         col_clear(pinfo->cinfo, COL_INFO);
134
135         /* ALC header dissection */
136         /* --------------------- */
137
138         alc.version = hi_nibble(tvb_get_guint8(tvb, offset));
139
140         if (tree)
141         {
142                 /* Create subtree for the ALC protocol */
143                 ti = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
144                 alc_tree = proto_item_add_subtree(ti, ett.main);
145
146                 /* Fill the ALC subtree */
147                 proto_tree_add_uint(alc_tree, hf.version, tvb, offset, 1, alc.version);
148
149         } else
150                 alc_tree = NULL;
151
152         /* This dissector supports only ALCv1 packets.
153          * If alc.version > 1 print only version field and quit.
154          */
155         if (alc.version == 1) {
156
157                 struct _lct_ptr l;
158                 struct _fec_ptr f;
159
160                 l.lct = &alc.lct;
161                 l.hf = &hf.lct;
162                 l.ett = &ett.lct;
163                 l.prefs = &preferences.lct;
164
165                 f.fec = &alc.fec;
166                 f.hf = &hf.fec;
167                 f.ett = &ett.fec;
168                 f.prefs = &preferences.fec;
169
170                 /* LCT header dissection */
171                 /* --------------------- */
172
173                 is_flute = lct_dissector(l, f, tvb, alc_tree, &offset);
174
175                 /* FEC header dissection */
176                 /* --------------------- */
177
178                 /* Only if it's present and if LCT dissector has determined FEC Encoding ID
179                  * FEC dissector should be called with fec->encoding_id* and fec->instance_id* filled
180                  */
181                 if (alc.fec.encoding_id_present && tvb_length(tvb) > offset)
182                         fec_dissector(f, tvb, alc_tree, &offset);
183
184                 /* Add the Payload item */
185                 if (tvb_length(tvb) > offset){
186                         if(is_flute){
187                                 new_tvb = tvb_new_subset_remaining(tvb,offset);
188                                 call_dissector(xml_handle, new_tvb, pinfo, alc_tree);
189                         }else{
190                                 proto_tree_add_none_format(alc_tree, hf.payload, tvb, offset, -1, "Payload (%u bytes)", tvb_length(tvb) - offset);
191                         }
192                 }
193
194                 /* Complete entry in Info column on summary display */
195                 /* ------------------------------------------------ */
196
197                 if (check_col(pinfo->cinfo, COL_INFO))
198                 {
199                         lct_info_column(&alc.lct, pinfo);
200                         fec_info_column(&alc.fec, pinfo);
201                 }
202
203                 /* Free g_allocated memory */
204                 lct_dissector_free(&alc.lct);
205                 fec_dissector_free(&alc.fec);
206
207         } else {
208
209                 if (tree)
210                         proto_tree_add_text(alc_tree, tvb, 0, -1, "Sorry, this dissector supports ALC version 1 only");
211
212                 /* Complete entry in Info column on summary display */
213                 if (check_col(pinfo->cinfo, COL_INFO))
214                         col_add_fstr(pinfo->cinfo, COL_INFO, "Version: %u (not supported)", alc.version);
215         }
216 }
217
218 void proto_reg_handoff_alc(void)
219 {
220         static dissector_handle_t handle;
221         static gboolean preferences_initialized = FALSE;
222         static struct _alc_prefs preferences_old;
223
224         if (!preferences_initialized)
225         {
226                 preferences_initialized = TRUE;
227                 handle = create_dissector_handle(dissect_alc, proto);
228                 dissector_add_handle("udp.port", handle);
229                 xml_handle = find_dissector("xml");
230
231         } else {
232
233                 if (preferences_old.use_default_udp_port)
234                         dissector_delete_uint("udp.port", preferences_old.default_udp_port, handle);
235         }
236
237         if (preferences.use_default_udp_port)
238                 dissector_add_uint("udp.port", preferences.default_udp_port, handle);
239
240         alc_prefs_save(&preferences, &preferences_old);
241
242 }
243
244 void proto_register_alc(void)
245 {
246         /* Setup ALC header fields */
247         static hf_register_info hf_ptr[] = {
248
249                 { &hf.version,
250                         { "Version", "alc.version", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
251
252                 LCT_FIELD_ARRAY(hf.lct, "alc"),
253                 FEC_FIELD_ARRAY(hf.fec, "alc"),
254
255                 { &hf.payload,
256                         { "Payload", "alc.payload", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }}
257         };
258
259         /* Setup protocol subtree array */
260         static gint *ett_ptr[] = {
261                 &ett.main,
262
263                 LCT_SUBTREE_ARRAY(ett.lct),
264                 FEC_SUBTREE_ARRAY(ett.fec)
265         };
266
267         module_t *module;
268
269         /* Clear hf and ett fields */
270         memset(&hf, 0xff, sizeof(struct _alc_hf));
271         memset(&ett, 0xff, sizeof(struct _alc_ett));
272
273         /* Register the protocol name and description */
274         proto = proto_register_protocol("Asynchronous Layered Coding", "ALC", "alc");
275
276         /* Register the header fields and subtrees used */
277         proto_register_field_array(proto, hf_ptr, array_length(hf_ptr));
278         proto_register_subtree_array(ett_ptr, array_length(ett_ptr));
279
280         /* Reset preferences */
281         alc_prefs_set_default(&preferences);
282
283         /* Register preferences */
284         module = prefs_register_protocol(proto, proto_reg_handoff_alc);
285         alc_prefs_register(&preferences, module);
286
287 }