From Lars Roland:
[obnox/wireshark/wip.git] / gtk / mgcp_stat.c
1 /* mgcp_stat.c
2  * mgcp-statistics for ethereal
3  * Copyright 2003 Lars Roland
4  *
5  * $Id: mgcp_stat.c,v 1.5 2003/04/25 20:54:18 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <gtk/gtk.h>
37 #include <string.h>
38 #include "menu.h"
39 #include "../epan/packet_info.h"
40 #include "../tap.h"
41 #include "../epan/value_string.h"
42 #include "../register.h"
43 #include "../plugins/mgcp/packet-mgcp.h"
44 #include "../timestats.h"
45 #include "gtk_stat_util.h"
46 #include "compat_macros.h"
47 #include "../simple_dialog.h"
48 #include "../file.h"
49 #include "../globals.h"
50
51
52
53 #define NUM_TIMESTATS 11
54
55 /* used to keep track of the statistics for an entire program interface */
56 typedef struct _mgcpstat_t {
57         GtkWidget *win;
58         GtkWidget *vbox;
59         char *filter;
60         gtk_table *table;
61         timestat_t rtd[NUM_TIMESTATS];
62         guint32 open_req_num;
63         guint32 disc_rsp_num;
64         guint32 req_dup_num;
65         guint32 rsp_dup_num;
66 } mgcpstat_t;
67
68 static const value_string mgcp_mesage_type[] = {
69   {  0, "Overall"},
70   {  1, "EPCF"},
71   {  2, "CRCX"},
72   {  3, "MDCX"},
73   {  4, "DLCX"},
74   {  5, "RQNT"},
75   {  6, "NTFY"},
76   {  7, "AUEP"},
77   {  8, "AUCX"},
78   {  9, "RSIP"},
79 };
80
81 static void
82 mgcpstat_reset(void *pms)
83 {
84         mgcpstat_t *ms=(mgcpstat_t *)pms;
85         int i;
86
87         for(i=0;i<NUM_TIMESTATS;i++) {
88                 ms->rtd[i].num=0;
89                 ms->rtd[i].min_num=0;
90                 ms->rtd[i].max_num=0;
91                 ms->rtd[i].min.secs=0;
92                 ms->rtd[i].min.nsecs=0;
93                 ms->rtd[i].max.secs=0;
94                 ms->rtd[i].max.nsecs=0;
95                 ms->rtd[i].tot.secs=0;
96                 ms->rtd[i].tot.nsecs=0;
97         }
98
99         ms->open_req_num=0;
100         ms->disc_rsp_num=0;
101         ms->req_dup_num=0;
102         ms->rsp_dup_num=0;
103
104 }
105
106
107 static int
108 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, void *pmi)
109 {
110         mgcpstat_t *ms=(mgcpstat_t *)pms;
111         mgcp_info_t *mi=pmi;
112         nstime_t delta;
113
114         switch (mi->mgcp_type) {
115
116         case MGCP_REQUEST:
117                 if(mi->is_duplicate){
118                         /* Duplicate is ignored */
119                         ms->req_dup_num++;
120                         return 0;
121                 }
122                 else {
123                         ms->open_req_num++;
124                         return 0;
125                 }
126         break;
127
128         case MGCP_RESPONSE:
129                 if(mi->is_duplicate){
130                         /* Duplicate is ignored */
131                         ms->rsp_dup_num++;
132                         return 0;
133                 }
134                 else if (!mi->request_available) {
135                         /* no request was seen */
136                         ms->disc_rsp_num++;
137                         return 0;
138                 }
139                 else {
140                         ms->open_req_num--;
141                         /* calculate time delta between request and response */
142                         delta.secs=pinfo->fd->abs_secs-mi->req_time.secs;
143                         delta.nsecs=pinfo->fd->abs_usecs*1000-mi->req_time.nsecs;
144                         if(delta.nsecs<0){
145                                 delta.nsecs+=1000000000;
146                                 delta.secs--;
147                         }
148
149                         time_stat_update(&(ms->rtd[0]),&delta, pinfo);
150
151                         if (strncasecmp(mi->code, "EPCF", 4) == 0 ) {
152                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
153                         }
154                         else if (strncasecmp(mi->code, "CRCX", 4) == 0 ) {
155                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
156                         }
157                         else if (strncasecmp(mi->code, "MDCX", 4) == 0 ) {
158                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
159                         }
160                         else if (strncasecmp(mi->code, "DLCX", 4) == 0 ) {
161                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
162                         }
163                         else if (strncasecmp(mi->code, "RQNT", 4) == 0 ) {
164                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
165                         }
166                         else if (strncasecmp(mi->code, "NTFY", 4) == 0 ) {
167                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
168                         }
169                         else if (strncasecmp(mi->code, "AUEP", 4) == 0 ) {
170                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
171                         }
172                         else if (strncasecmp(mi->code, "AUCX", 4) == 0 ) {
173                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
174                         }
175                         else if (strncasecmp(mi->code, "RSIP", 4) == 0 ) {
176                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
177                         }
178                         else {
179                                 time_stat_update(&(ms->rtd[10]),&delta, pinfo);
180                         }
181
182                         return 1;
183                 }
184         break;
185
186         default:
187                 return 0;
188         break;
189         }
190 }
191
192 static void
193 mgcpstat_draw(void *pms)
194 {
195         mgcpstat_t *ms=(mgcpstat_t *)pms;
196         int i;
197         int pos;
198         char str[256];
199
200         gtk_widget_destroy(ms->table->widget);
201         ms->table->height=5;
202         ms->table->width=7;
203         ms->table->widget=gtk_table_new(ms->table->height, ms->table->width, TRUE);
204         gtk_container_add(GTK_CONTAINER(ms->vbox), ms->table->widget);
205
206         pos=0;
207
208         add_table_entry(ms->table, "Type", 0, pos);
209         add_table_entry(ms->table, "Messages", 1, pos);
210         add_table_entry(ms->table, "Min RTD", 2, pos);
211         add_table_entry(ms->table, "Max RTD", 3, pos);
212         add_table_entry(ms->table, "Avg RTD", 4, pos);
213         add_table_entry(ms->table, "Min in Frame", 5, pos);
214         add_table_entry(ms->table, "Max in Frame", 6, pos);
215         pos++;
216
217         for(i=0;i<NUM_TIMESTATS;i++) {
218                 /* nothing seen, nothing to do */
219                 if(ms->rtd[i].num==0){
220                         continue;
221                 }
222
223                 sprintf(str, "%s", val_to_str(i,mgcp_mesage_type,"Other"));
224                 add_table_entry(ms->table, str, 0, pos);
225                 sprintf(str, "%d", ms->rtd[i].num);
226                 add_table_entry(ms->table, str, 1, pos);
227                 sprintf(str, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].min)));
228                 add_table_entry(ms->table, str, 2, pos);
229                 sprintf(str, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].max)));
230                 add_table_entry(ms->table, str, 3, pos);
231                 sprintf(str, "%8.2f msec", get_average(&(ms->rtd[i].tot), ms->rtd[i].num));
232                 add_table_entry(ms->table, str, 4, pos);
233                 sprintf(str, "%6u", ms->rtd[i].min_num);
234                 add_table_entry(ms->table, str, 5, pos);
235                 sprintf(str, "%6u", ms->rtd[i].max_num);
236                 add_table_entry(ms->table, str, 6, pos);
237                 pos++;
238         }
239
240         gtk_widget_show(ms->table->widget);
241 }
242
243 void protect_thread_critical_region(void);
244 void unprotect_thread_critical_region(void);
245 static void
246 win_destroy_cb(GtkWindow *win _U_, gpointer data)
247 {
248         mgcpstat_t *ms=(mgcpstat_t *)data;
249
250         protect_thread_critical_region();
251         remove_tap_listener(ms);
252         unprotect_thread_critical_region();
253
254         if(ms->filter){
255                 g_free(ms->filter);
256                 ms->filter=NULL;
257         }
258         g_free(ms->table);
259         ms->table=NULL;
260         g_free(ms);
261 }
262
263 void
264 gtk_mgcpstat_init(char *optarg)
265 {
266         mgcpstat_t *ms;
267         char *filter=NULL;
268         GtkWidget *stat_label;
269         GtkWidget *filter_label;
270         char filter_string[256];
271         GString *error_string;
272
273         if(!strncmp(optarg,"mgcp,rtd,",9)){
274                 filter=optarg+9;
275         } else {
276                 filter=g_malloc(1);
277                 *filter='\0';
278         }
279
280         ms=g_malloc(sizeof(mgcpstat_t));
281         ms->filter=g_malloc(strlen(filter)+1);
282         strcpy(ms->filter, filter);
283
284         mgcpstat_reset(ms);
285
286         ms->win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
287         gtk_window_set_title(GTK_WINDOW(ms->win), "MGCP Response Time Delay (RTD) Statistics");
288         SIGNAL_CONNECT(ms->win, "destroy", win_destroy_cb, ms);
289
290         ms->vbox=gtk_vbox_new(FALSE, 0);
291         gtk_container_add(GTK_CONTAINER(ms->win), ms->vbox);
292         gtk_container_set_border_width(GTK_CONTAINER(ms->vbox), 10);
293         gtk_widget_show(ms->vbox);
294
295         stat_label=gtk_label_new("MGCP Response Time Delay (RTD) Statistics");
296         gtk_box_pack_start(GTK_BOX(ms->vbox), stat_label, FALSE, FALSE, 0);
297         gtk_widget_show(stat_label);
298
299         snprintf(filter_string,255,"Filter:%s",filter?filter:"");
300         filter_label=gtk_label_new(filter_string);
301         gtk_box_pack_start(GTK_BOX(ms->vbox), filter_label, FALSE, FALSE, 0);
302         gtk_widget_show(filter_label);
303
304         ms->table =(gtk_table *)g_malloc(sizeof(gtk_table));
305         ms->table->height=5;
306         ms->table->width=7;
307         ms->table->widget=gtk_table_new(ms->table->height, ms->table->width, TRUE);
308         gtk_container_add(GTK_CONTAINER(ms->vbox), ms->table->widget);
309
310         add_table_entry(ms->table, "Type", 0, 0);
311         add_table_entry(ms->table, "Messages", 1, 0);
312         add_table_entry(ms->table, "Min RTD", 2, 0);
313         add_table_entry(ms->table, "Max RTD", 3, 0);
314         add_table_entry(ms->table, "Avg RTD", 4, 0);
315         add_table_entry(ms->table, "Min in Frame", 5, 0);
316         add_table_entry(ms->table, "Max in Frame", 6, 0);
317
318         gtk_widget_show(ms->table->widget);
319
320         error_string=register_tap_listener("mgcp", ms, filter, mgcpstat_reset, mgcpstat_packet, mgcpstat_draw);
321         if(error_string){
322                 simple_dialog(ESD_TYPE_WARN, NULL, error_string->str);
323                 g_string_free(error_string, TRUE);
324                 g_free(ms->table);
325                 g_free(ms->filter);
326                 g_free(ms);
327                 return;
328         }
329
330         gtk_widget_show_all(ms->win);
331         redissect_packets(&cfile);
332 }
333
334
335 static GtkWidget *dlg=NULL, *dlg_box;
336 static GtkWidget *filter_box;
337 static GtkWidget *filter_label, *filter_entry;
338 static GtkWidget *start_button;
339
340 static void
341 dlg_destroy_cb(void)
342 {
343         dlg=NULL;
344 }
345
346 static void
347 mgcpstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
348 {
349         char *filter;
350         char str[256];
351
352         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
353         if(filter[0]==0){
354                 gtk_mgcpstat_init("mgcp,rtd");
355         } else {
356                 sprintf(str,"mgcp,rtd,%s", filter);
357                 gtk_mgcpstat_init(str);
358         }
359 }
360
361
362
363 static void
364 gtk_mgcpstat_cb(GtkWidget *w _U_, gpointer d _U_)
365 {
366         /* if the window is already open, bring it to front */
367         if(dlg){
368                 gdk_window_raise(dlg->window);
369                 return;
370         }
371
372         dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
373         gtk_window_set_title(GTK_WINDOW(dlg), "MGCP RTD Statistics");
374         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
375         dlg_box=gtk_vbox_new(FALSE, 0);
376         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
377         gtk_widget_show(dlg_box);
378
379
380         /* filter box */
381         filter_box=gtk_hbox_new(FALSE, 10);
382         /* Filter label */
383         gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
384         filter_label=gtk_label_new("Filter:");
385         gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
386         gtk_widget_show(filter_label);
387
388         filter_entry=gtk_entry_new_with_max_length(250);
389         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
390         gtk_widget_show(filter_entry);
391
392         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
393         gtk_widget_show(filter_box);
394
395
396         /* the start button */
397         start_button=gtk_button_new_with_label("Create Stat");
398         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
399                               mgcpstat_start_button_clicked, NULL);
400         gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
401         gtk_widget_show(start_button);
402
403         gtk_widget_show_all(dlg);
404 }
405
406 void
407 register_tap_listener_gtkmgcpstat(void)
408 {
409         register_ethereal_tap("mgcp,rtd", gtk_mgcpstat_init);
410 }
411
412 void
413 register_tap_menu_gtkmgcpstat(void)
414 {
415         if (find_tap_id("mgcp"))
416                 register_tap_menu_item("MGCP/RTD", gtk_mgcpstat_cb);
417 }
418