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