Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-msnip.c
1 /* packet-msnip.c   2001 Ronnie Sahlberg <See AUTHORS for email>
2  * Routines for IGMP/MSNIP packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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                         MSNIP
28         code
29
30         0x23            x
31         0x24            x
32         0x25            x
33
34         MSNIP " Multicast Source Notification of Interest Protocol
35         Defined in draft-ietf-idmr-igmp-msnip-00.txt
36 */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <glib.h>
43
44 #include <epan/packet.h>
45 #include "packet-igmp.h"
46 #include "packet-msnip.h"
47
48
49 static int proto_msnip = -1;
50 static int hf_checksum = -1;
51 static int hf_checksum_bad = -1;
52 static int hf_type = -1;
53 static int hf_count = -1;
54 static int hf_holdtime = -1;
55 static int hf_groups = -1;
56 static int hf_maddr = -1;
57 static int hf_mask = -1;
58 static int hf_holdtime16 = -1;
59 static int hf_genid = -1;
60 static int hf_rec_type = -1;
61
62 static int ett_msnip = -1;
63 static int ett_groups = -1;
64
65
66 #define MSNIP_GM        0x23
67 #define MSNIP_IS        0x24
68 #define MSNIP_RMR       0x25
69 static const value_string msnip_types[] = {
70         {MSNIP_GM,      "Multicast Group Map"},
71         {MSNIP_IS,      "Multicast Interest Solicitation"},
72         {MSNIP_RMR,     "Multicast Receiver Membership Report"},
73         {0,                                     NULL}
74 };
75
76 #define MSNIP_RECTYPE_TRANSMIT  1
77 #define MSNIP_RECTYPE_HOLD      2
78 static const value_string msnip_rec_types[] = {
79         {MSNIP_RECTYPE_TRANSMIT,        "Request to start transmitting group"},
80         {MSNIP_RECTYPE_HOLD,            "Request to hold transmitting group"},
81         {0,                                     NULL}
82 };
83
84 static int
85 dissect_msnip_rmr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
86 {
87         guint8 count;
88
89         /* group count */
90         count = tvb_get_guint8(tvb, offset);
91         proto_tree_add_uint(parent_tree, hf_count, tvb, offset, 1, count);
92         offset += 1;
93
94         /* checksum */
95         igmp_checksum(parent_tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
96         offset += 2;
97
98         while (count--) {
99                 proto_tree *tree;
100                 proto_item *item;
101                 guint8 rec_type;
102                 guint32 maddr;
103                 int old_offset = offset;
104
105                 item = proto_tree_add_item(parent_tree, hf_groups,
106                                 tvb, offset, -1, FALSE);
107                 tree = proto_item_add_subtree(item, ett_groups);
108
109                 /* record type */
110                 rec_type = tvb_get_guint8(tvb, offset);
111                 proto_tree_add_uint(tree, hf_rec_type, tvb, offset, 1, rec_type);
112                 offset += 1;
113
114                 /* skip 3 unused bytes */
115                 offset += 3;
116
117                 /* multicast group */
118                 maddr = tvb_get_ipv4(tvb, offset);
119                 proto_tree_add_ipv4(tree, hf_maddr, tvb, offset, 4,
120                         maddr);
121                 offset += 4;
122
123                 if (item) {
124                         proto_item_set_text(item,"Group: %s %s",
125                                 ip_to_str((guint8 *)&maddr),
126                                 val_to_str(rec_type, msnip_rec_types,
127                                         "Unknown Type:0x%02x"));
128
129                         proto_item_set_len(item, offset-old_offset);
130                 }
131         }
132
133         return offset;
134 }
135
136 static int
137 dissect_msnip_is(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
138 {
139
140         /* skip reserved byte */
141         offset += 1;
142
143         /* checksum */
144         igmp_checksum(parent_tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
145         offset += 2;
146
147         /* 16 bit holdtime */
148         proto_tree_add_uint(parent_tree, hf_holdtime16, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
149         offset += 2;
150
151         /* Generation ID */
152         proto_tree_add_uint(parent_tree, hf_genid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
153         offset += 2;
154
155         return offset;
156 }
157
158
159 static int
160 dissect_msnip_gm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
161 {
162         guint8 count;
163
164         /* group count */
165         count = tvb_get_guint8(tvb, offset);
166         proto_tree_add_uint(parent_tree, hf_count, tvb, offset, 1, count);
167         offset += 1;
168
169         /* checksum */
170         igmp_checksum(parent_tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
171         offset += 2;
172
173         /* holdtime */
174         proto_tree_add_uint(parent_tree, hf_holdtime, tvb, offset, 4, count);
175         offset += 4;
176
177         while (count--) {
178                 proto_tree *tree;
179                 proto_item *item;
180                 guint32 maddr;
181                 guint8 masklen;
182                 int old_offset = offset;
183
184                 item = proto_tree_add_item(parent_tree, hf_groups,
185                                 tvb, offset, -1, FALSE);
186                 tree = proto_item_add_subtree(item, ett_groups);
187
188                 /* multicast group */
189                 maddr = tvb_get_ipv4(tvb, offset);
190                 proto_tree_add_ipv4(tree, hf_maddr, tvb, offset, 4,
191                         maddr);
192                 offset += 4;
193
194                 /* mask length */
195                 masklen = tvb_get_guint8(tvb, offset);
196                 proto_tree_add_uint(tree, hf_mask, tvb,
197                         offset, 1, masklen);
198                 offset += 1;
199
200                 /* skip 3 unused bytes */
201                 offset += 3;
202
203                 if (item) {
204                         proto_item_set_text(item,"Group: %s/%d",
205                                 ip_to_str((guint8 *)&maddr), masklen);
206
207                         proto_item_set_len(item, offset-old_offset);
208                 }
209         }
210
211         return offset;
212 }
213
214
215 /* This function is only called from the IGMP dissector */
216 int
217 dissect_msnip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
218 {
219         proto_tree *tree;
220         proto_item *item;
221         guint8 type;
222
223         if (!proto_is_protocol_enabled(find_protocol_by_id(proto_msnip))) {
224                 /* we are not enabled, skip entire packet to be nice
225                    to the igmp layer. (so clicking on IGMP will display the data)
226                  */
227                 return offset+tvb_length_remaining(tvb, offset);
228         }
229
230         item = proto_tree_add_item(parent_tree, proto_msnip, tvb, offset, -1, FALSE);
231         tree = proto_item_add_subtree(item, ett_msnip);
232
233
234         col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSNIP");
235         col_clear(pinfo->cinfo, COL_INFO);
236
237
238         type = tvb_get_guint8(tvb, offset);
239         if (check_col(pinfo->cinfo, COL_INFO)) {
240                 col_add_str(pinfo->cinfo, COL_INFO,
241                         val_to_str(type, msnip_types,
242                                 "Unknown Type:0x%02x"));
243         }
244
245         /* type of command */
246         proto_tree_add_uint(tree, hf_type, tvb, offset, 1, type);
247         offset += 1;
248
249         switch (type) {
250         case MSNIP_GM:
251                 offset = dissect_msnip_gm(tvb, pinfo, tree, offset);
252                 break;
253         case MSNIP_IS:
254                 offset = dissect_msnip_is(tvb, pinfo, tree, offset);
255                 break;
256         case MSNIP_RMR:
257                 offset = dissect_msnip_rmr(tvb, pinfo, tree, offset);
258                 break;
259         }
260
261         if (item) {
262                 proto_item_set_len(item, offset);
263         }
264
265         return offset;
266 }
267
268
269 void
270 proto_register_msnip(void)
271 {
272         static hf_register_info hf[] = {
273                 { &hf_type,
274                         { "Type", "msnip.type", FT_UINT8, BASE_HEX,
275                           VALS(msnip_types), 0, "MSNIP Packet Type", HFILL }},
276
277                 { &hf_checksum,
278                         { "Checksum", "msnip.checksum", FT_UINT16, BASE_HEX,
279                           NULL, 0, "MSNIP Checksum", HFILL }},
280
281                 { &hf_checksum_bad,
282                         { "Bad Checksum", "msnip.checksum_bad", FT_BOOLEAN, BASE_NONE,
283                           NULL, 0x0, "Bad MSNIP Checksum", HFILL }},
284
285                 { &hf_count,
286                         { "Count", "msnip.count", FT_UINT8, BASE_DEC,
287                           NULL, 0, "MSNIP Number of groups", HFILL }},
288
289                 { &hf_holdtime,
290                         { "Holdtime", "msnip.holdtime", FT_UINT32, BASE_DEC,
291                           NULL, 0, "MSNIP Holdtime in seconds", HFILL }},
292
293                 { &hf_groups,
294                         { "Groups", "msnip.groups", FT_NONE, BASE_NONE,
295                           NULL, 0, "MSNIP Groups", HFILL }},
296
297                 { &hf_maddr,
298                         { "Multicast group", "msnip.maddr", FT_IPv4, BASE_NONE,
299                           NULL, 0, "MSNIP Multicast Group", HFILL }},
300
301                 { &hf_mask,
302                         { "Netmask", "msnip.netmask", FT_UINT8, BASE_DEC,
303                           NULL, 0, "MSNIP Netmask", HFILL }},
304
305                 { &hf_holdtime16,
306                         { "Holdtime", "msnip.holdtime16", FT_UINT16, BASE_DEC,
307                           NULL, 0, "MSNIP Holdtime in seconds", HFILL }},
308
309                 { &hf_genid,
310                         { "Generation ID", "msnip.genid", FT_UINT16, BASE_DEC,
311                           NULL, 0, "MSNIP Generation ID", HFILL }},
312
313                 { &hf_rec_type,
314                         { "Record Type", "msnip.rec_type", FT_UINT8, BASE_DEC,
315                           VALS(msnip_rec_types), 0, "MSNIP Record Type", HFILL }},
316
317         };
318         static gint *ett[] = {
319                 &ett_msnip,
320                 &ett_groups,
321         };
322
323         proto_msnip = proto_register_protocol("MSNIP: Multicast Source Notification of Interest Protocol",
324             "MSNIP", "msnip");
325         proto_register_field_array(proto_msnip, hf, array_length(hf));
326         proto_register_subtree_array(ett, array_length(ett));
327 }
328