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