Don't do fcn calls in arg of g_?to??(); Macro may very well eval args multiple times.
[obnox/wireshark/wip.git] / epan / dissectors / packet-brp.c
index 8e93b0315ad7d3306ca4bf4ad273f7cbf93de1f3..9f4ceee3f1f7db4922231a29fcb228073dfe9ad9 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+/*
+ * This is a dissector for the BRP (Bandwidth Reservation Protocol). This protocol
+ * is used by various telecommunications vendors to establish VoD (Video
+ * On-Demand) sessions between a STB (Set Top Box) at the customer's home and the
+ * VoD server at the video head-end.
+ */
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
 #include <glib.h>
 #include <epan/packet.h>
+#include <epan/expert.h>
+#include <epan/prefs.h>
+
+/* Forward declaration we need below */
+void proto_reg_handoff_brp(void);
 
 #define PROTO_TAG_BRP   "BRP"
 
@@ -36,16 +48,16 @@ static int proto_brp = -1;
 /*static int global_brp_port = 1958; *//* The port is registered for another protocol */
 
 static const value_string brp_packettype_names[] = {
-    { 0, "BRP" },
-    { 1, "Setup Request - BRC -> BRS" },
-    { 2, "Setup Response - BRS -> BRC" },
-    { 3, "Teardown Request - BRC -> BRS" },
-    { 4, "Teardown Response - BRS -> BRC" },
-    { 5, "Heartbeat Request - BRS -> BRC" },
-    { 6, "Heartbeat Response - BRC -> BRS" },
-    { 7, "Unidirectional Flow Create Request - BRC -> BRS" },
-    { 8, "Flow Create Response - BRS -> BRC" },
-    { 9, "Flow Delete Request BRC -> BRS" },
+    {  0, "BRP" },
+    {  1, "Setup Request - BRC -> BRS" },
+    {  2, "Setup Response - BRS -> BRC" },
+    {  3, "Teardown Request - BRC -> BRS" },
+    {  4, "Teardown Response - BRS -> BRC" },
+    {  5, "Heartbeat Request - BRS -> BRC" },
+    {  6, "Heartbeat Response - BRC -> BRS" },
+    {  7, "Unidirectional Flow Create Request - BRC -> BRS" },
+    {  8, "Flow Create Response - BRS -> BRC" },
+    {  9, "Flow Delete Request BRC -> BRS" },
     { 10, "Flow Delete Response - BRS -> BRC" },
     { 11, "Flow Get Request - BRC -> BRS" },
     { 12, "Flow Get Response - BRS -> BRC" },
@@ -56,21 +68,28 @@ static const value_string brp_packettype_names[] = {
 };
 
 static const value_string brp_stat_vals[] = {
-    { 0, "OK" },
-    { 1, "Comm Error - Network connectivity has been lost (Client Message)." },
-    { 2, "No Bandwidth - There is insufficient bandwidth available in the network to honor the request (Server Message)." },
-    { 3, "Insufficient Resource - Either there is insufficient memory or resource available to transmit the request or, insufficient resources existed at the server to complete the request. Note that insufficient bandwidth in the network is handled by the previous status value. This is the catchall for all other resource deficiencies (Client/Server Message)." },
-    { 4, "No Such - The requested flow does not exist (Server Message)." },
-    { 5, "No Session - There is no active session. The server may return this in the event that the client and server are out of sync. In that eventuality, the client must reestablish its session and recreate any flows that it believes have been lost (Server Message)." },
-    { 6, "Invalid Argument - One of the input arguments to the call was not valid (Client/Server Message)." },
-    { 7, "Unreachable - The specified BRS is not reachable (Client Message)." },
-    { 8, "Internal Error - An internal fault has occurred. This is generally indicative of a fatal condition within the client system (Server Message)." },
-    { 9, "Already Exists - The flow or session that the client requested already exists (Server Message)." },
+    {  0, "OK" },
+    {  1, "Comm Error - Network connectivity has been lost (Client Message)." },
+    {  2, "No Bandwidth - There is insufficient bandwidth available in the network to honor the request (Server Message)." },
+    {  3, "Insufficient Resource - Either there is insufficient memory or resource available to transmit the request or,"
+           " insufficient resources existed at the server to complete the request. Note that insufficient bandwidth in the"
+           " network is handled by the previous status value. This is the catchall for all other resource deficiencies"
+           " (Client/Server Message)." },
+    {  4, "No Such - The requested flow does not exist (Server Message)." },
+    {  5, "No Session - There is no active session. The server may return this in the event that the client and server"
+           " are out of sync. In that eventuality, the client must reestablish its session and recreate any flows that"
+           " it believes have been lost (Server Message)." },
+    {  6, "Invalid Argument - One of the input arguments to the call was not valid (Client/Server Message)." },
+    {  7, "Unreachable - The specified BRS is not reachable (Client Message)." },
+    {  8, "Internal Error - An internal fault has occurred. This is generally indicative of a fatal condition within"
+           " the client system (Server Message)." },
+    {  9, "Already Exists - The flow or session that the client requested already exists (Server Message)." },
     { 10, "Flow Removed - The flow was removed or lost due to issues internal to the network (Server Message)." },
     { 11, "Invalid Sender - Received packet was from an unknown sender (Server Message)." },
     { 12, "Invalid Message - Input message is not defined or malformed (Client/Server Message)." },
     { 13, "Unsupported Version - The requested version (in a setup) is not supported (Server Message)." },
-    { 14, "Pending - The requested operation is proceeding and a status will be returned with the final result shortly (Server Message)." },
+    { 14, "Pending - The requested operation is proceeding and a status will be returned with the final result"
+           " shortly (Server Message)." },
     { 0, NULL }
 };
 
@@ -108,7 +127,10 @@ static gint ett_brp_flid = -1;
 static gint ett_brp_rmttl = -1;
 static gint ett_brp_fltype = -1;
 
-void
+/* Preferences */
+static guint global_brp_port = 0;
+
+int
 dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
 
@@ -133,19 +155,20 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     if (tree) { /* we are being asked for details */
 
         /* Here we add our tree/subtree so we can have a collapsible branch. */
-        brp_item = proto_tree_add_item( tree, proto_brp, tvb, 0, -1, ENC_BIG_ENDIAN );
+        brp_item = proto_tree_add_item( tree, proto_brp, tvb, 0, -1, ENC_NA );
         brp_tree = proto_item_add_subtree( brp_item, ett_brp);
 
         /* We use tvb_get_guint8 to get our type value out. */
         type = tvb_get_guint8(tvb, offset);
         offset += 0;
 
+        brp_item = proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
+        offset += 1;
+
         /* Now let's break down each packet and display it in the collapsible branch */
         switch(type)
-            {
+        {
         case 1: /* Setup Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_ver, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -153,8 +176,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 2: /* Setup Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_uint( brp_tree, hf_brp_stat, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -162,36 +183,26 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 3: /* Teardown Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             break;
 
         case 4: /* Teardown Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             break;
 
         case 5: /* Heartbeat Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             break;
 
         case 6: /* Heartbeat Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             break;
 
         case 7: /* Uni Flow Create Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_srcip, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -209,8 +220,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 8: /* Flow Create Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_stat, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -220,8 +229,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 9: /* Flow Delete Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_flid, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -229,8 +236,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 10: /* Flow Delete Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_stat, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -238,8 +243,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 11: /* Flow Get Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_flid, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -247,8 +250,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 12: /* Flow Get Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_stat, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -274,8 +275,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 13: /* Flow Get Next Request */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_flid, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -283,8 +282,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 14: /* Flow Get Next Response */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_trans, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset += 3;
             proto_tree_add_item( brp_tree, hf_brp_stat, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -310,8 +307,6 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         case 15: /* Flow Abort */
-            proto_tree_add_item( brp_tree, hf_brp_type, tvb, offset, 1, ENC_BIG_ENDIAN );
-            offset += 1;
             proto_tree_add_item( brp_tree, hf_brp_mbz, tvb, offset, 3, ENC_BIG_ENDIAN );
             offset +=3;
             proto_tree_add_item( brp_tree, hf_brp_flid, tvb, offset, 4, ENC_BIG_ENDIAN );
@@ -319,17 +314,20 @@ dissect_brp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
             break;
 
         default:
-            /* Should not get here */
-            DISSECTOR_ASSERT_NOT_REACHED();
+            /* Invalid type */
+            expert_add_info_format(pinfo, brp_item, PI_UNDECODED, PI_WARN, "Unknown packet type");
             break;
-            }
+        }
 
     }
+return offset;
 }
 
 /*--- proto_register_brp ----------------------------------------------*/
 void proto_register_brp (void)
 {
+    module_t *brp_module;
+
     /* A data field is something you can search/filter on.
     *
     * We create a structure to register our fields. It consists of an
@@ -397,16 +395,40 @@ void proto_register_brp (void)
     proto_brp = proto_register_protocol ("BRP Protocol", "BRP", "brp");
     proto_register_field_array (proto_brp, hf, array_length (hf));
     proto_register_subtree_array (ett, array_length (ett));
-    register_dissector("brp", dissect_brp, proto_brp);
+
+    /* Register preferences module */
+    brp_module = prefs_register_protocol(proto_brp, proto_reg_handoff_brp);
+
+    /* Register preferences */
+    prefs_register_uint_preference(brp_module, "port",
+                               "BRP Port",
+                               "Set the UDP port for BRP messages",
+                               10, &global_brp_port);
+
+    new_register_dissector("brp", dissect_brp, proto_brp);
 }
 
 /*--- proto_reg_handoff_brp -------------------------------------------*/
 void proto_reg_handoff_brp(void)
 {
-    dissector_handle_t brp_handle;
+    static gboolean initialized = FALSE;
+    static dissector_handle_t brp_handle;
+    static guint saved_brp_port;
+
+    if (!initialized) {
+       brp_handle = new_create_dissector_handle(dissect_brp, proto_brp);
+       dissector_add_handle("udp.port", brp_handle);
+       initialized = TRUE;
+    } else {
+      if (saved_brp_port != 0) {
+         dissector_delete_uint("udp.port", saved_brp_port, brp_handle);
+      }
+    }
 
-    brp_handle = create_dissector_handle(dissect_brp, proto_brp);
-    /*dissector_add("udp.port", global_brp_port, brp_handle);*/
-    dissector_add_handle("udp.port", brp_handle);  /* For 'Decoode as' */
+    /* Set the port number */
+    if (global_brp_port != 0) {
+       dissector_add_uint("udp.port", global_brp_port, brp_handle);
+    }
+    saved_brp_port = global_brp_port;
 }