renamed ui_util.c/.h to gui_utils.c/.h to prevent confusion with identical named...
[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$
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 #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
42 #include <epan/stat.h>
43 #include "stat_menu.h"
44 #include <epan/tap.h>
45 #include "../register.h"
46 #include "../plugins/mgcp/packet-mgcp.h"
47 #include "../timestats.h"
48 #include "gtk_stat_util.h"
49 #include "compat_macros.h"
50 #include "../simple_dialog.h"
51 #include "dlg_utils.h"
52 #include "../file.h"
53 #include "../globals.h"
54 #include "../tap_dfilter_dlg.h"
55 #include "tap_dfilter_dlg.h"
56 #include "gui_utils.h"
57
58
59 #define NUM_TIMESTATS 10
60
61 /* used to keep track of the statistics for an entire program interface */
62 typedef struct _mgcpstat_t {
63         GtkWidget *win;
64         GtkWidget *vbox;
65         char *filter;
66         GtkWidget *scrolled_window;
67         GtkCList *table;
68         timestat_t rtd[NUM_TIMESTATS];
69         guint32 open_req_num;
70         guint32 disc_rsp_num;
71         guint32 req_dup_num;
72         guint32 rsp_dup_num;
73 } mgcpstat_t;
74
75 static const value_string mgcp_mesage_type[] = {
76   {  0, "EPCF"},
77   {  1, "CRCX"},
78   {  2, "MDCX"},
79   {  3, "DLCX"},
80   {  4, "RQNT"},
81   {  5, "NTFY"},
82   {  6, "AUEP"},
83   {  7, "AUCX"},
84   {  8, "RSIP"},
85   {  0, NULL}
86 };
87
88 static void
89 mgcpstat_reset(void *pms)
90 {
91         mgcpstat_t *ms=(mgcpstat_t *)pms;
92         int i;
93
94         for(i=0;i<NUM_TIMESTATS;i++) {
95                 ms->rtd[i].num=0;
96                 ms->rtd[i].min_num=0;
97                 ms->rtd[i].max_num=0;
98                 ms->rtd[i].min.secs=0;
99                 ms->rtd[i].min.nsecs=0;
100                 ms->rtd[i].max.secs=0;
101                 ms->rtd[i].max.nsecs=0;
102                 ms->rtd[i].tot.secs=0;
103                 ms->rtd[i].tot.nsecs=0;
104         }
105
106         ms->open_req_num=0;
107         ms->disc_rsp_num=0;
108         ms->req_dup_num=0;
109         ms->rsp_dup_num=0;
110 }
111
112
113 static int
114 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pmi)
115 {
116         mgcpstat_t *ms=(mgcpstat_t *)pms;
117         const mgcp_info_t *mi=pmi;
118         nstime_t delta;
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                         return 0;
127                 }
128                 else {
129                         ms->open_req_num++;
130                         return 0;
131                 }
132         break;
133
134         case MGCP_RESPONSE:
135                 if(mi->is_duplicate){
136                         /* Duplicate is ignored */
137                         ms->rsp_dup_num++;
138                         return 0;
139                 }
140                 else if (!mi->request_available) {
141                         /* no request was seen */
142                         ms->disc_rsp_num++;
143                         return 0;
144                 }
145                 else {
146                         ms->open_req_num--;
147                         /* calculate time delta between request and response */
148                         delta.secs=pinfo->fd->abs_secs-mi->req_time.secs;
149                         delta.nsecs=pinfo->fd->abs_usecs*1000-mi->req_time.nsecs;
150                         if(delta.nsecs<0){
151                                 delta.nsecs+=1000000000;
152                                 delta.secs--;
153                         }
154
155                         if (strncasecmp(mi->code, "EPCF", 4) == 0 ) {
156                                 time_stat_update(&(ms->rtd[0]),&delta, pinfo);
157                         }
158                         else if (strncasecmp(mi->code, "CRCX", 4) == 0 ) {
159                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
160                         }
161                         else if (strncasecmp(mi->code, "MDCX", 4) == 0 ) {
162                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
163                         }
164                         else if (strncasecmp(mi->code, "DLCX", 4) == 0 ) {
165                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
166                         }
167                         else if (strncasecmp(mi->code, "RQNT", 4) == 0 ) {
168                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
169                         }
170                         else if (strncasecmp(mi->code, "NTFY", 4) == 0 ) {
171                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
172                         }
173                         else if (strncasecmp(mi->code, "AUEP", 4) == 0 ) {
174                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
175                         }
176                         else if (strncasecmp(mi->code, "AUCX", 4) == 0 ) {
177                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
178                         }
179                         else if (strncasecmp(mi->code, "RSIP", 4) == 0 ) {
180                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
181                         }
182                         else {
183                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
184                         }
185
186                         return 1;
187                 }
188         break;
189
190         default:
191                 return 0;
192         break;
193         }
194 }
195
196 static void
197 mgcpstat_draw(void *pms)
198 {
199         mgcpstat_t *ms=(mgcpstat_t *)pms;
200         int i;
201         /* gtk1 using a scrollable clist*/
202         char *str[7];
203
204         for(i=0;i<7;i++) {
205                 str[i]=g_malloc(sizeof(char[256]));
206         }
207
208         /* clear list before printing */
209         gtk_clist_clear(ms->table);
210
211         for(i=0;i<NUM_TIMESTATS;i++) {
212                 /* nothing seen, nothing to do */
213                 if(ms->rtd[i].num==0){
214                         continue;
215                 }
216
217                 g_snprintf(str[0], 256, "%s", val_to_str(i,mgcp_mesage_type,"Other"));
218                 g_snprintf(str[1], 256, "%d", ms->rtd[i].num);
219                 g_snprintf(str[2], 256, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].min)));
220                 g_snprintf(str[3], 256, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].max)));
221                 g_snprintf(str[4], 256, "%8.2f msec", get_average(&(ms->rtd[i].tot), ms->rtd[i].num));
222                 g_snprintf(str[5], 256, "%6u", ms->rtd[i].min_num);
223                 g_snprintf(str[6], 256, "%6u", ms->rtd[i].max_num);
224                 gtk_clist_append(ms->table, str);
225         }
226
227         gtk_widget_show(GTK_WIDGET(ms->table));
228         for(i=0;i<7;i++) {
229                 g_free(str[i]);
230         }
231 }
232
233 void protect_thread_critical_region(void);
234 void unprotect_thread_critical_region(void);
235 static void
236 win_destroy_cb(GtkWindow *win _U_, gpointer data)
237 {
238         mgcpstat_t *ms=(mgcpstat_t *)data;
239
240         protect_thread_critical_region();
241         remove_tap_listener(ms);
242         unprotect_thread_critical_region();
243
244         if(ms->filter){
245                 g_free(ms->filter);
246                 ms->filter=NULL;
247         }
248         g_free(ms);
249 }
250
251 static const gchar *titles[]={
252                         "Type",
253                         "Messages",
254                         "Min SRT",
255                         "Max SRT",
256                         "Avg SRT",
257                         "Min in Frame",
258                         "Max in Frame" };
259
260 void
261 gtk_mgcpstat_init(const char *optarg)
262 {
263         mgcpstat_t *ms;
264         const char *filter=NULL;
265         GString *error_string;
266         GtkWidget *bt_close;
267         GtkWidget *bbox;
268
269         if(strncmp(optarg,"mgcp,srt,",9) == 0){
270                 filter=optarg+9;
271         } else {
272                 filter="";
273         }
274
275         ms=g_malloc(sizeof(mgcpstat_t));
276         ms->filter=g_strdup(filter);
277
278         mgcpstat_reset(ms);
279
280         ms->win=window_new(GTK_WINDOW_TOPLEVEL, "MGCP SRT");
281         gtk_window_set_default_size(GTK_WINDOW(ms->win), 550, 150);
282
283         ms->vbox=gtk_vbox_new(FALSE, 3);
284
285         init_main_stat_window(ms->win, ms->vbox, "MGCP Service Response Time (SRT) Statistics", filter);
286
287         /* GTK1 using a scrollable clist*/
288         /* init a scrolled window*/
289         ms->scrolled_window = scrolled_window_new(NULL, NULL);
290
291         ms->table = create_stat_table(ms->scrolled_window, ms->vbox, 7, titles);
292
293         error_string=register_tap_listener("mgcp", ms, filter, mgcpstat_reset, mgcpstat_packet, mgcpstat_draw);
294         if(error_string){
295                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
296                 g_string_free(error_string, TRUE);
297                 g_free(ms->filter);
298                 g_free(ms);
299                 return;
300         }
301
302         /* Button row. */
303         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
304         gtk_box_pack_start(GTK_BOX(ms->vbox), bbox, FALSE, FALSE, 0);
305
306         bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
307         window_set_cancel_button(ms->win, bt_close, window_cancel_button_cb);
308
309         SIGNAL_CONNECT(ms->win, "delete_event", window_delete_event_cb, NULL);
310         SIGNAL_CONNECT(ms->win, "destroy", win_destroy_cb, ms);
311
312         gtk_widget_show_all(ms->win);
313         window_present(ms->win);
314         
315         cf_retap_packets(&cfile);
316 }
317
318 static tap_dfilter_dlg mgcp_srt_dlg = {
319         "MGCP Service Response Time (SRT) Statistics",
320         "mgcp,srt",
321         gtk_mgcpstat_init,
322         -1
323 };
324
325 void
326 register_tap_listener_gtkmgcpstat(void)
327 {
328         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
329         if (find_tap_id("mgcp")) {
330                 register_stat_cmd_arg("mgcp,srt", gtk_mgcpstat_init);
331
332                 register_stat_menu_item("MGCP...", REGISTER_STAT_GROUP_RESPONSE_TIME,
333                     gtk_tap_dfilter_dlg_cb, NULL, NULL, &(mgcp_srt_dlg));
334         }
335 }