Call subdissectors even if we're not building a protocol tree.
[obnox/wireshark/wip.git] / packet-tpkt.c
index d6f261bbcae8341f7a09460997cc069d3006d6d3..b50ea771c8a3380436be58fc9d26bddadd0bec9c 100644 (file)
@@ -7,22 +7,22 @@
  * Routine to dissect RFC 1006 TPKT packet containing OSI TP PDU
  * Copyright 2001, Martin Thomas <Martin_A_Thomas@yahoo.com>
  *
- * $Id: packet-tpkt.c,v 1.17 2002/03/05 22:15:21 guy Exp $
+ * $Id: packet-tpkt.c,v 1.24 2003/12/02 18:50:52 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <glib.h>
 #include <epan/packet.h>
 
-#ifdef HAVE_SYS_TYPES_H
-#  include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-#  include <netinet/in.h>
-#endif
-
 #include <stdio.h>
 #include <string.h>
 
@@ -52,6 +44,7 @@
 
 /* TPKT header fields             */
 static int proto_tpkt          = -1;
+static protocol_t *proto_tpkt_ptr;
 static int hf_tpkt_version     = -1;
 static int hf_tpkt_reserved    = -1;
 static int hf_tpkt_length      = -1;
@@ -65,21 +58,27 @@ static gboolean tpkt_desegment = TRUE;
 #define TCP_PORT_TPKT  102
 
 /* find the dissector for OSI TP (aka COTP) */
-static dissector_handle_t osi_tp_handle; 
+static dissector_handle_t osi_tp_handle;
 
 /*
  * Check whether this could be a TPKT-encapsulated PDU.
  * Returns -1 if it's not, and the PDU length from the TPKT header
  * if it is.
+ *
+ * "min_len" is the minimum length of the PDU; the length field in the
+ * TPKT header must be at least "4+min_len" in order for this to be a
+ * valid TPKT PDU for the protocol in question.
  */
 int
-is_tpkt(tvbuff_t *tvb)
+is_tpkt(tvbuff_t *tvb, int min_len)
 {
+       guint16 pkt_len;
+
        /*
         * If TPKT is disabled, don't dissect it, just return -1, meaning
         * "this isn't TPKT".
         */
-       if (!proto_is_protocol_enabled(proto_tpkt))
+       if (!proto_is_protocol_enabled(proto_tpkt_ptr))
                return -1;
 
        /* There should at least be 4 bytes left in the frame */
@@ -87,17 +86,25 @@ is_tpkt(tvbuff_t *tvb)
                return -1;      /* there aren't */
 
        /*
-        * The first octet should be 3 and the second one should be 0 
-        * The H.323 implementers guide suggests that this might not 
+        * The first octet should be 3 and the second one should be 0
+        * The H.323 implementers guide suggests that this might not
         * always be the case....
         */
        if (!(tvb_get_guint8(tvb, 0) == 3 && tvb_get_guint8(tvb, 1) == 0))
-               return -1;      /* They're not */
+               return -1;      /* they're not */
+
+       /*
+        * Get the length from the TPKT header.  Make sure it's large
+        * enough.
+        */
+       pkt_len = tvb_get_ntohs(tvb, 2);
+       if (pkt_len < 4 + min_len)
+               return -1;      /* it's not */
 
        /*
-        * Return the length from the TPKT header.
+        * Return the length from the header.
         */
-       return tvb_get_ntohs(tvb, 2);
+       return pkt_len;
 }
 
 /*
@@ -116,7 +123,46 @@ dissect_tpkt_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        tvbuff_t *next_tvb;
        const char *saved_proto;
 
+       /*
+        * If we're reassembling segmented TPKT PDUs, empty the COL_INFO
+        * column, so subdissectors can append information
+        * without having to worry about emptying the column.
+        *
+        * We use "col_add_str()" because the subdissector
+        * might be appending information to the column, in
+        * which case we'd have to zero the buffer out explicitly
+        * anyway.
+        */
+       if (tpkt_desegment && check_col(pinfo->cinfo, COL_INFO))
+               col_add_str(pinfo->cinfo, COL_INFO, "");
+
        while (tvb_reported_length_remaining(tvb, offset) != 0) {
+               /*
+                * Is the first byte of this putative TPKT header
+                * a valid TPKT version number, i.e. 3?
+                */
+               if (tvb_get_guint8(tvb, offset) != 3) {
+                       /*
+                        * No, so don't assume this is a TPKT header;
+                        * we might be in the middle of TPKT data,
+                        * so don't get the length and don't try to
+                        * do reassembly.
+                        */
+                       if (check_col(pinfo->cinfo, COL_PROTOCOL))
+                               col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
+                       if (check_col(pinfo->cinfo, COL_INFO))
+                               col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
+                       if (tree) {
+                               ti = proto_tree_add_item(tree, proto_tpkt, tvb,
+                                   offset, -1, FALSE);
+                               tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
+
+                               proto_tree_add_text(tpkt_tree, tvb, offset, -1,
+                                   "Continuation data");
+                       }
+                       return;
+               }
+
                length_remaining = tvb_length_remaining(tvb, offset);
 
                /*
@@ -176,7 +222,17 @@ dissect_tpkt_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
 
                if (check_col(pinfo->cinfo, COL_PROTOCOL))
                        col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
-               if (check_col(pinfo->cinfo, COL_INFO)) {
+               /*
+                * Don't add the TPKT header information if we're
+                * reassembling segmented TPKT PDUs or if this
+                * PDU isn't reassembled.
+                *
+                * XXX - the first is so that subdissectors can append
+                * information without getting TPKT stuff in the middle;
+                * why the second?
+                */
+               if (!tpkt_desegment && !pinfo->fragmented
+                   && check_col(pinfo->cinfo, COL_INFO)) {
                        col_add_fstr(pinfo->cinfo, COL_INFO,
                            "TPKT Data length = %u", data_len);
                }
@@ -270,53 +326,54 @@ dissect_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 void
 proto_register_tpkt(void)
 {
-       static hf_register_info hf[] = 
+       static hf_register_info hf[] =
        {
-               { 
+               {
                        &hf_tpkt_version,
-                       { 
-                               "Version", 
-                               "tpkt.version", 
-                               FT_UINT8, 
-                               BASE_DEC, 
-                               NULL, 
+                       {
+                               "Version",
+                               "tpkt.version",
+                               FT_UINT8,
+                               BASE_DEC,
+                               NULL,
                                0x0,
-                               "", HFILL 
+                               "", HFILL
                        }
                },
-               { 
+               {
                        &hf_tpkt_reserved,
-                       { 
-                               "Reserved", 
-                               "tpkt.reserved", 
-                               FT_UINT8, 
-                               BASE_DEC, 
-                               NULL, 
+                       {
+                               "Reserved",
+                               "tpkt.reserved",
+                               FT_UINT8,
+                               BASE_DEC,
+                               NULL,
                                0x0,
-                               "", HFILL 
+                               "", HFILL
                        }
                },
-               { 
+               {
                        &hf_tpkt_length,
-                       { 
-                               "Length", 
-                               "tpkt.length", 
-                               FT_UINT16, 
-                               BASE_DEC, 
-                               NULL, 
+                       {
+                               "Length",
+                               "tpkt.length",
+                               FT_UINT16,
+                               BASE_DEC,
+                               NULL,
                                0x0,
-                               "", HFILL 
+                               "", HFILL
                        }
                },
        };
-       
-       static gint *ett[] = 
+
+       static gint *ett[] =
        {
                &ett_tpkt,
        };
        module_t *tpkt_module;
 
        proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
+       proto_tpkt_ptr = find_protocol_by_id(proto_tpkt);
        proto_register_field_array(proto_tpkt, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));