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

Wireshark-dev: [Wireshark-dev] Converting to new proto tree too complex/errorprone

From: Joerg Mayer <jmayer@xxxxxxxxx>
Date: Sat, 21 Dec 2013 11:27:27 +0100
Hello Jakub,

converting a dissector to the new proto tree api is still unnecessarily
complex (and I managed to break all buildbots in the course).
I've attached a diff between the result of the conversion script output
and the currently committed source. Can you please take a look whether
at least some of this can be improved (if the script had been in perl
I'd probably would have tried to fix some of these things myself).

Ciao
   Jörg

-- 
Joerg Mayer                                           <jmayer@xxxxxxxxx>
We are stuck with technology when what we really want is just stuff that
works. Some say that should read Microsoft instead of technology.
--- epan/dissectors/packet-peekremote.c.new	2013-12-21 11:07:24.131752931 +0100
+++ epan/dissectors/packet-peekremote.c	2013-12-21 04:04:27.314317000 +0100
@@ -1,4 +1,3 @@
-#define NEW_PROTO_TREE_API
 /* packet-peekremote.c
  *
  * Routines for the disassembly of packets sent from Cisco WLAN
@@ -50,6 +49,8 @@
  *  and so on"
  */
 
+#define NEW_PROTO_TREE_API
+
 #include "config.h"
 
 #include <glib.h>
@@ -60,7 +61,10 @@
 void proto_register_peekremote(void);
 void proto_reg_handoff_peekremote(void);
 
-static int proto_peekremote = -1;
+/* hfi elements */
+#define THIS_HF_INIT HFI_INIT(proto_peekremote)
+static header_field_info *hfi_peekremote = NULL;
+
 static header_field_info hfi_peekremote_signal THIS_HF_INIT =
       { "Signal [dBm]",      "peekremote.signal", FT_INT8, BASE_DEC, NULL,
         0x0, NULL, HFILL };
@@ -99,6 +103,7 @@
 
 static gint ett_peekremote = -1;
 
+static dissector_handle_t peekremote_handle;
 static dissector_handle_t ieee80211_handle;
 
 static int
@@ -112,7 +117,7 @@
   col_clear(pinfo->cinfo, COL_INFO);
 
   if (tree) {
-    ti = proto_tree_add_item(tree, proto_peekremote, tvb, 0, -1, ENC_NA);
+    ti = proto_tree_add_item(tree, hfi_peekremote, tvb, 0, -1, ENC_NA);
     peekremote_tree = proto_item_add_subtree(ti, ett_peekremote);
 
     proto_tree_add_item(peekremote_tree, &hfi_peekremote_signal, tvb, 0, 1,  ENC_NA);
@@ -133,43 +138,38 @@
 void
 proto_register_peekremote(void)
 {
-  static &hfi_register_info hf[] = {
+#ifndef HAVE_HFI_SECTION_INIT
+  static header_field_info *hfi[] = {
     &hfi_peekremote_signal,
-
     &hfi_peekremote_noise,
-
     &hfi_peekremote_packetlength,
-
     &hfi_peekremote_slicelength,
-
     &hfi_peekremote_unknown4,
-
     &hfi_peekremote_speed,
-
     &hfi_peekremote_unknown6,
-
     &hfi_peekremote_timestamp,
-
     &hfi_peekremote_channel,
-
   };
+#endif
   static gint *ett[] = {
     &ett_peekremote,
   };
 
+  int proto_peekremote;
+
   proto_peekremote = proto_register_protocol(
     "AiroPeek/OmniPeek encapsulated IEEE 802.11", "PEEKREMOTE", "peekremote");
-  proto_register_field_array(proto_peekremote, hf, array_length(hf));
+  hfi_peekremote = proto_registrar_get_nth(proto_peekremote);
+  proto_register_fields(proto_peekremote, hfi, array_length(hfi));
   proto_register_subtree_array(ett, array_length(ett));
+
+  peekremote_handle = new_create_dissector_handle(dissect_peekremote, proto_peekremote);
 }
 
 void
 proto_reg_handoff_peekremote(void)
 {
-  dissector_handle_t peekremote_handle;
-
   ieee80211_handle = find_dissector("wlan_datapad");
 
-  peekremote_handle = new_create_dissector_handle(dissect_peekremote, proto_peekremote);
   dissector_add_uint("udp.port", 5000, peekremote_handle);
 }