Use "get_basename()" rather than finding the last component of "argv[0]"
[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.2 2000/01/07 22:05:42 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
45 static gint proto_vrrp = -1;
46 static gint ett_vrrp = -1;
47 static gint ett_vrrp_ver_type = -1;
48
49 static gint hf_vrrp_ver_type = -1;
50 static gint hf_vrrp_version = -1;
51 static gint hf_vrrp_type = -1;
52
53 struct vrrp_header {
54 #define VRRP_VERSION_MASK 0xf0
55 #define VRRP_TYPE_MASK 0x0f
56         guint8  ver_type;
57         guint8  vrouter_id;
58         guint8  priority;
59         guint8  count_ip_addrs;
60         guint8  auth_type;
61         guint8  adver_int;
62         guint16 checksum;
63         /* One or more IP addresses */
64         /* 8 octets of authentication data */
65 #define VRRP_AUTH_DATA_LEN 8
66 };
67
68
69 #define VRRP_TYPE_ADVERTISEMENT 1
70 static const value_string vrrp_type_vals[] = {
71         {VRRP_TYPE_ADVERTISEMENT, "Advertisement"}
72 };
73
74 #define VRRP_AUTH_TYPE_NONE 0
75 #define VRRP_AUTH_TYPE_SIMPLE_TEXT 1
76 #define VRRP_AUTH_TYPE_IP_AUTH_HDR 2
77 static const value_string vrrp_auth_vals[] = {
78         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
79         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
80         {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"}
81 };
82
83 #define VRRP_PRIORITY_MASTER_STOPPING 0
84 /* Values between 1 and 254 inclusive are for backup VRRP routers */
85 #define VRRP_PRIORITY_DEFAULT 100
86 #define VRRP_PRIORITY_OWNER 255
87 static const value_string vrrp_prio_vals[] = {
88         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
89         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
90         {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"}
91 };
92
93
94 void dissect_vrrp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
95 {
96         struct vrrp_header vrh;
97         gboolean short_hdr = FALSE;
98         gboolean short_packet = FALSE;
99         guint calculated_len = -1; /* initialize to silence false warning from gcc */
100
101         if (sizeof(struct vrrp_header) > END_OF_FRAME)
102                 short_hdr = short_packet = TRUE;
103         else {
104                 memcpy(&vrh, pd + offset, sizeof(struct vrrp_header));
105                 calculated_len = sizeof(struct vrrp_header) + vrh.count_ip_addrs*4 + VRRP_AUTH_DATA_LEN;
106                 if (calculated_len > END_OF_FRAME)
107                         short_packet = TRUE;
108         }
109
110         if (check_col(fd, COL_PROTOCOL))
111                 col_add_str(fd, COL_PROTOCOL, "VRRP");
112         
113         if (check_col(fd, COL_INFO)) {
114                 if (short_hdr)
115                         col_add_fstr(fd, COL_INFO, "Short packet header, length %u", END_OF_FRAME);
116                 else if (short_packet)
117                         col_add_fstr(fd, COL_INFO, "Packet length mismatch, calculated %u, real %u",
118                                      calculated_len, END_OF_FRAME);
119                 else
120                         col_add_fstr(fd, COL_INFO, "%s (v%u)", "Announcement", hi_nibble(vrh.ver_type));
121         }
122
123         if (tree) {
124                 proto_item *ti, *tv;
125                 proto_tree *vrrp_tree, *ver_type_tree;
126                 guint8 ip_count, auth_len, auth_buf[VRRP_AUTH_DATA_LEN+1];
127
128                 if (short_hdr) {
129                         dissect_data(pd, offset, fd, tree);
130                         return;
131                 }
132
133                 ti = proto_tree_add_item(tree, proto_vrrp, offset, END_OF_FRAME, NULL);
134                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
135
136                 tv = proto_tree_add_item_format(vrrp_tree, hf_vrrp_ver_type, offset, 1,
137                                                 vrh.ver_type, "Version %u, Packet type %u (%s)",
138                                                 hi_nibble(vrh.ver_type), lo_nibble(vrh.ver_type),
139                                                 val_to_str(lo_nibble(vrh.ver_type), vrrp_type_vals, "Unknown"));
140                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
141                 proto_tree_add_item(ver_type_tree, hf_vrrp_version, offset, 1, vrh.ver_type);
142                 proto_tree_add_item(ver_type_tree, hf_vrrp_type, offset, 1, vrh.ver_type);
143                 offset++;
144                 
145                 proto_tree_add_text(vrrp_tree, offset++, 1, "Virtual Router ID: %u", vrh.vrouter_id);
146                 proto_tree_add_text(vrrp_tree, offset++, 1, "Priority: %u (%s)", vrh.priority,
147                                     val_to_str(vrh.priority, vrrp_prio_vals, "Non-default backup priority"));
148                 proto_tree_add_text(vrrp_tree, offset++, 1, "Count IP Addrs: %u", vrh.count_ip_addrs);
149                 proto_tree_add_text(vrrp_tree, offset++, 1, "Authentication Type: %u (%s)", vrh.auth_type,
150                                     val_to_str(vrh.auth_type, vrrp_auth_vals, "Unknown"));
151                 proto_tree_add_text(vrrp_tree, offset++, 1, "Advertisement Interval: %u second%s",
152                                     vrh.adver_int, plurality(vrh.adver_int, "", "s"));
153                 proto_tree_add_text(vrrp_tree, offset, 2, "Checksum: 0x%x", htons(vrh.checksum));
154                 offset+=2;
155
156                 if (short_packet) {
157                         dissect_data(pd, offset, fd, vrrp_tree);
158                         return;
159                 }
160                 
161                 ip_count = vrh.count_ip_addrs;
162                 while (ip_count > 0) {
163                         proto_tree_add_text(vrrp_tree, offset, 4, "Virtual Router IP address: %s",
164                                             ip_to_str(pd+offset));
165                         offset+=4;
166                         ip_count--;
167                 }
168
169                 if (vrh.auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
170                         return; /* Contents of the authentication data is undefined */
171
172                 strncpy(auth_buf, pd+offset, VRRP_AUTH_DATA_LEN);
173                 auth_buf[VRRP_AUTH_DATA_LEN] = '\0';
174                 auth_len = strlen(auth_buf);
175                 if (auth_len > 0)
176                         proto_tree_add_text(vrrp_tree, offset, auth_len, "Authentication string: `%s'", auth_buf);
177                 offset+=8;
178
179         }
180
181         return;
182 }
183
184 void proto_register_vrrp(void)
185 {
186         static hf_register_info hf[] = {
187                 { &hf_vrrp_ver_type,
188                   {"VRRP message version and type", "vrrp.typever",
189                    FT_UINT8, BASE_DEC, NULL, 0x0,
190                    "VRRP version and type"}},
191
192                 { &hf_vrrp_version,
193                   {"VRRP protocol version", "vrrp.version",
194                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
195                    "VRRP version"}},
196
197                 { &hf_vrrp_type,
198                   {"VRRP packet type", "vrrp.type",
199                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
200                    "VRRP type"}}
201         };
202
203         static gint *ett[] = {
204                 &ett_vrrp,
205                 &ett_vrrp_ver_type
206         };
207
208         proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol", "vrrp");
209         proto_register_field_array(proto_vrrp, hf, array_length(hf));
210         proto_register_subtree_array(ett, array_length(ett));
211
212         return;
213 }