smb2-dissector: learn the "REPLAY_OPERATION" flag
[obnox/wireshark/wip.git] / ui / 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 <epan/tap.h>
45 #include <epan/dissectors/packet-bssap.h>
46 #include <epan/dissectors/packet-ansi_a.h>
47
48 #include "../stat_menu.h"
49
50 #include "ui/simple_dialog.h"
51
52 #include "ui/gtk/gui_stat_menu.h"
53 #include "ui/gtk/dlg_utils.h"
54 #include "ui/gtk/filter_dlg.h"
55 #include "ui/gtk/gui_utils.h"
56
57 #include "ui/gtk/old-gtk-compat.h"
58
59 enum
60 {
61    IEI_COLUMN,
62    MSG_NAME_COLUMN,
63    COUNT_COLUMN,
64    N_COLUMN /* The number of columns */
65 };
66
67 typedef struct _ansi_a_stat_dlg_t {
68     GtkWidget           *win;
69     GtkWidget           *scrolled_win;
70     GtkWidget           *table;
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 gboolean
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(FALSE);
119     }
120
121     return(TRUE);
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;
131     GtkListStore   *list_store;
132     GtkTreeIter     iter;
133
134     if (dlg_bsmap.win && tapdata)
135     {
136         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_bsmap.table))); /* Get store */
137
138         i = 0;
139         while (ansi_a_bsmap_strings[i].strptr){
140             /* Creates a new row at position. iter will be changed to point to this new row.
141              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
142              * The row will be filled with the values given to this function.
143              * :
144              * should generally be preferred when inserting rows in a sorted list store.
145              */
146             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
147                                  IEI_COLUMN, ansi_a_bsmap_strings[i].value,
148                                  MSG_NAME_COLUMN, (char *)ansi_a_bsmap_strings[i].strptr,
149                                  COUNT_COLUMN, stat_p->bsmap_message_type[ansi_a_bsmap_strings[i].value],
150                                  -1);
151             i++;
152         }
153     }
154
155     if (dlg_dtap.win && tapdata){
156         i = 0;
157         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_dtap.table))); /* Get store */
158
159
160         while (ansi_a_dtap_strings[i].strptr){
161             /* Creates a new row at position. iter will be changed to point to this new row.
162              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
163              * The row will be filled with the values given to this function.
164              * :
165              * should generally be preferred when inserting rows in a sorted list store.
166              */
167             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
168                                  IEI_COLUMN, ansi_a_dtap_strings[i].value,
169                                  MSG_NAME_COLUMN, (char *)ansi_a_dtap_strings[i].strptr,
170                                  COUNT_COLUMN, stat_p->dtap_message_type[ansi_a_dtap_strings[i].value],
171                                  -1);
172
173             i++;
174         }
175     }
176 }
177
178 static void
179 ansi_a_stat_gtk_win_destroy_cb(
180     GtkWindow   *win _U_,
181     gpointer     user_data )
182 {
183     memset((void *) user_data, 0, sizeof(ansi_a_stat_dlg_t));
184 }
185
186
187 /* Create list */
188 static
189 GtkWidget* create_list(void)
190 {
191
192     GtkListStore *list_store;
193     GtkWidget *list;
194     GtkTreeViewColumn *column;
195     GtkCellRenderer *renderer;
196     GtkTreeSortable *sortable;
197     GtkTreeView     *list_view;
198     GtkTreeSelection  *selection;
199
200     /* Create the store */
201     list_store = gtk_list_store_new(N_COLUMN,           /* Total number of columns XXX  */
202                                     G_TYPE_UINT,        /* IEI                          */
203                                     G_TYPE_STRING,      /* Message Name                 */
204                                     G_TYPE_UINT);       /* Count                        */
205
206     /* Create a view */
207     list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
208
209     list_view = GTK_TREE_VIEW(list);
210     sortable = GTK_TREE_SORTABLE(list_store);
211
212     /* Speed up the list display */
213     gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
214
215     /* Setup the sortable columns */
216     gtk_tree_sortable_set_sort_column_id(sortable, IEI_COLUMN, GTK_SORT_ASCENDING);
217     gtk_tree_view_set_headers_clickable(list_view, FALSE);
218
219     /* The view now holds a reference.  We can get rid of our own reference */
220     g_object_unref (G_OBJECT (list_store));
221
222     /*
223      * Create the first column packet, associating the "text" attribute of the
224      * cell_renderer to the first column of the model
225      */
226     renderer = gtk_cell_renderer_text_new ();
227     column = gtk_tree_view_column_new_with_attributes ("IEI",  renderer,
228                                                        "text", IEI_COLUMN,
229                                                        NULL);
230
231     gtk_tree_view_column_set_cell_data_func(column, renderer, present_as_hex_func,
232                                             GINT_TO_POINTER(IEI_COLUMN), NULL);
233
234     gtk_tree_view_column_set_sort_column_id(column, IEI_COLUMN);
235     gtk_tree_view_column_set_resizable(column, TRUE);
236     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
237     gtk_tree_view_column_set_min_width(column, 50);
238
239     /* Add the column to the view. */
240     gtk_tree_view_append_column (list_view, column);
241
242     /* Second column.. Message Name. */
243     renderer = gtk_cell_renderer_text_new ();
244     column = gtk_tree_view_column_new_with_attributes ("Message Name", renderer,
245                                                        "text", MSG_NAME_COLUMN,
246                                                        NULL);
247     gtk_tree_view_column_set_sort_column_id(column, MSG_NAME_COLUMN);
248     gtk_tree_view_column_set_resizable(column, TRUE);
249     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
250     gtk_tree_view_column_set_min_width(column, 280);
251     gtk_tree_view_append_column (list_view, column);
252
253     /* Third column.. Count. */
254     renderer = gtk_cell_renderer_text_new ();
255     column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
256                                                        "text", COUNT_COLUMN,
257                                                        NULL);
258
259
260     gtk_tree_view_column_set_sort_column_id(column, COUNT_COLUMN);
261     gtk_tree_view_column_set_resizable(column, TRUE);
262     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
263     gtk_tree_view_column_set_min_width(column, 50);
264     gtk_tree_view_append_column (list_view, column);
265
266     /* Now enable the sorting of each column */
267     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
268     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
269
270     /* Setup the selection handler */
271     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
272     gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
273
274     return list;
275
276 }
277 static void
278 ansi_a_stat_gtk_win_create(
279     ansi_a_stat_dlg_t   *dlg_p,
280     const char          *title)
281 {
282     GtkWidget           *vbox;
283     GtkWidget           *bt_close;
284     GtkWidget           *bbox;
285
286
287     dlg_p->win= dlg_window_new(title);  /* transient_for top_level */
288     gtk_window_set_destroy_with_parent (GTK_WINDOW(dlg_p->win), TRUE);
289
290     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 480, 450);
291
292     vbox=gtk_vbox_new(FALSE, 3);
293     gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
294     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
295
296     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
297     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
298
299     dlg_p->table = create_list();
300     gtk_widget_show(dlg_p->table);
301
302
303     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
304
305     /* Button row. */
306     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
307     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
308
309     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
310     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
311
312     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
313     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(ansi_a_stat_gtk_win_destroy_cb), dlg_p);
314
315     gtk_widget_show_all(dlg_p->win);
316     window_present(dlg_p->win);
317 }
318
319 void
320 ansi_a_stat_gtk_bsmap_cb(GtkAction *action _U_, gpointer user_data _U_)
321 {
322     /*
323      * if the window is already open, bring it to front
324      */
325     if (dlg_bsmap.win)
326     {
327         gdk_window_raise(gtk_widget_get_window(dlg_bsmap.win));
328         return;
329     }
330
331     ansi_a_stat_gtk_win_create(&dlg_bsmap, "ANSI A-I/F BSMAP Statistics");
332     ansi_a_stat_draw(&ansi_a_stat);
333 }
334
335 void
336 ansi_a_stat_gtk_dtap_cb(GtkAction *action _U_, gpointer user_data _U_)
337 {
338
339     /*
340      * if the window is already open, bring it to front
341      */
342     if (dlg_dtap.win)
343     {
344         gdk_window_raise(gtk_widget_get_window(dlg_dtap.win));
345         return;
346     }
347
348     ansi_a_stat_gtk_win_create(&dlg_dtap, "ANSI A-I/F DTAP Statistics");
349     ansi_a_stat_draw(&ansi_a_stat);
350 }
351
352 void
353 register_tap_listener_gtkansi_a_stat(void)
354 {
355     GString         *err_p;
356
357
358     memset((void *) &ansi_a_stat, 0, sizeof(ansi_a_stat_t));
359
360     err_p =
361         register_tap_listener("ansi_a", &ansi_a_stat, NULL, 0,
362                               ansi_a_stat_reset,
363                               ansi_a_stat_packet,
364                               ansi_a_stat_draw);
365
366     if (err_p != NULL)
367     {
368         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_p->str);
369         g_string_free(err_p, TRUE);
370
371         exit(1);
372     }
373 }
374