aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-sparkplug.c
blob: 5a111093a53cb782b20008b8edd9dbb5dc1db128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* packet-sparkplug.c
 * Routines for Sparkplug dissection
 * Copyright 2021 Graham Bloice <graham.bloice<at>trihedral.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"
#include <epan/packet.h>
#include <epan/expert.h>

/*
 * See
 *
 * https://cirrus-link.com/mqtt-sparkplug-tahu/
 *
 * and the specification is at
 *
 * https://www.eclipse.org/tahu/spec/Sparkplug%20Topic%20Namespace%20and%20State%20ManagementV2.2-with%20appendix%20B%20format%20-%20Eclipse.pdf
 *
 */

/* Prototypes */
/* (Required to prevent [-Wmissing-prototypes] warnings */
void proto_reg_handoff_sparkplug(void);
void proto_register_sparkplug(void);

/* Initialize the protocol field */
static int proto_sparkplugb = -1;

/* Initialize the subtree pointers */
static gint ett_sparkplugb = -1;
static gint ett_sparkplugb_namespace = -1;

/* The handle to the protobuf dissector */
dissector_handle_t protobuf_handle = NULL;

/* The hf items */
static int hf_sparkplugb_namespace = -1;
static int hf_sparkplugb_groupid = -1;
static int hf_sparkplugb_messagetype = -1;
static int hf_sparkplugb_edgenodeid = -1;
static int hf_sparkplugb_deviceid = -1;

/* The expert info items */
static expert_field ei_sparkplugb_missing_groupid = EI_INIT;
static expert_field ei_sparkplugb_missing_messagetype = EI_INIT;
static expert_field ei_sparkplugb_missing_edgenodeid = EI_INIT;

static gboolean
dissect_sparkplugb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    proto_item *ti;
    proto_tree *sparkplugb_tree, *namespace_tree;
    gchar **topic_elements, **current_element;
    char *topic = (char *)data;

    /* Confirm the expected topic data is present */
    if (topic == NULL)
      return FALSE;

    /* Parse the topic into the elements */
    topic_elements = g_strsplit(topic, "/", 5);

    /* Heuristic check that the first element of the topic is the SparkplugB namespace */
    if (!topic_elements || (g_strcmp0("spBv1.0", topic_elements[0]) != 0)) {
        g_strfreev(topic_elements);
        return FALSE;
    }

    /* Make entries in Protocol column */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "SparkplugB");

    /* Adjust the info column */
    col_clear(pinfo->cinfo, COL_INFO);
    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "SparkplugB");

    /* create display subtree for the protocol */
    ti = proto_tree_add_item(tree, proto_sparkplugb, tvb, 0, -1, ENC_NA);
    sparkplugb_tree = proto_item_add_subtree(ti, ett_sparkplugb);

    /* Add the elements parsed out from the topic string */
    namespace_tree = proto_tree_add_subtree(sparkplugb_tree, tvb, 0, 0, ett_sparkplugb_namespace, &ti, "Topic Namespace");
    proto_item_set_generated(ti);

    current_element = topic_elements;
    ti = proto_tree_add_string(namespace_tree, hf_sparkplugb_namespace, tvb, 0, 0, current_element[0]);
    proto_item_set_generated(ti);

    current_element += 1;
    if (current_element[0]) {
        ti = proto_tree_add_string(namespace_tree, hf_sparkplugb_groupid, tvb, 0, 0, current_element[0]);
        proto_item_set_generated(ti);
    }
    else {
        expert_add_info(pinfo, namespace_tree, &ei_sparkplugb_missing_groupid);
        return FALSE;
    }

    /* Adjust the info colum text with the message type */
    current_element += 1;
    if (current_element[0]) {
        col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, current_element[0]);
        col_set_fence(pinfo->cinfo, COL_INFO);

        ti = proto_tree_add_string(namespace_tree, hf_sparkplugb_messagetype, tvb, 0, 0, current_element[0]);
        proto_item_set_generated(ti);
    }
    else {
        expert_add_info(pinfo, namespace_tree, &ei_sparkplugb_missing_messagetype);
        return FALSE;
    }

    current_element += 1;
    if (current_element[0]) {
        ti = proto_tree_add_string(namespace_tree, hf_sparkplugb_edgenodeid, tvb, 0, 0, current_element[0]);
        proto_item_set_generated(ti);
    }
    else {
        expert_add_info(pinfo, namespace_tree, &ei_sparkplugb_missing_edgenodeid);
        return FALSE;
    }

    /* Device ID is optional */
    current_element += 1;
    if (current_element[0]) {
        ti = proto_tree_add_string(namespace_tree, hf_sparkplugb_deviceid, tvb, 0, 0, current_element[0]);
        proto_item_set_generated(ti);
    }

    g_strfreev(topic_elements);

    /* Now handoff the Payload message to the protobuf dissector */
    call_dissector_with_data(protobuf_handle, tvb, pinfo, sparkplugb_tree, "message,com.cirruslink.sparkplug.protobuf.Payload");

    return TRUE;
}

void proto_register_sparkplug(void)
{
    expert_module_t* expert_sparkplugb;

    static gint *ett[] = {
        &ett_sparkplugb,
        &ett_sparkplugb_namespace
    };

    static hf_register_info hf[] = {
        {&hf_sparkplugb_namespace, {"Namespace", "sparkplugb.namespace", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}},
        {&hf_sparkplugb_groupid, {"Group ID", "sparkplugb.groupid", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}},
        {&hf_sparkplugb_messagetype, {"Message Type", "sparkplugb.messagetype", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}},
        {&hf_sparkplugb_edgenodeid, {"Edge Node ID", "sparkplugb.edgenodeid", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}},
        {&hf_sparkplugb_deviceid, {"Device ID", "sparkplugb.deviceid", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    };

    static ei_register_info ei[] = {
        { &ei_sparkplugb_missing_groupid, { "sparkplugb.missing_groupid", PI_MALFORMED, PI_ERROR, "Missing Group ID", EXPFILL }},
        { &ei_sparkplugb_missing_messagetype, { "sparkplugb.missing_messagetype", PI_MALFORMED, PI_ERROR, "Missing Message Type", EXPFILL }},
        { &ei_sparkplugb_missing_edgenodeid, { "sparkplugb.missing_edgenodeid", PI_MALFORMED, PI_ERROR, "Missing Edge Node ID", EXPFILL }},
    };

    /* Register the protocol name and description, fields and trees */
    proto_sparkplugb = proto_register_protocol("SparkplugB", "SparkplugB", "sparkplugb");
    register_dissector("sparkplugb", dissect_sparkplugb, proto_sparkplugb);

    proto_register_field_array(proto_sparkplugb, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    expert_sparkplugb = expert_register_protocol(proto_sparkplugb);
    expert_register_field_array(expert_sparkplugb, ei, array_length(ei));
}

void proto_reg_handoff_sparkplug(void)
{
    protobuf_handle = find_dissector_add_dependency("protobuf", proto_sparkplugb);

    /* register as heuristic dissector with MQTT */
    heur_dissector_add("mqtt.topic", dissect_sparkplugb, "SparkplugB over MQTT",
                       "sparkplugb_mqtt", proto_sparkplugb, HEURISTIC_ENABLE);
}