Fix error in code that '\0'-terminates a string - it was putting the
[obnox/wireshark/wip.git] / packet-hsrp.c
1 /* packet-hsrp.c
2  * Routines for the Cisco Hot Standby Router Protocol (HSRP)
3  * RFC2281
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-hsrp.c,v 1.8 2000/10/21 09:46:21 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-vrrp.c
14  * 
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * 
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
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 #include "packet.h"
44
45 static gint proto_hsrp = -1;
46 static gint ett_hsrp = -1;
47
48 #define UDP_PORT_HSRP   1985
49
50 struct hsrp_packet {          /* Multicast to 224.0.0.2, TTL 1, UDP, port 1985 */
51         guint8  version;      /* RFC2281 describes version 0 */
52         guint8  opcode;
53         guint8  state;
54 #define HSRP_DEFAULT_HELLOTIME 3
55         guint8  hellotime;    /* In seconds */
56 #define HSRP_DEFAULT_HOLDTIME 10
57         guint8  holdtime;     /* In seconds */
58         guint8  priority;     /* Higher is stronger, highest IP address tie-breaker */
59         guint8  group;        /* Identifies the standby group */
60         guint8  reserved;
61         guint8  auth_data[8]; /* Clear-text password, recommended default is `cisco' */
62         guint32 virt_ip_addr; /* The virtual IP address used by this group */
63 };
64
65
66 #define HSRP_OPCODE_HELLO  0
67 #define HSRP_OPCODE_COUP   1
68 #define HSRP_OPCODE_RESIGN 2
69 static const value_string hsrp_opcode_vals[] = {
70         {HSRP_OPCODE_HELLO,  "Hello"},
71         {HSRP_OPCODE_COUP,   "Coup"},
72         {HSRP_OPCODE_RESIGN, "Resign"}
73 };
74
75 #define HSRP_STATE_INITIAL  0
76 #define HSRP_STATE_LEARN    1
77 #define HSRP_STATE_LISTEN   2
78 #define HSRP_STATE_SPEAK    4
79 #define HSRP_STATE_STANDBY  8
80 #define HSRP_STATE_ACTIVE  16
81 static const value_string hsrp_state_vals[] = {
82         {HSRP_STATE_INITIAL, "Initial"},
83         {HSRP_STATE_LEARN,   "Learn"},
84         {HSRP_STATE_LISTEN,  "Listen"},
85         {HSRP_STATE_SPEAK,   "Speak"},
86         {HSRP_STATE_STANDBY, "Standby"},
87         {HSRP_STATE_ACTIVE,  "Active"}
88 };
89
90 static void
91 dissect_hsrp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
92 {
93         struct hsrp_packet hsrp;
94         gboolean short_packet = FALSE;
95
96         OLD_CHECK_DISPLAY_AS_DATA(proto_hsrp, pd, offset, fd, tree);
97
98         if (sizeof(struct hsrp_packet) > END_OF_FRAME)
99                 short_packet = TRUE;
100         else
101                 memcpy(&hsrp, pd + offset, sizeof(struct hsrp_packet));
102
103         if (check_col(fd, COL_PROTOCOL))
104                 col_add_str(fd, COL_PROTOCOL, "HSRP");
105         
106         if (check_col(fd, COL_INFO)) {
107                 if (short_packet)
108                         col_add_fstr(fd, COL_INFO, "Short packet, length %u", END_OF_FRAME);
109                 else
110                         col_add_fstr(fd, COL_INFO, "%s (state %s)",
111                                      val_to_str(hsrp.opcode, hsrp_opcode_vals, "Unknown"),
112                                      val_to_str(hsrp.state, hsrp_state_vals, "Unknown"));
113         }
114
115         if (tree) {
116                 proto_item *ti;
117                 proto_tree *hsrp_tree;
118                 guint8 auth_buf[sizeof(hsrp.auth_data) + 1];
119
120                 if (short_packet) {
121                         old_dissect_data(pd, offset, fd, tree);
122                         return;
123                 }
124
125                 ti = proto_tree_add_item(tree, proto_hsrp, NullTVB, offset, END_OF_FRAME, FALSE);
126                 hsrp_tree = proto_item_add_subtree(ti, ett_hsrp);
127
128                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Version: %u", hsrp.version);
129                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Opcode: %u (%s)", hsrp.opcode,
130                                     val_to_str(hsrp.opcode, hsrp_opcode_vals, "Unknown"));
131                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "State: %u (%s)", hsrp.state,
132                                     val_to_str(hsrp.state, hsrp_state_vals, "Unknown"));
133                 
134                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Hellotime: %u second%s (%sdefault)",
135                                     hsrp.hellotime, plurality(hsrp.hellotime, "", "s"),
136                                     (hsrp.hellotime == HSRP_DEFAULT_HELLOTIME) ? "" : "non-");
137                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Holdtime: %u second%s (%sdefault)",
138                                     hsrp.holdtime, plurality(hsrp.holdtime, "", "s"),
139                                     (hsrp.holdtime == HSRP_DEFAULT_HOLDTIME) ? "" : "non-");
140                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Priority: %u", hsrp.priority);
141                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Group: %u", hsrp.group);
142                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 1, "Reserved: 0x%x", hsrp.reserved);
143
144                 memcpy(auth_buf, hsrp.auth_data, sizeof(hsrp.auth_data));
145                 auth_buf[sizeof(hsrp.auth_data)] = '\0';
146                 proto_tree_add_text(hsrp_tree, NullTVB, offset, 8, "Authentication Data: `%s'", auth_buf);
147                 offset+=8;
148
149                 proto_tree_add_text(hsrp_tree, NullTVB, offset++, 4, "Virtual IP address: %s",
150                                     ip_to_str((guint8 *)&hsrp.virt_ip_addr));
151                 
152         }
153
154         return;
155 }
156
157 void proto_register_hsrp(void)
158 {
159         static gint *ett[] = {
160                 &ett_hsrp,
161         };
162
163         proto_hsrp = proto_register_protocol("Cisco Hot Standby Router Protocol", "hsrp");
164         proto_register_subtree_array(ett, array_length(ett));
165
166         return;
167 }
168
169 void
170 proto_reg_handoff_hsrp(void)
171 {
172         old_dissector_add("udp.port", UDP_PORT_HSRP, dissect_hsrp);
173 }