Move the tap infrastructure to the epan directory.
[obnox/wireshark/wip.git] / gtk / h225_counter.c
1 /* h225_counter.c
2  * h225 message counter for ethereal
3  * Copyright 2003 Lars Roland
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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_info.h>
39 #include <epan/epan.h>
40 #include <epan/value_string.h>
41
42 #include "tap_menu.h"
43 #include <epan/tap.h>
44 #include "../register.h"
45 #include <epan/dissectors/packet-h225.h>
46 #include "gtk_stat_util.h"
47 #include "compat_macros.h"
48 #include "../simple_dialog.h"
49 #include "dlg_utils.h"
50 #include "../file.h"
51 #include "../globals.h"
52 #include "../tap_dfilter_dlg.h"
53 #include "tap_dfilter_dlg.h"
54 #include "ui_util.h"
55
56
57 static void gtk_h225counter_init(char *optarg);
58
59 static tap_dfilter_dlg h225_counter_dlg = {
60         "H.225 Messages and Message Reasons",
61         "h225,counter",
62         gtk_h225counter_init,
63         -1
64 };
65
66 /* following values represent the size of their valuestring arrays */
67
68 #define RAS_MSG_TYPES 33
69 #define CS_MSG_TYPES 13
70
71 #define GRJ_REASONS 8
72 #define RRJ_REASONS 18
73 #define URQ_REASONS 6
74 #define URJ_REASONS 6
75 #define ARJ_REASONS 22
76 #define BRJ_REASONS 8
77 #define DRQ_REASONS 3
78 #define DRJ_REASONS 4
79 #define LRJ_REASONS 16
80 #define IRQNAK_REASONS 4
81 #define REL_CMP_REASONS 26
82 #define FACILITY_REASONS 11
83
84
85 /* used to keep track of the statistics for an entire program interface */
86 typedef struct _h225counter_t {
87         GtkWidget *win;
88         GtkWidget *vbox;
89         char *filter;
90         GtkWidget *scrolled_window;
91         GtkCList *table;
92         guint32 ras_msg[RAS_MSG_TYPES + 1];
93         guint32 cs_msg[CS_MSG_TYPES + 1];
94         guint32 grj_reason[GRJ_REASONS + 1];
95         guint32 rrj_reason[RRJ_REASONS + 1];
96         guint32 urq_reason[URQ_REASONS + 1];
97         guint32 urj_reason[URJ_REASONS + 1];
98         guint32 arj_reason[ARJ_REASONS + 1];
99         guint32 brj_reason[BRJ_REASONS + 1];
100         guint32 drq_reason[DRQ_REASONS + 1];
101         guint32 drj_reason[DRJ_REASONS + 1];
102         guint32 lrj_reason[LRJ_REASONS + 1];
103         guint32 irqnak_reason[IRQNAK_REASONS + 1];
104         guint32 rel_cmp_reason[REL_CMP_REASONS + 1];
105         guint32 facility_reason[FACILITY_REASONS + 1];
106 } h225counter_t;
107
108
109 static void
110 h225counter_reset(void *phs)
111 {
112         h225counter_t *hs=(h225counter_t *)phs;
113         int i;
114
115         for(i=0;i<=RAS_MSG_TYPES;i++) {
116                 hs->ras_msg[i] = 0;
117         }
118         for(i=0;i<=CS_MSG_TYPES;i++) {
119                 hs->cs_msg[i] = 0;
120         }
121         for(i=0;i<=GRJ_REASONS;i++) {
122                 hs->grj_reason[i] = 0;
123         }
124         for(i=0;i<=RRJ_REASONS;i++) {
125                 hs->rrj_reason[i] = 0;
126         }
127         for(i=0;i<=URQ_REASONS;i++) {
128                 hs->urq_reason[i] = 0;
129         }
130         for(i=0;i<=URJ_REASONS;i++) {
131                 hs->urj_reason[i] = 0;
132         }
133         for(i=0;i<=ARJ_REASONS;i++) {
134                 hs->arj_reason[i] = 0;
135         }
136         for(i=0;i<=BRJ_REASONS;i++) {
137                 hs->brj_reason[i] = 0;
138         }
139         for(i=0;i<=DRQ_REASONS;i++) {
140                 hs->drq_reason[i] = 0;
141         }
142         for(i=0;i<=DRJ_REASONS;i++) {
143                 hs->drj_reason[i] = 0;
144         }
145         for(i=0;i<=LRJ_REASONS;i++) {
146                 hs->lrj_reason[i] = 0;
147         }
148         for(i=0;i<=IRQNAK_REASONS;i++) {
149                 hs->irqnak_reason[i] = 0;
150         }
151         for(i=0;i<=REL_CMP_REASONS;i++) {
152                 hs->rel_cmp_reason[i] = 0;
153         }
154         for(i=0;i<=FACILITY_REASONS;i++) {
155                 hs->facility_reason[i] = 0;
156         }
157 }
158
159 static int
160 h225counter_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *phi)
161 {
162         h225counter_t *hs=(h225counter_t *)phs;
163         h225_packet_info *pi=phi;
164
165         switch (pi->msg_type) {
166
167         case H225_RAS:
168                 if(pi->msg_tag==-1) { /* uninitialized */
169                         return 0;
170                 }
171                 else if (pi->msg_tag >= RAS_MSG_TYPES) { /* unknown */
172                         hs->ras_msg[RAS_MSG_TYPES]++;
173                 }
174                 else {
175                         hs->ras_msg[pi->msg_tag]++;
176                 }
177
178                 /* Look for reason tag */
179                 if(pi->reason==-1) { /* uninitialized */
180                         break;
181                 }
182
183                 switch(pi->msg_tag) {
184
185                 case 2: /* GRJ */
186                         if(pi->reason < GRJ_REASONS)
187                                 hs->grj_reason[pi->reason]++;
188                         else
189                                 hs->grj_reason[GRJ_REASONS]++;
190                         break;
191                 case 5: /* RRJ */
192                         if(pi->reason < RRJ_REASONS)
193                                 hs->rrj_reason[pi->reason]++;
194                         else
195                                 hs->rrj_reason[RRJ_REASONS]++;
196                         break;
197                 case 6: /* URQ */
198                         if(pi->reason < URQ_REASONS)
199                                 hs->urq_reason[pi->reason]++;
200                         else
201                                 hs->urq_reason[URQ_REASONS]++;
202                         break;
203                 case 8: /* URJ */
204                         if(pi->reason < URJ_REASONS)
205                                 hs->urj_reason[pi->reason]++;
206                         else
207                                 hs->urj_reason[URJ_REASONS]++;
208                         break;
209                 case 11: /* ARJ */
210                         if(pi->reason < ARJ_REASONS)
211                                 hs->arj_reason[pi->reason]++;
212                         else
213                                 hs->arj_reason[ARJ_REASONS]++;
214                         break;
215                 case 14: /* BRJ */
216                         if(pi->reason < BRJ_REASONS)
217                                 hs->brj_reason[pi->reason]++;
218                         else
219                                 hs->brj_reason[BRJ_REASONS]++;
220                         break;
221                 case 15: /* DRQ */
222                         if(pi->reason < DRQ_REASONS)
223                                 hs->drq_reason[pi->reason]++;
224                         else
225                                 hs->drq_reason[DRQ_REASONS]++;
226                         break;
227                 case 17: /* DRJ */
228                         if(pi->reason < DRJ_REASONS)
229                                 hs->drj_reason[pi->reason]++;
230                         else
231                                 hs->drj_reason[DRJ_REASONS]++;
232                         break;
233                 case 20: /* LRJ */
234                         if(pi->reason < LRJ_REASONS)
235                                 hs->lrj_reason[pi->reason]++;
236                         else
237                                 hs->lrj_reason[LRJ_REASONS]++;
238                         break;
239                 case 29: /* IRQ Nak */
240                         if(pi->reason < IRQNAK_REASONS)
241                                 hs->irqnak_reason[pi->reason]++;
242                         else
243                                 hs->irqnak_reason[IRQNAK_REASONS]++;
244                         break;
245
246                 default:
247                         /* do nothing */
248                         break;
249                 }
250
251                 break;
252
253         case H225_CS:
254                 if(pi->msg_tag==-1) { /* uninitialized */
255                         return 0;
256                 }
257                 else if (pi->msg_tag >= CS_MSG_TYPES) { /* unknown */
258                         hs->cs_msg[CS_MSG_TYPES]++;
259                 }
260                 else {
261                         hs->cs_msg[pi->msg_tag]++;
262                 }
263
264                 /* Look for reason tag */
265                 if(pi->reason==-1) { /* uninitialized */
266                         break;
267                 }
268
269                 switch(pi->msg_tag) {
270
271                 case 5: /* ReleaseComplete */
272                         if(pi->reason < REL_CMP_REASONS)
273                                 hs->rel_cmp_reason[pi->reason]++;
274                         else
275                                 hs->rel_cmp_reason[REL_CMP_REASONS]++;
276                         break;
277                 case 6: /* Facility */
278                         if(pi->reason < FACILITY_REASONS)
279                                 hs->facility_reason[pi->reason]++;
280                         else
281                                 hs->facility_reason[FACILITY_REASONS]++;
282                         break;
283                 default:
284                         /* do nothing */
285                         break;
286                 }
287
288                 break;
289
290         default:
291                 return 0;
292                 break;
293         }
294
295         return 1;
296 }
297
298 static void
299 h225counter_draw(void *phs)
300 {
301         h225counter_t *hs=(h225counter_t *)phs;
302         int i,j;
303         char *str[2];
304
305         for(i=0;i<2;i++) {
306                 str[i]=g_malloc(sizeof(char[256]));
307         }
308         /* Now print Message and Reason Counter Table */
309         /* clear list before printing */
310         gtk_clist_clear(hs->table);
311
312         for(i=0;i<=RAS_MSG_TYPES;i++) {
313                 if(hs->ras_msg[i]!=0) {
314                         g_snprintf(str[0], sizeof(char[256]), 
315                 "%s", val_to_str(i,RasMessage_vals,"unknown ras-messages  "));
316                         g_snprintf(str[1], sizeof(char[256]),
317                 "%d", hs->ras_msg[i]);
318                         gtk_clist_append(hs->table, str);
319
320                         /* reason counter */
321                         switch(i) {
322                         case 2: /* GRJ */
323                                 for(j=0;j<=GRJ_REASONS;j++) {
324                                         if(hs->grj_reason[j]!=0) {
325                                                 g_snprintf(str[0], sizeof(char[256]),
326                             "    %s", val_to_str(j,GatekeeperRejectReason_vals,"unknown reason   "));
327                                                 g_snprintf(str[1], sizeof(char[256]),
328                             "%d", hs->grj_reason[j]);
329                                                 gtk_clist_append(hs->table, str);
330                                         }
331                                 }
332                                 break;
333                         case 5: /* RRJ */
334                                 for(j=0;j<=RRJ_REASONS;j++) {
335                                         if(hs->rrj_reason[j]!=0) {
336                                                 g_snprintf(str[0], sizeof(char[256]),
337                             "    %s", val_to_str(j,RegistrationRejectReason_vals,"unknown reason   "));
338                                                 g_snprintf(str[1], sizeof(char[256]),
339                             "%d", hs->rrj_reason[j]);
340                                                 gtk_clist_append(hs->table, str);
341                                         }
342                                 }
343                                 break;
344                         case 6: /* URQ */
345                                 for(j=0;j<=URQ_REASONS;j++) {
346                                         if(hs->urq_reason[j]!=0) {
347                                                 g_snprintf(str[0], sizeof(char[256]),
348                             "    %s", val_to_str(j,UnregRequestReason_vals,"unknown reason   "));
349                                                 g_snprintf(str[1], sizeof(char[256]),
350                             "%d", hs->urq_reason[j]);
351                                                 gtk_clist_append(hs->table, str);
352                                         }
353                                 }
354                                 break;
355                         case 8: /* URJ */
356                                 for(j=0;j<=URJ_REASONS;j++) {
357                                         if(hs->urj_reason[j]!=0) {
358                                                 g_snprintf(str[0], sizeof(char[256]),
359                             "    %s", val_to_str(j,UnregRejectReason_vals,"unknown reason   "));
360                                                 g_snprintf(str[1], sizeof(char[256]),
361                             "%d", hs->urj_reason[j]);
362                                                 gtk_clist_append(hs->table, str);
363                                         }
364                                 }
365                                 break;
366                         case 11: /* ARJ */
367                                 for(j=0;j<=ARJ_REASONS;j++) {
368                                         if(hs->arj_reason[j]!=0) {
369                                                 g_snprintf(str[0], sizeof(char[256]),
370                             "    %s", val_to_str(j,AdmissionRejectReason_vals,"unknown reason   "));
371                                                 g_snprintf(str[1], sizeof(char[256]),
372                             "%d", hs->arj_reason[j]);
373                                                 gtk_clist_append(hs->table, str);
374                                         }
375                                 }
376                                 break;
377                         case 14: /* BRJ */
378                                 for(j=0;j<=BRJ_REASONS;j++) {
379                                         if(hs->brj_reason[j]!=0) {
380                                                 g_snprintf(str[0], sizeof(char[256]),
381                             "    %s", val_to_str(j,BandRejectReason_vals,"unknown reason   "));
382                                                 g_snprintf(str[1], sizeof(char[256]),
383                             "%d", hs->brj_reason[j]);
384                                                 gtk_clist_append(hs->table, str);
385                                         }
386                                 }
387                                 break;
388                         case 15: /* DRQ */
389                                 for(j=0;j<=DRQ_REASONS;j++) {
390                                         if(hs->drq_reason[j]!=0) {
391                                                 g_snprintf(str[0], sizeof(char[256]),
392                             "    %s", val_to_str(j,DisengageReason_vals,"unknown reason   "));
393                                                 g_snprintf(str[1], sizeof(char[256]),
394                             "%d", hs->drq_reason[j]);
395                                                 gtk_clist_append(hs->table, str);
396                                         }
397                                 }
398                                 break;
399                         case 17: /* DRJ */
400                                 for(j=0;j<=DRJ_REASONS;j++) {
401                                         if(hs->drj_reason[j]!=0) {
402                                                 g_snprintf(str[0], sizeof(char[256]),
403                             "    %s", val_to_str(j,DisengageRejectReason_vals,"unknown reason   "));
404                                                 g_snprintf(str[1], sizeof(char[256]),
405                             "%d", hs->drj_reason[j]);
406                                                 gtk_clist_append(hs->table, str);
407                                         }
408                                 }
409                                 break;
410                         case 20: /* LRJ */
411                                 for(j=0;j<=LRJ_REASONS;j++) {
412                                         if(hs->lrj_reason[j]!=0) {
413                                                 g_snprintf(str[0], sizeof(char[256]),
414                             "    %s", val_to_str(j,LocationRejectReason_vals,"unknown reason   "));
415                                                 g_snprintf(str[1], sizeof(char[256]),
416                             "%d", hs->lrj_reason[j]);
417                                                 gtk_clist_append(hs->table, str);
418                                         }
419                                 }
420                                 break;
421                         case 29: /* IRQNak */
422                                 for(j=0;j<=IRQNAK_REASONS;j++) {
423                                         if(hs->irqnak_reason[j]!=0) {
424                                                 g_snprintf(str[0], sizeof(char[256]),
425                             "    %s", val_to_str(j,InfoRequestNakReason_vals,"unknown reason   "));
426                                                 g_snprintf(str[1], sizeof(char[256]),
427                             "%d", hs->irqnak_reason[j]);
428                                                 gtk_clist_append(hs->table, str);
429                                         }
430                                 }
431                                 break;
432                         default:
433                                 break;
434                         }
435                         /* end of reason counter*/
436                 }
437         }
438
439         for(i=0;i<=CS_MSG_TYPES;i++) {
440                 if(hs->cs_msg[i]!=0) {
441                         g_snprintf(str[0], sizeof(char[256]),
442                 "%s", val_to_str(i,h323_message_body_vals,"unknown cs-messages   "));
443                         g_snprintf(str[1], sizeof(char[256]),
444                 "%d", hs->cs_msg[i]);
445                         gtk_clist_append(hs->table, str);
446
447                         /* reason counter */
448                         switch(i) {
449                         case 5: /* ReleaseComplete */
450                                 for(j=0;j<=REL_CMP_REASONS;j++) {
451                                         if(hs->rel_cmp_reason[j]!=0) {
452                                                 g_snprintf(str[0], sizeof(char[256]),
453                             "    %s", val_to_str(j,ReleaseCompleteReason_vals,"unknown reason   "));
454                                                 g_snprintf(str[1], sizeof(char[256]),
455                             "%d", hs->rel_cmp_reason[j]);
456                                                 gtk_clist_append(hs->table, str);
457                                         }
458                                 }
459                                 break;
460                         case 6: /* Facility */
461                                 for(j=0;j<=FACILITY_REASONS;j++) {
462                                         if(hs->facility_reason[j]!=0) {
463                                                 g_snprintf(str[0], sizeof(char[256]),
464                             "    %s", val_to_str(j,FacilityReason_vals,"unknown reason   "));
465                                                 g_snprintf(str[1], sizeof(char[256]),
466                             "%d", hs->facility_reason[j]);
467                                                 gtk_clist_append(hs->table, str);
468                                         }
469                                 }
470                                 break;
471                         default:
472                                 break;
473                         }
474                 }
475         }
476
477         gtk_widget_show(GTK_WIDGET(hs->table));
478
479 }
480
481 void protect_thread_critical_region(void);
482 void unprotect_thread_critical_region(void);
483 static void
484 win_destroy_cb(GtkWindow *win _U_, gpointer data)
485 {
486         h225counter_t *hs=(h225counter_t *)data;
487
488         protect_thread_critical_region();
489         remove_tap_listener(hs);
490         unprotect_thread_critical_region();
491
492         if(hs->filter){
493                 g_free(hs->filter);
494                 hs->filter=NULL;
495         }
496         g_free(hs);
497 }
498
499
500 static gchar *titles[]={"Message Type or Reason",
501                         "Count" };
502
503 static void
504 gtk_h225counter_init(char *optarg)
505 {
506         h225counter_t *hs;
507         char *filter=NULL;
508         GString *error_string;
509     GtkWidget *bbox;
510     GtkWidget *close_bt;
511
512         if(strncmp(optarg,"h225,counter,",13) == 0){
513                 filter=optarg+13;
514         } else {
515                 filter=g_malloc(1);
516                 *filter='\0';
517         }
518
519         hs=g_malloc(sizeof(h225counter_t));
520         hs->filter=g_strdup(filter);
521
522         h225counter_reset(hs);
523
524         hs->win=window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: H225 counters");
525         gtk_window_set_default_size(GTK_WINDOW(hs->win), 400, 200);
526
527         hs->vbox=gtk_vbox_new(FALSE, 3);
528         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
529
530         init_main_stat_window(hs->win, hs->vbox, "ITU-T H.225 Message and Message Reason Counter", filter);
531
532         /* init a scrolled window*/
533         hs->scrolled_window = scrolled_window_new(NULL, NULL);
534
535         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 2, titles);
536
537         error_string=register_tap_listener("h225", hs, filter, h225counter_reset, h225counter_packet, h225counter_draw);
538         if(error_string){
539                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
540                 g_string_free(error_string, TRUE);
541                 g_free(hs->filter);
542                 g_free(hs);
543                 return;
544         }
545
546         /* Button row. */
547         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
548         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
549
550         close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
551     window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
552
553     SIGNAL_CONNECT(hs->win, "delete_event", window_delete_event_cb, NULL);
554     SIGNAL_CONNECT(hs->win, "destroy", win_destroy_cb, hs);
555
556         gtk_widget_show_all(hs->win);
557     window_present(hs->win);
558
559         retap_packets(&cfile);
560 }
561
562 void
563 register_tap_listener_gtk_h225counter(void)
564 {
565         register_ethereal_tap("h225,counter", gtk_h225counter_init);
566
567         register_tap_menu_item("ITU-T H.225...", REGISTER_TAP_GROUP_NONE,
568             gtk_tap_dfilter_dlg_cb, NULL, NULL, &(h225_counter_dlg));
569 }