ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
April 17th, 2024 | 14:30-16:00 SGT (UTC+8) | Online

Wireshark-dev: [Wireshark-dev] [PATCH] range_string in proto_tree_add_item

From: Sebastien Tandel <sebastien@xxxxxxxxx>
Date: Sun, 04 Feb 2007 18:53:59 +0100
Hi,


   re-post of a patch described here ...
http://www.wireshark.org/lists/wireshark-dev/200701/msg00321.html

I also made changes to packet-ospf.c and packet-ansi_map(-template).c to
use this new feature. Unfortunately, SampleCaptures do not contain
traces with the required fields to test these changes.  I've already
made fake tests but not with these specific dissectors.


Regards,
Sebastien Tandel
Index: epan/proto.c
===================================================================
--- epan/proto.c	(r�vision 20712)
+++ epan/proto.c	(copie de travail)
@@ -4026,9 +4026,15 @@
 	value = fvalue_get_uinteger(&fi->value);
 
 	/* Fill in the textual info */
-	ret = g_snprintf(label_str, ITEM_LABEL_LENGTH,
+	if (hfinfo->display & BASE_RANGE_STRING) {
+	  ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, 
 			format,  hfinfo->name,
+			rval_to_str(value, hfinfo->strings, "Unknown"), value);
+	} else {
+	  ret = g_snprintf(label_str, ITEM_LABEL_LENGTH,
+			format,  hfinfo->name,
 			val_to_str(value, cVALS(hfinfo->strings), "Unknown"), value);
+	}
 	if ((ret == -1) || (ret >= ITEM_LABEL_LENGTH))
 		label_str[ITEM_LABEL_LENGTH - 1] = '\0';
 }
@@ -4094,9 +4100,15 @@
 	value = fvalue_get_sinteger(&fi->value);
 
 	/* Fill in the textual info */
-	ret = g_snprintf(label_str, ITEM_LABEL_LENGTH,
+	if (hfinfo->display & BASE_RANGE_STRING) {
+	  ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, 
 			format,  hfinfo->name,
+			rval_to_str(value, hfinfo->strings, "Unknown"), value);
+	} else {
+	  ret = g_snprintf(label_str, ITEM_LABEL_LENGTH,
+			format,  hfinfo->name,
 			val_to_str(value, cVALS(hfinfo->strings), "Unknown"), value);
+	}
 	if ((ret == -1) || (ret >= ITEM_LABEL_LENGTH))
 		label_str[ITEM_LABEL_LENGTH - 1] = '\0';
 }
@@ -4190,7 +4202,9 @@
 {
 	const char *format = NULL;
 
-	switch(hfinfo->display) {
+	/* bit operation to reset the potential BASE_RANGE_STRING (or others in
+	 * the future?) */
+	switch(hfinfo->display & BASE_STRUCTURE_RESET) {
 		case BASE_DEC:
 		case BASE_DEC_HEX:
 			format = "%s: %s (%u)";
@@ -4314,7 +4328,9 @@
 {
 	const char *format = NULL;
 
-	switch(hfinfo->display) {
+	/* bit operation to reset the potential BASE_RANGE_STRING (or others in
+	 * the future?)*/
+	switch(hfinfo->display & BASE_STRUCTURE_RESET) {
 		case BASE_DEC:
 		case BASE_DEC_HEX:
 			format = "%s: %s (%d)";
Index: epan/proto.h
===================================================================
--- epan/proto.h	(r�vision 20712)
+++ epan/proto.h	(copie de travail)
@@ -69,6 +69,10 @@
 /** Make a const true_false_string[] look like a _true_false_string pointer, used to set header_field_info.strings */
 #define TFS(x)	(const struct true_false_string*)(x)
 
+/** Make a const value_string[] look like a _value_string pointer, used to set
+ * header_field_info.strings */
+#define RVALS(x) (const struct _value_string*)(x)
+
 struct _protocol;
 
 /** Structure for information about a protocol */
@@ -133,6 +137,14 @@
     ep_strdup_printf("%s:%u: failed assertion \"%s\"", \
      file, lineno, __DISSECTOR_ASSERT_STRINGIFY(expression))))
 
+/* BASE_STRUCTURE_RESET constant is used in proto.c to reset the bits
+ * identifying special structures used in translation of value for display. 
+ * Its value means that we may have at most 16 base_display_e values */
+#define BASE_STRUCTURE_RESET 0x0F
+/* Following constants have to be ORed with a base_display_e when dissector
+ * want to use specials MACROs (for the moment, only RVALS) for a
+ * header_field_info */
+#define BASE_RANGE_STRING 0x10
 /** radix for decimal values, used in header_field_info.display */
 typedef enum {
 	BASE_NONE,	/**< none */
Index: epan/dissectors/packet-ospf.c
===================================================================
--- epan/dissectors/packet-ospf.c	(r�vision 20712)
+++ epan/dissectors/packet-ospf.c	(copie de travail)
@@ -595,7 +595,8 @@
        BASE_HEX, NULL, 0x0, "MPLS/TE Link Resource Class/Color", HFILL }},
     {&ospf_filter[OSPFF_LS_MPLS_BC_MODEL_ID],
      { "MPLS/DSTE Bandwidth Constraints Model Id", "ospf.mpls.bc", FT_UINT8,
-       BASE_DEC, NULL, 0x0, "MPLS/DSTE Bandwidth Constraints Model Id", HFILL }},
+       BASE_RANGE_STRING | BASE_DEC, RVALS(&mpls_link_stlv_bcmodel_rvals), 0x0, 
+       "MPLS/DSTE Bandwidth Constraints Model Id", HFILL }},
 
     {&ospf_filter[OSPFF_V2_OPTIONS],
      { "Options", "ospf.v2.options", FT_UINT8, BASE_HEX,
@@ -1762,15 +1763,8 @@
 		    proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
 					stlv_len);
 		    
-		    proto_tree_add_uint_format(stlv_tree,
-					       ospf_filter[OSPFF_LS_MPLS_BC_MODEL_ID],
-					       tvb, stlv_offset+4, 1, 
-					       tvb_get_guint8(tvb, stlv_offset+4),
-					       "MPLS/DSTE Bandwidth Constraints Model Id: %u %s",
-					       tvb_get_guint8(tvb, stlv_offset+4),
-					       rval_to_str(tvb_get_guint8(tvb, stlv_offset+4),
-							   mpls_link_stlv_bcmodel_rvals, 
-							   "Unknown"));
+		    proto_tree_add_item(stlv_tree, ospf_filter[OSPFF_LS_MPLS_BC_MODEL_ID],
+			tvb, stlv_offset+4, 1, FALSE);
 
 		    /* 3 octets reserved +5, +6 and +7 (all 0x00) */
 		    if(tvb_memeql(tvb, stlv_offset+5, allzero, 3) == -1) {
Index: asn1/ansi_map/packet-ansi_map-template.c
===================================================================
--- asn1/ansi_map/packet-ansi_map-template.c	(r�vision 20712)
+++ asn1/ansi_map/packet-ansi_map-template.c	(copie de travail)
@@ -2855,16 +2855,11 @@
 	int offset = 0;
     proto_item *item;
     proto_tree *subtree;
-	guint16 so;
 
 	item = get_ber_last_created_item();
 	subtree = proto_item_add_subtree(item, ett_cdmaserviceoption);
 
-	so = tvb_get_ntohs(tvb,offset);
-    proto_tree_add_uint_format(subtree, hf_ansi_map_cdmaserviceoption,
-					       tvb, offset, 2, so,
-					       "CDMAServiceOption: %u %s", so,
-					       rval_to_str(so,cdmaserviceoption_vals ,"Unknown"));
+	proto_tree_add_item(subtree, hf_ansi_map_cdmaserviceoption, tvb, offset, 2, FALSE);
 
 
 }
@@ -4352,7 +4347,7 @@
         "TerminationList (TL)", HFILL }},
 	{ &hf_ansi_map_cdmaserviceoption,
       { "CDMAServiceOption", "ansi_map.cdmaserviceoption",
-        FT_UINT16, BASE_DEC, NULL, 0x0,
+        FT_UINT16, BASE_RANGE_STRING | BASE_DEC, RVALS(&cdmaserviceoption_vals), 0x0,
         "CDMAServiceOption", HFILL }},
 	{ &hf_ansi_trans_cap_waddr,
       { "WIN Addressing (WADDR)", "ansi_map.trans_cap_waddr",

Index: epan/dissectors/packet-ansi_map.c
===================================================================
--- epan/dissectors/packet-ansi_map.c	(r�vision 20712)
+++ epan/dissectors/packet-ansi_map.c	(copie de travail)
@@ -1,6 +1,6 @@
 /* Do not modify this file.                                                   */
 /* It is created automatically by the ASN.1 to Wireshark dissector compiler   */
-/* .\packet-ansi_map.c                                                        */
+/* ./packet-ansi_map.c                                                        */
 /* ../../tools/asn2wrs.py -b -e -p ansi_map -c ansi_map.cnf -s packet-ansi_map-template ansi_map.asn */
 
 /* Input file: packet-ansi_map-template.c */
@@ -3584,16 +3584,11 @@
 	int offset = 0;
     proto_item *item;
     proto_tree *subtree;
-	guint16 so;
 
 	item = get_ber_last_created_item();
 	subtree = proto_item_add_subtree(item, ett_cdmaserviceoption);
 
-	so = tvb_get_ntohs(tvb,offset);
-    proto_tree_add_uint_format(subtree, hf_ansi_map_cdmaserviceoption,
-					       tvb, offset, 2, so,
-					       "CDMAServiceOption: %u %s", so,
-					       rval_to_str(so,cdmaserviceoption_vals ,"Unknown"));
+	proto_tree_add_item(subtree, hf_ansi_map_cdmaserviceoption, tvb, offset, 2, FALSE);
 
 
 }
@@ -14434,7 +14429,7 @@
 
 
 /*--- End of included file: packet-ansi_map-fn.c ---*/
-#line 3534 "packet-ansi_map-template.c"
+#line 3535 "packet-ansi_map-template.c"
 
 static int dissect_invokeData(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset) {
 
@@ -15256,7 +15251,7 @@
         "TerminationList (TL)", HFILL }},
 	{ &hf_ansi_map_cdmaserviceoption,
       { "CDMAServiceOption", "ansi_map.cdmaserviceoption",
-        FT_UINT16, BASE_DEC, NULL, 0x0,
+        FT_UINT16, BASE_RANGE_STRING | BASE_DEC, RVALS(&cdmaserviceoption_vals), 0x0,
         "CDMAServiceOption", HFILL }},
 	{ &hf_ansi_trans_cap_waddr,
       { "WIN Addressing (WADDR)", "ansi_map.trans_cap_waddr",
@@ -17854,7 +17849,7 @@
         "ansi_map.DropServiceRes", HFILL }},
 
 /*--- End of included file: packet-ansi_map-hfarr.c ---*/
-#line 4937 "packet-ansi_map-template.c"
+#line 4938 "packet-ansi_map-template.c"
   };
 
   /* List of subtrees */
@@ -18099,7 +18094,7 @@
     &ett_ansi_map_ReturnData,
 
 /*--- End of included file: packet-ansi_map-ettarr.c ---*/
-#line 4969 "packet-ansi_map-template.c"
+#line 4970 "packet-ansi_map-template.c"
   };