bf3745d92eb46473a080089e71648064175157e4
[obnox/wireshark/wip.git] / gtk / ansi_a_stat.c
1 /* ansi_a_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  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
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 the ANSI A-Interface:
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 <epan/stat_cmd_args.h>
44 #include "../stat_menu.h"
45 #include "gui_stat_menu.h"
46 #include "image/clist_ascend.xpm"
47 #include "image/clist_descend.xpm"
48 #include "simple_dialog.h"
49 #include "dlg_utils.h"
50 #include <epan/tap.h>
51 #include "../register.h"
52 #include <epan/dissectors/packet-bssap.h>
53 #include <epan/dissectors/packet-ansi_a.h>
54 #include "../globals.h"
55 #include "filter_dlg.h"
56 #include "compat_macros.h"
57 #include "gui_utils.h"
58
59
60 typedef struct column_arrows {
61     GtkWidget           *table;
62     GtkWidget           *ascend_pm;
63     GtkWidget           *descend_pm;
64 } column_arrows;
65
66 typedef struct _ansi_a_stat_dlg_t {
67     GtkWidget           *win;
68     GtkWidget           *scrolled_win;
69     GtkWidget           *table;
70     char                *entries[3];
71 } ansi_a_stat_dlg_t;
72
73 typedef struct _ansi_a_stat_t {
74     int                 bsmap_message_type[0xff];
75     int                 dtap_message_type[0xff];
76 } ansi_a_stat_t;
77
78
79 static ansi_a_stat_dlg_t        dlg_bsmap;
80 static ansi_a_stat_dlg_t        dlg_dtap;
81 static ansi_a_stat_t            ansi_a_stat;
82
83
84 static void
85 ansi_a_stat_reset(
86     void                *tapdata)
87 {
88     ansi_a_stat_t       *stat_p = tapdata;
89
90     memset(stat_p, 0, sizeof(ansi_a_stat_t));
91 }
92
93
94 static int
95 ansi_a_stat_packet(
96     void                *tapdata,
97     packet_info         *pinfo _U_,
98     epan_dissect_t      *edt _U_,
99     const void          *data)
100 {
101     ansi_a_stat_t       *stat_p = tapdata;
102     const ansi_a_tap_rec_t      *data_p = data;
103
104     switch (data_p->pdu_type)
105     {
106     case BSSAP_PDU_TYPE_BSMAP:
107         stat_p->bsmap_message_type[data_p->message_type]++;
108         break;
109
110     case BSSAP_PDU_TYPE_DTAP:
111         stat_p->dtap_message_type[data_p->message_type]++;
112         break;
113
114     default:
115         /*
116          * unknown PDU type !!!
117          */
118         return(0);
119     }
120
121     return(1);
122 }
123
124
125 static void
126 ansi_a_stat_draw(
127     void                *tapdata)
128 {
129     ansi_a_stat_t       *stat_p = tapdata;
130     int                 i, j;
131     char                *strp;
132
133     if (dlg_bsmap.win && tapdata)
134     {
135         i = 0;
136
137         while (ansi_a_bsmap_strings[i].strptr)
138         {
139             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg_bsmap.table), (gpointer)(long) i);
140
141             strp = g_strdup_printf("%d",
142                     stat_p->bsmap_message_type[ansi_a_bsmap_strings[i].value]);
143             gtk_clist_set_text(GTK_CLIST(dlg_bsmap.table), j, 2, strp);
144             g_free(strp);
145
146             i++;
147         }
148
149         gtk_clist_sort(GTK_CLIST(dlg_bsmap.table));
150     }
151
152     if (dlg_dtap.win && tapdata)
153     {
154         i = 0;
155
156         while (ansi_a_dtap_strings[i].strptr)
157         {
158             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg_dtap.table), (gpointer)(long) i);
159
160             strp = g_strdup_printf("%d",
161                     stat_p->dtap_message_type[ansi_a_dtap_strings[i].value]);
162             gtk_clist_set_text(GTK_CLIST(dlg_dtap.table), j, 2, strp);
163             g_free(strp);
164
165             i++;
166         }
167
168         gtk_clist_sort(GTK_CLIST(dlg_dtap.table));
169     }
170 }
171
172
173 static void
174 ansi_a_stat_gtk_click_column_cb(
175     GtkCList            *clist,
176     gint                column,
177     gpointer            data)
178 {
179     column_arrows       *col_arrows = (column_arrows *) data;
180     int                 i;
181
182
183     gtk_clist_freeze(clist);
184
185     for (i=0; i < 3; i++)
186     {
187         gtk_widget_hide(col_arrows[i].ascend_pm);
188         gtk_widget_hide(col_arrows[i].descend_pm);
189     }
190
191     if (column == clist->sort_column)
192     {
193         if (clist->sort_type == GTK_SORT_ASCENDING)
194         {
195             clist->sort_type = GTK_SORT_DESCENDING;
196             gtk_widget_show(col_arrows[column].descend_pm);
197         }
198         else
199         {
200             clist->sort_type = GTK_SORT_ASCENDING;
201             gtk_widget_show(col_arrows[column].ascend_pm);
202         }
203     }
204     else
205     {
206         /*
207          * Columns 0-1 sorted in descending order by default
208          * Columns 2 sorted in ascending order by default
209          */
210         if (column <= 1)
211         {
212             clist->sort_type = GTK_SORT_ASCENDING;
213             gtk_widget_show(col_arrows[column].ascend_pm);
214         }
215         else
216         {
217             clist->sort_type = GTK_SORT_DESCENDING;
218             gtk_widget_show(col_arrows[column].descend_pm);
219         }
220
221         gtk_clist_set_sort_column(clist, column);
222     }
223
224     gtk_clist_thaw(clist);
225     gtk_clist_sort(clist);
226 }
227
228
229 static gint
230 ansi_a_stat_gtk_sort_column(
231     GtkCList            *clist,
232     gconstpointer       ptr1,
233     gconstpointer       ptr2)
234 {
235     const GtkCListRow   *row1 = ptr1;
236     const GtkCListRow   *row2 = ptr2;
237     char                *text1 = NULL;
238     char                *text2 = NULL;
239     int                 i1, i2;
240
241     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
242     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
243
244     switch (clist->sort_column)
245     {
246     case 0:
247         /* FALLTHRU */
248
249     case 2:
250         i1 = strtol(text1, NULL, 0);
251         i2 = strtol(text2, NULL, 0);
252         return(i1 - i2);
253
254     case 1:
255         return(strcmp(text1, text2));
256     }
257
258     g_assert_not_reached();
259
260     return(0);
261 }
262
263
264 static void
265 ansi_a_stat_gtk_win_destroy_cb(
266     GtkWindow           *win _U_,
267     gpointer            user_data _U_)
268 {
269     memset((void *) user_data, 0, sizeof(ansi_a_stat_dlg_t));
270 }
271
272
273 static void
274 ansi_a_stat_gtk_win_create(
275     ansi_a_stat_dlg_t   *dlg_p,
276     const char          *title)
277 {
278 #define INIT_TABLE_NUM_COLUMNS  3
279     const char          *default_titles[] = { "IEI", "Message Name", "Count" };
280     int                 i;
281     column_arrows       *col_arrows;
282     GtkWidget           *column_lb;
283     GtkWidget           *vbox;
284     GtkWidget           *bt_close;
285     GtkWidget           *bbox;
286
287
288     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
289     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 480, 450);
290
291     vbox=gtk_vbox_new(FALSE, 3);
292     gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
293     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
294
295     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
296     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
297
298     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
299
300     col_arrows =
301         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
302
303     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
304     {
305         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
306
307         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
308
309         column_lb = gtk_label_new(default_titles[i]);
310
311         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
312             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
313
314         gtk_widget_show(column_lb);
315
316         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
317
318         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
319             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
320
321         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
322
323         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
324             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
325
326         if (i == 0)
327         {
328             /* default column sorting */
329             gtk_widget_show(col_arrows[i].ascend_pm);
330         }
331
332         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
333         gtk_widget_show(col_arrows[i].table);
334     }
335     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
336
337     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), ansi_a_stat_gtk_sort_column);
338     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
339     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
340
341     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 50);
342     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 280);
343     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 50);
344
345     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
346     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
347     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
348
349     g_signal_connect(dlg_p->table, "click-column", G_CALLBACK(ansi_a_stat_gtk_click_column_cb), col_arrows);
350
351         /* Button row. */
352     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
353     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
354
355     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
356     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
357
358     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
359     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(ansi_a_stat_gtk_win_destroy_cb), dlg_p);
360
361     gtk_widget_show_all(dlg_p->win);
362     window_present(dlg_p->win);
363 }
364
365
366 static void
367 ansi_a_stat_gtk_bsmap_cb(
368     GtkWidget           *w _U_,
369     gpointer            d _U_)
370 {
371     int                 i;
372
373
374     /*
375      * if the window is already open, bring it to front
376      */
377     if (dlg_bsmap.win)
378     {
379         gdk_window_raise(dlg_bsmap.win->window);
380         return;
381     }
382
383     ansi_a_stat_gtk_win_create(&dlg_bsmap, "ANSI A-I/F BSMAP Statistics");
384
385     i = 0;
386     while (ansi_a_bsmap_strings[i].strptr)
387     {
388         dlg_bsmap.entries[0] = g_strdup_printf("0x%02x",
389                                                ansi_a_bsmap_strings[i].value);
390
391         dlg_bsmap.entries[1] = g_strdup(ansi_a_bsmap_strings[i].strptr);
392
393         dlg_bsmap.entries[2] = g_strdup("0");
394
395         gtk_clist_insert(GTK_CLIST(dlg_bsmap.table), i, dlg_bsmap.entries);
396         gtk_clist_set_row_data(GTK_CLIST(dlg_bsmap.table), i, (gpointer)(long) i);
397
398         i++;
399     }
400
401     ansi_a_stat_draw(&ansi_a_stat);
402 }
403
404
405 static void
406 ansi_a_stat_gtk_bsmap_init(
407     const char          *optarg _U_, void* userdata _U_)
408 {
409     ansi_a_stat_gtk_bsmap_cb(NULL, NULL);
410 }
411
412
413 static void
414 ansi_a_stat_gtk_dtap_cb(
415     GtkWidget           *w _U_,
416     gpointer            d _U_)
417 {
418     int                 i;
419
420
421     /*
422      * if the window is already open, bring it to front
423      */
424     if (dlg_dtap.win)
425     {
426         gdk_window_raise(dlg_dtap.win->window);
427         return;
428     }
429
430     ansi_a_stat_gtk_win_create(&dlg_dtap, "ANSI A-I/F DTAP Statistics");
431
432     i = 0;
433     while (ansi_a_dtap_strings[i].strptr)
434     {
435         dlg_dtap.entries[0] = g_strdup_printf("0x%02x",
436                                               ansi_a_dtap_strings[i].value);
437
438         dlg_dtap.entries[1] = g_strdup(ansi_a_dtap_strings[i].strptr);
439
440         dlg_dtap.entries[2] = g_strdup("0");
441
442         gtk_clist_insert(GTK_CLIST(dlg_dtap.table), i, dlg_dtap.entries);
443         gtk_clist_set_row_data(GTK_CLIST(dlg_dtap.table), i, (gpointer)(long) i);
444
445         i++;
446     }
447
448     ansi_a_stat_draw(&ansi_a_stat);
449 }
450
451
452 static void
453 ansi_a_stat_gtk_dtap_init(
454     const char          *optarg _U_,
455     void* userdata _U_)
456 {
457     ansi_a_stat_gtk_dtap_cb(NULL, NULL);
458 }
459
460
461 void
462 register_tap_listener_gtkansi_a_stat(void)
463 {
464     GString             *err_p;
465
466
467     memset((void *) &ansi_a_stat, 0, sizeof(ansi_a_stat_t));
468
469     err_p =
470         register_tap_listener("ansi_a", &ansi_a_stat, NULL,
471             ansi_a_stat_reset,
472             ansi_a_stat_packet,
473             ansi_a_stat_draw);
474
475     if (err_p != NULL)
476     {
477         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
478         g_string_free(err_p, TRUE);
479
480         exit(1);
481     }
482
483     register_stat_menu_item("ANSI/A-Interface BSMAP", REGISTER_STAT_GROUP_TELEPHONY, 
484         ansi_a_stat_gtk_bsmap_cb, NULL, NULL ,NULL);
485     register_stat_cmd_arg("ansi_a,bsmap", ansi_a_stat_gtk_bsmap_init,NULL);
486
487     register_stat_menu_item("ANSI/A-Interface DTAP", REGISTER_STAT_GROUP_TELEPHONY,
488         ansi_a_stat_gtk_dtap_cb, NULL, NULL ,NULL);
489     register_stat_cmd_arg("ansi_a,dtap", ansi_a_stat_gtk_dtap_init, NULL);
490 }