Have "dissect_netbios_payload()" take as an argument a tvbuff containing
[obnox/wireshark/wip.git] / packet-msnip.c
1 /* packet-msnip.c   2001 Ronnie Sahlberg <rsahlber@bigpond.net.au>
2  * Routines for IGMP/MSNIP packet disassembly
3  *
4  * $Id: packet-msnip.c,v 1.1 2001/06/29 18:55:49 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 "packet.h"
51 #include "in_cksum.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 void 
91 msnip_checksum(proto_tree *tree,tvbuff_t *tvb, int len)
92 {
93         guint16 cksum,hdrcksum;
94         vec_t cksum_vec[1];
95
96         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, len);
97         cksum_vec[0].len = len;
98
99         hdrcksum = tvb_get_ntohs(tvb, 2);
100         cksum = in_cksum(&cksum_vec[0],1);
101
102         if (cksum==0) {
103                 proto_tree_add_uint_format(tree, hf_checksum, tvb, 2, 2, hdrcksum, "Header checksum: 0x%04x (correct)", hdrcksum);
104         } else {
105                 proto_tree_add_item_hidden(tree, hf_checksum_bad, tvb, 2, 2, TRUE);
106                 proto_tree_add_uint_format(tree, hf_checksum, tvb, 2, 2, hdrcksum, "Header checksum: 0x%04x (incorrect, should be 0x%04x)", hdrcksum,in_cksum_shouldbe(hdrcksum,cksum));
107         }
108
109         return;
110 }
111
112
113 static int
114 dissect_msnip_rmr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
115 {
116         guint8 count;
117
118         /* group count */
119         count = tvb_get_guint8(tvb, offset);
120         proto_tree_add_uint(parent_tree, hf_count, tvb, offset, 1, count);
121         offset += 1;
122
123         /* checksum */
124         msnip_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
125         offset += 2;
126
127         while (count--) {
128                 proto_tree *tree;
129                 proto_item *item;
130                 guint8 rec_type;
131                 guint32 maddr;
132                 int old_offset = offset;
133
134                 item = proto_tree_add_item(parent_tree, hf_groups, 
135                                 tvb, offset, 0, FALSE);
136                 tree = proto_item_add_subtree(item, ett_groups);
137
138                 /* record type */
139                 rec_type = tvb_get_guint8(tvb, offset);
140                 proto_tree_add_uint(tree, hf_rec_type, tvb, offset, 1, rec_type);
141                 offset += 1;
142
143                 /* skip 3 unused bytes */
144                 offset += 3;
145
146                 /* multicast group */
147                 tvb_memcpy(tvb, (guint8 *)&maddr, offset, 4);
148                 proto_tree_add_ipv4(tree, hf_maddr, tvb, offset, 4,
149                         maddr);
150                 offset += 4;
151
152                 if (item) {
153                         proto_item_set_text(item,"Group: %s %s",
154                                 ip_to_str((guint8 *)&maddr), 
155                                 val_to_str(rec_type, msnip_rec_types,
156                                         "Unknown Type:0x%02x"));
157
158                         proto_item_set_len(item, offset-old_offset);
159                 }
160         }
161
162         return offset;
163 }
164
165 static int
166 dissect_msnip_is(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
167 {
168
169         /* skip reserved byte */
170         offset += 1;
171
172         /* checksum */
173         msnip_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
174         offset += 2;
175
176         /* 16 bit holdtime */
177         proto_tree_add_uint(parent_tree, hf_holdtime16, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
178         offset += 2;
179
180         /* Generation ID */
181         proto_tree_add_uint(parent_tree, hf_genid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
182         offset += 2;
183
184         return offset;
185 }
186
187
188 static int
189 dissect_msnip_gm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
190 {
191         guint8 count;
192
193         /* group count */
194         count = tvb_get_guint8(tvb, offset);
195         proto_tree_add_uint(parent_tree, hf_count, tvb, offset, 1, count);
196         offset += 1;
197
198         /* checksum */
199         msnip_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
200         offset += 2;
201
202         /* holdtime */
203         proto_tree_add_uint(parent_tree, hf_holdtime, tvb, offset, 4, count);
204         offset += 4;
205
206         while (count--) {
207                 proto_tree *tree;
208                 proto_item *item;
209                 guint32 maddr;
210                 guint8 masklen;
211                 int old_offset = offset;
212
213                 item = proto_tree_add_item(parent_tree, hf_groups, 
214                                 tvb, offset, 0, FALSE);
215                 tree = proto_item_add_subtree(item, ett_groups);
216
217                 /* multicast group */
218                 tvb_memcpy(tvb, (guint8 *)&maddr, offset, 4);
219                 proto_tree_add_ipv4(tree, hf_maddr, tvb, offset, 4,
220                         maddr);
221                 offset += 4;
222
223                 /* mask length */
224                 masklen = tvb_get_guint8(tvb, offset);
225                 proto_tree_add_uint(tree, hf_mask, tvb, 
226                         offset, 1, masklen);
227                 offset += 1;
228
229                 /* skip 3 unused bytes */
230                 offset += 3;
231
232                 if (item) {
233                         proto_item_set_text(item,"Group: %s/%d",
234                                 ip_to_str((guint8 *)&maddr), masklen);
235
236                         proto_item_set_len(item, offset-old_offset);
237                 }
238         }
239         
240         return offset;
241 }
242
243
244 /* This function is only called from the IGMP dissector */
245 int
246 dissect_msnip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
247 {
248         proto_tree *tree;
249         proto_item *item;
250         guint8 type;
251
252         if (!proto_is_protocol_enabled(proto_msnip)) {
253                 /* we are not enabled, skip entire packet to be nice
254                    to the igmp layer. (so clicking on IGMP will display the data)
255                  */
256                 return offset+tvb_length_remaining(tvb, offset);
257         }
258
259         item = proto_tree_add_item(parent_tree, proto_msnip, tvb, offset, 0, FALSE);
260         tree = proto_item_add_subtree(item, ett_msnip);
261
262
263         if (check_col(pinfo->fd, COL_PROTOCOL)) {
264                 col_set_str(pinfo->fd, COL_PROTOCOL, "MSNIP");
265         }
266         if (check_col(pinfo->fd, COL_INFO)) {
267                 col_clear(pinfo->fd, COL_INFO);
268         }
269
270
271         type = tvb_get_guint8(tvb, offset);
272         if (check_col(pinfo->fd, COL_INFO)) {
273                 col_add_fstr(pinfo->fd, COL_INFO,
274                         "%s",val_to_str(type, msnip_types, 
275                                 "Unknown Type:0x%02x"));
276         }
277
278         /* type of command */
279         proto_tree_add_uint(tree, hf_type, tvb, offset, 1, type);
280         offset += 1;
281
282         switch (type) {
283         case MSNIP_GM:
284                 offset = dissect_msnip_gm(tvb, pinfo, tree, offset);
285                 break;
286         case MSNIP_IS:
287                 offset = dissect_msnip_is(tvb, pinfo, tree, offset);
288                 break;
289         case MSNIP_RMR:
290                 offset = dissect_msnip_rmr(tvb, pinfo, tree, offset);
291                 break;
292         }
293
294         if (item) {
295                 proto_item_set_len(item, offset);
296         }
297
298         return offset;
299 }
300
301
302 void
303 proto_register_msnip(void)
304 {
305         static hf_register_info hf[] = {
306                 { &hf_type,
307                         { "Type", "msnip.type", FT_UINT8, BASE_HEX,
308                           VALS(msnip_types), 0, "MSNIP Packet Type", HFILL }},
309
310                 { &hf_checksum,
311                         { "Checksum", "msnip.checksum", FT_UINT16, BASE_HEX,
312                           NULL, 0, "MSNIP Checksum", HFILL }},
313
314                 { &hf_checksum_bad,
315                         { "Bad Checksum", "msnip.checksum_bad", FT_BOOLEAN, BASE_NONE,
316                           NULL, 0, "Bad MSNIP Checksum", HFILL }},
317
318                 { &hf_count,
319                         { "Count", "msnip.count", FT_UINT8, BASE_DEC,
320                           NULL, 0, "MSNIP Number of groups", HFILL }},
321
322                 { &hf_holdtime,
323                         { "Holdtime", "msnip.holdtime", FT_UINT32, BASE_DEC,
324                           NULL, 0, "MSNIP Holdtime in seconds", HFILL }},
325
326                 { &hf_groups,
327                         { "Groups", "msnip.groups", FT_NONE, BASE_NONE,
328                           NULL, 0, "MSNIP Groups", HFILL }},
329
330                 { &hf_maddr,
331                         { "Multicast group", "msnip.maddr", FT_IPv4, BASE_NONE,
332                           NULL, 0, "MSNIP Multicast Group", HFILL }},
333
334                 { &hf_mask,
335                         { "Netmask", "msnip.netmask", FT_UINT8, BASE_DEC,
336                           NULL, 0, "MSNIP Netmask", HFILL }},
337
338                 { &hf_holdtime16,
339                         { "Holdtime", "msnip.holdtime16", FT_UINT16, BASE_DEC,
340                           NULL, 0, "MSNIP Holdtime in seconds", HFILL }},
341
342                 { &hf_genid,
343                         { "Generation ID", "msnip.genid", FT_UINT16, BASE_DEC,
344                           NULL, 0, "MSNIP Generation ID", HFILL }},
345
346                 { &hf_rec_type,
347                         { "Record Type", "msnip.rec_type", FT_UINT8, BASE_DEC,
348                           VALS(msnip_rec_types), 0, "MSNIP Record Type", HFILL }},
349
350         };
351         static gint *ett[] = {
352                 &ett_msnip,
353                 &ett_groups,
354         };
355
356         proto_msnip = proto_register_protocol("MSNIP : Multicast Source Notification of Interest Protocol",
357             "MSNIP", "msnip");
358         proto_register_field_array(proto_msnip, hf, array_length(hf));
359         proto_register_subtree_array(ett, array_length(ett));
360 }
361