Wireshark-dev: [Wireshark-dev] How can I re-use definition of hf[]?

From: "Tamazov, Artem" <artem.tamazov@xxxxxxxxxxx>
Date: Tue, 7 Apr 2009 11:06:12 -0500
Hello,

I would like to implement two dissectors which are very similar.
How can I re-use definition of hf[]?  

See sample code below (question is in comments):

==================
...
static int proto_PROTOABBREV_VARIATION_A = -1;
static int proto_PROTOABBREV_VARIATION_B = -1;
static int hf_PROTOABBREV_FIELDABBREV = -1;
static gint ett_PROTOABBREV = -1;

static int dissect_PROTOABBREV_VARIATION_A(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
...
}

static int dissect_PROTOABBREV_VARIATION_B(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
...
}

void proto_register_PROTOABBREV(void)
{
	static hf_register_info hf[] = {
		{ &hf_PROTOABBREV_FIELDABBREV,
			{ "FIELDNAME",           "PROTOABBREV.FIELDABBREV",
			FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK,
			"FIELDDESCR", HFILL }
		}
	};

	static gint *ett[] = {
		&ett_PROTOABBREV
	};

	proto_PROTOABBREV_VARIATION_A = proto_register_protocol("PROTONAME VARIATION A",
	    "PROTOSHORTNAME A", "PROTOABBREVA");
	proto_PROTOABBREV_VARIATION_B = proto_register_protocol("PROTONAME VARIATION B",
	    "PROTOSHORTNAME B", "PROTOABBREVB");

	proto_register_field_array(proto_PROTOABBREV_VARIATION_A, hf, array_length(hf));
	/* 
	 * *QUESTION*:
	 * 	AFAIK double registration of hf[] is wrong, although currently Wireshark
	 *	tolerates this. How to _properly_ re-use hf[] in variation B?
       */	
	proto_register_field_array(proto_PROTOABBREV_VARIATION_B, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_PROTOABBREV(void)
{
	dissector_handle_t PROTOABBREV_handle;
	PROTOABBREV_handle = new_create_dissector_handle(dissect_PROTOABBREV_VARIATION_A, proto_PROTOABBREV_VARIATION_A);
	dissector_add("PARENT_SUBFIELD", ID_VALUE, PROTOABBREV_handle);
	PROTOABBREV_handle = new_create_dissector_handle(dissect_PROTOABBREV_VARIATION_B, proto_PROTOABBREV_VARIATION_B);
	dissector_add("PARENT_SUBFIELD", ID_VALUE, PROTOABBREV_handle);
}
==================

I see one possible way -- using of C preprocessor capabilities:

==================
...
static int hf_PROTOABBREV_FIELDABBREV_for_A = -1;
static int hf_PROTOABBREV_FIELDABBREV_for_B = -1;
...
#define HF_INITIALIZER_FIELDABBREV(hf_handle)\
		{ &(hf_handle),\
			{ "FIELDNAME",           "PROTOABBREV.FIELDABBREV",\
			FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK,\
			"FIELDDESCR", HFILL }\
		}\
	}
...
	static hf_register_info hf_a[] = HF_INITIALIZER_FIELDABBREV(hf_PROTOABBREV_FIELDABBREV_for_A);
	static hf_register_info hf_b[] = HF_INITIALIZER_FIELDABBREV(hf_PROTOABBREV_FIELDABBREV_for_B);
...
	proto_register_field_array(proto_PROTOABBREV_VARIATION_A, hf_a, array_length(hf_a));
	proto_register_field_array(proto_PROTOABBREV_VARIATION_B, hf_b, array_length(hf_b));
...
==================

But this solution is not elegant, I guess.
Any ideas?

Thank you in advance, 
artem// 
============================================================
The information contained in this message may be privileged
and confidential and protected from disclosure. If the reader
of this message is not the intended recipient, or an employee
or agent responsible for delivering this message to the
intended recipient, you are hereby notified that any reproduction,
dissemination or distribution of this communication is strictly
prohibited. If you have received this communication in error,
please notify us immediately by replying to the message and
deleting it from your computer. Thank you. Tellabs
============================================================