dcerpc-netlogon: improve NetrLogonGetCapabilities dissection
[wireshark-sm.git] / epan / srt_table.c
blob2c0c083aa0aa92724f1d5a4333d5c790d4b820bf
1 /* srt_table.c
2 * Helper routines common to all SRT taps.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include <string.h>
15 #include "proto.h"
16 #include "packet_info.h"
17 #include "srt_table.h"
18 #include <wsutil/ws_assert.h>
20 struct register_srt {
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 int max_tables; /* Maximum number of tables expected (used by GUI to determine how to display tables) */
24 tap_packet_cb srt_func; /* function to be called for new incoming packets for SRT */
25 srt_init_cb srt_init; /* function to create dissector SRT tables */
26 srt_param_handler_cb param_cb; /* function to parse parameters of optional arguments of tap string */
27 void* param_data; /* Storage for tap parameter data */
30 int get_srt_proto_id(register_srt_t* srt)
32 if (!srt) {
33 return -1;
35 return srt->proto_id;
38 const char* get_srt_tap_listener_name(register_srt_t* srt)
40 return srt->tap_listen_str;
43 int get_srt_max_tables(register_srt_t* srt)
45 return srt->max_tables;
48 tap_packet_cb get_srt_packet_func(register_srt_t* srt)
50 return srt->srt_func;
53 void set_srt_table_param_data(register_srt_t* srt, void* data)
55 srt->param_data = data;
58 void* get_srt_table_param_data(register_srt_t* srt)
60 return srt->param_data;
63 void
64 free_srt_table_data(srt_stat_table *rst)
66 int i;
68 for(i=0;i<rst->num_procs;i++){
69 g_free(rst->procedures[i].procedure);
70 rst->procedures[i].procedure=NULL;
72 g_free(rst->filter_string);
73 rst->filter_string=NULL;
74 g_free(rst->procedures);
75 rst->procedures=NULL;
76 rst->num_procs=0;
79 void free_srt_table(register_srt_t *srt, GArray* srt_array)
81 unsigned i = 0;
82 srt_stat_table *srt_table;
84 for (i = 0; i < srt_array->len; i++)
86 srt_table = g_array_index(srt_array, srt_stat_table*, i);
88 free_srt_table_data(srt_table);
89 g_free(srt_table);
92 /* Clear the tables */
93 g_array_set_size(srt_array, 0);
95 /* Clear out any possible parameter data */
96 g_free(srt->param_data);
97 srt->param_data = NULL;
100 static void reset_srt_table_data(srt_stat_table *rst)
102 int i;
104 for(i=0;i<rst->num_procs;i++){
105 time_stat_init(&rst->procedures[i].stats);
109 void reset_srt_table(GArray* srt_array)
111 unsigned i = 0;
112 srt_stat_table *srt_table;
114 for (i = 0; i < srt_array->len; i++)
116 srt_table = g_array_index(srt_array, srt_stat_table*, i);
118 reset_srt_table_data(srt_table);
122 static wmem_tree_t *registered_srt_tables;
124 register_srt_t* get_srt_table_by_name(const char* name)
126 return (register_srt_t*)wmem_tree_lookup_string(registered_srt_tables, name, 0);
129 char* srt_table_get_tap_string(register_srt_t* srt)
131 GString *cmd_str = g_string_new(proto_get_protocol_filter_name(srt->proto_id));
132 g_string_append(cmd_str, ",srt");
133 return g_string_free(cmd_str, FALSE);
136 void srt_table_get_filter(register_srt_t* srt, const char *opt_arg, const char **filter, char** err)
138 char* cmd_str = srt_table_get_tap_string(srt);
139 unsigned len = (uint32_t)strlen(cmd_str);
140 unsigned pos = len;
141 *filter=NULL;
142 *err = NULL;
144 if(!strncmp(opt_arg, cmd_str, len))
146 if (srt->param_cb != NULL)
148 pos = srt->param_cb(srt, opt_arg + len, err);
149 if (*err != NULL)
150 return;
152 if (pos > 0)
153 pos += len;
156 if (opt_arg[pos] == ',')
158 *filter = opt_arg + pos+1;
162 g_free(cmd_str);
165 void srt_table_dissector_init(register_srt_t* srt, GArray* srt_array)
167 srt->srt_init(srt, srt_array);
170 void
171 register_srt_table(const int proto_id, const char* tap_listener, int max_tables, tap_packet_cb srt_packet_func, srt_init_cb init_cb, srt_param_handler_cb param_cb)
173 register_srt_t *table;
174 DISSECTOR_ASSERT(init_cb);
175 DISSECTOR_ASSERT(srt_packet_func);
177 table = wmem_new(wmem_epan_scope(), register_srt_t);
179 table->proto_id = proto_id;
180 if (tap_listener != NULL)
181 table->tap_listen_str = tap_listener;
182 else
183 table->tap_listen_str = proto_get_protocol_filter_name(proto_id);
184 table->max_tables = max_tables;
185 table->srt_func = srt_packet_func;
186 table->srt_init = init_cb;
187 table->param_cb = param_cb;
188 table->param_data = NULL;
190 if (registered_srt_tables == NULL)
191 registered_srt_tables = wmem_tree_new(wmem_epan_scope());
193 wmem_tree_insert_string(registered_srt_tables, proto_get_protocol_filter_name(proto_id), table, 0);
196 void srt_table_iterate_tables(wmem_foreach_func func, void *user_data)
198 wmem_tree_foreach(registered_srt_tables, func, user_data);
201 srt_stat_table*
202 init_srt_table(const char *name, const char *short_name, GArray *srt_array, int num_procs, const char* proc_column_name,
203 const char *filter_string, void* table_specific_data)
205 int i;
206 srt_stat_table *table = g_new(srt_stat_table, 1);
208 table->filter_string = g_strdup(filter_string);
210 table->name = name;
211 table->short_name = short_name;
212 table->proc_column_name = proc_column_name;
213 table->num_procs=num_procs;
214 table->procedures=g_new(srt_procedure_t, num_procs);
215 for(i=0;i<num_procs;i++){
216 time_stat_init(&table->procedures[i].stats);
217 table->procedures[i].proc_index = 0;
218 table->procedures[i].procedure = NULL;
221 g_array_insert_val(srt_array, srt_array->len, table);
223 table->table_specific_data = table_specific_data;
225 return table;
228 void
229 init_srt_table_row(srt_stat_table *rst, int indx, const char *procedure)
231 /* we have discovered a new procedure. Extend the table accordingly */
232 if(indx>=rst->num_procs){
233 int old_num_procs=rst->num_procs;
234 int i;
236 rst->num_procs=indx+1;
237 rst->procedures=(srt_procedure_t *)g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
238 for(i=old_num_procs;i<rst->num_procs;i++){
239 time_stat_init(&rst->procedures[i].stats);
240 rst->procedures[i].proc_index = i;
241 rst->procedures[i].procedure=NULL;
244 rst->procedures[indx].proc_index = indx;
245 rst->procedures[indx].procedure=g_strdup(procedure);
248 void
249 add_srt_table_data(srt_stat_table *rst, int indx, const nstime_t *req_time, packet_info *pinfo)
251 srt_procedure_t *rp;
252 nstime_t t, delta;
254 ws_assert(indx >= 0 && indx < rst->num_procs);
255 rp=&rst->procedures[indx];
257 /* calculate time delta between request and reply */
258 t=pinfo->abs_ts;
259 nstime_delta(&delta, &t, req_time);
261 time_stat_update(&rp->stats, &delta, pinfo);
265 * Editor modelines
267 * Local Variables:
268 * c-basic-offset: 4
269 * tab-width: 8
270 * indent-tabs-mode: nil
271 * End:
273 * ex: set shiftwidth=4 tabstop=8 expandtab:
274 * :indentSize=4:tabSize=8:noTabs=true: