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