Allow floating point values for stats_tree
[gd/wireshark/.git] / epan / dissectors / packet-collectd.c
index f04d65aa5e67d0036b415c29d2dbe58483f93b3c..823aa3a15a829f5471f682edb5f0be85b10bb7d8 100644 (file)
@@ -8,32 +8,19 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
 
 #include <epan/packet.h>
-#include <epan/prefs.h>
 #include <epan/expert.h>
 #include <epan/stats_tree.h>
 #include <epan/to_str.h>
 
 #include <wsutil/str_util.h>
 
-#define STR_NONNULL(str) ((str) ? (str) : "(null)")
+#define STR_NONNULL(str) ((str) ? ((const gchar*)str) : "(null)")
 
 #define TYPE_HOST            0x0000
 #define TYPE_TIME            0x0001
 void proto_register_collectd(void);
 
 typedef struct value_data_s {
-       gchar *host;
+       const guint8 *host;
        gint host_off;
        gint host_len;
        guint64 time_value;
        gint time_off;
        guint64 interval;
        gint interval_off;
-       gchar *plugin;
+       const guint8 *plugin;
        gint plugin_off;
        gint plugin_len;
-       gchar *plugin_instance;
+       const guint8 *plugin_instance;
        gint plugin_instance_off;
        gint plugin_instance_len;
-       gchar *type;
+       const guint8 *type;
        gint type_off;
        gint type_len;
-       gchar *type_instance;
+       const guint8 *type_instance;
        gint type_instance_off;
        gint type_instance_len;
 } value_data_t;
 
 typedef struct notify_data_s {
-       gchar *host;
+       const guint8 *host;
        gint host_off;
        gint host_len;
        guint64 time_value;
        gint time_off;
        guint64 severity;
        gint severity_off;
-       gchar *message;
+       const guint8 *message;
        gint message_off;
        gint message_len;
 } notify_data_t;
@@ -144,8 +131,7 @@ static const val64_string severity_names[] = {
        { 0, NULL }
 };
 
-#define UDP_PORT_COLLECTD 25826
-static guint collectd_udp_port = UDP_PORT_COLLECTD;
+#define UDP_PORT_COLLECTD 25826 /* Not IANA registered */
 
 static gint proto_collectd             = -1;
 static gint tap_collectd                = -1;
@@ -204,7 +190,7 @@ void proto_reg_handoff_collectd (void);
 static nstime_t
 collectd_time_to_nstime (guint64 t)
 {
-       nstime_t nstime = { 0, 0 };
+       nstime_t nstime = NSTIME_INIT_ZERO;;
        nstime.secs = (time_t) (t / 1073741824);
        nstime.nsecs = (int) (((double) (t % 1073741824)) / 1.073741824);
 
@@ -214,8 +200,8 @@ collectd_time_to_nstime (guint64 t)
 static void
 collectd_stats_tree_init (stats_tree *st)
 {
-       st_collectd_packets = stats_tree_create_node (st, "Packets", 0, FALSE);
-       st_collectd_values = stats_tree_create_node (st, "Values", 0, TRUE);
+       st_collectd_packets = stats_tree_create_node (st, "Packets", 0, STAT_DT_INT, FALSE);
+       st_collectd_values = stats_tree_create_node (st, "Values", 0, STAT_DT_INT, TRUE);
 
        st_collectd_values_hosts = stats_tree_create_pivot (st, "By host",
                                                           st_collectd_values);
@@ -355,7 +341,7 @@ collectd_proto_tree_add_assembled_notification (tvbuff_t *tvb,
 static int
 dissect_collectd_string (tvbuff_t *tvb, packet_info *pinfo, gint type_hf,
                         gint offset, gint *ret_offset, gint *ret_length,
-                        gchar **ret_string, proto_tree *tree_root,
+                        const guint8 **ret_string, proto_tree *tree_root,
                         proto_item **ret_item)
 {
        proto_tree *pt;
@@ -391,16 +377,15 @@ dissect_collectd_string (tvbuff_t *tvb, packet_info *pinfo, gint type_hf,
        *ret_offset = offset + 4;
        *ret_length = length - 4;
 
-       *ret_string = tvb_get_string_enc(wmem_packet_scope(), tvb, *ret_offset, *ret_length, ENC_ASCII);
+       proto_tree_add_uint (pt, hf_collectd_type, tvb, offset, 2, type);
+       proto_tree_add_uint (pt, hf_collectd_length, tvb, offset + 2, 2, length);
+       proto_tree_add_item_ret_string (pt, type_hf, tvb, *ret_offset, *ret_length, ENC_ASCII|ENC_NA, wmem_packet_scope(), ret_string);
+
        proto_item_append_text(pt, "\"%s\"", *ret_string);
 
        if (ret_item != NULL)
                *ret_item = pi;
 
-       proto_tree_add_uint (pt, hf_collectd_type, tvb, offset, 2, type);
-       proto_tree_add_uint (pt, hf_collectd_length, tvb, offset + 2, 2, length);
-       proto_tree_add_item (pt, type_hf, tvb, *ret_offset, *ret_length, ENC_ASCII|ENC_NA);
-
        return (0);
 } /* int dissect_collectd_string */
 
@@ -917,7 +902,7 @@ dissect_collectd (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* dat
 
        gint offset;
        gint size;
-       gchar *pkt_host = NULL;
+       const guint8 *pkt_host = NULL;
        gint pkt_plugins = 0, pkt_values = 0, pkt_messages = 0, pkt_unknown = 0, pkt_errors = 0;
        value_data_t vdispatch;
        notify_data_t ndispatch;
@@ -1349,7 +1334,6 @@ dissect_collectd (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* dat
 
 void proto_register_collectd(void)
 {
-       module_t *collectd_module;
        expert_module_t* expert_collectd;
 
        /* Setup list of header fields */
@@ -1485,43 +1469,16 @@ void proto_register_collectd(void)
        expert_register_field_array(expert_collectd, ei, array_length(ei));
 
        tap_collectd = register_tap ("collectd");
-
-       /*
-        * Create an unsigned integer preference to allow the user to specify the
-        * UDP port on which to capture DIS packets.
-        */
-       collectd_module = prefs_register_protocol (proto_collectd,
-                                                  proto_reg_handoff_collectd);
-
-       prefs_register_uint_preference (collectd_module, "udp.port",
-                                       "collectd UDP port",
-                                       "Set the UDP port for collectd messages",
-                                       10, &collectd_udp_port);
-} /* void proto_register_collectd */
+}
 
 void proto_reg_handoff_collectd (void)
 {
-       static gboolean first_run = TRUE;
-       static gint registered_udp_port = -1;
-       static dissector_handle_t collectd_handle;
-
-       if (first_run)
-               collectd_handle = new_create_dissector_handle (dissect_collectd,
-                                                          proto_collectd);
-
-       /* Change the dissector registration if the preferences have been
-        * changed. */
-       if (registered_udp_port != -1)
-               dissector_delete_uint ("udp.port", registered_udp_port,
-                                 collectd_handle);
-
-       dissector_add_uint ("udp.port", collectd_udp_port, collectd_handle);
-       registered_udp_port = collectd_udp_port;
+       dissector_handle_t collectd_handle;
 
-       if (first_run)
-               collectd_stats_tree_register ();
+       collectd_handle = create_dissector_handle(dissect_collectd, proto_collectd);
+       dissector_add_uint_with_preference("udp.port", UDP_PORT_COLLECTD, collectd_handle);
 
-       first_run = FALSE;
+       collectd_stats_tree_register ();
 } /* void proto_reg_handoff_collectd */
 
 /*