carve out the (currently disabled) welcome page into it's own file to slightly reduce...
[obnox/wireshark/wip.git] / gtk / mtp3_stat.c
1 /* mtp3_stat.c
2  *
3  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * Modified from gsm_map_stat.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 MTP3:
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 <epan/emem.h>
52 #include "../register.h"
53 #include "../globals.h"
54 #include "filter_dlg.h"
55 #include "compat_macros.h"
56 #include "gui_utils.h"
57
58 #include <epan/dissectors/packet-mtp3.h>
59 #include "mtp3_stat.h"
60
61 typedef struct column_arrows {
62     GtkWidget           *table;
63     GtkWidget           *ascend_pm;
64     GtkWidget           *descend_pm;
65 } column_arrows;
66
67 #define MTP3_INIT_TABLE_NUM_COLUMNS             6
68
69 typedef struct _my_columns_t {
70     guint32             value;
71     const gchar         *strptr;
72     GtkJustification    just;
73 } my_columns_t;
74
75 static my_columns_t columns[MTP3_INIT_TABLE_NUM_COLUMNS] = {
76     { 80,       "OPC",                  GTK_JUSTIFY_LEFT },
77     { 80,       "DPC",                  GTK_JUSTIFY_LEFT },
78     { 110,      "SI",                   GTK_JUSTIFY_LEFT },
79     { 80,       "Num MSUs",             GTK_JUSTIFY_RIGHT },
80     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
81     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT }
82 };
83
84 typedef struct _mtp3_stat_dlg_t {
85     GtkWidget           *win;
86     GtkWidget           *scrolled_win;
87     GtkWidget           *table;
88     char                *entries[MTP3_INIT_TABLE_NUM_COLUMNS];
89 } mtp3_stat_dlg_t;
90
91 static mtp3_stat_dlg_t  dlg;
92
93 mtp3_stat_t             mtp3_stat[MTP3_MAX_NUM_OPC_DPC];
94 guint8                  mtp3_num_used;
95
96
97 static void
98 mtp3_stat_reset(
99     void                *tapdata)
100 {
101     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
102
103     mtp3_num_used = 0;
104     memset(stat_p, 0, MTP3_MAX_NUM_OPC_DPC * sizeof(mtp3_stat_t));
105
106     if (dlg.win != NULL)
107     {
108         gtk_clist_clear(GTK_CLIST(dlg.table));
109     }
110 }
111
112
113 static int
114 mtp3_stat_packet(
115     void                *tapdata,
116     packet_info         *pinfo _U_,
117     epan_dissect_t      *edt _U_,
118     const void          *data)
119 {
120     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
121     const mtp3_tap_rec_t        *data_p = data;
122     int                         i;
123
124     if (data_p->si_code >= MTP3_NUM_SI_CODE)
125     {
126         /*
127          * we thought this si_code was not used ?
128          * is MTP3_NUM_SI_CODE out of date ?
129          */
130         return(0);
131     }
132
133     /*
134      * look for opc/dpc pair
135      */
136     i = 0;
137     while (i < mtp3_num_used)
138     {
139         if (memcmp(&data_p->addr_opc, &(*stat_p)[i].addr_opc, sizeof(mtp3_addr_pc_t)) == 0)
140         {
141             if (memcmp(&data_p->addr_dpc, &(*stat_p)[i].addr_dpc, sizeof(mtp3_addr_pc_t)) == 0)
142             {
143                 break;
144             }
145         }
146
147         i++;
148     }
149
150     if (i == mtp3_num_used)
151     {
152         if (mtp3_num_used == MTP3_MAX_NUM_OPC_DPC)
153         {
154             /*
155              * too many
156              */
157             return(0);
158         }
159
160         mtp3_num_used++;
161     }
162
163     (*stat_p)[i].addr_opc = data_p->addr_opc;
164     (*stat_p)[i].addr_dpc = data_p->addr_dpc;
165     (*stat_p)[i].si_code[data_p->si_code].num_msus++;
166     (*stat_p)[i].si_code[data_p->si_code].size += data_p->size;
167
168     return(1);
169 }
170
171
172 static void
173 mtp3_stat_draw(
174     void                *tapdata)
175 {
176     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
177     int                 i, j, row_offset;
178     char                *str;
179
180     if (!dlg.win || !tapdata)
181     {
182         return;
183     }
184
185     str=ep_alloc(256);
186     i = 0;
187
188     while (i < mtp3_num_used)
189     {
190         row_offset = i * MTP3_NUM_SI_CODE;
191
192         mtp3_addr_to_str_buf(&(*stat_p)[i].addr_opc, str, 256);
193         dlg.entries[0] = g_strdup(str);
194
195         mtp3_addr_to_str_buf(&(*stat_p)[i].addr_dpc, str, 256);
196         dlg.entries[1] = g_strdup(str);
197
198         for (j=0; j < MTP3_NUM_SI_CODE; j++)
199         {
200             dlg.entries[2] = g_strdup(mtp3_service_indicator_code_short_vals[j].strptr);
201
202             dlg.entries[3] = g_strdup_printf("%u", (*stat_p)[i].si_code[j].num_msus);
203
204             dlg.entries[4] = g_strdup_printf("%.0f", (*stat_p)[i].si_code[j].size);
205
206             dlg.entries[5] =
207                 g_strdup_printf("%.2f",
208                     (*stat_p)[i].si_code[j].size/(*stat_p)[i].si_code[j].num_msus);
209
210             gtk_clist_insert(GTK_CLIST(dlg.table), row_offset + j, dlg.entries);
211         }
212
213         i++;
214     }
215
216     gtk_clist_sort(GTK_CLIST(dlg.table));
217 }
218
219
220 static void
221 mtp3_stat_gtk_click_column_cb(
222     GtkCList            *clist,
223     gint                column,
224     gpointer            data)
225 {
226     column_arrows       *col_arrows = (column_arrows *) data;
227     int                 i;
228
229
230     gtk_clist_freeze(clist);
231
232     for (i=0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
233     {
234         gtk_widget_hide(col_arrows[i].ascend_pm);
235         gtk_widget_hide(col_arrows[i].descend_pm);
236     }
237
238     if (column == clist->sort_column)
239     {
240         if (clist->sort_type == GTK_SORT_ASCENDING)
241         {
242             clist->sort_type = GTK_SORT_DESCENDING;
243             gtk_widget_show(col_arrows[column].descend_pm);
244         }
245         else
246         {
247             clist->sort_type = GTK_SORT_ASCENDING;
248             gtk_widget_show(col_arrows[column].ascend_pm);
249         }
250     }
251     else
252     {
253         /*
254          * Columns 0-1 sorted in descending order by default
255          */
256         if (column <= 1)
257         {
258             clist->sort_type = GTK_SORT_ASCENDING;
259             gtk_widget_show(col_arrows[column].ascend_pm);
260         }
261         else
262         {
263             clist->sort_type = GTK_SORT_DESCENDING;
264             gtk_widget_show(col_arrows[column].descend_pm);
265         }
266
267         gtk_clist_set_sort_column(clist, column);
268     }
269
270     gtk_clist_thaw(clist);
271     gtk_clist_sort(clist);
272 }
273
274
275 static gint
276 mtp3_stat_gtk_sort_column(
277     GtkCList            *clist,
278     gconstpointer       ptr1,
279     gconstpointer       ptr2)
280 {
281     const GtkCListRow   *row1 = ptr1;
282     const GtkCListRow   *row2 = ptr2;
283     char                *text1 = NULL;
284     char                *text2 = NULL;
285     int                 i1, i2;
286
287     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
288     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
289
290     switch (clist->sort_column)
291     {
292     case 0:
293     case 1:
294     case 2:
295         /* text columns */
296         return(strcmp(text1, text2));
297
298     default:
299         /* number columns */
300         i1 = strtol(text1, NULL, 0);
301         i2 = strtol(text2, NULL, 0);
302         return(i1 - i2);
303     }
304
305     g_assert_not_reached();
306
307     return(0);
308 }
309
310
311 static void
312 mtp3_stat_gtk_win_destroy_cb(
313     GtkWindow           *win _U_,
314     gpointer            user_data _U_)
315 {
316     memset((void *) user_data, 0, sizeof(mtp3_stat_dlg_t));
317 }
318
319
320 static void
321 mtp3_stat_gtk_win_create(
322     mtp3_stat_dlg_t     *dlg_p,
323     const char          *title)
324 {
325     int                 i;
326     column_arrows       *col_arrows;
327     GtkWidget           *column_lb;
328     GtkWidget           *vbox;
329     GtkWidget           *bt_close;
330     GtkWidget           *bbox;
331
332
333     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
334     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 640, 390);
335
336     vbox = gtk_vbox_new(FALSE, 3);
337         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
338     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
339
340     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
341     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
342
343     dlg_p->table = gtk_clist_new(MTP3_INIT_TABLE_NUM_COLUMNS);
344
345     col_arrows =
346         (column_arrows *) g_malloc(sizeof(column_arrows) * MTP3_INIT_TABLE_NUM_COLUMNS);
347
348     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
349     {
350         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
351
352         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
353
354         column_lb = gtk_label_new(columns[i].strptr);
355
356         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
357             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
358
359         gtk_widget_show(column_lb);
360
361         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
362
363         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
364             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
365
366         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
367
368         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
369             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
370
371         if (i == 0)
372         {
373             /* default column sorting */
374             gtk_widget_show(col_arrows[i].ascend_pm);
375         }
376
377         gtk_clist_set_column_justification(GTK_CLIST(dlg_p->table), i, columns[i].just);
378
379         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
380         gtk_widget_show(col_arrows[i].table);
381     }
382     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
383
384     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), mtp3_stat_gtk_sort_column);
385     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
386     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
387
388     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
389     {
390         gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), i, columns[i].value);
391     }
392
393     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
394     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
395     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
396
397     SIGNAL_CONNECT(dlg_p->table, "click-column", mtp3_stat_gtk_click_column_cb, col_arrows);
398
399     /* Button row. */
400     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
401         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
402
403     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
404     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
405
406     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
407     SIGNAL_CONNECT(dlg_p->win, "destroy", mtp3_stat_gtk_win_destroy_cb, dlg_p);
408
409     gtk_widget_show_all(dlg_p->win);
410     window_present(dlg_p->win);
411 }
412
413
414 static void
415 mtp3_stat_gtk_cb(
416     GtkWidget           *w _U_,
417     gpointer            d _U_)
418 {
419
420     /*
421      * if the window is already open, bring it to front
422      */
423     if (dlg.win)
424     {
425         gdk_window_raise(dlg.win->window);
426         return;
427     }
428
429     mtp3_stat_gtk_win_create(&dlg, "MTP3 Statistics");
430
431     mtp3_stat_draw(&mtp3_stat);
432 }
433
434
435 static void
436 mtp3_stat_gtk_init( const char *optarg _U_, void* userdata _U_)
437 {
438     mtp3_stat_gtk_cb(NULL, NULL);
439 }
440
441
442 void
443 register_tap_listener_gtkmtp3_stat(void)
444 {
445     GString             *err_p;
446
447
448     memset((void *) &mtp3_stat, 0, sizeof(mtp3_stat_t));
449
450     err_p =
451         register_tap_listener("mtp3", &mtp3_stat, NULL,
452             mtp3_stat_reset,
453             mtp3_stat_packet,
454             mtp3_stat_draw);
455
456     if (err_p != NULL)
457     {
458         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, err_p->str);
459         g_string_free(err_p, TRUE);
460
461         exit(1);
462     }
463
464     register_stat_menu_item("MTP3/MSUs",  REGISTER_STAT_GROUP_TELEPHONY,
465         mtp3_stat_gtk_cb, NULL, NULL, NULL);
466     register_stat_cmd_arg("mtp3,msus", mtp3_stat_gtk_init,NULL);
467 }