Use "tvb_get_ntohieee_float()" to fetch floating-point numbers from the
[obnox/wireshark/wip.git] / packet-mrdisc.c
1 /* packet-mrdisc.c   2001 Ronnie Sahlberg <See AUTHORS for email>
2  * Routines for IGMP/MRDISC packet disassembly
3  *
4  * $Id: packet-mrdisc.c,v 1.7 2002/02/01 11:01:57 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24 /*
25
26
27                         MRDISC
28         code                    
29
30         0x24            x       
31         0x25            x
32         0x26            x
33
34         MRDISC : IGMP Multicast Router DISCovery
35         Defined in draft-ietf-idmr-igmp-mrdisc-06.txt
36         TTL==1 and IP.DST==224.0.0.2 for all packets.
37 */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <glib.h>
50
51 #include <epan/packet.h>
52 #include "packet-igmp.h"
53 #include "packet-mrdisc.h"
54
55
56 static int proto_mrdisc = -1;
57 static int hf_checksum = -1;
58 static int hf_checksum_bad = -1;
59 static int hf_type = -1;
60 static int hf_advint = -1;
61 static int hf_numopts = -1;
62 static int hf_options = -1;
63 static int hf_option = -1;
64 static int hf_option_len = -1;
65 static int hf_qi = -1;
66 static int hf_rv = -1;
67 static int hf_option_bytes = -1;
68
69 static int ett_mrdisc = -1;
70 static int ett_options = -1;
71
72 #define MRDISC_MRA      0x24
73 #define MRDISC_MRS      0x25
74 #define MRDISC_MRT      0x26
75 static const value_string mrdisc_types[] = {
76         {MRDISC_MRA,    "Multicast Router Advertisement"},
77         {MRDISC_MRS,    "Multicast Router Solicitation"},
78         {MRDISC_MRT,    "Multicast Router Termination"},
79         {0,                                     NULL}
80 };
81
82 #define MRDISC_QI       0x01
83 #define MRDISC_RV       0x02
84 static const value_string mrdisc_options[] = {
85         {MRDISC_QI,     "Query Interval"},
86         {MRDISC_RV,     "Robustness Variable"},
87         {0,                                     NULL}
88 };
89
90
91 static int
92 dissect_mrdisc_mra(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
93 {
94         guint16 num;
95
96         /* Advertising Interval */
97         proto_tree_add_item(parent_tree, hf_advint, tvb, offset, 1, FALSE);
98         offset += 1;
99
100         /* checksum */
101         igmp_checksum(parent_tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
102         offset += 2;
103
104         /* skip unused bytes */
105         offset += 2;
106
107         /* number of options */
108         num = tvb_get_ntohs(tvb, offset);
109         proto_tree_add_uint(parent_tree, hf_numopts, tvb,
110                 offset, 2, num);
111         offset += 2;
112
113         /* process any options */
114         while (num--) {
115                 proto_tree *tree;
116                 proto_item *item;
117                 guint8 type,len;
118                 int old_offset = offset;
119
120                 item = proto_tree_add_item(parent_tree, hf_options, 
121                         tvb, offset, -1, FALSE);
122                 tree = proto_item_add_subtree(item, ett_options);
123
124                 type = tvb_get_guint8(tvb, offset);
125                 proto_tree_add_uint(tree, hf_option, tvb, offset, 1, type);
126                 offset += 1;
127
128                 len = tvb_get_guint8(tvb, offset);
129                 proto_tree_add_uint(tree, hf_option_len, tvb, offset, 1, len);
130                 offset += 1;
131
132                 switch (type) {
133                 case MRDISC_QI:
134                         if (item) {
135                                 proto_item_set_text(item,"Option: %s == %d",
136                                         val_to_str(type, mrdisc_options, "unknown %x"),
137                                         tvb_get_ntohs(tvb, offset));
138                         }
139
140                         proto_tree_add_item(tree, hf_qi, tvb, offset, len,
141                                 FALSE);
142                         offset += len;
143                         break;
144                 case MRDISC_RV:
145                         if (item) {
146                                 proto_item_set_text(item,"Option: %s == %d",
147                                         val_to_str(type, mrdisc_options, "unknown %x"),
148                                         tvb_get_ntohs(tvb, offset));
149                         }
150
151                         proto_tree_add_item(tree, hf_rv, tvb, offset, len,
152                                 FALSE);
153                         offset += len;
154                         break;
155                 default:
156                         if (item) {
157                                 proto_item_set_text(item,"Option: unknown");
158                         }
159
160                         proto_tree_add_item(tree, hf_option_bytes,
161                                 tvb, offset, len, FALSE);
162                         offset += len;
163                 }
164                 if (item) {
165                         proto_item_set_len(item, offset-old_offset);
166                 }
167         }
168         
169         return offset;
170 }
171
172
173 static int
174 dissect_mrdisc_mrst(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
175 {
176         /* skip reserved byte */
177         offset += 1;
178
179         /* checksum */
180         igmp_checksum(parent_tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
181         offset += 2;
182
183         return offset;
184 }
185
186
187 /* This function is only called from the IGMP dissector */
188 int
189 dissect_mrdisc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
190 {
191         proto_tree *tree;
192         proto_item *item;
193         guint8 type;
194
195         if (!proto_is_protocol_enabled(proto_mrdisc)) {
196                 /* we are not enabled, skip entire packet to be nice
197                    to the igmp layer. (so clicking on IGMP will display the data)
198                  */
199                 return offset+tvb_length_remaining(tvb, offset);
200         }
201
202         item = proto_tree_add_item(parent_tree, proto_mrdisc, tvb, offset, 0, FALSE);
203         tree = proto_item_add_subtree(item, ett_mrdisc);
204
205
206         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
207                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MRDISC");
208         }
209         if (check_col(pinfo->cinfo, COL_INFO)) {
210                 col_clear(pinfo->cinfo, COL_INFO);
211         }
212
213
214         type = tvb_get_guint8(tvb, offset);
215         if (check_col(pinfo->cinfo, COL_INFO)) {
216                 col_add_fstr(pinfo->cinfo, COL_INFO,
217                         "%s",val_to_str(type, mrdisc_types, 
218                                 "Unknown Type:0x%02x"));
219         }
220
221         /* type of command */
222         proto_tree_add_uint(tree, hf_type, tvb, offset, 1, type);
223         offset += 1;
224
225         switch (type) {
226         case MRDISC_MRA:
227                 offset = dissect_mrdisc_mra(tvb, pinfo, tree, offset);
228                 break;
229         case MRDISC_MRS:
230         case MRDISC_MRT:
231                 /* MRS and MRT packets looks the same */
232                 offset = dissect_mrdisc_mrst(tvb, pinfo, tree, offset);
233                 break;
234         }
235         return offset;
236 }
237
238
239 void
240 proto_register_mrdisc(void)
241 {
242         static hf_register_info hf[] = {
243                 { &hf_type,
244                         { "Type", "mrdisc.type", FT_UINT8, BASE_HEX,
245                           VALS(mrdisc_types), 0, "MRDISC Packet Type", HFILL }},
246
247                 { &hf_checksum,
248                         { "Checksum", "mrdisc.checksum", FT_UINT16, BASE_HEX,
249                           NULL, 0, "MRDISC Checksum", HFILL }},
250
251                 { &hf_checksum_bad,
252                         { "Bad Checksum", "mrdisc.checksum_bad", FT_BOOLEAN, BASE_NONE,
253                           NULL, 0, "Bad MRDISC Checksum", HFILL }},
254
255                 { &hf_advint,
256                         { "Advertising Interval", "mrdisc.adv_int", FT_UINT8, BASE_DEC,
257                           NULL, 0, "MRDISC Advertising Interval in seconds", HFILL }},
258
259                 { &hf_numopts,
260                         { "Number Of Options", "mrdisc.num_opts", FT_UINT16, BASE_DEC,
261                           NULL, 0, "MRDISC Number Of Options", HFILL }},
262
263                 { &hf_options,
264                         { "Options", "mrdisc.options", FT_NONE, BASE_NONE,
265                           NULL, 0, "MRDISC Options", HFILL }},
266
267                 { &hf_option,
268                         { "Option", "mrdisc.option", FT_UINT8, BASE_DEC,
269                           VALS(mrdisc_options), 0, "MRDISC Option Type", HFILL }},
270
271                 { &hf_option_len,
272                         { "Length", "mrdisc.opt_len", FT_UINT8, BASE_DEC,
273                           NULL, 0, "MRDISC Option Length", HFILL }},
274
275                 { &hf_qi,
276                         { "Query Interval", "mrdisc.query_int", FT_UINT16, BASE_DEC,
277                           NULL, 0, "MRDISC Query Interval", HFILL }},
278
279                 { &hf_rv,
280                         { "Robustness Variable", "mrdisc.rob_var", FT_UINT16, BASE_DEC,
281                           NULL, 0, "MRDISC Robustness Variable", HFILL }},
282
283                 { &hf_option_bytes,
284                         { "Data", "mrdisc.option_data", FT_BYTES, BASE_NONE,
285                           NULL, 0, "MRDISC Unknown Option Data", HFILL }},
286
287         };
288         static gint *ett[] = {
289                 &ett_mrdisc,
290                 &ett_options,
291         };
292
293         proto_mrdisc = proto_register_protocol("Multicast Router DISCovery protocol",
294             "MRDISC", "mrdisc");
295         proto_register_field_array(proto_mrdisc, hf, array_length(hf));
296         proto_register_subtree_array(ett, array_length(ett));
297 }