Fix up a bunch of arguments to "dissect_ber_identifier()" to match its
[obnox/wireshark/wip.git] / epan / dissectors / packet-rmt-norm.c
1 /* packet-rmt-norm.c
2  * Reliable Multicast Transport (RMT)
3  * NORM Protocol Instantiation dissector
4  * Copyright 2005, Stefano Pettini <spettini@users.sourceforge.net>
5  *
6  * Negative-acknowledgment (NACK)-Oriented Reliable Multicast (NORM):
7  * ------------------------------------------------------------------
8  *
9  * This protocol is designed to provide end-to-end reliable transport of
10  * bulk data objects or streams over generic IP multicast routing and
11  * forwarding services.  NORM uses a selective, negative acknowledgment
12  * mechanism for transport reliability and offers additional protocol
13  * mechanisms to allow for operation with minimal "a priori"
14  * coordination among senders and receivers.
15  *
16  * References:
17  *     RFC 3490, Negative-acknowledgment (NACK)-Oriented Reliable Multicast (NORM) Protocol
18  *
19  * $Id$
20  *
21  * Ethereal - Network traffic analyzer
22  * By Gerald Combs <gerald@ethereal.com>
23  * Copyright 1998 Gerald Combs
24  *
25  * This program is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU General Public License
27  * as published by the Free Software Foundation; either version 2
28  * of the License, or (at your option) any later version.
29  * 
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  * 
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <stdio.h>
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-norm.h"
54
55 /* String tables */
56 const value_string string_norm_type[] =
57 {
58         { 1, "NORM_INFO" },
59         { 2, "NORM_DATA" },
60         { 3, "NORM_CMD" },
61         { 4, "NORM_NACK" },
62         { 5, "NORM_ACK" },
63         { 6, "NORM_REPORT" },
64         { 0, NULL }
65 };
66
67 /* Initialize the protocol and registered fields */
68 /* ============================================= */
69
70 static int proto = -1;
71
72 static struct _norm_hf hf;
73 static struct _norm_ett ett;
74
75 static gboolean preferences_initialized = FALSE;
76 static struct _norm_prefs preferences;
77 static struct _norm_prefs preferences_old;
78
79 /* Preferences */
80 /* =========== */
81
82 /* Set/Reset preferences to default values */
83 static void norm_prefs_set_default(struct _norm_prefs *prefs)
84 {
85         fec_prefs_set_default(&prefs->fec);
86 }
87
88 /* Register preferences */
89 static void norm_prefs_register(struct _norm_prefs *prefs, module_t *module)
90 {
91         fec_prefs_register(&prefs->fec, module);
92 }
93
94 /* Save preferences to alc_prefs_old */
95 static void norm_prefs_save(struct _norm_prefs *p, struct _norm_prefs *p_old)
96 {
97         *p_old = *p;
98 }
99
100 /* Code to actually dissect the packets */
101 /* ==================================== */
102
103 static void dissect_norm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
104 {
105         /* Logical packet representation */
106         struct _norm norm;
107         
108         /* Offset for subpacket dissection */
109         guint offset;
110         
111         /* Set up structures needed to add the protocol subtree and manage it */
112         proto_item *ti;
113         proto_tree *norm_tree;
114         
115         /* Structures and variables initialization */
116         offset = 0;
117         memset(&norm, 0, sizeof(struct _norm));
118         
119         /* Update packet info */
120         pinfo->current_proto = "NORM";
121         
122         /* Make entries in Protocol column and Info column on summary display */
123         if (check_col(pinfo->cinfo, COL_PROTOCOL))
124                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "NORM");
125         if (check_col(pinfo->cinfo, COL_INFO)) 
126                 col_clear(pinfo->cinfo, COL_INFO);
127         
128         /* NORM header dissection, part 1 */
129         /* ------------------------------ */
130                 
131         norm.version = hi_nibble(tvb_get_guint8(tvb, offset));
132         
133         if (tree)
134         {
135                 /* Create subtree for the NORM protocol */
136                 ti = proto_tree_add_item(tree, proto, tvb, offset, -1, FALSE);
137                 norm_tree = proto_item_add_subtree(ti, ett.main);
138                 
139                 /* Fill the NORM subtree */
140                 proto_tree_add_uint(norm_tree, hf.version, tvb, offset, 1, norm.version);
141         
142         } else
143                 norm_tree = NULL;
144                 
145         /* This dissector supports only NORMv1 packets.
146          * If norm.version > 1 print only version field and quit.
147          */
148         if (norm.version == 1) {
149         
150                 /* NORM header dissection, part 2 */
151                 /* ------------------------------ */
152                 
153                 norm.type = lo_nibble(tvb_get_guint8(tvb, offset));
154                 norm.hlen = tvb_get_guint8(tvb, offset+1);
155                 norm.sequence = tvb_get_ntohs(tvb, offset+2);
156                 norm.source_id = tvb_get_ntohl(tvb, offset+4);
157                 
158                 if (tree)
159                 {
160                         proto_tree_add_uint(norm_tree, hf.type, tvb, offset, 1, norm.type);
161                         proto_tree_add_uint(norm_tree, hf.hlen, tvb, offset+1, 1, norm.hlen);
162                         proto_tree_add_uint(norm_tree, hf.sequence, tvb, offset+2, 2, norm.sequence);
163                         proto_tree_add_uint(norm_tree, hf.source_id, tvb, offset+4, 4, norm.source_id);
164                 }
165                 
166                 offset += 8;
167         
168                 /* Add the Payload item */
169                 if (tvb_length(tvb) > offset)
170                         proto_tree_add_none_format(norm_tree, hf.payload, tvb, offset, -1, "Payload (%u bytes)", tvb_length(tvb) - offset);
171                 
172                 /* Complete entry in Info column on summary display */
173                 /* ------------------------------------------------ */
174                 
175                 if (check_col(pinfo->cinfo, COL_INFO))
176                         switch (norm.type)
177                         {
178                         case 1:
179                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "INFO");
180                                 break;
181                         
182                         case 2:
183                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "DATA");
184                                 break;
185                         
186                         case 3:
187                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "CMD");
188                                 break;
189                         
190                         case 4:
191                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "NACK");
192                                 break;
193                         
194                         case 5:
195                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "ACK");
196                                 break;
197                         
198                         case 6:
199                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "REPORT");
200                                 break;
201                         
202                         default:
203                                 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "Unknown type");
204                                 break;                  
205                         }
206
207         } else {
208
209                 if (tree)
210                         proto_tree_add_text(norm_tree, tvb, 0, -1, "Sorry, this dissector supports NORM 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)", norm.version);
215         }
216 }
217
218 void proto_reg_handoff_norm(void)
219 {
220         static dissector_handle_t handle;
221
222         if (!preferences_initialized)
223         {
224                 preferences_initialized = TRUE;         
225                 handle = create_dissector_handle(dissect_norm, proto);
226                 dissector_add_handle("udp.port", handle);
227         }
228
229         norm_prefs_save(&preferences, &preferences_old);
230 }
231
232 void proto_register_norm(void)
233 {                 
234         /* Setup NORM header fields */
235         static hf_register_info hf_ptr[] = {
236                 
237                 { &hf.version,
238                         { "Version", "norm.version", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
239                 { &hf.type,
240                         { "Message Type", "norm.type", FT_UINT8, BASE_DEC, VALS(string_norm_type), 0x0, "", HFILL }},
241                 { &hf.hlen,
242                         { "Header length", "norm.hlen", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
243                 { &hf.sequence,
244                         { "Sequence", "norm.sequence", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
245                 { &hf.source_id,
246                         { "Source ID", "norm.source_id", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
247                 
248                 FEC_FIELD_ARRAY(hf.fec, "alc"),
249
250                 { &hf.payload,
251                         { "Payload", "norm.payload", FT_NONE, BASE_NONE, NULL, 0x0, "", HFILL }}
252         };
253
254         /* Setup protocol subtree array */
255         static gint *ett_ptr[] = {
256                 &ett.main,
257                 
258                 FEC_SUBTREE_ARRAY(ett.fec)
259         };
260
261         module_t *module;
262         
263         /* Clear hf and ett fields */
264         memset(&hf, 0xff, sizeof(struct _norm_hf));
265         memset(&ett, 0xff, sizeof(struct _norm_ett));
266         
267         /* Register the protocol name and description */
268         proto = proto_register_protocol("Negative-acknowledgment Oriented Reliable Multicast", "NORM", "norm");
269
270         /* Register the header fields and subtrees used */
271         proto_register_field_array(proto, hf_ptr, array_length(hf_ptr));
272         proto_register_subtree_array(ett_ptr, array_length(ett_ptr));
273         
274         /* Reset preferences */
275         norm_prefs_set_default(&preferences);
276         norm_prefs_save(&preferences, &preferences_old);
277         
278         /* Register preferences */
279         module = prefs_register_protocol(proto, proto_reg_handoff_norm);
280         norm_prefs_register(&preferences, module);      
281 }