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