ssl,dtls: use ProtocolVersion from Server Hello
[metze/wireshark/wip.git] / epan / rtd_table.c
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  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "packet_info.h"
29 #include "proto.h"
30 #include "rtd_table.h"
31
32 struct register_rtd {
33     int proto_id;              /* protocol id (0-indexed) */
34     const char* tap_listen_str;      /* string used in register_tap_listener (NULL to use protocol name) */
35     tap_packet_cb rtd_func;    /* function to be called for new incoming packets for RTD */
36     guint num_tables;
37     guint num_timestats;
38     const value_string* vs_type;
39     rtd_filter_check_cb filter_check;
40 };
41
42 int get_rtd_proto_id(register_rtd_t* rtd)
43 {
44     if (!rtd) {
45         return -1;
46     }
47     return rtd->proto_id;
48 }
49
50 const char* get_rtd_tap_listener_name(register_rtd_t* rtd)
51 {
52     return rtd->tap_listen_str;
53 }
54
55 tap_packet_cb get_rtd_packet_func(register_rtd_t* rtd)
56 {
57     return rtd->rtd_func;
58 }
59
60 guint get_rtd_num_tables(register_rtd_t* rtd) {
61     return rtd->num_tables;
62 }
63
64 const value_string* get_rtd_value_string(register_rtd_t* rtd)
65 {
66     return rtd->vs_type;
67 }
68
69 static GSList *registered_rtd_tables = NULL;
70
71 static gint
72 insert_sorted_by_table_name(gconstpointer aparam, gconstpointer bparam)
73 {
74     const register_rtd_t *a = (register_rtd_t *)aparam;
75     const register_rtd_t *b = (register_rtd_t *)bparam;
76
77     return g_ascii_strcasecmp(proto_get_protocol_short_name(find_protocol_by_id(a->proto_id)), proto_get_protocol_short_name(find_protocol_by_id(b->proto_id)));
78 }
79
80 void
81 register_rtd_table(const int proto_id, const char* tap_listener, guint num_tables, guint num_timestats, const value_string* vs_type,
82                    tap_packet_cb rtd_packet_func, rtd_filter_check_cb filter_check_cb)
83 {
84     register_rtd_t *table;
85     DISSECTOR_ASSERT(rtd_packet_func);
86
87     table = g_new(register_rtd_t,1);
88
89     table->proto_id      = proto_id;
90     if (tap_listener != NULL)
91         table->tap_listen_str = tap_listener;
92     else
93         table->tap_listen_str = proto_get_protocol_filter_name(proto_id);
94     table->rtd_func      = rtd_packet_func;
95     table->num_tables = num_tables;
96     table->num_timestats = num_timestats;
97     table->vs_type = vs_type;
98     table->filter_check = filter_check_cb;
99
100     registered_rtd_tables = g_slist_insert_sorted(registered_rtd_tables, table, insert_sorted_by_table_name);
101 }
102
103 void free_rtd_table(rtd_stat_table* table, rtd_gui_free_cb gui_callback, void *callback_data)
104 {
105     guint i;
106
107     for (i = 0; i < table->num_rtds; i++)
108     {
109         g_free(table->time_stats[i].rtd);
110     }
111     g_free(table->time_stats);
112     table->time_stats = NULL;
113     table->num_rtds = 0;
114
115     /* Give GUI the first crack at it before we clean up */
116     if (gui_callback)
117         gui_callback(table, callback_data);
118 }
119
120 void reset_rtd_table(rtd_stat_table* table, rtd_gui_reset_cb gui_callback, void *callback_data)
121 {
122     guint i = 0;
123
124     for (i = 0; i < table->num_rtds; i++)
125         memset(table->time_stats[i].rtd, 0, sizeof(timestat_t)*table->time_stats[i].num_timestat);
126
127     /* Give GUI the first crack at it before we clean up */
128     if (gui_callback)
129         gui_callback(table, callback_data);
130
131 }
132
133 register_rtd_t* get_rtd_table_by_name(const char* name)
134 {
135     guint i, size = g_slist_length(registered_rtd_tables);
136     register_rtd_t* rtd;
137     GSList   *slist;
138
139     for (i = 0; i < size; i++) {
140         slist = g_slist_nth(registered_rtd_tables, i);
141         rtd = (register_rtd_t*)slist->data;
142
143         if (strcmp(name, proto_get_protocol_filter_name(rtd->proto_id)) == 0)
144             return rtd;
145     }
146
147     return NULL;
148 }
149
150 gchar* rtd_table_get_tap_string(register_rtd_t* rtd)
151 {
152     GString *cmd_str = g_string_new(proto_get_protocol_filter_name(rtd->proto_id));
153     g_string_append(cmd_str, ",rtd");
154     return g_string_free(cmd_str, FALSE);
155 }
156
157 void rtd_table_get_filter(register_rtd_t* rtd, const char *opt_arg, const char **filter, char** err)
158 {
159     gchar* cmd_str = rtd_table_get_tap_string(rtd);
160     guint len = (guint) strlen(cmd_str);
161     *filter=NULL;
162     *err=NULL;
163
164     if (!strncmp(opt_arg, cmd_str, len))
165     {
166         if (opt_arg[len] == ',')
167         {
168            *filter = opt_arg + len+1;
169         }
170         }
171
172     if (rtd->filter_check)
173         rtd->filter_check(opt_arg, filter, err);
174
175     g_free(cmd_str);
176 }
177
178 void rtd_table_dissector_init(register_rtd_t* rtd, rtd_stat_table* table, rtd_gui_init_cb gui_callback, void *callback_data)
179 {
180     guint i;
181
182     table->num_rtds = rtd->num_tables;
183     table->time_stats = g_new0(rtd_timestat, rtd->num_tables);
184
185     for (i = 0; i < table->num_rtds; i++)
186     {
187         table->time_stats[i].num_timestat = rtd->num_timestats;
188         table->time_stats[i].rtd = g_new0(timestat_t, rtd->num_timestats);
189     }
190
191     if (gui_callback)
192         gui_callback(table, callback_data);
193 }
194
195 void rtd_table_iterate_tables(GFunc func, gpointer user_data)
196 {
197     g_slist_foreach(registered_rtd_tables, func, user_data);
198 }
199
200 /*
201  * Editor modelines
202  *
203  * Local Variables:
204  * c-basic-offset: 4
205  * tab-width: 8
206  * indent-tabs-mode: nil
207  * End:
208  *
209  * ex: set shiftwidth=4 tabstop=8 expandtab:
210  * :indentSize=4:tabSize=8:noTabs=true:
211  */