Second step in introducing asn context to BER dissectors just like in PER.
[obnox/wireshark/wip.git] / tap-camelcounter.c
1 /* tap_camelcounter.c
2  * camel message counter for tshark
3  * Copyright 2006 Florent DROUIN
4  * This part of code is extracted from tap-h225counter.c from Lars Roland
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/packet_info.h"
37 #include "epan/tap.h"
38 #include "epan/value_string.h"
39 #include "register.h"
40 #include "epan/stat_cmd_args.h"
41 #include "epan/asn1.h"
42 #include "epan/camel-persistentdata.h"
43
44 void register_tap_listener_camelcounter(void);
45
46 /* used to keep track of the statistics for an entire program interface */
47 struct camelcounter_t {
48   char *filter; 
49   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
50 };
51
52
53 static void camelcounter_reset(void *phs)
54 {
55   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
56   memset(p_counter,0,sizeof(struct camelcounter_t));
57 }
58
59 static int camelcounter_packet(void *phs,
60                                packet_info *pinfo _U_,
61                                epan_dissect_t *edt _U_, 
62                                const void *phi)
63 {
64   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
65   const struct camelsrt_info_t * pi=phi;
66   if (pi->opcode != 255)
67     p_counter->camel_msg[pi->opcode]++;
68   
69   return 1;
70 }
71
72
73 static void camelcounter_draw(void *phs)
74 {
75   struct camelcounter_t * p_counter= (struct camelcounter_t *)phs;
76   int i;
77   printf("\n");
78   printf("CAMEL Message and Response Status Counter:\n");
79   printf("------------------------------------------\n");
80
81   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
82     /* Message counter */
83     if(p_counter->camel_msg[i]!=0) {
84       printf("%30s ", val_to_str(i,camel_opr_code_strings,"Unknown message "));
85       printf("%6d\n", p_counter->camel_msg[i]);
86     } 
87   } /* Message Type */
88   printf("------------------------------------------\n");
89 }
90
91 static void camelcounter_init(const char *optarg, void* userdata _U_)
92 {
93   struct camelcounter_t *p_camelcounter;
94   const char *filter=NULL;
95   const char *emptyfilter=""; 
96   GString *error_string;
97
98   if(!strncmp(optarg,"camel,counter,",13)){
99     filter=optarg+13;
100   } else {
101     filter=NULL;
102   }
103
104   p_camelcounter = g_malloc(sizeof(struct camelcounter_t));
105   if(filter){
106     p_camelcounter->filter=g_malloc(strlen(filter)+1);
107     strcpy(p_camelcounter->filter,filter);
108   } else {
109     p_camelcounter->filter=NULL;
110   }
111   
112   camelcounter_reset(p_camelcounter);
113
114   if (filter) {
115     error_string=register_tap_listener("CAMEL",
116                                        p_camelcounter,
117                                        filter,
118                                        NULL,
119                                        camelcounter_packet,
120                                        camelcounter_draw);
121   } else {
122     error_string=register_tap_listener("CAMEL",
123                                        p_camelcounter,
124                                        emptyfilter,
125                                        NULL,
126                                        camelcounter_packet,
127                                        camelcounter_draw);
128   }
129
130   if(error_string){
131     /* error, we failed to attach to the tap. clean up */
132     g_free(p_camelcounter->filter);
133     g_free(p_camelcounter);
134
135     fprintf(stderr, "tshark: Couldn't register camel,counter tap: %s\n",
136             error_string->str);
137     g_string_free(error_string, TRUE);
138     exit(1);
139   }
140 }
141
142
143 void  /* Next line mandatory */
144 register_tap_listener_camelcounter(void)
145 {
146   register_stat_cmd_arg("camel,counter", camelcounter_init, NULL);
147 }