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