For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-lsc.c
index 53ffaf1b92b099f9be1e8f96f58b83e5b6946d47..3d7c89c12528baee379813d3bb04e3b89768a950 100644 (file)
 # include "config.h"
 #endif
 
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 
 #include <glib.h>
 
 #include <epan/packet.h>
-#include <epan/emem.h>
 #include <epan/prefs.h>
 
 #include <packet-tcp.h>
@@ -136,8 +133,6 @@ static int hf_lsc_mode = -1;
 
 /* Preferences */
 static guint global_lsc_port = 0;
-static guint lsc_port = 0;
-
 
 /* Initialize the subtree pointers */
 static gint ett_lsc = -1;
@@ -153,16 +148,13 @@ dissect_lsc_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
   guint expected_len;
 
   /* Protocol is LSC, packet summary is not yet known */
-  if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
-    col_set_str(pinfo->cinfo, COL_PROTOCOL, "LSC");
-  if (check_col(pinfo->cinfo, COL_INFO))
-    col_clear(pinfo->cinfo, COL_INFO);
+  col_set_str(pinfo->cinfo, COL_PROTOCOL, "LSC");
+  col_clear(pinfo->cinfo, COL_INFO);
 
   /* Too little data? */
-  if (tvb->length < LSC_MIN_LEN)
+  if (tvb_length(tvb) < LSC_MIN_LEN)
   {
-    if (check_col(pinfo->cinfo, COL_INFO))
-      col_set_str(pinfo->cinfo, COL_INFO, "[Too short]");
+    col_set_str(pinfo->cinfo, COL_INFO, "[Too short]");
     return;
   }
 
@@ -213,15 +205,15 @@ dissect_lsc_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                  val_to_str(op_code, op_code_vals, "Unknown op code (0x%x)"),
                  stream);
 
-    if (tvb->length < expected_len)
+    if (tvb_length(tvb) < expected_len)
       col_append_str(pinfo->cinfo, COL_INFO, " [Too short]");
-    else if (tvb->length > expected_len)
+    else if (tvb_length(tvb) > expected_len)
       col_append_str(pinfo->cinfo, COL_INFO, " [Too long]");
   }
 
   if (tree) {
     /* Create display subtree for the protocol */
-    ti = proto_tree_add_item(tree, proto_lsc, tvb, 0, -1, FALSE);
+    ti = proto_tree_add_item(tree, proto_lsc, tvb, 0, -1, ENC_NA);
     lsc_tree = proto_item_add_subtree(ti, ett_lsc);
 
     /* Add already fetched items to the tree */
@@ -354,64 +346,64 @@ dissect_lsc_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 /* Register the protocol with Wireshark */
 void
 proto_register_lsc(void)
-{                 
+{
   module_t *lsc_module;
 
   /* Setup list of header fields */
   static hf_register_info hf[] = {
     { &hf_lsc_version,
       { "Version", "lsc.version",
-        FT_UINT8, BASE_DEC, NULL, 0,          
+        FT_UINT8, BASE_DEC, NULL, 0,
         "Version of the Pegasus LSC protocol", HFILL }
     },
     { &hf_lsc_trans_id,
       { "Transaction ID", "lsc.trans_id",
-        FT_UINT8, BASE_DEC, NULL, 0,          
-        "Transaction ID", HFILL }
+        FT_UINT8, BASE_DEC, NULL, 0,
+        NULL, HFILL }
     },
     { &hf_lsc_op_code,
       { "Op Code", "lsc.op_code",
-        FT_UINT8, BASE_HEX, VALS(op_code_vals), 0,          
+        FT_UINT8, BASE_HEX, VALS(op_code_vals), 0,
         "Operation Code", HFILL }
     },
     { &hf_lsc_status_code,
       { "Status Code", "lsc.status_code",
-        FT_UINT8, BASE_HEX, VALS(status_code_vals), 0,          
-        "Status Code", HFILL }
+        FT_UINT8, BASE_HEX, VALS(status_code_vals), 0,
+        NULL, HFILL }
     },
     { &hf_lsc_stream_handle,
       { "Stream Handle", "lsc.stream_handle",
-        FT_UINT32, BASE_DEC, NULL, 0,          
+        FT_UINT32, BASE_DEC, NULL, 0,
         "Stream identification handle", HFILL }
     },
     { &hf_lsc_start_npt,
       { "Start NPT", "lsc.start_npt",
-        FT_INT32, BASE_DEC, NULL, 0,          
+        FT_INT32, BASE_DEC, NULL, 0,
         "Start Time (milliseconds)", HFILL }
     },
     { &hf_lsc_stop_npt,
       { "Stop NPT", "lsc.stop_npt",
-        FT_INT32, BASE_DEC, NULL, 0,          
+        FT_INT32, BASE_DEC, NULL, 0,
         "Stop Time (milliseconds)", HFILL }
     },
     { &hf_lsc_current_npt,
       { "Current NPT", "lsc.current_npt",
-        FT_INT32, BASE_DEC, NULL, 0,          
+        FT_INT32, BASE_DEC, NULL, 0,
         "Current Time (milliseconds)", HFILL }
     },
     { &hf_lsc_scale_num,
       { "Scale Numerator", "lsc.scale_num",
-        FT_INT16, BASE_DEC, NULL, 0,          
-        "Scale Numerator", HFILL }
+        FT_INT16, BASE_DEC, NULL, 0,
+        NULL, HFILL }
     },
     { &hf_lsc_scale_denom,
       { "Scale Denominator", "lsc.scale_denum",
-        FT_UINT16, BASE_DEC, NULL, 0,          
-        "Scale Denominator", HFILL }
+        FT_UINT16, BASE_DEC, NULL, 0,
+        NULL, HFILL }
     },
     { &hf_lsc_mode,
       { "Server Mode", "lsc.mode",
-        FT_UINT8, BASE_HEX, VALS(mode_vals), 0,          
+        FT_UINT8, BASE_HEX, VALS(mode_vals), 0,
         "Current Server Mode", HFILL }
     }
   };
@@ -429,10 +421,10 @@ proto_register_lsc(void)
   proto_register_field_array(proto_lsc, hf, array_length(hf));
   proto_register_subtree_array(ett, array_length(ett));
 
-  /* Register preferences module */       
+  /* Register preferences module */
   lsc_module = prefs_register_protocol(proto_lsc, proto_reg_handoff_lsc);
-     
-  /* Register preferences */        
+
+  /* Register preferences */
   prefs_register_uint_preference(lsc_module, "port",
                            "LSC Port",
                            "Set the TCP or UDP port for Pegasus LSC messages",
@@ -442,15 +434,28 @@ proto_register_lsc(void)
 void
 proto_reg_handoff_lsc(void)
 {
-  dissector_handle_t lsc_udp_handle;
-  dissector_handle_t lsc_tcp_handle;
-
-  lsc_udp_handle = create_dissector_handle(dissect_lsc_udp, proto_lsc);
-  lsc_tcp_handle = create_dissector_handle(dissect_lsc_tcp, proto_lsc);
+  static gboolean initialized = FALSE;
+  static dissector_handle_t lsc_udp_handle;
+  static dissector_handle_t lsc_tcp_handle;
+  static guint saved_lsc_port;
+
+  if (!initialized) {
+    lsc_udp_handle = create_dissector_handle(dissect_lsc_udp, proto_lsc);
+    lsc_tcp_handle = create_dissector_handle(dissect_lsc_tcp, proto_lsc);
+    dissector_add_handle("udp.port", lsc_udp_handle);   /* for 'decode-as' */
+    dissector_add_handle("tcp.port", lsc_tcp_handle);   /* ...             */
+    initialized = TRUE;
+  } else {
+    if (saved_lsc_port != 0) {
+      dissector_delete_uint("udp.port", saved_lsc_port, lsc_udp_handle);
+      dissector_delete_uint("tcp.port", saved_lsc_port, lsc_tcp_handle);
+    }
+  }
 
   /* Set the port number */
-  lsc_port = global_lsc_port;
-
-  dissector_add("udp.port", lsc_port, lsc_udp_handle);
-  dissector_add("tcp.port", lsc_port, lsc_tcp_handle);
+  if (global_lsc_port != 0) {
+    dissector_add_uint("udp.port", global_lsc_port, lsc_udp_handle);
+    dissector_add_uint("tcp.port", global_lsc_port, lsc_tcp_handle);
+  }
+  saved_lsc_port = global_lsc_port;
 }