1 /* rtd_table.c
2  * Helper routines common to all RTD taps.
3  * Based on srt_table.c
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #include "config.h"
13 
14 #include <string.h>
15 
16 #include "proto.h"
17 #include "packet_info.h"
18 #include "rtd_table.h"
19 
20 struct register_rtd {
21     int proto_id;              /* protocol id (0-indexed) */
22     const char* tap_listen_str;      /* string used in register_tap_listener (NULL to use protocol name) */
23     tap_packet_cb rtd_func;    /* function to be called for new incoming packets for RTD */
24     guint num_tables;
25     guint num_timestats;
26     const value_string* vs_type;
27     rtd_filter_check_cb filter_check;
28 };
29 
get_rtd_proto_id(register_rtd_t * rtd)30 int get_rtd_proto_id(register_rtd_t* rtd)
31 {
32     if (!rtd) {
33         return -1;
34     }
35     return rtd->proto_id;
36 }
37 
get_rtd_tap_listener_name(register_rtd_t * rtd)38 const char* get_rtd_tap_listener_name(register_rtd_t* rtd)
39 {
40     return rtd->tap_listen_str;
41 }
42 
get_rtd_packet_func(register_rtd_t * rtd)43 tap_packet_cb get_rtd_packet_func(register_rtd_t* rtd)
44 {
45     return rtd->rtd_func;
46 }
47 
get_rtd_num_tables(register_rtd_t * rtd)48 guint get_rtd_num_tables(register_rtd_t* rtd) {
49     return rtd->num_tables;
50 }
51 
get_rtd_value_string(register_rtd_t * rtd)52 const value_string* get_rtd_value_string(register_rtd_t* rtd)
53 {
54     return rtd->vs_type;
55 }
56 
57 static wmem_tree_t *registered_rtd_tables = NULL;
58 
59 void
register_rtd_table(const int proto_id,const char * tap_listener,guint num_tables,guint num_timestats,const value_string * vs_type,tap_packet_cb rtd_packet_func,rtd_filter_check_cb filter_check_cb)60 register_rtd_table(const int proto_id, const char* tap_listener, guint num_tables, guint num_timestats, const value_string* vs_type,
61                    tap_packet_cb rtd_packet_func, rtd_filter_check_cb filter_check_cb)
62 {
63     register_rtd_t *table;
64     DISSECTOR_ASSERT(rtd_packet_func);
65 
66     table = wmem_new(wmem_epan_scope(), register_rtd_t);
67 
68     table->proto_id      = proto_id;
69     if (tap_listener != NULL)
70         table->tap_listen_str = tap_listener;
71     else
72         table->tap_listen_str = proto_get_protocol_filter_name(proto_id);
73     table->rtd_func      = rtd_packet_func;
74     table->num_tables = num_tables;
75     table->num_timestats = num_timestats;
76     table->vs_type = vs_type;
77     table->filter_check = filter_check_cb;
78 
79     if (registered_rtd_tables == NULL)
80         registered_rtd_tables = wmem_tree_new(wmem_epan_scope());
81 
82     wmem_tree_insert_string(registered_rtd_tables, proto_get_protocol_filter_name(proto_id), table, 0);
83 }
84 
free_rtd_table(rtd_stat_table * table)85 void free_rtd_table(rtd_stat_table* table)
86 {
87     guint i;
88 
89     for (i = 0; i < table->num_rtds; i++)
90     {
91         g_free(table->time_stats[i].rtd);
92     }
93     g_free(table->time_stats);
94     table->time_stats = NULL;
95     table->num_rtds = 0;
96 }
97 
reset_rtd_table(rtd_stat_table * table)98 void reset_rtd_table(rtd_stat_table* table)
99 {
100     guint i = 0;
101 
102     for (i = 0; i < table->num_rtds; i++)
103         memset(table->time_stats[i].rtd, 0, sizeof(timestat_t)*table->time_stats[i].num_timestat);
104 }
105 
get_rtd_table_by_name(const char * name)106 register_rtd_t* get_rtd_table_by_name(const char* name)
107 {
108     return (register_rtd_t*)wmem_tree_lookup_string(registered_rtd_tables, name, 0);
109 }
110 
rtd_table_get_tap_string(register_rtd_t * rtd)111 gchar* rtd_table_get_tap_string(register_rtd_t* rtd)
112 {
113     GString *cmd_str = g_string_new(proto_get_protocol_filter_name(rtd->proto_id));
114     g_string_append(cmd_str, ",rtd");
115     return g_string_free(cmd_str, FALSE);
116 }
117 
rtd_table_get_filter(register_rtd_t * rtd,const char * opt_arg,const char ** filter,char ** err)118 void rtd_table_get_filter(register_rtd_t* rtd, const char *opt_arg, const char **filter, char** err)
119 {
120     gchar* cmd_str = rtd_table_get_tap_string(rtd);
121     guint len = (guint) strlen(cmd_str);
122     *filter=NULL;
123     *err=NULL;
124 
125     if (!strncmp(opt_arg, cmd_str, len))
126     {
127         if (opt_arg[len] == ',')
128         {
129            *filter = opt_arg + len+1;
130         }
131 	}
132 
133     if (rtd->filter_check)
134         rtd->filter_check(opt_arg, filter, err);
135 
136     g_free(cmd_str);
137 }
138 
rtd_table_dissector_init(register_rtd_t * rtd,rtd_stat_table * table,rtd_gui_init_cb gui_callback,void * callback_data)139 void rtd_table_dissector_init(register_rtd_t* rtd, rtd_stat_table* table, rtd_gui_init_cb gui_callback, void *callback_data)
140 {
141     guint i;
142 
143     table->num_rtds = rtd->num_tables;
144     table->time_stats = g_new0(rtd_timestat, rtd->num_tables);
145 
146     for (i = 0; i < table->num_rtds; i++)
147     {
148         table->time_stats[i].num_timestat = rtd->num_timestats;
149         table->time_stats[i].rtd = g_new0(timestat_t, rtd->num_timestats);
150     }
151 
152     if (gui_callback)
153         gui_callback(table, callback_data);
154 }
155 
rtd_table_iterate_tables(wmem_foreach_func func,gpointer user_data)156 void rtd_table_iterate_tables(wmem_foreach_func func, gpointer user_data)
157 {
158     wmem_tree_foreach(registered_rtd_tables, func, user_data);
159 }
160 
161 /*
162  * Editor modelines
163  *
164  * Local Variables:
165  * c-basic-offset: 4
166  * tab-width: 8
167  * indent-tabs-mode: nil
168  * End:
169  *
170  * ex: set shiftwidth=4 tabstop=8 expandtab:
171  * :indentSize=4:tabSize=8:noTabs=true:
172  */
173