change a whole bunch of ethereal into wireshark
[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 <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include <glib.h>
50
51 #include <epan/packet.h>
52 #include <epan/prefs.h>
53
54 #include "packet-rmt-alc.h"
55
56 /* Initialize the protocol and registered fields */
57 /* ============================================= */
58
59 static int proto = -1;
60
61 static struct _alc_hf hf;
62 static struct _alc_ett ett;
63
64 static gboolean preferences_initialized = FALSE;
65 static struct _alc_prefs preferences;
66 static struct _alc_prefs preferences_old;
67
68 /* Preferences */
69 /* =========== */
70
71 /* Set/Reset preferences to default values */
72 static void alc_prefs_set_default(struct _alc_prefs *prefs)
73 {
74         prefs->use_default_udp_port = FALSE;
75         prefs->default_udp_port = 4001;
76         
77         lct_prefs_set_default(&prefs->lct);
78         fec_prefs_set_default(&prefs->fec);
79 }
80
81 /* Register preferences */
82 static void alc_prefs_register(struct _alc_prefs *prefs, module_t *module)
83 {
84         prefs_register_bool_preference(module,
85                 "default.udp_port.enabled",
86                 "Use default UDP port",
87                 "Whether that payload of UDP packets with a specific destination port should be automatically dissected as ALC packets",
88                  &prefs->use_default_udp_port);
89
90         prefs_register_uint_preference(module,
91                 "default.udp_port",
92                 "Default UDP destination port",
93                 "Specifies the UDP destination port for automatic dissection of ALC packets",
94                  10, &prefs->default_udp_port);
95                  
96         lct_prefs_register(&prefs->lct, module);
97         fec_prefs_register(&prefs->fec, module);
98 }
99
100 /* Save preferences to alc_prefs_old */
101 static void alc_prefs_save(struct _alc_prefs *p, struct _alc_prefs *p_old)
102 {
103         *p_old = *p;
104 }
105
106 /* Code to actually dissect the packets */
107 /* ==================================== */
108
109 static void dissect_alc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
110 {
111         /* Logical packet representation */
112         struct _alc alc;
113         
114         /* Offset for subpacket dissection */
115         guint offset;
116         
117         /* Set up structures needed to add the protocol subtree and manage it */
118         proto_item *ti;
119         proto_tree *alc_tree;
120         
121         /* Structures and variables initialization */
122         offset = 0;
123         memset(&alc, 0, sizeof(struct _alc));
124         
125         /* Update packet info */
126         pinfo->current_proto = "ALC";
127         
128         /* Make entries in Protocol column and Info column on summary display */
129         if (check_col(pinfo->cinfo, COL_PROTOCOL))
130                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ALC");
131         if (check_col(pinfo->cinfo, COL_INFO)) 
132                 col_clear(pinfo->cinfo, COL_INFO);
133         
134         /* ALC header dissection */
135         /* --------------------- */
136                 
137         alc.version = hi_nibble(tvb_get_guint8(tvb, offset));
138         
139         if (tree)
140         {
141                 /* Create subtree for the ALC protocol */
142                 ti = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
143                 alc_tree = proto_item_add_subtree(ti, ett.main);
144                 
145                 /* Fill the ALC subtree */
146                 proto_tree_add_uint(alc_tree, hf.version, tvb, offset, 1, alc.version);
147         
148         } else
149                 alc_tree = NULL;
150                 
151         /* This dissector supports only ALCv1 packets.
152          * If alc.version > 1 print only version field and quit.
153          */
154         if (alc.version == 1) {
155         
156                 struct _lct_ptr l;
157                 struct _fec_ptr f;
158                 
159                 l.lct = &alc.lct;
160                 l.hf = &hf.lct;
161                 l.ett = &ett.lct;
162                 l.prefs = &preferences.lct;
163                 
164                 f.fec = &alc.fec;
165                 f.hf = &hf.fec;
166                 f.ett = &ett.fec;
167                 f.prefs = &preferences.fec;
168                 
169                 /* LCT header dissection */
170                 /* --------------------- */
171                         
172                 lct_dissector(l, f, tvb, alc_tree, &offset);
173                 
174                 /* FEC header dissection */
175                 /* --------------------- */
176                 
177                 /* Only if it's present and if LCT dissector has determined FEC Encoding ID
178                  * FEC dissector should be called with fec->encoding_id* and fec->instance_id* filled
179                  */
180                 if (alc.fec.encoding_id_present && tvb_length(tvb) > offset)
181                         fec_dissector(f, tvb, alc_tree, &offset);
182                 
183                 /* Add the Payload item */
184                 if (tvb_length(tvb) > offset)
185                         proto_tree_add_none_format(alc_tree, hf.payload, tvb, offset, -1, "Payload (%u bytes)", tvb_length(tvb) - offset);
186                 
187                 /* Complete entry in Info column on summary display */
188                 /* ------------------------------------------------ */
189                 
190                 if (check_col(pinfo->cinfo, COL_INFO))
191                 {
192                         lct_info_column(&alc.lct, pinfo);
193                         fec_info_column(&alc.fec, pinfo);
194                 }
195                 
196                 /* Free g_allocated memory */
197                 lct_dissector_free(&alc.lct);
198                 fec_dissector_free(&alc.fec);
199         
200         } else {
201
202                 if (tree)
203                         proto_tree_add_text(alc_tree, tvb, 0, -1, "Sorry, this dissector supports ALC version 1 only");
204                 
205                 /* Complete entry in Info column on summary display */
206                 if (check_col(pinfo->cinfo, COL_INFO))
207                         col_add_fstr(pinfo->cinfo, COL_INFO, "Version: %u (not supported)", alc.version);
208         }
209 }
210
211 void proto_reg_handoff_alc(void)
212 {
213         static dissector_handle_t handle;
214
215         if (!preferences_initialized)
216         {
217                 preferences_initialized = TRUE;         
218                 handle = create_dissector_handle(dissect_alc, proto);
219                 dissector_add_handle("udp.port", handle);
220                 
221         } else {
222                 
223                 if (preferences_old.use_default_udp_port)
224                         dissector_delete("udp.port", preferences_old.default_udp_port, handle);
225         }
226
227         if (preferences.use_default_udp_port)
228                 dissector_add("udp.port", preferences.default_udp_port, handle);
229                 
230         alc_prefs_save(&preferences, &preferences_old);
231 }
232
233 void proto_register_alc(void)
234 {                 
235         /* Setup ALC header fields */
236         static hf_register_info hf_ptr[] = {
237                 
238                 { &hf.version,
239                         { "Version", "alc.version", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
240                 
241                 LCT_FIELD_ARRAY(hf.lct, "alc"),
242                 FEC_FIELD_ARRAY(hf.fec, "alc"),
243
244                 { &hf.payload,
245                         { "Payload", "alc.payload", FT_NONE, BASE_NONE, NULL, 0x0, "", HFILL }}
246         };
247
248         /* Setup protocol subtree array */
249         static gint *ett_ptr[] = {
250                 &ett.main,
251                 
252                 LCT_SUBTREE_ARRAY(ett.lct),
253                 FEC_SUBTREE_ARRAY(ett.fec)
254         };
255
256         module_t *module;
257         
258         /* Clear hf and ett fields */
259         memset(&hf, 0xff, sizeof(struct _alc_hf));
260         memset(&ett, 0xff, sizeof(struct _alc_ett));
261         
262         /* Register the protocol name and description */
263         proto = proto_register_protocol("Asynchronous Layered Coding", "ALC", "alc");
264
265         /* Register the header fields and subtrees used */
266         proto_register_field_array(proto, hf_ptr, array_length(hf_ptr));
267         proto_register_subtree_array(ett_ptr, array_length(ett_ptr));
268         
269         /* Reset preferences */
270         alc_prefs_set_default(&preferences);
271         alc_prefs_save(&preferences, &preferences_old);
272         
273         /* Register preferences */
274         module = prefs_register_protocol(proto, proto_reg_handoff_alc);
275         alc_prefs_register(&preferences, module);       
276 }