Fix for bug 4695:
[obnox/wireshark/wip.git] / tap-h225counter.c
1 /* tap_h225counter.c
2  * h225 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 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37 #include "epan/packet.h"
38 #include "epan/packet_info.h"
39 #include <epan/tap.h>
40 #include <epan/stat_cmd_args.h>
41 #include "epan/value_string.h"
42 #include "register.h"
43 #include <epan/dissectors/packet-h225.h>
44
45 /* following values represent the size of their valuestring arrays */
46
47 #define RAS_MSG_TYPES 33
48 #define CS_MSG_TYPES 13
49
50 #define GRJ_REASONS 8
51 #define RRJ_REASONS 18
52 #define URQ_REASONS 6
53 #define URJ_REASONS 6
54 #define ARJ_REASONS 22
55 #define BRJ_REASONS 8
56 #define DRQ_REASONS 3
57 #define DRJ_REASONS 4
58 #define LRJ_REASONS 16
59 #define IRQNAK_REASONS 4
60 #define REL_CMP_REASONS 26
61 #define FACILITY_REASONS 11
62
63
64 /* used to keep track of the statistics for an entire program interface */
65 typedef struct _h225counter_t {
66         char *filter;
67         guint32 ras_msg[RAS_MSG_TYPES + 1];
68         guint32 cs_msg[CS_MSG_TYPES + 1];
69         guint32 grj_reason[GRJ_REASONS + 1];
70         guint32 rrj_reason[RRJ_REASONS + 1];
71         guint32 urq_reason[URQ_REASONS + 1];
72         guint32 urj_reason[URJ_REASONS + 1];
73         guint32 arj_reason[ARJ_REASONS + 1];
74         guint32 brj_reason[BRJ_REASONS + 1];
75         guint32 drq_reason[DRQ_REASONS + 1];
76         guint32 drj_reason[DRJ_REASONS + 1];
77         guint32 lrj_reason[LRJ_REASONS + 1];
78         guint32 irqnak_reason[IRQNAK_REASONS + 1];
79         guint32 rel_cmp_reason[REL_CMP_REASONS + 1];
80         guint32 facility_reason[FACILITY_REASONS + 1];
81 } h225counter_t;
82
83
84 static void
85 h225counter_reset(void *phs)
86 {
87         h225counter_t *hs=(h225counter_t *)phs;
88         char *save_filter = hs->filter;
89
90         memset(hs, 0, sizeof(h225counter_t));
91
92         hs->filter = save_filter;
93 }
94
95 static int
96 h225counter_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
97 {
98         h225counter_t *hs=(h225counter_t *)phs;
99         const h225_packet_info *pi=phi;
100
101         switch (pi->msg_type) {
102
103         case H225_RAS:
104                 if(pi->msg_tag==-1) { /* uninitialized */
105                         return 0;
106                 }
107                 else if (pi->msg_tag >= RAS_MSG_TYPES) { /* unknown */
108                         hs->ras_msg[RAS_MSG_TYPES]++;
109                 }
110                 else {
111                         hs->ras_msg[pi->msg_tag]++;
112                 }
113
114                 /* Look for reason tag */
115                 if(pi->reason==-1) { /* uninitialized */
116                         break;
117                 }
118
119                 switch(pi->msg_tag) {
120
121                 case 2: /* GRJ */
122                         if(pi->reason < GRJ_REASONS)
123                                 hs->grj_reason[pi->reason]++;
124                         else
125                                 hs->grj_reason[GRJ_REASONS]++;
126                         break;
127                 case 5: /* RRJ */
128                         if(pi->reason < RRJ_REASONS)
129                                 hs->rrj_reason[pi->reason]++;
130                         else
131                                 hs->rrj_reason[RRJ_REASONS]++;
132                         break;
133                 case 6: /* URQ */
134                         if(pi->reason < URQ_REASONS)
135                                 hs->urq_reason[pi->reason]++;
136                         else
137                                 hs->urq_reason[URQ_REASONS]++;
138                         break;
139                 case 8: /* URJ */
140                         if(pi->reason < URJ_REASONS)
141                                 hs->urj_reason[pi->reason]++;
142                         else
143                                 hs->urj_reason[URJ_REASONS]++;
144                         break;
145                 case 11: /* ARJ */
146                         if(pi->reason < ARJ_REASONS)
147                                 hs->arj_reason[pi->reason]++;
148                         else
149                                 hs->arj_reason[ARJ_REASONS]++;
150                         break;
151                 case 14: /* BRJ */
152                         if(pi->reason < BRJ_REASONS)
153                                 hs->brj_reason[pi->reason]++;
154                         else
155                                 hs->brj_reason[BRJ_REASONS]++;
156                         break;
157                 case 15: /* DRQ */
158                         if(pi->reason < DRQ_REASONS)
159                                 hs->drq_reason[pi->reason]++;
160                         else
161                                 hs->drq_reason[DRQ_REASONS]++;
162                         break;
163                 case 17: /* DRJ */
164                         if(pi->reason < DRJ_REASONS)
165                                 hs->drj_reason[pi->reason]++;
166                         else
167                                 hs->drj_reason[DRJ_REASONS]++;
168                         break;
169                 case 20: /* LRJ */
170                         if(pi->reason < LRJ_REASONS)
171                                 hs->lrj_reason[pi->reason]++;
172                         else
173                                 hs->lrj_reason[LRJ_REASONS]++;
174                         break;
175                 case 29: /* IRQ Nak */
176                         if(pi->reason < IRQNAK_REASONS)
177                                 hs->irqnak_reason[pi->reason]++;
178                         else
179                                 hs->irqnak_reason[IRQNAK_REASONS]++;
180                         break;
181
182                 default:
183                         /* do nothing */
184                         break;
185                 }
186
187                 break;
188
189         case H225_CS:
190                 if(pi->msg_tag==-1) { /* uninitialized */
191                         return 0;
192                 }
193                 else if (pi->msg_tag >= CS_MSG_TYPES) { /* unknown */
194                         hs->cs_msg[CS_MSG_TYPES]++;
195                 }
196                 else {
197                         hs->cs_msg[pi->msg_tag]++;
198                 }
199
200                 /* Look for reason tag */
201                 if(pi->reason==-1) { /* uninitialized */
202                         break;
203                 }
204
205                 switch(pi->msg_tag) {
206
207                 case 5: /* ReleaseComplete */
208                         if(pi->reason < REL_CMP_REASONS)
209                                 hs->rel_cmp_reason[pi->reason]++;
210                         else
211                                 hs->rel_cmp_reason[REL_CMP_REASONS]++;
212                         break;
213                 case 6: /* Facility */
214                         if(pi->reason < FACILITY_REASONS)
215                                 hs->facility_reason[pi->reason]++;
216                         else
217                                 hs->facility_reason[FACILITY_REASONS]++;
218                         break;
219                 default:
220                         /* do nothing */
221                         break;
222                 }
223
224                 break;
225
226         default:
227                 return 0;
228         }
229
230         return 1;
231 }
232
233
234 static void
235 h225counter_draw(void *phs)
236 {
237         h225counter_t *hs=(h225counter_t *)phs;
238         int i,j;
239
240         printf("================== H225 Message and Reason Counter ==================\n");
241         printf("RAS-Messages:\n");
242         for(i=0;i<=RAS_MSG_TYPES;i++) {
243                 if(hs->ras_msg[i]!=0) {
244                         printf("  %s : %u\n", val_to_str(i,h225_RasMessage_vals,"unknown ras-messages  "), hs->ras_msg[i]);
245                         /* reason counter */
246                         switch(i) {
247                         case 2: /* GRJ */
248                                 for(j=0;j<=GRJ_REASONS;j++) {
249                                         if(hs->grj_reason[j]!=0) {
250                                                 printf("    %s : %u\n", val_to_str(j,GatekeeperRejectReason_vals,"unknown reason   "), hs->grj_reason[j]);
251                                         }
252                                 }
253                                 break;
254                         case 5: /* RRJ */
255                                 for(j=0;j<=RRJ_REASONS;j++) {
256                                         if(hs->rrj_reason[j]!=0) {
257                                                 printf("    %s : %u\n", val_to_str(j,RegistrationRejectReason_vals,"unknown reason   "), hs->rrj_reason[j]);
258                                         }
259                                 }
260                                 break;
261                         case 6: /* URQ */
262                                 for(j=0;j<=URQ_REASONS;j++) {
263                                         if(hs->urq_reason[j]!=0) {
264                                                 printf("    %s : %u\n", val_to_str(j,UnregRequestReason_vals,"unknown reason   "), hs->urq_reason[j]);
265                                         }
266                                 }
267                                 break;
268                         case 8: /* URJ */
269                                 for(j=0;j<=URJ_REASONS;j++) {
270                                         if(hs->urj_reason[j]!=0) {
271                                                 printf("    %s : %u\n", val_to_str(j,UnregRejectReason_vals,"unknown reason   "), hs->urj_reason[j]);
272                                         }
273                                 }
274                                 break;
275                         case 11: /* ARJ */
276                                 for(j=0;j<=ARJ_REASONS;j++) {
277                                         if(hs->arj_reason[j]!=0) {
278                                                 printf("    %s : %u\n", val_to_str(j,AdmissionRejectReason_vals,"unknown reason   "), hs->arj_reason[j]);
279                                         }
280                                 }
281                                 break;
282                         case 14: /* BRJ */
283                                 for(j=0;j<=BRJ_REASONS;j++) {
284                                         if(hs->brj_reason[j]!=0) {
285                                                 printf("    %s : %u\n", val_to_str(j,BandRejectReason_vals,"unknown reason   "), hs->brj_reason[j]);
286                                         }
287                                 }
288                                 break;
289                         case 15: /* DRQ */
290                                 for(j=0;j<=DRQ_REASONS;j++) {
291                                         if(hs->drq_reason[j]!=0) {
292                                                 printf("    %s : %u\n", val_to_str(j,DisengageReason_vals,"unknown reason   "), hs->drq_reason[j]);
293                                         }
294                                 }
295                                 break;
296                         case 17: /* DRJ */
297                                 for(j=0;j<=DRJ_REASONS;j++) {
298                                         if(hs->drj_reason[j]!=0) {
299                                                 printf("    %s : %u\n", val_to_str(j,DisengageRejectReason_vals,"unknown reason   "), hs->drj_reason[j]);
300                                         }
301                                 }
302                                 break;
303                         case 20: /* LRJ */
304                                 for(j=0;j<=LRJ_REASONS;j++) {
305                                         if(hs->lrj_reason[j]!=0) {
306                                                 printf("    %s : %u\n", val_to_str(j,LocationRejectReason_vals,"unknown reason   "), hs->lrj_reason[j]);
307                                         }
308                                 }
309                         case 29: /* IRQNak */
310                                 for(j=0;j<=IRQNAK_REASONS;j++) {
311                                         if(hs->irqnak_reason[j]!=0) {
312                                                 printf("    %s : %u\n", val_to_str(j,InfoRequestNakReason_vals,"unknown reason   "), hs->irqnak_reason[j]);
313                                         }
314                                 }
315                                 break;
316                         default:
317                                 break;
318                         }
319                         /* end of reason counter*/
320                 }
321         }
322         printf("Call Signalling:\n");
323         for(i=0;i<=CS_MSG_TYPES;i++) {
324                 if(hs->cs_msg[i]!=0) {
325                         printf("  %s : %u\n", val_to_str(i,T_h323_message_body_vals,"unknown cs-messages   "), hs->cs_msg[i]);
326                         /* reason counter */
327                         switch(i) {
328                         case 5: /* ReleaseComplete */
329                                 for(j=0;j<=REL_CMP_REASONS;j++) {
330                                         if(hs->rel_cmp_reason[j]!=0) {
331                                                 printf("    %s : %u\n", val_to_str(j,h225_ReleaseCompleteReason_vals,"unknown reason   "), hs->rel_cmp_reason[j]);
332                                         }
333                                 }
334                                 break;
335                         case 6: /* Facility */
336                                 for(j=0;j<=FACILITY_REASONS;j++) {
337                                         if(hs->facility_reason[j]!=0) {
338                                                 printf("    %s : %u\n", val_to_str(j,FacilityReason_vals,"unknown reason   "), hs->facility_reason[j]);
339                                         }
340                                 }
341                                 break;
342                         default:
343                                 break;
344                         }
345                 }
346         }
347         printf("=====================================================================\n");
348 }
349
350
351 static void
352 h225counter_init(const char *optarg, void* userdata _U_)
353 {
354         h225counter_t *hs;
355         GString *error_string;
356
357         hs = g_malloc(sizeof(h225counter_t));
358         if(!strncmp(optarg,"h225,counter,",13)){
359                 hs->filter=g_strdup(optarg+13);
360         } else {
361                 hs->filter=NULL;
362         }
363
364         h225counter_reset(hs);
365
366         error_string=register_tap_listener("h225", hs, hs->filter, 0, NULL, h225counter_packet, h225counter_draw);
367         if(error_string){
368                 /* error, we failed to attach to the tap. clean up */
369                 g_free(hs->filter);
370                 g_free(hs);
371
372                 fprintf(stderr, "tshark: Couldn't register h225,counter tap: %s\n",
373                     error_string->str);
374                 g_string_free(error_string, TRUE);
375                 exit(1);
376         }
377 }
378
379
380 void
381 register_tap_listener_h225counter(void)
382 {
383         register_stat_cmd_arg("h225,counter", h225counter_init,NULL);
384 }