Use "proto_tree_add_item()" whenever possible; this fixes some bugs
[obnox/wireshark/wip.git] / packet-ucp.c
index 886d53fbe37c4ad3b44595ac4b7d5f367032a6de..ff5b9ed4484601f5315a95893ec9cc50edc79778 100644 (file)
@@ -2,7 +2,7 @@
  * Routines for Universal Computer Protocol dissection
  * Copyright 2001, Tom Uijldert <tom.uijldert@cmg.nl>
  *
- * $Id: packet-ucp.c,v 1.1 2001/10/08 17:30:23 guy Exp $
+ * $Id: packet-ucp.c,v 1.9 2001/12/17 23:17:34 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -57,6 +57,9 @@
 #include "packet.h"
 /* #include "packet-ucp.h" */                  /* We autoregister      */
 
+/* Prototypes  */
+static void dissect_ucp(tvbuff_t *, packet_info *, proto_tree *);
+
 /*
  * Convert ASCII-hex character to binary equivalent. No checks, assume
  * is valid hex character.
@@ -625,10 +628,6 @@ static const value_string vals_xser_service[] = {
  * \retval     UCP_SHORTENED   Packet may be there, but not complete
  * \retval     UCP_MALFORMED   Hmmmm, not UCP after all...
  * \retval     UCP_INV_CHK     Nice packet, but checksum doesn't add up...
- *
- * Note that this will not recognize any frames as UCP frames if the
- * capture was made with a snapshot length shorter than the maximum
- * frame length, as it checksums the entire packet.
  */
 static int
 check_ucp(tvbuff_t *tvb, int *endpkt)
@@ -638,9 +637,6 @@ check_ucp(tvbuff_t *tvb, int *endpkt)
     int                 pkt_check, tmp;
     int                 length;
 
-    if (!tvb_offset_exists(tvb, 0) || tvb_get_guint8(tvb, 0) != UCP_STX)
-       return UCP_MALFORMED;
-
     length = tvb_find_guint8(tvb, offset, -1, UCP_ETX);
     if (length == -1) {
        *endpkt = tvb_reported_length_remaining(tvb, offset);
@@ -650,10 +646,6 @@ check_ucp(tvbuff_t *tvb, int *endpkt)
        *endpkt = 0;
        return UCP_MALFORMED;
     }
-    if (length > (int) tvb_length(tvb)) {
-       *endpkt = 0;
-       return UCP_MALFORMED;
-    }
     for (; offset < (guint) (length - 2); offset++)
        checksum += tvb_get_guint8(tvb, offset);
     checksum &= 0xFF;
@@ -681,8 +673,10 @@ ucp_mktime(char *datestr)
     struct tm   r_time;
 
     r_time.tm_mday = 10 * (datestr[0] - '0') + (datestr[1] - '0');
-    r_time.tm_mon  = 10 * (datestr[2] - '0') + (datestr[3] - '0');
+    r_time.tm_mon  = (10 * (datestr[2] - '0') + (datestr[3] - '0')) - 1;
     r_time.tm_year = 10 * (datestr[4] - '0') + (datestr[5] - '0');
+    if (r_time.tm_year < 90) 
+       r_time.tm_year += 100;
     r_time.tm_hour = 10 * (datestr[6] - '0') + (datestr[7] - '0');
     r_time.tm_min  = 10 * (datestr[8] - '0') + (datestr[9] - '0');
     if (datestr[10])
@@ -691,7 +685,7 @@ ucp_mktime(char *datestr)
 }
 
 /*!
- * Scanning rouines to add standard types (byte, int, string, data)
+ * Scanning routines to add standard types (byte, int, string, data)
  * to the protocol-tree. Each field is seperated with a slash ('/').
  *
  * \param      tree    The protocol tree to add to
@@ -1457,7 +1451,9 @@ add_5xO(proto_tree *tree, tvbuff_t *tvb)
     UcpHandleByte(hf_ucp_parm_Dst);
     UcpHandleInt(hf_ucp_parm_Rsn);
     UcpHandleTime(hf_ucp_parm_DSCTS);
-    ucp_handle_mt(tree, tvb, &offset);
+    UcpHandleByte(hf_ucp_parm_MT);
+    UcpHandleString(hf_ucp_parm_NB);
+    UcpHandleData(hf_ucp_data_section);
     UcpHandleByte(hf_ucp_parm_MMS);
     UcpHandleByte(hf_ucp_parm_PR);
     UcpHandleByte(hf_ucp_parm_DCs);
@@ -1529,22 +1525,23 @@ add_6xO(proto_tree *tree, tvbuff_t *tvb, guint8 OT)
 #undef UcpHandleData
 
 /* Code to actually dissect the packets */
+/*
+ * Overlapping data for these functions
+ */
+static int      result, endpkt;
+
+/*
+ * The heuristic dissector
+ */
 static gboolean
-dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+dissect_ucp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-    int                 offset = 0;    /* Offset in packet within tvbuff       */
-    int                 tmpoff;        /* Local offset value (per field)       */
     guint8      O_R;           /* Request or response                  */
-    guint8      OT;            /* Operation type                       */
-    guint       intval;
-    int                 result, endpkt;
-    int                 i;
 
-    /* Set up structures needed to add the protocol subtree and manage it */
-    proto_item *ti;
-    proto_item *sub_ti;
-    proto_tree *ucp_tree;
-    tvbuff_t   *tmp_tvb;
+    /* This runs atop TCP, so we are guaranteed that there is at least one
+       byte in the tvbuff. */
+    if (tvb_get_guint8(tvb, 0) != UCP_STX)
+       return FALSE;
 
     result = check_ucp(tvb, &endpkt);
 
@@ -1562,6 +1559,33 @@ dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     O_R = tvb_get_guint8(tvb, UCP_O_R_OFFSET);
     if (match_strval(O_R, vals_hdr_O_R) == NULL)
        return FALSE;
+    /*
+     * Ok, looks like a valid packet, go dissect.
+     */
+    dissect_ucp(tvb, pinfo, tree);
+    return TRUE;
+}
+
+/*
+ * The actual dissector
+ */
+static void
+dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
+    int                 offset = 0;    /* Offset in packet within tvbuff       */
+    int                 tmpoff;        /* Local offset value (per field)       */
+    guint8      O_R;           /* Request or response                  */
+    guint8      OT;            /* Operation type                       */
+    guint       intval;
+    int                 i;
+
+    /* Set up structures needed to add the protocol subtree and manage it */
+    proto_item *ti;
+    proto_item *sub_ti;
+    proto_tree *ucp_tree;
+    tvbuff_t   *tmp_tvb;
+
+    O_R = tvb_get_guint8(tvb, UCP_O_R_OFFSET);
     /*
      * So do an atoi() on the operation type
      */
@@ -1569,18 +1593,18 @@ dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     OT  = 10 * OT + (tvb_get_guint8(tvb, UCP_OT_OFFSET + 1) - '0');
 
     /* Make entries in Protocol column and Info column on summary display */
-    if (check_col(pinfo->fd, COL_PROTOCOL))
-           col_set_str(pinfo->fd, COL_PROTOCOL, "UCP");
+    if (check_col(pinfo->cinfo, COL_PROTOCOL))
+           col_set_str(pinfo->cinfo, COL_PROTOCOL, "UCP");
 
-    if (check_col(pinfo->fd, COL_INFO)) {
-       col_clear(pinfo->fd, COL_INFO);
-       col_append_fstr(pinfo->fd, COL_INFO, "%s (%s)",
+    if (check_col(pinfo->cinfo, COL_INFO)) {
+       col_clear(pinfo->cinfo, COL_INFO);
+       col_append_fstr(pinfo->cinfo, COL_INFO, "%s (%s)",
                     val_to_str(OT,  vals_hdr_OT,  "unknown operation"),
                     match_strval(O_R, vals_hdr_O_R));
        if (result == UCP_SHORTENED)
-           col_append_str(pinfo->fd, COL_INFO, " [short packet]");
+           col_append_str(pinfo->cinfo, COL_INFO, " [short packet]");
        else if (result == UCP_INV_CHK)
-           col_append_str(pinfo->fd, COL_INFO, " [checksum invalid]");
+           col_append_str(pinfo->cinfo, COL_INFO, " [checksum invalid]");
     }
 
     /* In the interest of speed, if "tree" is NULL, don't do any work not
@@ -1627,7 +1651,7 @@ dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
         * Variable part starts here. Don't dissect if not complete.
         */
        if (result == UCP_SHORTENED)
-           return TRUE;
+           return;
        tmp_tvb = tvb_new_subset(tvb, offset, -1, -1);
        sub_ti = proto_tree_add_item(ucp_tree, hf_ucp_oper_section, tvb,
                                     offset, endpkt - offset, FALSE);
@@ -1722,7 +1746,7 @@ dissect_ucp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                break;
        }
     }
-    return TRUE;
+    return;
 }
 
 /* Register the protocol with Ethereal */
@@ -2476,9 +2500,17 @@ proto_register_ucp(void)
 void
 proto_reg_handoff_ucp(void)
 {
+    dissector_handle_t ucp_handle;
+
     /*
      * UCP can be spoken on any port so, when not on a specific port, try this
      * one whenever TCP is spoken.
      */
-    heur_dissector_add("tcp", dissect_ucp, proto_ucp);
+    heur_dissector_add("tcp", dissect_ucp_heur, proto_ucp);
+
+    /*
+     * Also register as one that can be selected by a TCP port number.
+     */
+    ucp_handle = create_dissector_handle(dissect_ucp, proto_ucp);
+    dissector_add_handle("tcp.port", ucp_handle);
 }