Have the time field in the Graph Analyzis windos use the same time format as used...
[metze/wireshark/wip.git] / ui / gtk / h225_counter.c
1 /* h225_counter.c
2  * H.225 message counter for Wireshark
3  * Copyright 2003 Lars Roland
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include <epan/packet.h>
39 #include <epan/packet_info.h>
40 #include <epan/epan.h>
41 #include <epan/value_string.h>
42 #include <epan/tap.h>
43 #include <epan/dissectors/packet-h225.h>
44
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../stat_menu.h"
48
49 #include "ui/gtk/gui_stat_util.h"
50 #include "ui/gtk/dlg_utils.h"
51 #include "ui/gtk/tap_param_dlg.h"
52 #include "ui/gtk/gui_utils.h"
53 #include "ui/gtk/main.h"
54
55 #include "ui/gtk/old-gtk-compat.h"
56
57 static void gtk_h225counter_init(const char *optarg, void *userdata);
58
59 static tap_param h225_counter_params[] = {
60         { PARAM_FILTER, "Filter", NULL }
61 };
62
63 static tap_param_dlg h225_counter_dlg = {
64         "H.225 Messages and Message Reasons",
65         "h225,counter",
66         gtk_h225counter_init,
67         -1,
68         G_N_ELEMENTS(h225_counter_params),
69         h225_counter_params
70 };
71
72 /* following values represent the size of their valuestring arrays */
73
74 #define RAS_MSG_TYPES 33
75 #define CS_MSG_TYPES 13
76
77 #define GRJ_REASONS 8
78 #define RRJ_REASONS 18
79 #define URQ_REASONS 6
80 #define URJ_REASONS 6
81 #define ARJ_REASONS 22
82 #define BRJ_REASONS 8
83 #define DRQ_REASONS 3
84 #define DRJ_REASONS 4
85 #define LRJ_REASONS 16
86 #define IRQNAK_REASONS 4
87 #define REL_CMP_REASONS 26
88 #define FACILITY_REASONS 11
89
90
91 /* used to keep track of the statistics for an entire program interface */
92 typedef struct _h225counter_t {
93         GtkWidget *win;
94         GtkWidget *vbox;
95         char *filter;
96         GtkWidget *scrolled_window;
97         GtkTreeView *table;
98         guint32 ras_msg[RAS_MSG_TYPES + 1];
99         guint32 cs_msg[CS_MSG_TYPES + 1];
100         guint32 grj_reason[GRJ_REASONS + 1];
101         guint32 rrj_reason[RRJ_REASONS + 1];
102         guint32 urq_reason[URQ_REASONS + 1];
103         guint32 urj_reason[URJ_REASONS + 1];
104         guint32 arj_reason[ARJ_REASONS + 1];
105         guint32 brj_reason[BRJ_REASONS + 1];
106         guint32 drq_reason[DRQ_REASONS + 1];
107         guint32 drj_reason[DRJ_REASONS + 1];
108         guint32 lrj_reason[LRJ_REASONS + 1];
109         guint32 irqnak_reason[IRQNAK_REASONS + 1];
110         guint32 rel_cmp_reason[REL_CMP_REASONS + 1];
111         guint32 facility_reason[FACILITY_REASONS + 1];
112 } h225counter_t;
113
114
115 static void
116 h225counter_reset(void *phs)
117 {
118         h225counter_t *hs=(h225counter_t *)phs;
119         int i;
120
121         for(i=0;i<=RAS_MSG_TYPES;i++) {
122                 hs->ras_msg[i] = 0;
123         }
124         for(i=0;i<=CS_MSG_TYPES;i++) {
125                 hs->cs_msg[i] = 0;
126         }
127         for(i=0;i<=GRJ_REASONS;i++) {
128                 hs->grj_reason[i] = 0;
129         }
130         for(i=0;i<=RRJ_REASONS;i++) {
131                 hs->rrj_reason[i] = 0;
132         }
133         for(i=0;i<=URQ_REASONS;i++) {
134                 hs->urq_reason[i] = 0;
135         }
136         for(i=0;i<=URJ_REASONS;i++) {
137                 hs->urj_reason[i] = 0;
138         }
139         for(i=0;i<=ARJ_REASONS;i++) {
140                 hs->arj_reason[i] = 0;
141         }
142         for(i=0;i<=BRJ_REASONS;i++) {
143                 hs->brj_reason[i] = 0;
144         }
145         for(i=0;i<=DRQ_REASONS;i++) {
146                 hs->drq_reason[i] = 0;
147         }
148         for(i=0;i<=DRJ_REASONS;i++) {
149                 hs->drj_reason[i] = 0;
150         }
151         for(i=0;i<=LRJ_REASONS;i++) {
152                 hs->lrj_reason[i] = 0;
153         }
154         for(i=0;i<=IRQNAK_REASONS;i++) {
155                 hs->irqnak_reason[i] = 0;
156         }
157         for(i=0;i<=REL_CMP_REASONS;i++) {
158                 hs->rel_cmp_reason[i] = 0;
159         }
160         for(i=0;i<=FACILITY_REASONS;i++) {
161                 hs->facility_reason[i] = 0;
162         }
163 }
164
165 static int
166 h225counter_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
167 {
168         h225counter_t *hs=(h225counter_t *)phs;
169         const h225_packet_info *pi=phi;
170
171         switch (pi->msg_type) {
172
173         case H225_RAS:
174                 if(pi->msg_tag==-1) { /* uninitialized */
175                         return 0;
176                 }
177                 else if (pi->msg_tag >= RAS_MSG_TYPES) { /* unknown */
178                         hs->ras_msg[RAS_MSG_TYPES]++;
179                 }
180                 else {
181                         hs->ras_msg[pi->msg_tag]++;
182                 }
183
184                 /* Look for reason tag */
185                 if(pi->reason==-1) { /* uninitialized */
186                         break;
187                 }
188
189                 switch(pi->msg_tag) {
190
191                 case 2: /* GRJ */
192                         if(pi->reason < GRJ_REASONS)
193                                 hs->grj_reason[pi->reason]++;
194                         else
195                                 hs->grj_reason[GRJ_REASONS]++;
196                         break;
197                 case 5: /* RRJ */
198                         if(pi->reason < RRJ_REASONS)
199                                 hs->rrj_reason[pi->reason]++;
200                         else
201                                 hs->rrj_reason[RRJ_REASONS]++;
202                         break;
203                 case 6: /* URQ */
204                         if(pi->reason < URQ_REASONS)
205                                 hs->urq_reason[pi->reason]++;
206                         else
207                                 hs->urq_reason[URQ_REASONS]++;
208                         break;
209                 case 8: /* URJ */
210                         if(pi->reason < URJ_REASONS)
211                                 hs->urj_reason[pi->reason]++;
212                         else
213                                 hs->urj_reason[URJ_REASONS]++;
214                         break;
215                 case 11: /* ARJ */
216                         if(pi->reason < ARJ_REASONS)
217                                 hs->arj_reason[pi->reason]++;
218                         else
219                                 hs->arj_reason[ARJ_REASONS]++;
220                         break;
221                 case 14: /* BRJ */
222                         if(pi->reason < BRJ_REASONS)
223                                 hs->brj_reason[pi->reason]++;
224                         else
225                                 hs->brj_reason[BRJ_REASONS]++;
226                         break;
227                 case 15: /* DRQ */
228                         if(pi->reason < DRQ_REASONS)
229                                 hs->drq_reason[pi->reason]++;
230                         else
231                                 hs->drq_reason[DRQ_REASONS]++;
232                         break;
233                 case 17: /* DRJ */
234                         if(pi->reason < DRJ_REASONS)
235                                 hs->drj_reason[pi->reason]++;
236                         else
237                                 hs->drj_reason[DRJ_REASONS]++;
238                         break;
239                 case 20: /* LRJ */
240                         if(pi->reason < LRJ_REASONS)
241                                 hs->lrj_reason[pi->reason]++;
242                         else
243                                 hs->lrj_reason[LRJ_REASONS]++;
244                         break;
245                 case 29: /* IRQ Nak */
246                         if(pi->reason < IRQNAK_REASONS)
247                                 hs->irqnak_reason[pi->reason]++;
248                         else
249                                 hs->irqnak_reason[IRQNAK_REASONS]++;
250                         break;
251
252                 default:
253                         /* do nothing */
254                         break;
255                 }
256
257                 break;
258
259         case H225_CS:
260                 if(pi->msg_tag==-1) { /* uninitialized */
261                         return 0;
262                 }
263                 else if (pi->msg_tag >= CS_MSG_TYPES) { /* unknown */
264                         hs->cs_msg[CS_MSG_TYPES]++;
265                 }
266                 else {
267                         hs->cs_msg[pi->msg_tag]++;
268                 }
269
270                 /* Look for reason tag */
271                 if(pi->reason==-1) { /* uninitialized */
272                         break;
273                 }
274
275                 switch(pi->msg_tag) {
276
277                 case 5: /* ReleaseComplete */
278                         if(pi->reason < REL_CMP_REASONS)
279                                 hs->rel_cmp_reason[pi->reason]++;
280                         else
281                                 hs->rel_cmp_reason[REL_CMP_REASONS]++;
282                         break;
283                 case 6: /* Facility */
284                         if(pi->reason < FACILITY_REASONS)
285                                 hs->facility_reason[pi->reason]++;
286                         else
287                                 hs->facility_reason[FACILITY_REASONS]++;
288                         break;
289                 default:
290                         /* do nothing */
291                         break;
292                 }
293
294                 break;
295
296         default:
297                 return 0;
298         }
299
300         return 1;
301 }
302
303 static void
304 h225counter_draw(void *phs)
305 {
306         h225counter_t *hs=(h225counter_t *)phs;
307         int i,j;
308         char str[256];
309         GtkListStore *store;
310         GtkTreeIter iter;
311
312         /* Now print Message and Reason Counter Table */
313         /* clear list before printing */
314         store = GTK_LIST_STORE(gtk_tree_view_get_model(hs->table));
315         gtk_list_store_clear(store);
316
317         for(i=0;i<=RAS_MSG_TYPES;i++) {
318                 if(hs->ras_msg[i]!=0) {
319                         g_snprintf(str, 256, "%s", val_to_str(i,h225_RasMessage_vals,"unknown ras-messages"));
320                         gtk_list_store_append(store, &iter);
321                         gtk_list_store_set(store, &iter,
322                                    0, val_to_str(i,h225_RasMessage_vals,"unknown ras-messages"),
323                                    1, hs->ras_msg[i], -1);
324
325                         /* reason counter */
326                         switch(i) {
327                         case 2: /* GRJ */
328                                 for(j=0;j<=GRJ_REASONS;j++) {
329                                         if(hs->grj_reason[j]!=0) {
330                                                 g_snprintf(str, 256,"    %s",
331                                                                 val_to_str(j,GatekeeperRejectReason_vals,"unknown reason"));
332                                                 gtk_list_store_append(store, &iter);
333                                                 gtk_list_store_set(store, &iter,
334                                                         0, str,
335                                                         1, hs->grj_reason[j], -1);
336                                         }
337                                 }
338                                 break;
339                         case 5: /* RRJ */
340                                 for(j=0;j<=RRJ_REASONS;j++) {
341                                         if(hs->rrj_reason[j]!=0) {
342                                                 g_snprintf(str, 256,"    %s",
343                                                                 val_to_str(j,RegistrationRejectReason_vals,"unknown reason"));
344                                                 gtk_list_store_append(store, &iter);
345                                                 gtk_list_store_set(store, &iter,
346                                                         0, str,
347                                                         1, hs->rrj_reason[j], -1);
348                                         }
349                                 }
350                                 break;
351                         case 6: /* URQ */
352                                 for(j=0;j<=URQ_REASONS;j++) {
353                                         if(hs->urq_reason[j]!=0) {
354                                                 g_snprintf(str, 256,"    %s",
355                                                                 val_to_str(j,UnregRequestReason_vals,"unknown reason"));
356                                                 gtk_list_store_append(store, &iter);
357                                                 gtk_list_store_set(store, &iter,
358                                                         0, str,
359                                                         1, hs->urq_reason[j], -1);
360                                         }
361                                 }
362                                 break;
363                         case 8: /* URJ */
364                                 for(j=0;j<=URJ_REASONS;j++) {
365                                         if(hs->urj_reason[j]!=0) {
366                                                 g_snprintf(str, 256,"    %s",
367                                                                 val_to_str(j,UnregRejectReason_vals,"unknown reason"));
368                                                 gtk_list_store_append(store, &iter);
369                                                 gtk_list_store_set(store, &iter,
370                                                         0, str,
371                                                         1, hs->urj_reason[j], -1);
372                                         }
373                                 }
374                                 break;
375                         case 11: /* ARJ */
376                                 for(j=0;j<=ARJ_REASONS;j++) {
377                                         if(hs->arj_reason[j]!=0) {
378                                                 g_snprintf(str, 256,"    %s",
379                                                                 val_to_str(j,AdmissionRejectReason_vals,"unknown reason"));
380                                                 gtk_list_store_append(store, &iter);
381                                                 gtk_list_store_set(store, &iter,
382                                                         0, str,
383                                                         1, hs->arj_reason[j], -1);
384                                         }
385                                 }
386                                 break;
387                         case 14: /* BRJ */
388                                 for(j=0;j<=BRJ_REASONS;j++) {
389                                         if(hs->brj_reason[j]!=0) {
390                                                 g_snprintf(str, 256,"    %s",
391                                                                 val_to_str(j,BandRejectReason_vals,"unknown reason"));
392                                                 gtk_list_store_append(store, &iter);
393                                                 gtk_list_store_set(store, &iter,
394                                                         0, str,
395                                                         1, hs->brj_reason[j], -1);
396                                         }
397                                 }
398                                 break;
399                         case 15: /* DRQ */
400                                 for(j=0;j<=DRQ_REASONS;j++) {
401                                         if(hs->drq_reason[j]!=0) {
402                                                 g_snprintf(str, 256,"    %s",
403                                                                 val_to_str(j,DisengageReason_vals,"unknown reason"));
404                                                 gtk_list_store_append(store, &iter);
405                                                 gtk_list_store_set(store, &iter,
406                                                         0, str,
407                                                         1, hs->drq_reason[j], -1);
408                                         }
409                                 }
410                                 break;
411                         case 17: /* DRJ */
412                                 for(j=0;j<=DRJ_REASONS;j++) {
413                                         if(hs->drj_reason[j]!=0) {
414                                                 g_snprintf(str, 256,"    %s",
415                                                                 val_to_str(j,DisengageRejectReason_vals,"unknown reason"));
416                                                 gtk_list_store_set(store, &iter,
417                                                         0, str,
418                                                         1, hs->drj_reason[j], -1);
419                                         }
420                                 }
421                                 break;
422                         case 20: /* LRJ */
423                                 for(j=0;j<=LRJ_REASONS;j++) {
424                                         if(hs->lrj_reason[j]!=0) {
425                                                 g_snprintf(str, 256,"    %s",
426                                                                 val_to_str(j,LocationRejectReason_vals,"unknown reason"));
427                                                 gtk_list_store_append(store, &iter);
428                                                 gtk_list_store_set(store, &iter,
429                                                         0, str,
430                                                         1, hs->lrj_reason[j], -1);
431                                         }
432                                 }
433                                 break;
434                         case 29: /* IRQNak */
435                                 for(j=0;j<=IRQNAK_REASONS;j++) {
436                                         if(hs->irqnak_reason[j]!=0) {
437                                                 g_snprintf(str, 256,"    %s",
438                                                                 val_to_str(j,InfoRequestNakReason_vals,"unknown reason"));
439                                                 gtk_list_store_append(store, &iter);
440                                                 gtk_list_store_set(store, &iter,
441                                                         0, str,
442                                                         1, hs->irqnak_reason[j], -1);
443                                         }
444                                 }
445                                 break;
446                         default:
447                                 break;
448                         }
449                         /* end of reason counter*/
450                 }
451         }
452
453         for(i=0;i<=CS_MSG_TYPES;i++) {
454                 if(hs->cs_msg[i]!=0) {
455                         gtk_list_store_append(store, &iter);
456                         gtk_list_store_set(store, &iter,
457                                    0, val_to_str(i,T_h323_message_body_vals,"unknown cs-messages"),
458                                    1, hs->cs_msg[i], -1);
459
460                         /* reason counter */
461                         switch(i) {
462                         case 5: /* ReleaseComplete */
463                                 for(j=0;j<=REL_CMP_REASONS;j++) {
464                                         if(hs->rel_cmp_reason[j]!=0) {
465                                                 g_snprintf(str, 256,"    %s",
466                                                                 val_to_str(j,h225_ReleaseCompleteReason_vals,"unknown reason"));
467                                                 gtk_list_store_append(store, &iter);
468                                                 gtk_list_store_set(store, &iter,
469                                                         0, str,
470                                                         1, hs->rel_cmp_reason[j], -1);
471                                         }
472                                 }
473                                 break;
474                         case 6: /* Facility */
475                                 for(j=0;j<=FACILITY_REASONS;j++) {
476                                         if(hs->facility_reason[j]!=0) {
477                                                 g_snprintf(str, 256,"    %s",
478                                                                 val_to_str(j,FacilityReason_vals,"unknown reason"));
479                                                 gtk_list_store_append(store, &iter);
480                                                 gtk_list_store_set(store, &iter,
481                                                         0, str,
482                                                         1, hs->facility_reason[j], -1);
483                                         }
484                                 }
485                                 break;
486                         default:
487                                 break;
488                         }
489                 }
490         }
491 }
492
493 static void
494 win_destroy_cb(GtkWindow *win _U_, gpointer data)
495 {
496         h225counter_t *hs=(h225counter_t *)data;
497
498         protect_thread_critical_region();
499         remove_tap_listener(hs);
500         unprotect_thread_critical_region();
501
502         if(hs->filter){
503                 g_free(hs->filter);
504                 hs->filter=NULL;
505         }
506         g_free(hs);
507 }
508
509 static const stat_column titles[]={
510   {G_TYPE_STRING, LEFT, "Message Type or Reason"},
511   {G_TYPE_UINT, RIGHT, "Count" }
512 };
513
514 static void
515 gtk_h225counter_init(const char *optarg, void *userdata _U_)
516 {
517         h225counter_t *hs;
518         GString *error_string;
519         GtkWidget *bbox;
520         GtkWidget *close_bt;
521
522         hs=g_malloc(sizeof(h225counter_t));
523
524         if(strncmp(optarg,"h225,counter,",13) == 0){
525                 hs->filter=g_strdup(optarg+13);
526         } else {
527                 hs->filter=NULL;
528         }
529
530         h225counter_reset(hs);
531
532         hs->win = dlg_window_new("Wireshark: H.225 counters");  /* transient_for top_level */
533         gtk_window_set_destroy_with_parent (GTK_WINDOW(hs->win), TRUE);
534
535         gtk_window_set_default_size(GTK_WINDOW(hs->win), 400, 200);
536
537         hs->vbox=gtk_vbox_new(FALSE, 3);
538         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
539
540         init_main_stat_window(hs->win, hs->vbox, "H.225 Message and Message Reason Counter", hs->filter);
541
542         /* init a scrolled window*/
543         hs->scrolled_window = scrolled_window_new(NULL, NULL);
544
545         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 2, titles);
546
547         error_string=register_tap_listener("h225", hs, hs->filter, 0, h225counter_reset, h225counter_packet, h225counter_draw);
548         if(error_string){
549                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
550                 g_string_free(error_string, TRUE);
551                 g_free(hs->filter);
552                 g_free(hs);
553                 return;
554         }
555
556         /* Button row. */
557         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
558         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
559
560         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
561         window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
562
563         g_signal_connect(hs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
564         g_signal_connect(hs->win, "destroy", G_CALLBACK(win_destroy_cb), hs);
565
566         gtk_widget_show_all(hs->win);
567         window_present(hs->win);
568
569         cf_retap_packets(&cfile);
570         gdk_window_raise(gtk_widget_get_window(hs->win));
571 }
572
573 void
574 register_tap_listener_gtk_h225counter(void)
575 {
576         register_dfilter_stat(&h225_counter_dlg, "_H.225",
577             REGISTER_STAT_GROUP_TELEPHONY);
578 }
579
580 void h225_counter_cb(GtkAction *action, gpointer user_data _U_)
581 {
582         tap_param_dlg_cb(action, &h225_counter_dlg);
583 }