Add a routine to get the representation of a protocol tree item and use it.

Add proto_item_get_display_repr(), which returns a string, allocated
with a specified wmem scope, containing the display representation of
the value of a proto_item.

Use it in the LLDP dissector, to append that string to the parent
protocol tree item; use packet scope, so it doesn't hang around forever
(the previous code used the NULL scope, meaning explicit freeing was
required, but it wasn't explicitly freeing the value, so it was
leaking).

Change-Id: I146380118833b1daef9dea8bd9463001e5b9325f
Reviewed-on: https://code.wireshark.org/review/36931
Petri-Dish: Guy Harris <gharris@sonic.net>
Reviewed-by: Guy Harris <gharris@sonic.net>
This commit is contained in:
Guy Harris 2020-04-24 22:31:11 -07:00 committed by Guy Harris
parent 9750d00ff1
commit 9f03cde5a5
4 changed files with 35 additions and 55 deletions

View File

@ -1144,6 +1144,7 @@ libwireshark.so.0 libwireshark0 #MINVER#
proto_item_add_subtree@Base 1.9.1
proto_item_append_text@Base 1.9.1
proto_item_fill_label@Base 1.9.1
proto_item_get_display_repr@Base 3.3.0
proto_item_get_len@Base 1.9.1
proto_item_get_parent@Base 1.9.1
proto_item_get_parent_nth@Base 1.9.1

View File

@ -3582,9 +3582,6 @@ dissect_cisco_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
guint32 offset = 0;
guint length = tvb_reported_length(tvb);
field_info *fi;
gchar* value_str;
proto_tree *upoe_data = NULL;
proto_item *tf = NULL;
proto_item *parent_item = proto_tree_get_parent(tree);
@ -3614,49 +3611,37 @@ dissect_cisco_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
/* ACI */
case 0xc9:
tf = proto_tree_add_item(tree, hf_cisco_aci_portstate, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xca:
tf = proto_tree_add_item(tree, hf_cisco_aci_noderole, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xcb:
tf = proto_tree_add_item(tree, hf_cisco_aci_nodeid, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += 4;
length -= 4;
break;
case 0xcc:
tf = proto_tree_add_item(tree, hf_cisco_aci_unknowncc, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xcd:
tf = proto_tree_add_item(tree, hf_cisco_aci_pod, tvb, offset, 2, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += 2;
length -= 2;
break;
case 0xce:
tf = proto_tree_add_item(tree, hf_cisco_aci_fabricname, tvb, offset, length, ENC_ASCII|ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
@ -3664,9 +3649,7 @@ dissect_cisco_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
proto_tree_add_item(tree, hf_cisco_aci_apiclist, tvb, offset, length, ENC_NA);
while (length > 0) {
tf = proto_tree_add_item(tree, hf_cisco_aci_apicid, tvb, offset, 1, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": ID %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset++;
length--;
proto_tree_add_item(tree, hf_cisco_aci_apicipv4, tvb, offset, 4, ENC_NA);
@ -3679,81 +3662,61 @@ dissect_cisco_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
break;
case 0xd0:
tf = proto_tree_add_item(tree, hf_cisco_aci_nodeip, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += 4;
length -= 4;
break;
case 0xd1:
tf = proto_tree_add_item(tree, hf_cisco_aci_unknownd1, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd2:
tf = proto_tree_add_item(tree, hf_cisco_aci_version, tvb, offset, length, ENC_ASCII|ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd3:
tf = proto_tree_add_item(tree, hf_cisco_aci_fabricvlan, tvb, offset, 2, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += 2;
length -= 2;
break;
case 0xd4:
tf = proto_tree_add_item(tree, hf_cisco_aci_serialno, tvb, offset, length, ENC_ASCII|ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd6:
tf = proto_tree_add_item(tree, hf_cisco_aci_model, tvb, offset, length, ENC_ASCII|ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd7:
tf = proto_tree_add_item(tree, hf_cisco_aci_nodename, tvb, offset, length, ENC_ASCII|ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd8:
tf = proto_tree_add_item(tree, hf_cisco_aci_portmode, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xd9:
tf = proto_tree_add_item(tree, hf_cisco_aci_unknownd9, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;
case 0xda:
tf = proto_tree_add_item(tree, hf_cisco_aci_apicmode, tvb, offset, length, ENC_NA);
fi = PITEM_FINFO(tf);
value_str = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
proto_item_append_text(parent_item, ": %s", value_str);
proto_item_append_text(parent_item, ": %s", proto_item_get_display_repr(wmem_packet_scope(), tf));
offset += length;
length -= length;
break;

View File

@ -6823,6 +6823,16 @@ proto_item_get_len(const proto_item *pi)
return fi ? fi->length : -1;
}
char *
proto_item_get_display_repr(wmem_allocator_t *scope, proto_item *pi)
{
field_info *fi;
fi = PITEM_FINFO(pi);
DISSECTOR_ASSERT(fi->hfinfo != NULL);
return fvalue_to_string_repr(scope, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
}
proto_tree *
proto_tree_create_root(packet_info *pinfo)
{

View File

@ -1065,7 +1065,13 @@ WS_DLL_PUBLIC void proto_item_set_end(proto_item *ti, tvbuff_t *tvb, gint end);
@return the current length */
WS_DLL_PUBLIC int proto_item_get_len(const proto_item *ti);
/** Get display representation of a proto_item.
* Can be used, for example, to append that to the parent item of
* that item.
@param scope the wmem scope to use to allocate the string
@param ti the item from which to get the display representation
@return the display representation */
WS_DLL_PUBLIC char *proto_item_get_display_repr(wmem_allocator_t *scope, proto_item *pi);
/** Creates a new proto_tree root.
@return the new tree root */