More rewrite of prohibited APIs (sprintf, strcpy, strcat).
[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.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/stat_cmd_args.h"
42 #include "epan/asn1.h"
43 #include "epan/camel-persistentdata.h"
44
45 void register_tap_listener_camelcounter(void);
46
47 /* used to keep track of the statistics for an entire program interface */
48 struct camelcounter_t {
49   char *filter; 
50   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
51 };
52
53
54 static void camelcounter_reset(void *phs)
55 {
56   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
57   memset(p_counter,0,sizeof(struct camelcounter_t));
58 }
59
60 static int camelcounter_packet(void *phs,
61                                packet_info *pinfo _U_,
62                                epan_dissect_t *edt _U_, 
63                                const void *phi)
64 {
65   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
66   const struct camelsrt_info_t * pi=phi;
67   if (pi->opcode != 255)
68     p_counter->camel_msg[pi->opcode]++;
69   
70   return 1;
71 }
72
73
74 static void camelcounter_draw(void *phs)
75 {
76   struct camelcounter_t * p_counter= (struct camelcounter_t *)phs;
77   int i;
78   printf("\n");
79   printf("CAMEL Message and Response Status Counter:\n");
80   printf("------------------------------------------\n");
81
82   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
83     /* Message counter */
84     if(p_counter->camel_msg[i]!=0) {
85       printf("%30s ", val_to_str(i,camel_opr_code_strings,"Unknown message "));
86       printf("%6d\n", p_counter->camel_msg[i]);
87     } 
88   } /* Message Type */
89   printf("------------------------------------------\n");
90 }
91
92 static void camelcounter_init(const char *optarg, void* userdata _U_)
93 {
94   struct camelcounter_t *p_camelcounter;
95   const char *filter=NULL;
96   const char *emptyfilter=""; 
97   GString *error_string;
98
99   if(!strncmp(optarg,"camel,counter,",13)){
100     filter=optarg+13;
101   } else {
102     filter=NULL;
103   }
104
105   p_camelcounter = g_malloc(sizeof(struct camelcounter_t));
106   if(filter){
107     p_camelcounter->filter=g_strdup(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 }