change all file offsets from long to gint64 so we can - theoretically - handle files...
[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/camel-persistentdata.h"
42
43 void register_tap_listener_camelcounter(void);
44
45 /* used to keep track of the statistics for an entire program interface */
46 struct camelcounter_t {
47   char *filter; 
48   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
49 };
50
51
52 static void camelcounter_reset(void *phs)
53 {
54   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
55   memset(p_counter,0,sizeof(struct camelcounter_t));
56 }
57
58 static int camelcounter_packet(void *phs,
59                                packet_info *pinfo _U_,
60                                epan_dissect_t *edt _U_, 
61                                const void *phi)
62 {
63   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
64   const struct camelsrt_info_t * pi=phi;
65   if (pi->opcode != 255)
66     p_counter->camel_msg[pi->opcode]++;
67   
68   return 1;
69 }
70
71
72 static void camelcounter_draw(void *phs)
73 {
74   struct camelcounter_t * p_counter= (struct camelcounter_t *)phs;
75   int i;
76   printf("\n");
77   printf("CAMEL Message and Response Status Counter:\n");
78   printf("------------------------------------------\n");
79
80   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
81     /* Message counter */
82     if(p_counter->camel_msg[i]!=0) {
83       printf("%30s ", val_to_str(i,camel_opr_code_strings,"Unknown message "));
84       printf("%6d\n", p_counter->camel_msg[i]);
85     } 
86   } /* Message Type */
87   printf("------------------------------------------\n");
88 }
89
90 static void camelcounter_init(const char *optarg, void* userdata _U_)
91 {
92   struct camelcounter_t *p_camelcounter;
93   const char *filter=NULL;
94   const char *emptyfilter=""; 
95   GString *error_string;
96
97   if(!strncmp(optarg,"camel,counter,",13)){
98     filter=optarg+13;
99   } else {
100     filter=NULL;
101   }
102
103   p_camelcounter = g_malloc(sizeof(struct camelcounter_t));
104   if(filter){
105     p_camelcounter->filter=g_malloc(strlen(filter)+1);
106     strcpy(p_camelcounter->filter,filter);
107   } else {
108     p_camelcounter->filter=NULL;
109   }
110   
111   camelcounter_reset(p_camelcounter);
112
113   if (filter) {
114     error_string=register_tap_listener("CAMEL",
115                                        p_camelcounter,
116                                        filter,
117                                        NULL,
118                                        camelcounter_packet,
119                                        camelcounter_draw);
120   } else {
121     error_string=register_tap_listener("CAMEL",
122                                        p_camelcounter,
123                                        emptyfilter,
124                                        NULL,
125                                        camelcounter_packet,
126                                        camelcounter_draw);
127   }
128
129   if(error_string){
130     /* error, we failed to attach to the tap. clean up */
131     g_free(p_camelcounter->filter);
132     g_free(p_camelcounter);
133
134     fprintf(stderr, "tshark: Couldn't register camel,counter tap: %s\n",
135             error_string->str);
136     g_string_free(error_string, TRUE);
137     exit(1);
138   }
139 }
140
141
142 void  /* Next line mandatory */
143 register_tap_listener_camelcounter(void)
144 {
145   register_stat_cmd_arg("camel,counter", camelcounter_init, NULL);
146 }