Ethereal-dev: [Ethereal-dev] Patch: Allow CHDLC and PPP_HDLC dissectors to share FCS decode ro

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Jesper Peterson <jesper@xxxxxxxxxx>
Date: Tue, 26 Aug 2003 17:11:51 +1200

Attached is a patch which extracts the FCS decoding section of the PPP_HDLC dissector to allow the CHDLC dissector to use the same routine. The ppp_options used for preferences has been renamed to fcs_options and exported via packet-ppp.h so CHDLC gets a separate (but identical) FCS preference. This means prefs.h has to be included before packet-ppp.h so a couple of ppp related files (packet-{gtp,null,raw,vj}.c) had their includes slightly re-arranged.

Also attached are a couple of test files for CHDLC and PPP_HDLC with 32-bit FCSs. The TCP CRCs are wrong in the data but the FCSs are correct. Our test gear is strange that way.

--
Jesper Peterson, Senior Software Developer
http://www.endace.com, +64 7 839 0540

Index: packet-chdlc.c
===================================================================
RCS file: /cvsroot/ethereal/packet-chdlc.c,v
retrieving revision 1.19
diff -u -r1.19 packet-chdlc.c
--- packet-chdlc.c	27 Jan 2003 19:28:52 -0000	1.19
+++ packet-chdlc.c	26 Aug 2003 05:08:05 -0000
@@ -29,9 +29,11 @@
 #include <glib.h>
 #include <epan/packet.h>
 #include "etypes.h"
+#include "prefs.h"
 #include "chdlctypes.h"
 #include <epan/resolv.h>
 #include "packet-chdlc.h"
+#include "packet-ppp.h"
 #include "packet-ip.h"
 
 /*
@@ -104,7 +106,7 @@
 
 void
 capture_chdlc( const guchar *pd, int offset, int len, packet_counts *ld ) {
-  if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
+  if (!BYTES_ARE_IN_FRAME(offset, len, 4)) {
     ld->other++;
     return;
   }
@@ -140,6 +142,8 @@
   }
 }
 
+static gint chdlc_fcs_decode = 0; /* 0 = No FCS, 1 = 16 bit FCS, 2 = 32 bit FCS */
+
 static void
 dissect_chdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
@@ -187,6 +191,8 @@
     proto_tree_add_uint(fh_tree, hf_chdlc_addr, tvb, 0, 1, addr);
   }
 
+  decode_fcs(tvb, fh_tree, chdlc_fcs_decode, 2);
+
   chdlctype(proto, tvb, 4, pinfo, tree, fh_tree, hf_chdlc_proto);
 }
 
@@ -205,6 +211,8 @@
     &ett_chdlc,
   };
 
+  module_t *chdlc_module;
+
   proto_chdlc = proto_register_protocol("Cisco HDLC", "CHDLC", "chdlc");
   proto_register_field_array(proto_chdlc, hf, array_length(hf));
   proto_register_subtree_array(ett, array_length(ett));
@@ -214,6 +222,17 @@
 	"Cisco HDLC frame type", FT_UINT16, BASE_HEX);
 
   register_dissector("chdlc", dissect_chdlc, proto_chdlc);
+
+  /* Register the preferences for the chdlc protocol */
+  chdlc_module = prefs_register_protocol(proto_chdlc, NULL);
+
+  prefs_register_enum_preference(chdlc_module,
+	"fcs_type",
+	"CHDLC Frame Checksum Type",
+	"The type of CHDLC frame checksum (none, 16-bit, 32-bit)",
+	&chdlc_fcs_decode,
+	fcs_options, FALSE);
+
 }
 
 void
Index: packet-gtp.c
===================================================================
RCS file: /cvsroot/ethereal/packet-gtp.c,v
retrieving revision 1.55
diff -u -r1.55 packet-gtp.c
--- packet-gtp.c	9 Jul 2003 07:12:23 -0000	1.55
+++ packet-gtp.c	26 Aug 2003 05:08:08 -0000
@@ -36,10 +36,10 @@
 #include <glib.h>
 
 #include <epan/packet.h>
+#include "prefs.h"
 #include "packet-gtp.h"
 #include "packet-ipv6.h"
 #include "packet-ppp.h"
-#include "prefs.h"
 
 static dissector_table_t ppp_subdissector_table;
 
Index: packet-null.c
===================================================================
RCS file: /cvsroot/ethereal/packet-null.c,v
retrieving revision 1.58
diff -u -r1.58 packet-null.c
--- packet-null.c	2 Jul 2003 00:08:17 -0000	1.58
+++ packet-null.c	26 Aug 2003 05:08:23 -0000
@@ -34,6 +34,7 @@
 #include <epan/packet.h>
 #include "packet-null.h"
 #include <epan/atalk-utils.h>
+#include "prefs.h"
 #include "packet-ip.h"
 #include "packet-ipx.h"
 #include "packet-osi.h"
Index: packet-ppp.c
===================================================================
RCS file: /cvsroot/ethereal/packet-ppp.c,v
retrieving revision 1.114
diff -u -r1.114 packet-ppp.c
--- packet-ppp.c	11 Jul 2003 09:30:48 -0000	1.114
+++ packet-ppp.c	26 Aug 2003 05:08:24 -0000
@@ -168,6 +168,14 @@
 #define NO_FCS 0
 #define FCS_16 1
 #define FCS_32 2
+
+const enum_val_t fcs_options[] = {
+  {"None", NO_FCS},
+  {"16-Bit", FCS_16},
+  {"32-Bit", FCS_32},
+  {NULL, -1}
+};
+
 gboolean ppp_vj_decomp = TRUE; /* Default to VJ header decompression */
 
 /*
@@ -1474,6 +1482,137 @@
     return (fcs ^ 0xffffffff);
 }
 
+tvbuff_t *
+decode_fcs(tvbuff_t *tvb, proto_tree *fh_tree, int fcs_decode, int proto_offset)
+{
+  tvbuff_t   *next_tvb;
+  gint       len, reported_len;
+  int        rx_fcs_offset;
+  guint32    rx_fcs_exp;
+  guint32    rx_fcs_got;
+
+  /*
+   * Remove the FCS, if any, from the packet data.
+   */
+  switch (fcs_decode) {
+
+  case NO_FCS:
+    next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
+    break;
+
+  case FCS_16:
+    /*
+     * Do we have the entire packet, and does it include a 2-byte FCS?
+     */
+    len = tvb_length_remaining(tvb, proto_offset);
+    reported_len = tvb_reported_length_remaining(tvb, proto_offset);
+    if (reported_len < 2 || len < 0) {
+      /*
+       * The packet is claimed not to even have enough data for a 2-byte FCS,
+       * or we're already past the end of the captured data.
+       * Don't slice anything off.
+       */
+      next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
+    } else if (len < reported_len) {
+      /*
+       * The packet is claimed to have enough data for a 2-byte FCS, but
+       * we didn't capture all of the packet.
+       * Slice off the 2-byte FCS from the reported length, and trim the
+       * captured length so it's no more than the reported length; that
+       * will slice off what of the FCS, if any, is in the captured
+       * length.
+       */
+      reported_len -= 2;
+      if (len > reported_len)
+        len = reported_len;
+      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
+    } else {
+      /*
+       * We have the entire packet, and it includes a 2-byte FCS.
+       * Slice it off.
+       */
+      len -= 2;
+      reported_len -= 2;
+      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
+
+      /*
+       * Compute the FCS and put it into the tree.
+       */
+      rx_fcs_offset = proto_offset + len;
+      rx_fcs_exp = fcs16(0xFFFF, tvb);
+      rx_fcs_got = tvb_get_letohs(tvb, rx_fcs_offset);
+      if (rx_fcs_got != rx_fcs_exp) {
+        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 2,
+                            "FCS 16: 0x%04x (incorrect, should be 0x%04x)",
+                            rx_fcs_got, rx_fcs_exp);
+      } else {
+        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 2,
+                            "FCS 16: 0x%04x (correct)",
+                            rx_fcs_got);
+      }
+    }
+    break;
+
+  case FCS_32:
+    /*
+     * Do we have the entire packet, and does it include a 4-byte FCS?
+     */
+    len = tvb_length_remaining(tvb, proto_offset);
+    reported_len = tvb_reported_length_remaining(tvb, proto_offset);
+    if (reported_len < 4) {
+      /*
+       * The packet is claimed not to even have enough data for a 4-byte FCS.
+       * Just pass on the tvbuff as is.
+       */
+      next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
+    } else if (len < reported_len) {
+      /*
+       * The packet is claimed to have enough data for a 4-byte FCS, but
+       * we didn't capture all of the packet.
+       * Slice off the 4-byte FCS from the reported length, and trim the
+       * captured length so it's no more than the reported length; that
+       * will slice off what of the FCS, if any, is in the captured
+       * length.
+       */
+      reported_len -= 4;
+      if (len > reported_len)
+        len = reported_len;
+      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
+    } else {
+      /*
+       * We have the entire packet, and it includes a 4-byte FCS.
+       * Slice it off.
+       */
+      len -= 4;
+      reported_len -= 4;
+      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
+
+      /*
+       * Compute the FCS and put it into the tree.
+       */
+      rx_fcs_offset = proto_offset + len;
+      rx_fcs_exp = fcs32(0xFFFFFFFF, tvb);
+      rx_fcs_got = tvb_get_letohl(tvb, rx_fcs_offset);
+      if (rx_fcs_got != rx_fcs_exp) {
+        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 4,
+                            "FCS 32: 0x%08x (incorrect, should be 0x%08x)",
+                            rx_fcs_got, rx_fcs_exp);
+      } else {
+        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 4,
+                            "FCS 32: 0x%08x (correct)",
+                            rx_fcs_got);
+      }
+    }
+    break;
+
+  default:
+   g_assert_not_reached();
+   next_tvb = NULL;
+  }
+
+  return next_tvb;
+}
+
 void
 capture_ppp_hdlc( const guchar *pd, int offset, int len, packet_counts *ld ) {
   if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
@@ -2827,11 +2966,7 @@
   proto_tree *fh_tree = NULL;
   guint8     byte0;
   int        proto_offset;
-  gint       len, reported_len;
   tvbuff_t  *next_tvb;
-  int        rx_fcs_offset;
-  guint32    rx_fcs_exp;
-  guint32    rx_fcs_got;
 
   byte0 = tvb_get_guint8(tvb, 0);
   if (byte0 == CHDLC_ADDR_UNICAST || byte0 == CHDLC_ADDR_MULTICAST) {
@@ -2892,124 +3027,7 @@
     }
   }
 
-  /*
-   * Remove the FCS, if any, from the packet data.
-   */
-  switch (ppp_fcs_decode) {
-
-  case NO_FCS:
-    next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
-    break;
-
-  case FCS_16:
-    /*
-     * Do we have the entire packet, and does it include a 2-byte FCS?
-     */
-    len = tvb_length_remaining(tvb, proto_offset);
-    reported_len = tvb_reported_length_remaining(tvb, proto_offset);
-    if (reported_len < 2 || len < 0) {
-      /*
-       * The packet is claimed not to even have enough data for a 2-byte FCS,
-       * or we're already past the end of the captured data.
-       * Don't slice anything off.
-       */
-      next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
-    } else if (len < reported_len) {
-      /*
-       * The packet is claimed to have enough data for a 2-byte FCS, but
-       * we didn't capture all of the packet.
-       * Slice off the 2-byte FCS from the reported length, and trim the
-       * captured length so it's no more than the reported length; that
-       * will slice off what of the FCS, if any, is in the captured
-       * length.
-       */
-      reported_len -= 2;
-      if (len > reported_len)
-        len = reported_len;
-      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
-    } else {
-      /*
-       * We have the entire packet, and it includes a 2-byte FCS.
-       * Slice it off.
-       */
-      len -= 2;
-      reported_len -= 2;
-      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
-
-      /*
-       * Compute the FCS and put it into the tree.
-       */
-      rx_fcs_offset = proto_offset + len;
-      rx_fcs_exp = fcs16(0xFFFF, tvb);
-      rx_fcs_got = tvb_get_letohs(tvb, rx_fcs_offset);
-      if (rx_fcs_got != rx_fcs_exp) {
-        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 2,
-                            "FCS 16: 0x%04x (incorrect, should be 0x%04x)",
-                            rx_fcs_got, rx_fcs_exp);
-      } else {
-        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 2,
-                            "FCS 16: 0x%04x (correct)",
-                            rx_fcs_got);
-      }
-    }
-    break;
-
-  case FCS_32:
-    /*
-     * Do we have the entire packet, and does it include a 4-byte FCS?
-     */
-    len = tvb_length_remaining(tvb, proto_offset);
-    reported_len = tvb_reported_length_remaining(tvb, proto_offset);
-    if (reported_len < 4) {
-      /*
-       * The packet is claimed not to even have enough data for a 4-byte FCS.
-       * Just pass on the tvbuff as is.
-       */
-      next_tvb = tvb_new_subset(tvb, proto_offset, -1, -1);
-    } else if (len < reported_len) {
-      /*
-       * The packet is claimed to have enough data for a 4-byte FCS, but
-       * we didn't capture all of the packet.
-       * Slice off the 4-byte FCS from the reported length, and trim the
-       * captured length so it's no more than the reported length; that
-       * will slice off what of the FCS, if any, is in the captured
-       * length.
-       */
-      reported_len -= 4;
-      if (len > reported_len)
-        len = reported_len;
-      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
-    } else {
-      /*
-       * We have the entire packet, and it includes a 4-byte FCS.
-       * Slice it off.
-       */
-      len -= 4;
-      reported_len -= 4;
-      next_tvb = tvb_new_subset(tvb, proto_offset, len, reported_len);
-
-      /*
-       * Compute the FCS and put it into the tree.
-       */
-      rx_fcs_offset = proto_offset + len;
-      rx_fcs_exp = fcs32(0xFFFFFFFF, tvb);
-      rx_fcs_got = tvb_get_letohl(tvb, rx_fcs_offset);
-      if (rx_fcs_got != rx_fcs_exp) {
-        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 4,
-                            "FCS 32: 0x%08x (incorrect, should be 0x%08x)",
-                            rx_fcs_got, rx_fcs_exp);
-      } else {
-        proto_tree_add_text(fh_tree, tvb, rx_fcs_offset, 4,
-                            "FCS 32: 0x%08x (correct)",
-                            rx_fcs_got);
-      }
-    }
-    break;
-
-  default:
-   g_assert_not_reached();
-   next_tvb = NULL;
-  }
+  next_tvb = decode_fcs(tvb, fh_tree, ppp_fcs_decode, proto_offset);
 
   dissect_ppp_common(next_tvb, pinfo, tree, fh_tree, ti);
 }
@@ -3262,13 +3280,6 @@
     &ett_ppp,
   };
 
-  static enum_val_t ppp_options[] = {
-    {"None", 0},
-    {"16-Bit", 1},
-    {"32-Bit", 2},
-    {NULL, -1}
-  };
-
   module_t *ppp_module;
 
   proto_ppp = proto_register_protocol("Point-to-Point Protocol", "PPP", "ppp");
@@ -3291,7 +3302,7 @@
 	"PPP Frame Checksum Type",
 	"The type of PPP frame checksum (none, 16-bit, 32-bit)",
 	&ppp_fcs_decode,
-	ppp_options, FALSE);
+	fcs_options, FALSE);
   prefs_register_bool_preference(ppp_module,
 	"decompress_vj",
 	"Decompress Van Jacobson-compressed frames",
Index: packet-ppp.h
===================================================================
RCS file: /cvsroot/ethereal/packet-ppp.h,v
retrieving revision 1.13
diff -u -r1.13 packet-ppp.h
--- packet-ppp.h	29 Apr 2003 17:56:49 -0000	1.13
+++ packet-ppp.h	26 Aug 2003 05:08:24 -0000
@@ -29,9 +29,16 @@
                                  TRUE  = Decompress VJ */
 void capture_ppp_hdlc(const guchar *, int, int, packet_counts *);
 
+tvbuff_t *decode_fcs(tvbuff_t *tvb, proto_tree *fh_tree, int fcs_decode, int proto_offset);
+
 /*
  * Used by the GTP dissector as well.
  */
 extern const value_string ppp_vals[];
+
+/*
+ * Used by CHDLC dissector as well.
+ */
+extern const enum_val_t fcs_options[];
 
 #endif
Index: packet-raw.c
===================================================================
RCS file: /cvsroot/ethereal/packet-raw.c,v
retrieving revision 1.35
diff -u -r1.35 packet-raw.c
--- packet-raw.c	28 Aug 2002 21:00:29 -0000	1.35
+++ packet-raw.c	26 Aug 2003 05:08:24 -0000
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <glib.h>
 #include <epan/packet.h>
+#include "prefs.h"
 #include "packet-raw.h"
 #include "packet-ip.h"
 #include "packet-ppp.h"
Index: packet-vj.c
===================================================================
RCS file: /cvsroot/ethereal/packet-vj.c,v
retrieving revision 1.16
diff -u -r1.16 packet-vj.c
--- packet-vj.c	28 Aug 2002 21:00:36 -0000	1.16
+++ packet-vj.c	26 Aug 2003 05:08:25 -0000
@@ -66,6 +66,7 @@
 #include <glib.h>
 #include <string.h>
 #include <epan/packet.h>
+#include "prefs.h"
 #include "packet-ppp.h"
 #include "ppptypes.h"
 #include "ipproto.h"

Attachment: pos-chdlc.pcap
Description: Binary data

Attachment: pos-ppp.pcap
Description: Binary data