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 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "packet_info.h"
29 #include "srt_table.h"
32 int proto_id; /* protocol id (0-indexed) */
33 const char* tap_listen_str; /* string used in register_tap_listener (NULL to use protocol name) */
34 int max_tables; /* Maximum number of tables expected (used by GUI to determine how to display tables) */
35 tap_packet_cb srt_func; /* function to be called for new incoming packets for SRT */
36 srt_init_cb srt_init; /* function to create dissector SRT tables */
37 srt_param_handler_cb param_cb; /* function to parse parameters of optional arguments of tap string */
38 void* param_data; /* Storage for tap parameter data */
41 int get_srt_proto_id(register_srt_t* srt)
49 const char* get_srt_tap_listener_name(register_srt_t* srt)
51 return srt->tap_listen_str;
54 int get_srt_max_tables(register_srt_t* srt)
56 return srt->max_tables;
59 tap_packet_cb get_srt_packet_func(register_srt_t* srt)
64 void set_srt_table_param_data(register_srt_t* srt, void* data)
66 srt->param_data = data;
69 void* get_srt_table_param_data(register_srt_t* srt)
71 return srt->param_data;
75 free_srt_table_data(srt_stat_table *rst)
79 for(i=0;i<rst->num_procs;i++){
80 g_free(rst->procedures[i].procedure);
81 rst->procedures[i].procedure=NULL;
83 g_free(rst->filter_string);
84 rst->filter_string=NULL;
85 g_free(rst->procedures);
90 void free_srt_table(register_srt_t *srt, GArray* srt_array, srt_gui_free_cb gui_callback, void *callback_data)
93 srt_stat_table *srt_table;
95 for (i = 0; i < srt_array->len; i++)
97 srt_table = g_array_index(srt_array, srt_stat_table*, i);
99 /* Give GUI the first crack at it before we clean up */
101 gui_callback(srt_table, callback_data);
103 free_srt_table_data(srt_table);
107 /* Clear the tables */
108 g_array_set_size(srt_array, 0);
110 /* Clear out any possible parameter data */
111 g_free(srt->param_data);
112 srt->param_data = NULL;
115 static void reset_srt_table_data(srt_stat_table *rst)
119 for(i=0;i<rst->num_procs;i++){
120 time_stat_init(&rst->procedures[i].stats);
124 void reset_srt_table(GArray* srt_array, srt_gui_reset_cb gui_callback, void *callback_data)
127 srt_stat_table *srt_table;
129 for (i = 0; i < srt_array->len; i++)
131 srt_table = g_array_index(srt_array, srt_stat_table*, i);
133 /* Give GUI the first crack at it before we clean up */
135 gui_callback(srt_table, callback_data);
137 reset_srt_table_data(srt_table);
141 static wmem_tree_t *registered_srt_tables = NULL;
143 register_srt_t* get_srt_table_by_name(const char* name)
145 return (register_srt_t*)wmem_tree_lookup_string(registered_srt_tables, name, 0);
148 gchar* srt_table_get_tap_string(register_srt_t* srt)
150 GString *cmd_str = g_string_new(proto_get_protocol_filter_name(srt->proto_id));
151 g_string_append(cmd_str, ",srt");
152 return g_string_free(cmd_str, FALSE);
155 void srt_table_get_filter(register_srt_t* srt, const char *opt_arg, const char **filter, char** err)
157 gchar* cmd_str = srt_table_get_tap_string(srt);
158 guint len = (guint32)strlen(cmd_str);
163 if(!strncmp(opt_arg, cmd_str, len))
165 if (srt->param_cb != NULL)
167 pos = srt->param_cb(srt, opt_arg + len, err);
175 if (opt_arg[pos] == ',')
177 *filter = opt_arg + pos+1;
184 void srt_table_dissector_init(register_srt_t* srt, GArray* srt_array, srt_gui_init_cb gui_callback, void *callback_data)
186 srt->srt_init(srt, srt_array, gui_callback, callback_data);
190 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)
192 register_srt_t *table;
193 DISSECTOR_ASSERT(init_cb);
194 DISSECTOR_ASSERT(srt_packet_func);
196 table = wmem_new(wmem_epan_scope(), register_srt_t);
198 table->proto_id = proto_id;
199 if (tap_listener != NULL)
200 table->tap_listen_str = tap_listener;
202 table->tap_listen_str = proto_get_protocol_filter_name(proto_id);
203 table->max_tables = max_tables;
204 table->srt_func = srt_packet_func;
205 table->srt_init = init_cb;
206 table->param_cb = param_cb;
207 table->param_data = NULL;
209 if (registered_srt_tables == NULL)
210 registered_srt_tables = wmem_tree_new(wmem_epan_scope());
212 wmem_tree_insert_string(registered_srt_tables, proto_get_protocol_filter_name(proto_id), table, 0);
215 void srt_table_iterate_tables(wmem_foreach_func func, gpointer user_data)
217 wmem_tree_foreach(registered_srt_tables, func, user_data);
221 init_srt_table(const char *name, const char *short_name, GArray *srt_array, int num_procs, const char* proc_column_name,
222 const char *filter_string, srt_gui_init_cb gui_callback, void* gui_data, void* table_specific_data)
225 srt_stat_table *table = g_new(srt_stat_table, 1);
227 table->filter_string = g_strdup(filter_string);
230 table->short_name = short_name;
231 table->proc_column_name = proc_column_name;
232 table->num_procs=num_procs;
233 table->procedures=(srt_procedure_t *)g_malloc(sizeof(srt_procedure_t)*num_procs);
234 for(i=0;i<num_procs;i++){
235 time_stat_init(&table->procedures[i].stats);
236 table->procedures[i].proc_index = 0;
237 table->procedures[i].procedure = NULL;
240 g_array_insert_val(srt_array, srt_array->len, table);
242 table->table_specific_data = table_specific_data;
245 gui_callback(table, gui_data);
251 init_srt_table_row(srt_stat_table *rst, int indx, const char *procedure)
253 /* we have discovered a new procedure. Extend the table accordingly */
254 if(indx>=rst->num_procs){
255 int old_num_procs=rst->num_procs;
258 rst->num_procs=indx+1;
259 rst->procedures=(srt_procedure_t *)g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
260 for(i=old_num_procs;i<rst->num_procs;i++){
261 time_stat_init(&rst->procedures[i].stats);
262 rst->procedures[i].proc_index = i;
263 rst->procedures[i].procedure=NULL;
266 rst->procedures[indx].proc_index = indx;
267 rst->procedures[indx].procedure=g_strdup(procedure);
271 add_srt_table_data(srt_stat_table *rst, int indx, const nstime_t *req_time, packet_info *pinfo)
276 g_assert(indx >= 0 && indx < rst->num_procs);
277 rp=&rst->procedures[indx];
279 /* calculate time delta between request and reply */
281 nstime_delta(&delta, &t, req_time);
283 time_stat_update(&rp->stats, &delta, pinfo);
292 * indent-tabs-mode: nil
295 * ex: set shiftwidth=4 tabstop=8 expandtab:
296 * :indentSize=4:tabSize=8:noTabs=true: