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