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