add
[obnox/wireshark/wip.git] / gtk / isup_stat.c
1 /* isup_stat.c
2  *
3  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * MUCH code modified from service_response_time_table.c.
7  *
8  * $Id$
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * This TAP provides statistics for ISUP:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <string.h>
39
40 #include "epan/packet_info.h"
41 #include "epan/epan.h"
42 #include "epan/value_string.h"
43 #include "tap_menu.h"
44 #include "image/clist_ascend.xpm"
45 #include "image/clist_descend.xpm"
46 #include "simple_dialog.h"
47 #include "dlg_utils.h"
48 #include <epan/tap.h>
49 #include "../register.h"
50 #include "../globals.h"
51 #include "filter_dlg.h"
52 #include "compat_macros.h"
53 #include <epan/dissectors/packet-isup.h>
54 #include "ui_util.h"
55
56
57 typedef struct column_arrows {
58     GtkWidget           *table;
59     GtkWidget           *ascend_pm;
60     GtkWidget           *descend_pm;
61 } column_arrows;
62
63 typedef struct _isup_stat_dlg_t {
64     GtkWidget           *win;
65     GtkWidget           *scrolled_win;
66     GtkWidget           *table;
67     char                *entries[4];
68 } isup_stat_dlg_t;
69
70 typedef struct _isup_stat_t {
71     int                 message_type[ISUP_MAX_NUM_MESSAGE_TYPES];
72 } isup_stat_t;
73
74
75 static isup_stat_dlg_t  dlg;
76 static isup_stat_t      stat;
77
78
79 static void
80 isup_stat_reset(
81     void                *tapdata)
82 {
83     isup_stat_t         *stat_p = tapdata;
84
85     memset(stat_p, 0, sizeof(isup_stat_t));
86 }
87
88
89 static int
90 isup_stat_packet(
91     void                *tapdata,
92     packet_info         *pinfo _U_,
93     epan_dissect_t      *edt _U_,
94     const void          *data)
95 {
96     isup_stat_t         *stat_p = tapdata;
97     const isup_tap_rec_t        *data_p = data;
98
99 #if 0   /* always false because message_type is 8 bit value */
100     if (data_p->message_type >= ISUP_MAX_NUM_MESSAGE_TYPES)
101     {
102         /*
103          * unknown message type !!!
104          */
105         return(0);
106     }
107 #endif
108
109     stat_p->message_type[data_p->message_type]++;
110
111     return(1);
112 }
113
114
115 static void
116 isup_stat_draw(
117     void                *tapdata)
118 {
119     isup_stat_t         *stat_p = tapdata;
120     int                 i, j;
121     char                *strp;
122
123     if (dlg.win && tapdata)
124     {
125         i = 0;
126
127         while (isup_message_type_value[i].strptr)
128         {
129             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
130
131             strp = g_strdup_printf("%d",
132                     stat_p->message_type[isup_message_type_value[i].value]);
133             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 3, strp);
134             g_free(strp);
135
136             i++;
137         }
138
139         gtk_clist_sort(GTK_CLIST(dlg.table));
140     }
141 }
142
143
144 static void
145 isup_stat_gtk_click_column_cb(
146     GtkCList            *clist,
147     gint                column,
148     gpointer            data)
149 {
150     column_arrows       *col_arrows = (column_arrows *) data;
151     int                 i;
152
153
154     gtk_clist_freeze(clist);
155
156     for (i=0; i < 4; i++)
157     {
158         gtk_widget_hide(col_arrows[i].ascend_pm);
159         gtk_widget_hide(col_arrows[i].descend_pm);
160     }
161
162     if (column == clist->sort_column)
163     {
164         if (clist->sort_type == GTK_SORT_ASCENDING)
165         {
166             clist->sort_type = GTK_SORT_DESCENDING;
167             gtk_widget_show(col_arrows[column].descend_pm);
168         }
169         else
170         {
171             clist->sort_type = GTK_SORT_ASCENDING;
172             gtk_widget_show(col_arrows[column].ascend_pm);
173         }
174     }
175     else
176     {
177         /*
178          * Columns 0-2 sorted in descending order by default
179          * Columns 3 sorted in ascending order by default
180          */
181         if (column <= 2)
182         {
183             clist->sort_type = GTK_SORT_ASCENDING;
184             gtk_widget_show(col_arrows[column].ascend_pm);
185         }
186         else
187         {
188             clist->sort_type = GTK_SORT_DESCENDING;
189             gtk_widget_show(col_arrows[column].descend_pm);
190         }
191
192         gtk_clist_set_sort_column(clist, column);
193     }
194
195     gtk_clist_thaw(clist);
196     gtk_clist_sort(clist);
197 }
198
199
200 static gint
201 isup_stat_gtk_sort_column(
202     GtkCList            *clist,
203     gconstpointer       ptr1,
204     gconstpointer       ptr2)
205 {
206     const GtkCListRow           *row1 = (const GtkCListRow *) ptr1;
207     const GtkCListRow           *row2 = (const GtkCListRow *) ptr2;
208     char                *text1 = NULL;
209     char                *text2 = NULL;
210     int                 i1, i2;
211
212     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
213     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
214
215     switch (clist->sort_column)
216     {
217     case 0:
218         /* FALLTHRU */
219
220     case 3:
221         i1 = strtol(text1, NULL, 0);
222         i2 = strtol(text2, NULL, 0);
223         return(i1 - i2);
224
225     case 2:
226     case 1:
227         return(strcmp(text1, text2));
228     }
229
230     g_assert_not_reached();
231
232     return(0);
233 }
234
235
236
237 static void
238 isup_stat_gtk_win_destroy_cb(
239     GtkWindow           *win _U_,
240     gpointer            user_data _U_)
241 {
242     memset((void *) user_data, 0, sizeof(isup_stat_dlg_t));
243 }
244
245
246 static void
247 isup_stat_gtk_win_create(
248     isup_stat_dlg_t     *dlg_p,
249     char                *title)
250 {
251 #define INIT_TABLE_NUM_COLUMNS  4
252     char                *default_titles[] = { "ID", "Acronym", "Message Name", "Count" };
253     int                 i;
254     column_arrows       *col_arrows;
255     GtkWidget           *column_lb;
256     GtkWidget           *vbox;
257     GtkWidget           *bt_close;
258     GtkWidget           *bbox;
259
260
261     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
262     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 560, 450);
263
264     vbox = gtk_vbox_new(FALSE, 3);
265         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
266     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
267
268     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
269     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
270
271     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
272
273     col_arrows =
274         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
275
276     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
277     {
278         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
279
280         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
281
282         column_lb = gtk_label_new(default_titles[i]);
283
284         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
285             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
286
287         gtk_widget_show(column_lb);
288
289         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
290
291         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
292             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
293
294         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
295
296         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
297             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
298
299         if (i == 0)
300         {
301             /* default column sorting */
302             gtk_widget_show(col_arrows[i].ascend_pm);
303         }
304
305         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
306         gtk_widget_show(col_arrows[i].table);
307     }
308     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
309
310     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), isup_stat_gtk_sort_column);
311     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
312     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
313
314     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 60);
315     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 60);
316     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 290);
317     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 3, 50);
318
319     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
320     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
321     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
322
323     SIGNAL_CONNECT(dlg_p->table, "click-column", isup_stat_gtk_click_column_cb, col_arrows);
324
325         /* Button row. */
326     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
327     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
328
329     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
330     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
331
332     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
333     SIGNAL_CONNECT(dlg_p->win, "destroy", isup_stat_gtk_win_destroy_cb, dlg_p);
334
335     gtk_widget_show_all(dlg_p->win);
336     window_present(dlg_p->win);
337 }
338
339
340 /*
341  * Never gets called ?
342  */
343 static void
344 isup_stat_gtk_init(
345     char                *optarg)
346 {
347     /* does not appear to be called */
348
349     optarg = optarg;
350 }
351
352
353 static void
354 isup_stat_gtk_cb(
355     GtkWidget           *w _U_,
356     gpointer            d _U_)
357 {
358     int                 i;
359     char                str[100];
360
361
362     /*
363      * if the window is already open, bring it to front
364      */
365     if (dlg.win)
366     {
367         gdk_window_raise(dlg.win->window);
368         return;
369     }
370
371     isup_stat_gtk_win_create(&dlg, "ISUP Message Type Statistics");
372
373     i = 0;
374     while (isup_message_type_value[i].strptr)
375     {
376         g_snprintf(str, 100, "%u", isup_message_type_value[i].value);
377         dlg.entries[0] = g_strdup(str);
378
379         dlg.entries[1] = g_strdup(isup_message_type_value_acro[i].strptr);
380
381         dlg.entries[2] = g_strdup(isup_message_type_value[i].strptr);
382
383         dlg.entries[3] = g_strdup("0");
384
385         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
386         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
387
388         i++;
389     }
390
391     isup_stat_draw(NULL);
392 }
393
394
395 void
396 register_tap_listener_gtkisup_stat(void)
397 {
398     GString             *err_p;
399
400
401     register_tap_listener_cmd_arg("isup,", isup_stat_gtk_init);
402
403     memset((void *) &stat, 0, sizeof(isup_stat_t));
404
405     err_p =
406         register_tap_listener("isup", &stat, NULL,
407             isup_stat_reset,
408             isup_stat_packet,
409             isup_stat_draw);
410
411     if (err_p != NULL)
412     {
413         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
414         g_string_free(err_p, TRUE);
415
416         exit(1);
417     }
418
419     register_tap_menu_item("ISUP Message Types", REGISTER_TAP_GROUP_TELEPHONY,
420         isup_stat_gtk_cb, NULL, NULL, NULL);
421 }