Manually revert the changes to CMakeLists.txt from commit 40602 - they should not...
[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 <epan/dissectors/packet-h225.h>
43
44 /* following values represent the size of their valuestring arrays */
45
46 #define RAS_MSG_TYPES 33
47 #define CS_MSG_TYPES 13
48
49 #define GRJ_REASONS 8
50 #define RRJ_REASONS 18
51 #define URQ_REASONS 6
52 #define URJ_REASONS 6
53 #define ARJ_REASONS 22
54 #define BRJ_REASONS 8
55 #define DRQ_REASONS 3
56 #define DRJ_REASONS 4
57 #define LRJ_REASONS 16
58 #define IRQNAK_REASONS 4
59 #define REL_CMP_REASONS 26
60 #define FACILITY_REASONS 11
61
62
63 /* used to keep track of the statistics for an entire program interface */
64 typedef struct _h225counter_t {
65         char *filter;
66         guint32 ras_msg[RAS_MSG_TYPES + 1];
67         guint32 cs_msg[CS_MSG_TYPES + 1];
68         guint32 grj_reason[GRJ_REASONS + 1];
69         guint32 rrj_reason[RRJ_REASONS + 1];
70         guint32 urq_reason[URQ_REASONS + 1];
71         guint32 urj_reason[URJ_REASONS + 1];
72         guint32 arj_reason[ARJ_REASONS + 1];
73         guint32 brj_reason[BRJ_REASONS + 1];
74         guint32 drq_reason[DRQ_REASONS + 1];
75         guint32 drj_reason[DRJ_REASONS + 1];
76         guint32 lrj_reason[LRJ_REASONS + 1];
77         guint32 irqnak_reason[IRQNAK_REASONS + 1];
78         guint32 rel_cmp_reason[REL_CMP_REASONS + 1];
79         guint32 facility_reason[FACILITY_REASONS + 1];
80 } h225counter_t;
81
82
83 static void
84 h225counter_reset(void *phs)
85 {
86         h225counter_t *hs=(h225counter_t *)phs;
87         char *save_filter = hs->filter;
88
89         memset(hs, 0, sizeof(h225counter_t));
90
91         hs->filter = save_filter;
92 }
93
94 static int
95 h225counter_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
96 {
97         h225counter_t *hs=(h225counter_t *)phs;
98         const h225_packet_info *pi=phi;
99
100         switch (pi->msg_type) {
101
102         case H225_RAS:
103                 if(pi->msg_tag==-1) { /* uninitialized */
104                         return 0;
105                 }
106                 else if (pi->msg_tag >= RAS_MSG_TYPES) { /* unknown */
107                         hs->ras_msg[RAS_MSG_TYPES]++;
108                 }
109                 else {
110                         hs->ras_msg[pi->msg_tag]++;
111                 }
112
113                 /* Look for reason tag */
114                 if(pi->reason==-1) { /* uninitialized */
115                         break;
116                 }
117
118                 switch(pi->msg_tag) {
119
120                 case 2: /* GRJ */
121                         if(pi->reason < GRJ_REASONS)
122                                 hs->grj_reason[pi->reason]++;
123                         else
124                                 hs->grj_reason[GRJ_REASONS]++;
125                         break;
126                 case 5: /* RRJ */
127                         if(pi->reason < RRJ_REASONS)
128                                 hs->rrj_reason[pi->reason]++;
129                         else
130                                 hs->rrj_reason[RRJ_REASONS]++;
131                         break;
132                 case 6: /* URQ */
133                         if(pi->reason < URQ_REASONS)
134                                 hs->urq_reason[pi->reason]++;
135                         else
136                                 hs->urq_reason[URQ_REASONS]++;
137                         break;
138                 case 8: /* URJ */
139                         if(pi->reason < URJ_REASONS)
140                                 hs->urj_reason[pi->reason]++;
141                         else
142                                 hs->urj_reason[URJ_REASONS]++;
143                         break;
144                 case 11: /* ARJ */
145                         if(pi->reason < ARJ_REASONS)
146                                 hs->arj_reason[pi->reason]++;
147                         else
148                                 hs->arj_reason[ARJ_REASONS]++;
149                         break;
150                 case 14: /* BRJ */
151                         if(pi->reason < BRJ_REASONS)
152                                 hs->brj_reason[pi->reason]++;
153                         else
154                                 hs->brj_reason[BRJ_REASONS]++;
155                         break;
156                 case 15: /* DRQ */
157                         if(pi->reason < DRQ_REASONS)
158                                 hs->drq_reason[pi->reason]++;
159                         else
160                                 hs->drq_reason[DRQ_REASONS]++;
161                         break;
162                 case 17: /* DRJ */
163                         if(pi->reason < DRJ_REASONS)
164                                 hs->drj_reason[pi->reason]++;
165                         else
166                                 hs->drj_reason[DRJ_REASONS]++;
167                         break;
168                 case 20: /* LRJ */
169                         if(pi->reason < LRJ_REASONS)
170                                 hs->lrj_reason[pi->reason]++;
171                         else
172                                 hs->lrj_reason[LRJ_REASONS]++;
173                         break;
174                 case 29: /* IRQ Nak */
175                         if(pi->reason < IRQNAK_REASONS)
176                                 hs->irqnak_reason[pi->reason]++;
177                         else
178                                 hs->irqnak_reason[IRQNAK_REASONS]++;
179                         break;
180
181                 default:
182                         /* do nothing */
183                         break;
184                 }
185
186                 break;
187
188         case H225_CS:
189                 if(pi->msg_tag==-1) { /* uninitialized */
190                         return 0;
191                 }
192                 else if (pi->msg_tag >= CS_MSG_TYPES) { /* unknown */
193                         hs->cs_msg[CS_MSG_TYPES]++;
194                 }
195                 else {
196                         hs->cs_msg[pi->msg_tag]++;
197                 }
198
199                 /* Look for reason tag */
200                 if(pi->reason==-1) { /* uninitialized */
201                         break;
202                 }
203
204                 switch(pi->msg_tag) {
205
206                 case 5: /* ReleaseComplete */
207                         if(pi->reason < REL_CMP_REASONS)
208                                 hs->rel_cmp_reason[pi->reason]++;
209                         else
210                                 hs->rel_cmp_reason[REL_CMP_REASONS]++;
211                         break;
212                 case 6: /* Facility */
213                         if(pi->reason < FACILITY_REASONS)
214                                 hs->facility_reason[pi->reason]++;
215                         else
216                                 hs->facility_reason[FACILITY_REASONS]++;
217                         break;
218                 default:
219                         /* do nothing */
220                         break;
221                 }
222
223                 break;
224
225         default:
226                 return 0;
227         }
228
229         return 1;
230 }
231
232
233 static void
234 h225counter_draw(void *phs)
235 {
236         h225counter_t *hs=(h225counter_t *)phs;
237         int i,j;
238
239         printf("================== H225 Message and Reason Counter ==================\n");
240         printf("RAS-Messages:\n");
241         for(i=0;i<=RAS_MSG_TYPES;i++) {
242                 if(hs->ras_msg[i]!=0) {
243                         printf("  %s : %u\n", val_to_str(i,h225_RasMessage_vals,"unknown ras-messages  "), hs->ras_msg[i]);
244                         /* reason counter */
245                         switch(i) {
246                         case 2: /* GRJ */
247                                 for(j=0;j<=GRJ_REASONS;j++) {
248                                         if(hs->grj_reason[j]!=0) {
249                                                 printf("    %s : %u\n", val_to_str(j,GatekeeperRejectReason_vals,"unknown reason   "), hs->grj_reason[j]);
250                                         }
251                                 }
252                                 break;
253                         case 5: /* RRJ */
254                                 for(j=0;j<=RRJ_REASONS;j++) {
255                                         if(hs->rrj_reason[j]!=0) {
256                                                 printf("    %s : %u\n", val_to_str(j,RegistrationRejectReason_vals,"unknown reason   "), hs->rrj_reason[j]);
257                                         }
258                                 }
259                                 break;
260                         case 6: /* URQ */
261                                 for(j=0;j<=URQ_REASONS;j++) {
262                                         if(hs->urq_reason[j]!=0) {
263                                                 printf("    %s : %u\n", val_to_str(j,UnregRequestReason_vals,"unknown reason   "), hs->urq_reason[j]);
264                                         }
265                                 }
266                                 break;
267                         case 8: /* URJ */
268                                 for(j=0;j<=URJ_REASONS;j++) {
269                                         if(hs->urj_reason[j]!=0) {
270                                                 printf("    %s : %u\n", val_to_str(j,UnregRejectReason_vals,"unknown reason   "), hs->urj_reason[j]);
271                                         }
272                                 }
273                                 break;
274                         case 11: /* ARJ */
275                                 for(j=0;j<=ARJ_REASONS;j++) {
276                                         if(hs->arj_reason[j]!=0) {
277                                                 printf("    %s : %u\n", val_to_str(j,AdmissionRejectReason_vals,"unknown reason   "), hs->arj_reason[j]);
278                                         }
279                                 }
280                                 break;
281                         case 14: /* BRJ */
282                                 for(j=0;j<=BRJ_REASONS;j++) {
283                                         if(hs->brj_reason[j]!=0) {
284                                                 printf("    %s : %u\n", val_to_str(j,BandRejectReason_vals,"unknown reason   "), hs->brj_reason[j]);
285                                         }
286                                 }
287                                 break;
288                         case 15: /* DRQ */
289                                 for(j=0;j<=DRQ_REASONS;j++) {
290                                         if(hs->drq_reason[j]!=0) {
291                                                 printf("    %s : %u\n", val_to_str(j,DisengageReason_vals,"unknown reason   "), hs->drq_reason[j]);
292                                         }
293                                 }
294                                 break;
295                         case 17: /* DRJ */
296                                 for(j=0;j<=DRJ_REASONS;j++) {
297                                         if(hs->drj_reason[j]!=0) {
298                                                 printf("    %s : %u\n", val_to_str(j,DisengageRejectReason_vals,"unknown reason   "), hs->drj_reason[j]);
299                                         }
300                                 }
301                                 break;
302                         case 20: /* LRJ */
303                                 for(j=0;j<=LRJ_REASONS;j++) {
304                                         if(hs->lrj_reason[j]!=0) {
305                                                 printf("    %s : %u\n", val_to_str(j,LocationRejectReason_vals,"unknown reason   "), hs->lrj_reason[j]);
306                                         }
307                                 }
308                                 break;
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 }