Tvbuffify the VRRP dissector, and add code to check the checksum.
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 6 Jan 2001 05:43:13 +0000 (05:43 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 6 Jan 2001 05:43:13 +0000 (05:43 +0000)
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@2836 f5534014-38df-0310-8fa8-9805f1628bb7

packet-vrrp.c

index fcecdb0b710c2e3ad7cdaae9a3c4ec4d306bd1d9..5aa53e0f0223a6790c7b67790e237a535bd808b9 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Heikki Vatiainen <hessu@cs.tut.fi>
  *
- * $Id: packet-vrrp.c,v 1.11 2001/01/03 16:41:07 gram Exp $
+ * $Id: packet-vrrp.c,v 1.12 2001/01/06 05:43:13 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -42,6 +42,7 @@
 #include <glib.h>
 #include "packet.h"
 #include "packet-ip.h"
+#include "in_cksum.h"
 
 static gint proto_vrrp = -1;
 static gint ett_vrrp = -1;
@@ -51,21 +52,9 @@ static gint hf_vrrp_ver_type = -1;
 static gint hf_vrrp_version = -1;
 static gint hf_vrrp_type = -1;
 
-struct vrrp_header {
 #define VRRP_VERSION_MASK 0xf0
 #define VRRP_TYPE_MASK 0x0f
-        guint8  ver_type;
-        guint8  vrouter_id;
-        guint8  priority;
-        guint8  count_ip_addrs;
-        guint8  auth_type;
-        guint8  adver_int;
-        guint16 checksum;
-        /* One or more IP addresses */
-        /* 8 octets of authentication data */
 #define VRRP_AUTH_DATA_LEN 8
-};
-
 
 #define VRRP_TYPE_ADVERTISEMENT 1
 static const value_string vrrp_type_vals[] = {
@@ -96,96 +85,122 @@ static const value_string vrrp_prio_vals[] = {
 
 
 static void
-dissect_vrrp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+dissect_vrrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-        struct vrrp_header vrh;
-        gboolean short_hdr = FALSE;
-        gboolean short_packet = FALSE;
-        guint calculated_len = -1; /* initialize to silence false warning from gcc */
-
-       OLD_CHECK_DISPLAY_AS_DATA(proto_vrrp, pd, offset, fd, tree);
-
-        if (sizeof(struct vrrp_header) > END_OF_FRAME)
-                short_hdr = short_packet = TRUE;
-        else {
-                memcpy(&vrh, pd + offset, sizeof(struct vrrp_header));
-                calculated_len = sizeof(struct vrrp_header) + vrh.count_ip_addrs*4 + VRRP_AUTH_DATA_LEN;
-                if (calculated_len > END_OF_FRAME)
-                        short_packet = TRUE;
-        }
+        int offset = 0;
+        gint vrrp_len;
+        guint8  ver_type;
+       vec_t cksum_vec[1];
+
+        CHECK_DISPLAY_AS_DATA(proto_vrrp, tvb, pinfo, tree);
+
+        pinfo->current_proto = "VRRP";
 
-        if (check_col(fd, COL_PROTOCOL))
-                col_set_str(fd, COL_PROTOCOL, "VRRP");
+        if (check_col(pinfo->fd, COL_PROTOCOL))
+                col_set_str(pinfo->fd, COL_PROTOCOL, "VRRP");
+        if (check_col(pinfo->fd, COL_INFO))
+                col_clear(pinfo->fd, COL_INFO);
         
-        if (check_col(fd, COL_INFO)) {
-                if (short_hdr)
-                        col_add_fstr(fd, COL_INFO, "Short packet header, length %u", END_OF_FRAME);
-                else if (short_packet)
-                        col_add_fstr(fd, COL_INFO, "Packet length mismatch, calculated %u, real %u",
-                                     calculated_len, END_OF_FRAME);
-                else
-                        col_add_fstr(fd, COL_INFO, "%s (v%u)", "Announcement", hi_nibble(vrh.ver_type));
+       ver_type = tvb_get_guint8(tvb, 0);
+        if (check_col(pinfo->fd, COL_INFO)) {
+                col_add_fstr(pinfo->fd, COL_INFO, "%s (v%u)",
+                             "Announcement", hi_nibble(ver_type));
         }
 
         if (tree) {
                 proto_item *ti, *tv;
                 proto_tree *vrrp_tree, *ver_type_tree;
-                guint8 ip_count, auth_len, auth_buf[VRRP_AUTH_DATA_LEN+1];
+                guint8 priority, ip_count, auth_type, adver_int;
+                guint16 cksum, computed_cksum;
+                guint8 auth_buf[VRRP_AUTH_DATA_LEN+1];
 
-                if (short_hdr) {
-                        old_dissect_data(pd, offset, fd, tree);
-                        return;
-                }
-
-                ti = proto_tree_add_item(tree, proto_vrrp, NullTVB, offset, END_OF_FRAME, FALSE);
+                ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0,
+                                         tvb_length(tvb), FALSE);
                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
 
-                tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type, NullTVB, offset, 1,
-                                                vrh.ver_type, "Version %u, Packet type %u (%s)",
-                                                hi_nibble(vrh.ver_type), lo_nibble(vrh.ver_type),
-                                                val_to_str(lo_nibble(vrh.ver_type), vrrp_type_vals, "Unknown"));
+                tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
+                                                tvb, offset, 1, ver_type,
+                                                "Version %u, Packet type %u (%s)",
+                                                hi_nibble(ver_type), lo_nibble(ver_type),
+                                                val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
-                proto_tree_add_uint(ver_type_tree, hf_vrrp_version, NullTVB, offset, 1, vrh.ver_type);
-                proto_tree_add_uint(ver_type_tree, hf_vrrp_type, NullTVB, offset, 1, vrh.ver_type);
+                proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
+                                    offset, 1, ver_type);
+                proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
+                                    ver_type);
                 offset++;
                 
-                proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Virtual Router ID: %u", vrh.vrouter_id);
-                proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Priority: %u (%s)", vrh.priority,
-                                    val_to_str(vrh.priority, vrrp_prio_vals, "Non-default backup priority"));
-                proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Count IP Addrs: %u", vrh.count_ip_addrs);
-                proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Authentication Type: %u (%s)", vrh.auth_type,
-                                    val_to_str(vrh.auth_type, vrrp_auth_vals, "Unknown"));
-                proto_tree_add_text(vrrp_tree, NullTVB, offset++, 1, "Advertisement Interval: %u second%s",
-                                    vrh.adver_int, plurality(vrh.adver_int, "", "s"));
-                proto_tree_add_text(vrrp_tree, NullTVB, offset, 2, "Checksum: 0x%x", htons(vrh.checksum));
-                offset+=2;
+                proto_tree_add_text(vrrp_tree, tvb, offset, 1,
+                                    "Virtual Router ID: %u",
+                                    tvb_get_guint8(tvb, offset));
+                offset++;
+
+                priority = tvb_get_guint8(tvb, offset);
+                proto_tree_add_text(vrrp_tree, tvb, offset, 1, "Priority: %u (%s)",
+                                    priority,
+                                    val_to_str(priority, vrrp_prio_vals, "Non-default backup priority"));
+                offset++;
 
-                if (short_packet) {
-                        old_dissect_data(pd, offset, fd, vrrp_tree);
-                        return;
+                ip_count = tvb_get_guint8(tvb, offset);
+                proto_tree_add_text(vrrp_tree, tvb, offset, 1,
+                                    "Count IP Addrs: %u", ip_count);
+                offset++;
+
+                auth_type = tvb_get_guint8(tvb, offset);
+                proto_tree_add_text(vrrp_tree, tvb, offset, 1,
+                                    "Authentication Type: %u (%s)", auth_type,
+                                    val_to_str(auth_type, vrrp_auth_vals, "Unknown"));
+                offset++;
+
+                adver_int = tvb_get_guint8(tvb, offset);
+                proto_tree_add_text(vrrp_tree, tvb, offset, 1,
+                                    "Advertisement Interval: %u second%s",
+                                    adver_int, plurality(adver_int, "", "s"));
+                offset++;
+
+                cksum = tvb_get_ntohs(tvb, offset);
+                vrrp_len = tvb_reported_length(tvb);
+                if (!pinfo->fragmented && tvb_length(tvb) >= vrrp_len) {
+                        /* The packet isn't part of a fragmented datagram
+                           and isn't truncated, so we can checksum it. */
+                        cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
+                        cksum_vec[0].len = vrrp_len;
+                        computed_cksum = in_cksum(&cksum_vec[0], 1);
+                        if (computed_cksum == 0) {
+                                proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                                    "Checksum: 0x%04x (correct)",
+                                                    cksum);
+                        } else {
+                                proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                                    "Checksum: 0x%04x (incorrect, should be 0x%04x)",
+                                                    cksum,
+                                                    in_cksum_shouldbe(cksum, computed_cksum));
+                        }
+                } else {
+                        proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                            "Checksum: 0x%04x", cksum);
                 }
-                
-                ip_count = vrh.count_ip_addrs;
+                offset+=2;
+
                 while (ip_count > 0) {
-                        proto_tree_add_text(vrrp_tree, NullTVB, offset, 4, "Virtual Router IP address: %s",
-                                            ip_to_str(pd+offset));
+                        proto_tree_add_text(vrrp_tree, tvb, offset, 4,
+                                            "Virtual Router IP address: %s",
+                                            ip_to_str(tvb_get_ptr(tvb, offset, 4)));
                         offset+=4;
                         ip_count--;
                 }
 
-                if (vrh.auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
+                if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
                         return; /* Contents of the authentication data is undefined */
 
-                strncpy(auth_buf, pd+offset, VRRP_AUTH_DATA_LEN);
-                auth_buf[VRRP_AUTH_DATA_LEN] = '\0';
-                auth_len = strlen(auth_buf);
-                if (auth_len > 0)
-                        proto_tree_add_text(vrrp_tree, NullTVB, offset, auth_len, "Authentication string: `%s'", auth_buf);
+                tvb_get_nstringz0(tvb, offset, VRRP_AUTH_DATA_LEN, auth_buf);
+                if (auth_buf[0] != '\0')
+                        proto_tree_add_text(vrrp_tree, tvb, offset,
+                                            VRRP_AUTH_DATA_LEN,
+                                            "Authentication string: `%s'",
+                                            auth_buf);
                 offset+=8;
-
         }
-
-        return;
 }
 
 void proto_register_vrrp(void)
@@ -223,5 +238,5 @@ void proto_register_vrrp(void)
 void
 proto_reg_handoff_vrrp(void)
 {
-       old_dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp);
+        dissector_add("ip.proto", IP_PROTO_VRRP, dissect_vrrp);
 }