Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

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

From: Abhik Sarkar <sarkar.abhik@xxxxxxxxx>
Date: Wed, 8 Apr 2009 11:10:15 +0400
How about registering a third common protocol PROTO, putting the common fields in that and then calling dissect methods in the common dissector from the variant dissectors? Would that work?

On Tue, Apr 7, 2009 at 8:06 PM, Tamazov, Artem <artem.tamazov@xxxxxxxxxxx> wrote:

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
============================================================
___________________________________________________________________________
Sent via:    Wireshark-dev mailing list <wireshark-dev@xxxxxxxxxxxxx>
Archives:    http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
            mailto:wireshark-dev-request@xxxxxxxxxxxxx?subject=unsubscribe