#include <stdio.h>
#include <wireshark/epan/packet.h>
#include <wireshark/epan/prefs.h>

// Define protocol IDs
static int proto_my_protocol = -1;
static dissector_handle_t my_protocol_handle;

// Define fields
static int hf_my_protocol_field1 = -1;
static int hf_my_protocol_field2 = -1;

// Define the protocol subtree
static gint ett_my_protocol = -1;

void dissect_my_protocol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
    proto_item *ti = NULL;
    proto_tree *my_protocol_tree = NULL;

    // Add the protocol to the tree
    ti = proto_tree_add_item(tree, proto_my_protocol, tvb, 0, -1, ENC_NA);
    my_protocol_tree = proto_item_add_subtree(ti, ett_my_protocol);

    // Add fields to the protocol tree
    proto_tree_add_item(my_protocol_tree, hf_my_protocol_field1, tvb, 0, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(my_protocol_tree, hf_my_protocol_field2, tvb, 1, 2, ENC_BIG_ENDIAN);
}

void proto_register_my_protocol(void) {
    // Register the protocol
    static hf_register_info hf[] = {
        { &hf_my_protocol_field1,
          { "Field 1", "my_protocol.field1", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_my_protocol_field2,
          { "Field 2", "my_protocol.field2", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
    };

    // Register the protocol subtree
    static gint *ett[] = {
        &ett_my_protocol,
    };

    // Register the protocol
    proto_my_protocol = proto_register_protocol("My Protocol", "My Protocol", "my_protocol");
    proto_register_field_array(proto_my_protocol, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    // Register the dissector
    my_protocol_handle = create_dissector_handle(dissect_my_protocol, proto_my_protocol);
    register_dissector("my_protocol", dissect_my_protocol, my_protocol_handle);
}

void proto_reg_handoff_my_protocol(void) {
    // Register the dissector
    dissector_add("udp.port", my_protocol_handle);
}