Add a comment explaining why we're defining S_ISDIR and company.
[obnox/wireshark/wip.git] / packet-vrrp.c
1 /* packet-vrrp.c
2  * Routines for the Virtual Router Redundancy Protocol (VRRP)
3  * RFC2338
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-vrrp.c,v 1.6 2000/05/31 05:07:53 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "packet-ip.h"
45
46 static gint proto_vrrp = -1;
47 static gint ett_vrrp = -1;
48 static gint ett_vrrp_ver_type = -1;
49
50 static gint hf_vrrp_ver_type = -1;
51 static gint hf_vrrp_version = -1;
52 static gint hf_vrrp_type = -1;
53
54 struct vrrp_header {
55 #define VRRP_VERSION_MASK 0xf0
56 #define VRRP_TYPE_MASK 0x0f
57         guint8  ver_type;
58         guint8  vrouter_id;
59         guint8  priority;
60         guint8  count_ip_addrs;
61         guint8  auth_type;
62         guint8  adver_int;
63         guint16 checksum;
64         /* One or more IP addresses */
65         /* 8 octets of authentication data */
66 #define VRRP_AUTH_DATA_LEN 8
67 };
68
69
70 #define VRRP_TYPE_ADVERTISEMENT 1
71 static const value_string vrrp_type_vals[] = {
72         {VRRP_TYPE_ADVERTISEMENT, "Advertisement"}
73 };
74
75 #define VRRP_AUTH_TYPE_NONE 0
76 #define VRRP_AUTH_TYPE_SIMPLE_TEXT 1
77 #define VRRP_AUTH_TYPE_IP_AUTH_HDR 2
78 static const value_string vrrp_auth_vals[] = {
79         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
80         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
81         {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"}
82 };
83
84 #define VRRP_PRIORITY_MASTER_STOPPING 0
85 /* Values between 1 and 254 inclusive are for backup VRRP routers */
86 #define VRRP_PRIORITY_DEFAULT 100
87 #define VRRP_PRIORITY_OWNER 255
88 static const value_string vrrp_prio_vals[] = {
89         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
90         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
91         {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"}
92 };
93
94
95 static void
96 dissect_vrrp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
97 {
98         struct vrrp_header vrh;
99         gboolean short_hdr = FALSE;
100         gboolean short_packet = FALSE;
101         guint calculated_len = -1; /* initialize to silence false warning from gcc */
102
103         if (sizeof(struct vrrp_header) > END_OF_FRAME)
104                 short_hdr = short_packet = TRUE;
105         else {
106                 memcpy(&vrh, pd + offset, sizeof(struct vrrp_header));
107                 calculated_len = sizeof(struct vrrp_header) + vrh.count_ip_addrs*4 + VRRP_AUTH_DATA_LEN;
108                 if (calculated_len > END_OF_FRAME)
109                         short_packet = TRUE;
110         }
111
112         if (check_col(fd, COL_PROTOCOL))
113                 col_add_str(fd, COL_PROTOCOL, "VRRP");
114         
115         if (check_col(fd, COL_INFO)) {
116                 if (short_hdr)
117                         col_add_fstr(fd, COL_INFO, "Short packet header, length %u", END_OF_FRAME);
118                 else if (short_packet)
119                         col_add_fstr(fd, COL_INFO, "Packet length mismatch, calculated %u, real %u",
120                                      calculated_len, END_OF_FRAME);
121                 else
122                         col_add_fstr(fd, COL_INFO, "%s (v%u)", "Announcement", hi_nibble(vrh.ver_type));
123         }
124
125         if (tree) {
126                 proto_item *ti, *tv;
127                 proto_tree *vrrp_tree, *ver_type_tree;
128                 guint8 ip_count, auth_len, auth_buf[VRRP_AUTH_DATA_LEN+1];
129
130                 if (short_hdr) {
131                         dissect_data(pd, offset, fd, tree);
132                         return;
133                 }
134
135                 ti = proto_tree_add_item(tree, proto_vrrp, NullTVB, offset, END_OF_FRAME, FALSE);
136                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
137
138                 tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type, NullTVB, offset, 1,
139                                                 vrh.ver_type, "Version %u, Packet type %u (%s)",
140                                                 hi_nibble(vrh.ver_type), lo_nibble(vrh.ver_type),
141                                                 val_to_str(lo_nibble(vrh.ver_type), vrrp_type_vals, "Unknown"));
142                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
143                 proto_tree_add_uint(ver_type_tree, hf_vrrp_version, NullTVB, offset, 1, vrh.ver_type);
144                 proto_tree_add_uint(ver_type_tree, hf_vrrp_type, NullTVB, offset, 1, vrh.ver_type);
145                 offset++;
146                 
147                 proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Virtual Router ID: %u", vrh.vrouter_id);
148                 proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Priority: %u (%s)", vrh.priority,
149                                     val_to_str(vrh.priority, vrrp_prio_vals, "Non-default backup priority"));
150                 proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Count IP Addrs: %u", vrh.count_ip_addrs);
151                 proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Authentication Type: %u (%s)", vrh.auth_type,
152                                     val_to_str(vrh.auth_type, vrrp_auth_vals, "Unknown"));
153                 proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Advertisement Interval: %u second%s",
154                                     vrh.adver_int, plurality(vrh.adver_int, "", "s"));
155                 proto_tree_add_text(vrrp_tree, NullTVB, offset, 2, "Checksum: 0x%x", htons(vrh.checksum));
156                 offset+=2;
157
158                 if (short_packet) {
159                         dissect_data(pd, offset, fd, vrrp_tree);
160                         return;
161                 }
162                 
163                 ip_count = vrh.count_ip_addrs;
164                 while (ip_count > 0) {
165                         proto_tree_add_text(vrrp_tree, NullTVB, offset, 4, "Virtual Router IP address: %s",
166                                             ip_to_str(pd+offset));
167                         offset+=4;
168                         ip_count--;
169                 }
170
171                 if (vrh.auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
172                         return; /* Contents of the authentication data is undefined */
173
174                 strncpy(auth_buf, pd+offset, VRRP_AUTH_DATA_LEN);
175                 auth_buf[VRRP_AUTH_DATA_LEN] = '\0';
176                 auth_len = strlen(auth_buf);
177                 if (auth_len > 0)
178                         proto_tree_add_text(vrrp_tree, NullTVB, offset, auth_len, "Authentication string: `%s'", auth_buf);
179                 offset+=8;
180
181         }
182
183         return;
184 }
185
186 void proto_register_vrrp(void)
187 {
188         static hf_register_info hf[] = {
189                 { &hf_vrrp_ver_type,
190                   {"VRRP message version and type", "vrrp.typever",
191                    FT_UINT8, BASE_DEC, NULL, 0x0,
192                    "VRRP version and type"}},
193
194                 { &hf_vrrp_version,
195                   {"VRRP protocol version", "vrrp.version",
196                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
197                    "VRRP version"}},
198
199                 { &hf_vrrp_type,
200                   {"VRRP packet type", "vrrp.type",
201                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
202                    "VRRP type"}}
203         };
204
205         static gint *ett[] = {
206                 &ett_vrrp,
207                 &ett_vrrp_ver_type
208         };
209
210         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol", "vrrp");
211         proto_register_field_array(proto_vrrp, hf, array_length(hf));
212         proto_register_subtree_array(ett, array_length(ett));
213
214         return;
215 }
216
217 void
218 proto_reg_handoff_vrrp(void)
219 {
220         dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp);
221 }