Have tap listeners specify whether the "packet" routine requires
[obnox/wireshark/wip.git] / gtk / mgcp_stat.c
1 /* mgcp_stat.c
2  * mgcp-statistics for Wireshark
3  * Copyright 2003 Lars Roland
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include <epan/packet_info.h>
39 #include <epan/epan.h>
40 #include <epan/value_string.h>
41 #include <epan/tap.h>
42 #include "epan/dissectors/packet-mgcp.h"
43
44 #include "../register.h"
45 #include "../timestats.h"
46 #include "../simple_dialog.h"
47 #include "../file.h"
48 #include "../globals.h"
49 #include "../stat_menu.h"
50
51 #include "gtk/gui_stat_util.h"
52 #include "gtk/dlg_utils.h"
53 #include "gtk/tap_dfilter_dlg.h"
54 #include "gtk/gui_utils.h"
55 #include "gtk/main.h"
56
57
58 #define NUM_TIMESTATS 10
59
60 /* used to keep track of the statistics for an entire program interface */
61 typedef struct _mgcpstat_t {
62         GtkWidget *win;
63         GtkWidget *vbox;
64         char *filter;
65         GtkWidget *scrolled_window;
66         GtkCList *table;
67         timestat_t rtd[NUM_TIMESTATS];
68         guint32 open_req_num;
69         guint32 disc_rsp_num;
70         guint32 req_dup_num;
71         guint32 rsp_dup_num;
72 } mgcpstat_t;
73
74 static const value_string mgcp_mesage_type[] = {
75   {  0, "EPCF"},
76   {  1, "CRCX"},
77   {  2, "MDCX"},
78   {  3, "DLCX"},
79   {  4, "RQNT"},
80   {  5, "NTFY"},
81   {  6, "AUEP"},
82   {  7, "AUCX"},
83   {  8, "RSIP"},
84   {  0, NULL}
85 };
86
87 static void
88 mgcpstat_reset(void *pms)
89 {
90         mgcpstat_t *ms=(mgcpstat_t *)pms;
91         int i;
92
93         for(i=0;i<NUM_TIMESTATS;i++) {
94                 ms->rtd[i].num=0;
95                 ms->rtd[i].min_num=0;
96                 ms->rtd[i].max_num=0;
97                 ms->rtd[i].min.secs=0;
98                 ms->rtd[i].min.nsecs=0;
99                 ms->rtd[i].max.secs=0;
100                 ms->rtd[i].max.nsecs=0;
101                 ms->rtd[i].tot.secs=0;
102                 ms->rtd[i].tot.nsecs=0;
103         }
104
105         ms->open_req_num=0;
106         ms->disc_rsp_num=0;
107         ms->req_dup_num=0;
108         ms->rsp_dup_num=0;
109 }
110
111
112 static int
113 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pmi)
114 {
115         mgcpstat_t *ms=(mgcpstat_t *)pms;
116         const mgcp_info_t *mi=pmi;
117         nstime_t delta;
118         int ret = 0;
119
120         switch (mi->mgcp_type) {
121
122         case MGCP_REQUEST:
123                 if(mi->is_duplicate){
124                         /* Duplicate is ignored */
125                         ms->req_dup_num++;
126                 }
127                 else {
128                         ms->open_req_num++;
129                 }
130                 break;
131
132         case MGCP_RESPONSE:
133                 if(mi->is_duplicate){
134                         /* Duplicate is ignored */
135                         ms->rsp_dup_num++;
136                 }
137                 else if (!mi->request_available) {
138                         /* no request was seen */
139                         ms->disc_rsp_num++;
140                 }
141                 else {
142                         ms->open_req_num--;
143                         /* calculate time delta between request and response */
144                         nstime_delta(&delta, &pinfo->fd->abs_ts, &mi->req_time);
145
146                         if (g_ascii_strncasecmp(mi->code, "EPCF", 4) == 0 ) {
147                                 time_stat_update(&(ms->rtd[0]),&delta, pinfo);
148                         }
149                         else if (g_ascii_strncasecmp(mi->code, "CRCX", 4) == 0 ) {
150                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
151                         }
152                         else if (g_ascii_strncasecmp(mi->code, "MDCX", 4) == 0 ) {
153                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
154                         }
155                         else if (g_ascii_strncasecmp(mi->code, "DLCX", 4) == 0 ) {
156                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
157                         }
158                         else if (g_ascii_strncasecmp(mi->code, "RQNT", 4) == 0 ) {
159                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
160                         }
161                         else if (g_ascii_strncasecmp(mi->code, "NTFY", 4) == 0 ) {
162                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
163                         }
164                         else if (g_ascii_strncasecmp(mi->code, "AUEP", 4) == 0 ) {
165                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
166                         }
167                         else if (g_ascii_strncasecmp(mi->code, "AUCX", 4) == 0 ) {
168                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
169                         }
170                         else if (g_ascii_strncasecmp(mi->code, "RSIP", 4) == 0 ) {
171                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
172                         }
173                         else {
174                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
175                         }
176
177                         ret = 1;
178                 }
179                 break;
180
181         default:
182                 break;
183         }
184
185         return ret;
186 }
187
188 static void
189 mgcpstat_draw(void *pms)
190 {
191         mgcpstat_t *ms=(mgcpstat_t *)pms;
192         int i;
193         char *str[7];
194
195         for(i=0;i<7;i++) {
196                 str[i]=g_malloc(sizeof(char[256]));
197         }
198
199         /* clear list before printing */
200         gtk_clist_clear(ms->table);
201
202         for(i=0;i<NUM_TIMESTATS;i++) {
203                 /* nothing seen, nothing to do */
204                 if(ms->rtd[i].num==0){
205                         continue;
206                 }
207
208                 g_snprintf(str[0], sizeof(char[256]), "%s", val_to_str(i,mgcp_mesage_type,"Other"));
209                 g_snprintf(str[1], sizeof(char[256]), "%d", ms->rtd[i].num);
210                 g_snprintf(str[2], sizeof(char[256]), "%8.2f msec", nstime_to_msec(&(ms->rtd[i].min)));
211                 g_snprintf(str[3], sizeof(char[256]), "%8.2f msec", nstime_to_msec(&(ms->rtd[i].max)));
212                 g_snprintf(str[4], sizeof(char[256]), "%8.2f msec", get_average(&(ms->rtd[i].tot), ms->rtd[i].num));
213                 g_snprintf(str[5], sizeof(char[256]), "%6u", ms->rtd[i].min_num);
214                 g_snprintf(str[6], sizeof(char[256]), "%6u", ms->rtd[i].max_num);
215                 gtk_clist_append(ms->table, str);
216         }
217
218         gtk_widget_show(GTK_WIDGET(ms->table));
219         for(i=0;i<7;i++) {
220                 g_free(str[i]);
221         }
222 }
223
224 static void
225 win_destroy_cb(GtkWindow *win _U_, gpointer data)
226 {
227         mgcpstat_t *ms=(mgcpstat_t *)data;
228
229         protect_thread_critical_region();
230         remove_tap_listener(ms);
231         unprotect_thread_critical_region();
232
233         if(ms->filter){
234                 g_free(ms->filter);
235                 ms->filter=NULL;
236         }
237         g_free(ms);
238 }
239
240 static const gchar *titles[]={
241                         "Type",
242                         "Messages",
243                         "Min SRT",
244                         "Max SRT",
245                         "Avg SRT",
246                         "Min in Frame",
247                         "Max in Frame" };
248
249 static void
250 gtk_mgcpstat_init(const char *optarg, void *userdata _U_)
251 {
252         mgcpstat_t *ms;
253         GString *error_string;
254         GtkWidget *bt_close;
255         GtkWidget *bbox;
256
257         ms=g_malloc(sizeof(mgcpstat_t));
258
259         if(strncmp(optarg,"mgcp,srt,",9) == 0){
260                 ms->filter=g_strdup(optarg+9);
261         } else {
262                 ms->filter=NULL;
263         }
264
265         mgcpstat_reset(ms);
266
267         ms->win=window_new(GTK_WINDOW_TOPLEVEL, "MGCP SRT");
268         gtk_window_set_default_size(GTK_WINDOW(ms->win), 550, 150);
269
270         ms->vbox=gtk_vbox_new(FALSE, 3);
271
272         init_main_stat_window(ms->win, ms->vbox, "MGCP Service Response Time (SRT) Statistics", ms->filter);
273
274         /* init a scrolled window*/
275         ms->scrolled_window = scrolled_window_new(NULL, NULL);
276
277         ms->table = create_stat_table(ms->scrolled_window, ms->vbox, 7, titles);
278
279         error_string=register_tap_listener("mgcp", ms, ms->filter, 0, mgcpstat_reset, mgcpstat_packet, mgcpstat_draw);
280         if(error_string){
281                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
282                 g_string_free(error_string, TRUE);
283                 g_free(ms->filter);
284                 g_free(ms);
285                 return;
286         }
287
288         /* Button row. */
289         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
290         gtk_box_pack_start(GTK_BOX(ms->vbox), bbox, FALSE, FALSE, 0);
291
292         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
293         window_set_cancel_button(ms->win, bt_close, window_cancel_button_cb);
294
295         g_signal_connect(ms->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
296         g_signal_connect(ms->win, "destroy", G_CALLBACK(win_destroy_cb), ms);
297
298         gtk_widget_show_all(ms->win);
299         window_present(ms->win);
300
301         cf_retap_packets(&cfile);
302         gdk_window_raise(ms->win->window);
303 }
304
305 static tap_dfilter_dlg mgcp_srt_dlg = {
306         "MGCP Service Response Time (SRT) Statistics",
307         "mgcp,srt",
308         gtk_mgcpstat_init,
309         -1
310 };
311
312 void
313 register_tap_listener_gtkmgcpstat(void)
314 {
315         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
316         if (find_tap_id("mgcp")) {
317                 register_dfilter_stat(&mgcp_srt_dlg, "MGCP",
318                     REGISTER_STAT_GROUP_RESPONSE_TIME);
319         }
320 }