Give the code that computes protocol statistics a progress dialog box,
[obnox/wireshark/wip.git] / packet-gvrp.c
1 /* packet-gvrp.c
2  * Routines for GVRP (GARP VLAN Registration Protocol) dissection
3  * Copyright 2000, Kevin Shi <techishi@ms22.hinet.net>
4  *
5  * $Id: packet-gvrp.c,v 1.4 2001/01/03 06:55:28 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <glib.h>
43
44 #ifdef NEED_SNPRINTF_H
45 # include "snprintf.h"
46 #endif
47
48 #include "packet.h"
49 #include "llcsaps.h"
50
51 /* Initialize the protocol and registered fields */
52 static int proto_gvrp = -1;
53 static int hf_gvrp_proto_id = -1;
54 static int hf_gvrp_attribute_type = -1;
55 static int hf_gvrp_attribute_length = -1;
56 static int hf_gvrp_attribute_event = -1;
57 static int hf_gvrp_attribute_value = -1;
58 /*static int hf_gvrp_end_of_mark = -1;*/
59
60 /* Initialize the subtree pointers */
61 static gint ett_gvrp = -1;
62 /*static gint ett_gvrp_message = -1;
63 static gint ett_gvrp_attribute_list = -1;
64 static gint ett_gvrp_attribute = -1;*/
65
66 /* Constant definitions */
67 #define GARP_DEFAULT_PROTOCOL_ID        0x0001
68 #define GARP_END_OF_MARK                0x00
69
70 #define GVRP_ATTRIBUTE_TYPE             0x01
71
72 static const value_string attribute_type_vals[] = {
73         { GVRP_ATTRIBUTE_TYPE, "VID" },
74         { 0,                   NULL }
75 };
76
77 /* The length of GVRP LeaveAll attribute should be 2 octets (one for length
78  * and the other for event) */
79 #define GVRP_LENGTH_LEAVEALL            (sizeof(guint8)+sizeof(guint8))
80
81 /* The length of GVRP attribute other than LeaveAll should be 4 octets (one
82  * for length, one for event, and the last two for VID value).
83  */
84 #define GVRP_LENGTH_NON_LEAVEALL        (sizeof(guint8)+sizeof(guint8)+sizeof(guint16))
85
86 /* Packet offset definitions */
87 #define GARP_PROTOCOL_ID                0
88
89 /* Event definitions */
90 #define GVRP_EVENT_LEAVEALL             0
91 #define GVRP_EVENT_JOINEMPTY            1
92 #define GVRP_EVENT_JOININ               2
93 #define GVRP_EVENT_LEAVEEMPTY           3
94 #define GVRP_EVENT_LEAVEIN              4
95 #define GVRP_EVENT_EMPTY                5
96
97 static const value_string event_vals[] = {
98         { GVRP_EVENT_LEAVEALL,   "Leave All" },
99         { GVRP_EVENT_JOINEMPTY,  "Join Empty" },
100         { GVRP_EVENT_JOININ,     "Join In" },
101         { GVRP_EVENT_LEAVEEMPTY, "Leave Empty" },
102         { GVRP_EVENT_LEAVEIN,    "Leave In" },
103         { GVRP_EVENT_EMPTY,      "Empty" },
104         { 0,                     NULL }
105 };
106
107 /* Code to actually dissect the packets */
108 void
109 dissect_gvrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
110 {
111     proto_item   *ti;
112     proto_tree   *gvrp_tree;
113     guint16       protocol_id;
114     guint8        octet;
115     int           msg_index, attr_index, offset = 0, length = tvb_reported_length(tvb);
116
117     CHECK_DISPLAY_AS_DATA(proto_gvrp, tvb, pinfo, tree);
118
119     pinfo->current_proto = "GVRP";
120     
121     if (check_col(pinfo->fd, COL_PROTOCOL)) 
122         col_set_str(pinfo->fd, COL_PROTOCOL, "GVRP");
123     
124     if (check_col(pinfo->fd, COL_INFO)) 
125         col_set_str(pinfo->fd, COL_INFO, "GVRP");
126
127     if (tree)
128     {
129         ti = proto_tree_add_item(tree, proto_gvrp, tvb, 0, length, FALSE);
130
131         gvrp_tree = proto_item_add_subtree(ti, ett_gvrp);
132
133         /* Read in GARP protocol ID */
134         protocol_id = tvb_get_ntohs(tvb, GARP_PROTOCOL_ID);
135     
136         proto_tree_add_uint_format(gvrp_tree, hf_gvrp_proto_id, tvb,
137                                    GARP_PROTOCOL_ID, sizeof(guint16), 
138                                    protocol_id,
139                                    "Protocol Identifier: 0x%04x (%s)", 
140                                    protocol_id,
141                                    protocol_id == GARP_DEFAULT_PROTOCOL_ID ? 
142                                      "GARP VLAN Registration Protocol" :
143                                      "Unknown Protocol");
144
145         /* Currently only one protocol ID is supported */
146         if (protocol_id != GARP_DEFAULT_PROTOCOL_ID)
147         {
148             proto_tree_add_text(gvrp_tree, tvb, GARP_PROTOCOL_ID, sizeof(guint16), 
149  "   (Warning: this version of Ethereal only knows about protocol id = 1)");
150             dissect_data(tvb, GARP_PROTOCOL_ID + sizeof(guint16), pinfo, tree);
151             return;
152         }
153
154         offset += sizeof(guint16);
155         length -= sizeof(guint16);
156
157         msg_index = 0;
158
159         /* Begin to parse GARP messages */
160         while (length)
161         {
162             proto_item   *msg_item;
163             int           msg_start = offset;
164
165             /* Read in attribute type. */
166             octet = tvb_get_guint8(tvb, offset);
167
168             /* Check for end of mark */
169             if (octet == GARP_END_OF_MARK)
170             {
171                 /* End of GARP PDU */
172                 if (msg_index)
173                 {
174                     proto_tree_add_text(gvrp_tree, tvb, offset, sizeof(guint8),
175                                         "End of mark");
176                     break;
177                 }
178                 else
179                 {
180                     dissect_data(tvb, offset, pinfo, tree);
181                     return;
182                 }
183             }
184
185             offset += sizeof(guint8);
186             length -= sizeof(guint8);
187
188             msg_item = proto_tree_add_text(gvrp_tree, tvb, msg_start, 0,
189                                            "Message %d", msg_index + 1);
190
191             proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_type, tvb,
192                                 msg_start, sizeof(guint8), octet);
193
194             /* GVRP only supports one attribute type. */
195             if (octet != GVRP_ATTRIBUTE_TYPE)
196             {
197                 dissect_data(tvb, offset, pinfo, tree);
198                 return;
199             }
200
201             attr_index = 0;
202
203             while (length)
204             {
205                 int          attr_start = offset;
206                 proto_item   *attr_item;
207
208                 /* Read in attribute length. */
209                 octet = tvb_get_guint8(tvb, offset);
210
211                 /* Check for end of mark */
212                 if (octet == GARP_END_OF_MARK)
213                 {
214                     /* If at least one message has been already read, 
215                      * check for another end of mark.
216                      */
217                     if (attr_index)
218                     {
219                         proto_tree_add_text(gvrp_tree, tvb, offset,
220                                             sizeof(guint8), "  End of mark");
221
222                         offset += sizeof(guint8);
223                         length -= sizeof(guint8);
224
225                         proto_item_set_len(msg_item, offset - msg_start);
226                         break;
227                     }
228                     else
229                     {
230                         dissect_data(tvb, offset, pinfo, tree);
231                         return;
232                     }
233                 }
234                 else
235                 {
236                     guint8   event;
237
238                     offset += sizeof(guint8);
239                     length -= sizeof(guint8);
240
241                     attr_item = proto_tree_add_text(gvrp_tree, tvb,
242                          attr_start, 0, "  Attribute %d", attr_index + 1);
243
244                     proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_length,
245                          tvb, attr_start, sizeof(guint8), octet);
246
247                     /* Read in attribute event */
248                     event = tvb_get_guint8(tvb, offset);
249
250                     proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_event,
251                          tvb, offset, sizeof(guint8), event);
252
253                     offset += sizeof(guint8);
254                     length -= sizeof(guint8);
255
256                     switch (event) {
257
258                     case GVRP_EVENT_LEAVEALL:
259                         if (octet != GVRP_LENGTH_LEAVEALL)
260                         {
261                             dissect_data(tvb, offset, pinfo, tree);
262                             return;
263                         }
264                         break;
265
266                      case GVRP_EVENT_JOINEMPTY:
267                      case GVRP_EVENT_JOININ:
268                      case GVRP_EVENT_LEAVEEMPTY:
269                      case GVRP_EVENT_LEAVEIN:
270                      case GVRP_EVENT_EMPTY:
271                         if (octet != GVRP_LENGTH_NON_LEAVEALL)
272                         {
273                             dissect_data(tvb, offset, pinfo, tree);
274                             return;
275                         }
276
277                         /* Show attribute value */
278                         proto_tree_add_item(gvrp_tree, hf_gvrp_attribute_value,
279                             tvb, offset, sizeof(guint16), FALSE);
280
281                         offset += sizeof(guint16);
282                         length -= sizeof(guint16);
283                         break;
284
285                      default:
286                         dissect_data(tvb, offset, pinfo, tree);
287                         return;
288                     }
289                 }
290
291                 proto_item_set_len(attr_item, offset - attr_start);
292
293                 attr_index++;
294             }
295
296             msg_index++;
297         }
298     }
299 }
300
301
302 /* Register the protocol with Ethereal */
303 void
304 proto_register_gvrp(void)
305 {                
306     static hf_register_info hf[] = {
307         { &hf_gvrp_proto_id,
308             { "Protocol ID", "garp.protocol_id",
309             FT_UINT16,      BASE_HEX,      NULL,  0x0,
310             "" }
311         },
312         { &hf_gvrp_attribute_type,
313             { "Type",        "garp.attribute_type",
314             FT_UINT8,        BASE_HEX,      VALS(attribute_type_vals),  0x0,
315             "" }
316         },
317         { &hf_gvrp_attribute_length,
318             { "Length",      "garp.attribute_length",
319             FT_UINT8,        BASE_DEC,      NULL,  0x0,
320             "" }
321         },
322         { &hf_gvrp_attribute_event,
323             { "Event",       "garp.attribute_event",
324             FT_UINT8,        BASE_DEC,      VALS(event_vals),  0x0,
325             "" }
326         },
327         { &hf_gvrp_attribute_value,
328             { "Value",       "garp.attribute_value",
329             FT_UINT16,       BASE_DEC,      NULL,  0x0,
330             "" }
331         }
332     };
333
334     static gint *ett[] = {
335         &ett_gvrp
336     };
337
338     /* Register the protocol name and description for GVRP */
339     proto_gvrp = proto_register_protocol("GARP VLAN Registration Protocol",
340                                          "GVRP", "gvrp");
341
342     /* Required function calls to register the header fields and subtrees
343      * used by GVRP */
344     proto_register_field_array(proto_gvrp, hf, array_length(hf));
345     proto_register_subtree_array(ett, array_length(ett));
346 }