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