From Laurent Rabret: handle the cases where there's no capture file, or
[obnox/wireshark/wip.git] / gtk / gsm_a_stat.c
1 /* gsm_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 GSM 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-gsm_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 _gsm_a_stat_dlg_t {
65     GtkWidget           *win;
66     GtkWidget           *scrolled_win;
67     GtkWidget           *table;
68     char                *entries[3];
69 } gsm_a_stat_dlg_t;
70
71 typedef struct _gsm_a_stat_t {
72     int         bssmap_message_type[0xff];
73     int         dtap_mm_message_type[0xff];
74     int         dtap_rr_message_type[0xff];
75     int         dtap_cc_message_type[0xff];
76     int         dtap_gmm_message_type[0xff];
77     int         dtap_sms_message_type[0xff];
78     int         dtap_sm_message_type[0xff];
79     int         dtap_ss_message_type[0xff];
80 } gsm_a_stat_t;
81
82
83 static gsm_a_stat_dlg_t         dlg_bssmap;
84 static gsm_a_stat_dlg_t         dlg_dtap_mm;
85 static gsm_a_stat_dlg_t         dlg_dtap_rr;
86 static gsm_a_stat_dlg_t         dlg_dtap_cc;
87 static gsm_a_stat_dlg_t         dlg_dtap_gmm;
88 static gsm_a_stat_dlg_t         dlg_dtap_sms;
89 static gsm_a_stat_dlg_t         dlg_dtap_sm;
90 static gsm_a_stat_dlg_t         dlg_dtap_ss;
91 static gsm_a_stat_t             stat;
92
93
94 static void
95 gsm_a_stat_reset(
96     void                *tapdata)
97 {
98     gsm_a_stat_t        *stat_p = tapdata;
99
100     memset(stat_p, 0, sizeof(gsm_a_stat_t));
101 }
102
103
104 static int
105 gsm_a_stat_packet(
106     void                *tapdata,
107     packet_info         *pinfo _U_,
108     epan_dissect_t      *edt _U_,
109     const void          *data)
110 {
111     gsm_a_stat_t        *stat_p = tapdata;
112     const gsm_a_tap_rec_t       *data_p = data;
113
114     switch (data_p->pdu_type)
115     {
116     case BSSAP_PDU_TYPE_BSSMAP:
117         stat_p->bssmap_message_type[data_p->message_type]++;
118         break;
119
120     case BSSAP_PDU_TYPE_DTAP:
121         switch (data_p->protocol_disc)
122         {
123         case PD_CC:
124             stat_p->dtap_cc_message_type[data_p->message_type]++;
125             break;
126         case PD_MM:
127             stat_p->dtap_mm_message_type[data_p->message_type]++;
128             break;
129         case PD_RR:
130             stat_p->dtap_rr_message_type[data_p->message_type]++;
131             break;
132         case PD_GMM:
133             stat_p->dtap_gmm_message_type[data_p->message_type]++;
134             break;
135         case PD_SMS:
136             stat_p->dtap_sms_message_type[data_p->message_type]++;
137             break;
138         case PD_SM:
139             stat_p->dtap_sm_message_type[data_p->message_type]++;
140             break;
141         case PD_SS:
142             stat_p->dtap_ss_message_type[data_p->message_type]++;
143             break;
144         default:
145             /*
146              * unsupported PD
147              */
148             return(0);
149         }
150         break;
151
152     default:
153         /*
154          * unknown PDU type !!!
155          */
156         return(0);
157     }
158
159     return(1);
160 }
161
162
163 static void
164 gsm_a_stat_draw_aux(
165     gsm_a_stat_dlg_t    *dlg_p,
166     int                 *message_count,
167     const value_string  *msg_strings)
168 {
169     int                 i, j;
170     char                *strp;
171
172
173     if (dlg_p->win != NULL)
174     {
175         i = 0;
176
177         while (msg_strings[i].strptr)
178         {
179             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg_p->table), (gpointer) i);
180
181             strp = g_strdup_printf("%d", message_count[msg_strings[i].value]);
182             gtk_clist_set_text(GTK_CLIST(dlg_p->table), j, 2, strp);
183             g_free(strp);
184
185             i++;
186         }
187
188         gtk_clist_sort(GTK_CLIST(dlg_p->table));
189     }
190 }
191
192 static void
193 gsm_a_stat_draw(
194     void                *tapdata)
195 {
196     gsm_a_stat_t        *stat_p = tapdata;
197
198         if (!tapdata) return;
199
200     if (dlg_bssmap.win != NULL)
201     {
202         gsm_a_stat_draw_aux(&dlg_bssmap,
203             stat_p->bssmap_message_type,
204             gsm_a_bssmap_msg_strings);
205     }
206
207     if (dlg_dtap_mm.win != NULL)
208     {
209         gsm_a_stat_draw_aux(&dlg_dtap_mm,
210             stat_p->dtap_mm_message_type,
211             gsm_a_dtap_msg_mm_strings);
212     }
213
214     if (dlg_dtap_rr.win != NULL)
215     {
216         gsm_a_stat_draw_aux(&dlg_dtap_rr,
217             stat_p->dtap_rr_message_type,
218             gsm_a_dtap_msg_rr_strings);
219     }
220
221     if (dlg_dtap_cc.win != NULL)
222     {
223         gsm_a_stat_draw_aux(&dlg_dtap_cc,
224             stat_p->dtap_cc_message_type,
225             gsm_a_dtap_msg_cc_strings);
226     }
227
228     if (dlg_dtap_gmm.win != NULL)
229     {
230         gsm_a_stat_draw_aux(&dlg_dtap_gmm,
231             stat_p->dtap_gmm_message_type,
232             gsm_a_dtap_msg_gmm_strings);
233     }
234
235     if (dlg_dtap_sms.win != NULL)
236     {
237         gsm_a_stat_draw_aux(&dlg_dtap_sms,
238             stat_p->dtap_sms_message_type,
239             gsm_a_dtap_msg_sms_strings);
240     }
241
242     if (dlg_dtap_sm.win != NULL)
243     {
244         gsm_a_stat_draw_aux(&dlg_dtap_sm,
245             stat_p->dtap_sm_message_type,
246             gsm_a_dtap_msg_sm_strings);
247     }
248
249     if (dlg_dtap_ss.win != NULL)
250     {
251         gsm_a_stat_draw_aux(&dlg_dtap_ss,
252             stat_p->dtap_ss_message_type,
253             gsm_a_dtap_msg_ss_strings);
254     }
255 }
256
257
258 static void
259 gsm_a_stat_gtk_click_column_cb(
260     GtkCList            *clist,
261     gint                column,
262     gpointer            data)
263 {
264     column_arrows       *col_arrows = (column_arrows *) data;
265     int                 i;
266
267
268     gtk_clist_freeze(clist);
269
270     for (i=0; i < 3; i++)
271     {
272         gtk_widget_hide(col_arrows[i].ascend_pm);
273         gtk_widget_hide(col_arrows[i].descend_pm);
274     }
275
276     if (column == clist->sort_column)
277     {
278         if (clist->sort_type == GTK_SORT_ASCENDING)
279         {
280             clist->sort_type = GTK_SORT_DESCENDING;
281             gtk_widget_show(col_arrows[column].descend_pm);
282         }
283         else
284         {
285             clist->sort_type = GTK_SORT_ASCENDING;
286             gtk_widget_show(col_arrows[column].ascend_pm);
287         }
288     }
289     else
290     {
291         /*
292          * Columns 0-1 sorted in descending order by default
293          * Columns 2 sorted in ascending order by default
294          */
295         if (column <= 1)
296         {
297             clist->sort_type = GTK_SORT_ASCENDING;
298             gtk_widget_show(col_arrows[column].ascend_pm);
299         }
300         else
301         {
302             clist->sort_type = GTK_SORT_DESCENDING;
303             gtk_widget_show(col_arrows[column].descend_pm);
304         }
305
306         gtk_clist_set_sort_column(clist, column);
307     }
308
309     gtk_clist_thaw(clist);
310     gtk_clist_sort(clist);
311 }
312
313
314 static gint
315 gsm_a_stat_gtk_sort_column(
316     GtkCList            *clist,
317     gconstpointer       ptr1,
318     gconstpointer       ptr2)
319 {
320     const GtkCListRow   *row1 = ptr1;
321     const GtkCListRow   *row2 = ptr2;
322     char                *text1 = NULL;
323     char                *text2 = NULL;
324     int                 i1, i2;
325
326     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
327     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
328
329     switch (clist->sort_column)
330     {
331     case 0:
332         /* FALLTHRU */
333
334     case 2:
335         i1 = strtol(text1, NULL, 0);
336         i2 = strtol(text2, NULL, 0);
337         return(i1 - i2);
338
339     case 1:
340         return(strcmp(text1, text2));
341     }
342
343     g_assert_not_reached();
344
345     return(0);
346 }
347
348
349 static void
350 gsm_a_stat_gtk_win_destroy_cb(
351     GtkWindow           *win _U_,
352     gpointer            user_data _U_)
353 {
354     memset((void *) user_data, 0, sizeof(gsm_a_stat_dlg_t));
355 }
356
357
358 static void
359 gsm_a_stat_gtk_win_create(
360     gsm_a_stat_dlg_t    *dlg_p,
361     char                *title)
362 {
363 #define INIT_TABLE_NUM_COLUMNS  3
364     char                *default_titles[] = { "IEI", "Message Name", "Count" };
365     int                 i;
366     column_arrows       *col_arrows;
367     GtkWidget           *column_lb;
368     GtkWidget           *vbox;
369     GtkWidget           *bt_close;
370     GtkWidget           *bbox;
371
372
373     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
374     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 490, 500);
375
376     vbox = gtk_vbox_new(FALSE, 3);
377         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
378     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
379
380     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
381     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
382
383     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
384
385     col_arrows =
386         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
387
388     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
389     {
390         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
391
392         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
393
394         column_lb = gtk_label_new(default_titles[i]);
395
396         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
397             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
398
399         gtk_widget_show(column_lb);
400
401         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
402
403         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
404             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
405
406         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
407
408         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
409             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
410
411         if (i == 0)
412         {
413             /* default column sorting */
414             gtk_widget_show(col_arrows[i].ascend_pm);
415         }
416
417         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
418         gtk_widget_show(col_arrows[i].table);
419     }
420     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
421
422     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), gsm_a_stat_gtk_sort_column);
423     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
424     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
425
426     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 50);
427     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 330);
428     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 50);
429
430     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
431     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
432     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
433
434     SIGNAL_CONNECT(dlg_p->table, "click-column", gsm_a_stat_gtk_click_column_cb, col_arrows);
435
436         /* Button row. */
437     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
438     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
439
440     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
441     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
442
443     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
444     SIGNAL_CONNECT(dlg_p->win, "destroy", gsm_a_stat_gtk_win_destroy_cb, dlg_p);
445
446     gtk_widget_show_all(dlg_p->win);
447     window_present(dlg_p->win);
448 }
449
450
451 /*
452  * Never gets called ?
453  */
454 static void
455 gsm_a_stat_gtk_init(
456     char                *optarg)
457 {
458     /* does not appear to be called */
459
460     optarg = optarg;
461 }
462
463
464 static void
465 gsm_a_stat_gtk_bssmap_cb(
466     GtkWidget           *w _U_,
467     gpointer            d _U_)
468 {
469     int                 i;
470     char                str[100];
471
472
473     /*
474      * if the window is already open, bring it to front
475      */
476     if (dlg_bssmap.win)
477     {
478         gdk_window_raise(dlg_bssmap.win->window);
479         return;
480     }
481
482     gsm_a_stat_gtk_win_create(&dlg_bssmap, "GSM A-I/F BSSMAP Statistics");
483
484     i = 0;
485     while (gsm_a_bssmap_msg_strings[i].strptr)
486     {
487         g_snprintf(str, 100, "0x%02x", gsm_a_bssmap_msg_strings[i].value);
488         dlg_bssmap.entries[0] = g_strdup(str);
489
490         dlg_bssmap.entries[1] = g_strdup(gsm_a_bssmap_msg_strings[i].strptr);
491
492         dlg_bssmap.entries[2] = g_strdup("0");
493
494         gtk_clist_insert(GTK_CLIST(dlg_bssmap.table), i, dlg_bssmap.entries);
495         gtk_clist_set_row_data(GTK_CLIST(dlg_bssmap.table), i, (gpointer) i);
496
497         i++;
498     }
499
500     gsm_a_stat_draw(NULL);
501 }
502
503
504 static void
505 gsm_a_stat_gtk_dtap_cb(
506     GtkWidget           *w _U_,
507     gpointer            d _U_,
508     gsm_a_stat_dlg_t    *dlg_dtap_p,
509     char                *title,
510     const value_string  *dtap_msg_strings)
511 {
512     int                 i;
513     char                str[100];
514
515
516     /*
517      * if the window is already open, bring it to front
518      */
519     if (dlg_dtap_p->win)
520     {
521         gdk_window_raise(dlg_dtap_p->win->window);
522         return;
523     }
524
525     gsm_a_stat_gtk_win_create(dlg_dtap_p, title);
526
527     i = 0;
528     while (dtap_msg_strings[i].strptr)
529     {
530         g_snprintf(str, 100, "0x%02x", dtap_msg_strings[i].value);
531         dlg_dtap_p->entries[0] = g_strdup(str);
532
533         dlg_dtap_p->entries[1] = g_strdup(dtap_msg_strings[i].strptr);
534
535         dlg_dtap_p->entries[2] = g_strdup("0");
536
537         gtk_clist_insert(GTK_CLIST(dlg_dtap_p->table), i, dlg_dtap_p->entries);
538         gtk_clist_set_row_data(GTK_CLIST(dlg_dtap_p->table), i, (gpointer) i);
539
540         i++;
541     }
542
543     gsm_a_stat_draw(NULL);
544 }
545
546 static void
547 gsm_a_stat_gtk_dtap_mm_cb(
548     GtkWidget           *w _U_,
549     gpointer            d _U_)
550 {
551     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_mm,
552         "GSM A-I/F DTAP Mobility Management Statistics",
553         gsm_a_dtap_msg_mm_strings);
554 }
555
556 static void
557 gsm_a_stat_gtk_dtap_rr_cb(
558     GtkWidget           *w _U_,
559     gpointer            d _U_)
560 {
561     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_rr,
562         "GSM A-I/F DTAP Radio Resource Management Statistics",
563         gsm_a_dtap_msg_rr_strings);
564 }
565
566 static void
567 gsm_a_stat_gtk_dtap_cc_cb(
568     GtkWidget           *w _U_,
569     gpointer            d _U_)
570 {
571     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_cc,
572         "GSM A-I/F DTAP Call Control Statistics",
573         gsm_a_dtap_msg_cc_strings);
574 }
575
576 static void
577 gsm_a_stat_gtk_dtap_gmm_cb(
578     GtkWidget           *w _U_,
579     gpointer            d _U_)
580 {
581     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_gmm,
582         "GSM A-I/F DTAP GPRS Mobility Management Statistics",
583         gsm_a_dtap_msg_gmm_strings);
584 }
585
586 static void
587 gsm_a_stat_gtk_dtap_sms_cb(
588     GtkWidget           *w _U_,
589     gpointer            d _U_)
590 {
591     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_sms,
592         "GSM A-I/F DTAP Short Message Service Statistics",
593         gsm_a_dtap_msg_sms_strings);
594 }
595
596 static void
597 gsm_a_stat_gtk_dtap_sm_cb(
598     GtkWidget           *w _U_,
599     gpointer            d _U_)
600 {
601     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_sm,
602         "GSM A-I/F DTAP GPRS Session Management Statistics",
603         gsm_a_dtap_msg_sm_strings);
604 }
605
606 static void
607 gsm_a_stat_gtk_dtap_ss_cb(
608     GtkWidget           *w _U_,
609     gpointer            d _U_)
610 {
611     gsm_a_stat_gtk_dtap_cb(w, d, &dlg_dtap_ss,
612         "GSM A-I/F DTAP Supplementary Services Statistics",
613         gsm_a_dtap_msg_ss_strings);
614 }
615
616
617 void
618 register_tap_listener_gtkgsm_a_stat(void)
619 {
620     GString             *err_p;
621
622
623     register_ethereal_tap("gsm_a,", gsm_a_stat_gtk_init);
624
625     memset((void *) &stat, 0, sizeof(gsm_a_stat_t));
626
627     err_p =
628         register_tap_listener("gsm_a", &stat, NULL,
629             gsm_a_stat_reset,
630             gsm_a_stat_packet,
631             gsm_a_stat_draw);
632
633     if (err_p != NULL)
634     {
635         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
636         g_string_free(err_p, TRUE);
637
638         exit(1);
639     }
640
641     register_tap_menu_item("GSM/A-Interface BSSMAP", REGISTER_TAP_GROUP_NONE,
642         gsm_a_stat_gtk_bssmap_cb, NULL, NULL, NULL);
643
644     register_tap_menu_item("GSM/A-Interface DTAP/Mobility Management", REGISTER_TAP_GROUP_NONE,
645         gsm_a_stat_gtk_dtap_mm_cb, NULL, NULL, NULL);
646
647     register_tap_menu_item("GSM/A-Interface DTAP/Radio Resource Management", REGISTER_TAP_GROUP_NONE,
648         gsm_a_stat_gtk_dtap_rr_cb, NULL, NULL, NULL);
649
650     register_tap_menu_item("GSM/A-Interface DTAP/Call Control", REGISTER_TAP_GROUP_NONE,
651         gsm_a_stat_gtk_dtap_cc_cb, NULL, NULL, NULL);
652
653     register_tap_menu_item("GSM/A-Interface DTAP/GPRS Mobility Management", REGISTER_TAP_GROUP_NONE,
654         gsm_a_stat_gtk_dtap_gmm_cb, NULL, NULL, NULL);
655
656     register_tap_menu_item("GSM/A-Interface DTAP/Short Message Service", REGISTER_TAP_GROUP_NONE,
657         gsm_a_stat_gtk_dtap_sms_cb, NULL, NULL, NULL);
658
659     register_tap_menu_item("GSM/A-Interface DTAP/GPRS Session Management", REGISTER_TAP_GROUP_NONE,
660         gsm_a_stat_gtk_dtap_sm_cb, NULL, NULL, NULL);
661
662     register_tap_menu_item("GSM/A-Interface DTAP/Supplementary Services", REGISTER_TAP_GROUP_NONE,
663         gsm_a_stat_gtk_dtap_ss_cb, NULL, NULL, NULL);
664 }