Update from Paul Ionescu to set the reported length of the tvbuff for
[obnox/wireshark/wip.git] / packet-dec-bpdu.c
1 /* packet-dec-bpdu.c
2  * Routines for DEC BPDU (DEC Spanning Tree Protocol) disassembly
3  *
4  * $Id: packet-dec-bpdu.c,v 1.3 2001/01/07 00:23:03 guy Exp $
5  *
6  * Copyright 2001 Paul Ionescu <paul@acorp.ro>
7  * 
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "resolv.h"
45 #include "etypes.h"
46
47 /* Offsets of fields within a BPDU */
48
49 #define BPDU_DEC_CODE            0
50 #define BPDU_TYPE                1
51 #define BPDU_VERSION             2
52 #define BPDU_FLAGS               3
53 #define BPDU_ROOT_PRI            4
54 #define BPDU_ROOT_MAC            6
55 #define BPDU_ROOT_PATH_COST     12
56 #define BPDU_BRIDGE_PRI         14
57 #define BPDU_BRIDGE_MAC         16
58 #define BPDU_PORT_IDENTIFIER    22
59 #define BPDU_MESSAGE_AGE        23
60 #define BPDU_HELLO_TIME         24
61 #define BPDU_MAX_AGE            25
62 #define BPDU_FORWARD_DELAY      26
63
64 #define DEC_BPDU_SIZE           27
65
66
67 static int proto_dec_bpdu = -1;
68
69 static gint ett_dec_bpdu = -1;
70
71 static void
72 dissect_dec_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
73       guint8  protocol_identifier;
74       guint8  protocol_version;
75       guint8  bpdu_type;
76       guint8  flags;
77       proto_tree *bpdu_tree;
78       proto_item *ti;
79
80       CHECK_DISPLAY_AS_DATA(proto_dec_bpdu, tvb, pinfo, tree);
81
82       pinfo->current_proto = "DEC_STP";
83
84       if (check_col(pinfo->fd, COL_PROTOCOL)) {
85             col_set_str(pinfo->fd, COL_PROTOCOL, "DEC_STP");
86       }
87
88       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
89       flags=tvb_get_guint8(tvb,BPDU_FLAGS);
90       
91       if (check_col(pinfo->fd, COL_INFO)) {
92             if (bpdu_type == 25)
93                   col_add_fstr(pinfo->fd, COL_INFO, "Hello Packet");
94             else if (bpdu_type == 0x02)
95                   col_add_fstr(pinfo->fd, COL_INFO, "Topology Change Notification");
96       }
97       
98       tvb_set_reported_length(tvb, DEC_BPDU_SIZE);
99
100       if (tree) {
101             protocol_identifier = tvb_get_guint8(tvb, BPDU_DEC_CODE);
102
103             protocol_version = tvb_get_guint8(tvb, BPDU_VERSION);
104
105             ti = proto_tree_add_protocol_format(tree, proto_dec_bpdu, tvb, 0, DEC_BPDU_SIZE,
106                                 "DEC Spanning Tree Protocol");
107             bpdu_tree = proto_item_add_subtree(ti, ett_dec_bpdu);
108
109             proto_tree_add_text(bpdu_tree, tvb, BPDU_DEC_CODE, 1, "Protocol ID: 0x%02x (%s)",
110                  protocol_identifier,
111                  protocol_identifier==0xe1?"DEC Spanning Tree Protocol":
112                  "Unknown protocol, the dissection may be wrong");
113
114             proto_tree_add_text(bpdu_tree, tvb, BPDU_TYPE,1, "BPDU Type: %u (%s)", bpdu_type,
115                 (bpdu_type==25?"Hello Packet":(bpdu_type==2?"Topology change notice":"Unknown")));
116
117             proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION,1, "BPDU Version: %u (%s)",
118                 protocol_version,protocol_version==1?"DEC STP Version 1":"Unknown Version");
119
120             proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS,1, "Flags: 0x%02x",flags);
121
122             if (flags & 0x80)
123                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "      1... ....  Use short timers");
124             if (flags & 0x02)
125                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "      .... ..1.  Topology Change Acknowledgment");
126             if (flags & 0x01)
127                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "      .... ...1  Topology Change");
128             
129             proto_tree_add_text(bpdu_tree, tvb, BPDU_ROOT_PRI,2, "Root priority: %u",
130                 tvb_get_ntohs(tvb,BPDU_ROOT_PRI));
131             proto_tree_add_text(bpdu_tree, tvb, BPDU_ROOT_MAC,6, "Root MAC: %s",
132                 ether_to_str(tvb_get_ptr(tvb,BPDU_ROOT_MAC,6)));
133             proto_tree_add_text(bpdu_tree, tvb, BPDU_ROOT_PATH_COST,2, "Root path cost: %u",
134                 tvb_get_ntohs(tvb,BPDU_ROOT_PATH_COST));
135             proto_tree_add_text(bpdu_tree, tvb, BPDU_BRIDGE_PRI,2, "Root priority: %u",
136                 tvb_get_ntohs(tvb,BPDU_BRIDGE_PRI));
137             proto_tree_add_text(bpdu_tree, tvb, BPDU_BRIDGE_MAC,6, "Root MAC: %s",
138                 ether_to_str(tvb_get_ptr(tvb,BPDU_BRIDGE_MAC,6)));
139             proto_tree_add_text(bpdu_tree, tvb, BPDU_PORT_IDENTIFIER,1, "Port identifier: %u",
140                 tvb_get_guint8(tvb,BPDU_PORT_IDENTIFIER));
141             proto_tree_add_text(bpdu_tree, tvb, BPDU_MESSAGE_AGE,1, "Age: %u",
142                 tvb_get_guint8(tvb,BPDU_MESSAGE_AGE));
143             proto_tree_add_text(bpdu_tree, tvb, BPDU_HELLO_TIME,1, "Hello time: %u",
144                 tvb_get_guint8(tvb,BPDU_HELLO_TIME));
145             proto_tree_add_text(bpdu_tree, tvb, BPDU_MAX_AGE,1, "Max Age: %u",
146                 tvb_get_guint8(tvb,BPDU_MAX_AGE));
147             proto_tree_add_text(bpdu_tree, tvb, BPDU_FORWARD_DELAY,1, "Forward Delay: %u",
148                 tvb_get_guint8(tvb,BPDU_FORWARD_DELAY));
149
150       }
151 }
152
153 void
154 proto_register_dec_bpdu(void)
155 {
156   static gint *ett[] = {
157     &ett_dec_bpdu,
158   };
159
160   proto_dec_bpdu = proto_register_protocol("DEC Spanning Tree Protocol", "DEC_STP", "dec_stp");
161   proto_register_subtree_array(ett, array_length(ett));
162 }
163
164 void
165 proto_reg_handoff_dec_bpdu(void)
166 {
167   dissector_add("ethertype", ETHERTYPE_DEC_LB, dissect_dec_bpdu); 
168 }