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