Squelch more const warnings (and fix some memory leaks that found).
[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 "tap_menu.h"
43 #include <epan/tap.h>
44 #include "../register.h"
45 #include "../plugins/mgcp/packet-mgcp.h"
46 #include "../timestats.h"
47 #include "gtk_stat_util.h"
48 #include "compat_macros.h"
49 #include "../simple_dialog.h"
50 #include "dlg_utils.h"
51 #include "../file.h"
52 #include "../globals.h"
53 #include "../tap_dfilter_dlg.h"
54 #include "tap_dfilter_dlg.h"
55 #include "ui_util.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
119         switch (mi->mgcp_type) {
120
121         case MGCP_REQUEST:
122                 if(mi->is_duplicate){
123                         /* Duplicate is ignored */
124                         ms->req_dup_num++;
125                         return 0;
126                 }
127                 else {
128                         ms->open_req_num++;
129                         return 0;
130                 }
131         break;
132
133         case MGCP_RESPONSE:
134                 if(mi->is_duplicate){
135                         /* Duplicate is ignored */
136                         ms->rsp_dup_num++;
137                         return 0;
138                 }
139                 else if (!mi->request_available) {
140                         /* no request was seen */
141                         ms->disc_rsp_num++;
142                         return 0;
143                 }
144                 else {
145                         ms->open_req_num--;
146                         /* calculate time delta between request and response */
147                         delta.secs=pinfo->fd->abs_secs-mi->req_time.secs;
148                         delta.nsecs=pinfo->fd->abs_usecs*1000-mi->req_time.nsecs;
149                         if(delta.nsecs<0){
150                                 delta.nsecs+=1000000000;
151                                 delta.secs--;
152                         }
153
154                         if (strncasecmp(mi->code, "EPCF", 4) == 0 ) {
155                                 time_stat_update(&(ms->rtd[0]),&delta, pinfo);
156                         }
157                         else if (strncasecmp(mi->code, "CRCX", 4) == 0 ) {
158                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
159                         }
160                         else if (strncasecmp(mi->code, "MDCX", 4) == 0 ) {
161                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
162                         }
163                         else if (strncasecmp(mi->code, "DLCX", 4) == 0 ) {
164                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
165                         }
166                         else if (strncasecmp(mi->code, "RQNT", 4) == 0 ) {
167                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
168                         }
169                         else if (strncasecmp(mi->code, "NTFY", 4) == 0 ) {
170                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
171                         }
172                         else if (strncasecmp(mi->code, "AUEP", 4) == 0 ) {
173                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
174                         }
175                         else if (strncasecmp(mi->code, "AUCX", 4) == 0 ) {
176                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
177                         }
178                         else if (strncasecmp(mi->code, "RSIP", 4) == 0 ) {
179                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
180                         }
181                         else {
182                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
183                         }
184
185                         return 1;
186                 }
187         break;
188
189         default:
190                 return 0;
191         break;
192         }
193 }
194
195 static void
196 mgcpstat_draw(void *pms)
197 {
198         mgcpstat_t *ms=(mgcpstat_t *)pms;
199         int i;
200         /* gtk1 using a scrollable clist*/
201         char *str[7];
202
203         for(i=0;i<7;i++) {
204                 str[i]=g_malloc(sizeof(char[256]));
205         }
206
207         /* clear list before printing */
208         gtk_clist_clear(ms->table);
209
210         for(i=0;i<NUM_TIMESTATS;i++) {
211                 /* nothing seen, nothing to do */
212                 if(ms->rtd[i].num==0){
213                         continue;
214                 }
215
216                 g_snprintf(str[0], 256, "%s", val_to_str(i,mgcp_mesage_type,"Other"));
217                 g_snprintf(str[1], 256, "%d", ms->rtd[i].num);
218                 g_snprintf(str[2], 256, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].min)));
219                 g_snprintf(str[3], 256, "%8.2f msec", nstime_to_msec(&(ms->rtd[i].max)));
220                 g_snprintf(str[4], 256, "%8.2f msec", get_average(&(ms->rtd[i].tot), ms->rtd[i].num));
221                 g_snprintf(str[5], 256, "%6u", ms->rtd[i].min_num);
222                 g_snprintf(str[6], 256, "%6u", ms->rtd[i].max_num);
223                 gtk_clist_append(ms->table, str);
224         }
225
226         gtk_widget_show(GTK_WIDGET(ms->table));
227         for(i=0;i<7;i++) {
228                 g_free(str[i]);
229         }
230 }
231
232 void protect_thread_critical_region(void);
233 void unprotect_thread_critical_region(void);
234 static void
235 win_destroy_cb(GtkWindow *win _U_, gpointer data)
236 {
237         mgcpstat_t *ms=(mgcpstat_t *)data;
238
239         protect_thread_critical_region();
240         remove_tap_listener(ms);
241         unprotect_thread_critical_region();
242
243         if(ms->filter){
244                 g_free(ms->filter);
245                 ms->filter=NULL;
246         }
247         g_free(ms);
248 }
249
250 static const gchar *titles[]={
251                         "Type",
252                         "Messages",
253                         "Min SRT",
254                         "Max SRT",
255                         "Avg SRT",
256                         "Min in Frame",
257                         "Max in Frame" };
258
259 void
260 gtk_mgcpstat_init(const char *optarg)
261 {
262         mgcpstat_t *ms;
263         const char *filter=NULL;
264         GString *error_string;
265         GtkWidget *bt_close;
266         GtkWidget *bbox;
267
268         if(strncmp(optarg,"mgcp,srt,",9) == 0){
269                 filter=optarg+9;
270         } else {
271                 filter="";
272         }
273
274         ms=g_malloc(sizeof(mgcpstat_t));
275         ms->filter=g_strdup(filter);
276
277         mgcpstat_reset(ms);
278
279         ms->win=window_new(GTK_WINDOW_TOPLEVEL, "MGCP SRT");
280         gtk_window_set_default_size(GTK_WINDOW(ms->win), 550, 150);
281
282         ms->vbox=gtk_vbox_new(FALSE, 3);
283
284         init_main_stat_window(ms->win, ms->vbox, "MGCP Service Response Time (SRT) Statistics", filter);
285
286         /* GTK1 using a scrollable clist*/
287         /* init a scrolled window*/
288         ms->scrolled_window = scrolled_window_new(NULL, NULL);
289
290         ms->table = create_stat_table(ms->scrolled_window, ms->vbox, 7, titles);
291
292         error_string=register_tap_listener("mgcp", ms, filter, mgcpstat_reset, mgcpstat_packet, mgcpstat_draw);
293         if(error_string){
294                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
295                 g_string_free(error_string, TRUE);
296                 g_free(ms->filter);
297                 g_free(ms);
298                 return;
299         }
300
301         /* Button row. */
302         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
303         gtk_box_pack_start(GTK_BOX(ms->vbox), bbox, FALSE, FALSE, 0);
304
305         bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
306         window_set_cancel_button(ms->win, bt_close, window_cancel_button_cb);
307
308         SIGNAL_CONNECT(ms->win, "delete_event", window_delete_event_cb, NULL);
309         SIGNAL_CONNECT(ms->win, "destroy", win_destroy_cb, ms);
310
311         gtk_widget_show_all(ms->win);
312         window_present(ms->win);
313         
314         cf_retap_packets(&cfile);
315 }
316
317 static tap_dfilter_dlg mgcp_srt_dlg = {
318         "MGCP Service Response Time (SRT) Statistics",
319         "mgcp,srt",
320         gtk_mgcpstat_init,
321         -1
322 };
323
324 void
325 register_tap_listener_gtkmgcpstat(void)
326 {
327         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
328         if (find_tap_id("mgcp")) {
329                 register_tap_listener_cmd_arg("mgcp,srt", gtk_mgcpstat_init);
330
331                 register_tap_menu_item("MGCP...", REGISTER_TAP_GROUP_RESPONSE_TIME,
332                     gtk_tap_dfilter_dlg_cb, NULL, NULL, &(mgcp_srt_dlg));
333         }
334 }