e16c2e1ced753766575651d2366c3ff5d5afe560
[metze/wireshark/wip.git] / ui / cli / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28 #include "config.h"
29
30 #include <stdio.h>
31
32 #include <string.h>
33 #include "epan/packet.h"
34 #include "epan/packet_info.h"
35 #include "epan/tap.h"
36 #include "epan/value_string.h"
37 #include "epan/stat_cmd_args.h"
38 #include "epan/asn1.h"
39 #include "epan/camel-persistentdata.h"
40
41 void register_tap_listener_camelcounter(void);
42
43 /* used to keep track of the statistics for an entire program interface */
44 struct camelcounter_t {
45   char *filter;
46   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
47 };
48
49
50 static void camelcounter_reset(void *phs)
51 {
52   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
53   memset(p_counter,0,sizeof(struct camelcounter_t));
54 }
55
56 static int camelcounter_packet(void *phs,
57                                packet_info *pinfo _U_,
58                                epan_dissect_t *edt _U_,
59                                const void *phi)
60 {
61   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
62   const struct camelsrt_info_t * pi=(const struct camelsrt_info_t *)phi;
63   if (pi->opcode != 255)
64     p_counter->camel_msg[pi->opcode]++;
65
66   return 1;
67 }
68
69
70 static void camelcounter_draw(void *phs)
71 {
72   struct camelcounter_t * p_counter= (struct camelcounter_t *)phs;
73   int i;
74   printf("\n");
75   printf("CAMEL Message and Response Status Counter:\n");
76   printf("------------------------------------------\n");
77
78   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
79     /* Message counter */
80     if(p_counter->camel_msg[i]!=0) {
81       printf("%30s ", val_to_str(i,camel_opr_code_strings,"Unknown message "));
82       printf("%6d\n", p_counter->camel_msg[i]);
83     }
84   } /* Message Type */
85   printf("------------------------------------------\n");
86 }
87
88 static void camelcounter_init(const char *opt_arg, void* userdata _U_)
89 {
90   struct camelcounter_t *p_camelcounter;
91   GString *error_string;
92
93   p_camelcounter = g_new(struct camelcounter_t,1);
94   if(!strncmp(opt_arg,"camel,counter,",13)){
95     p_camelcounter->filter=g_strdup(opt_arg+13);
96   } else {
97     p_camelcounter->filter=NULL;
98   }
99
100   camelcounter_reset(p_camelcounter);
101
102   error_string=register_tap_listener("CAMEL",
103                                      p_camelcounter,
104                                      p_camelcounter->filter,
105                                      0,
106                                      NULL,
107                                      camelcounter_packet,
108                                      camelcounter_draw);
109
110   if(error_string){
111     /* error, we failed to attach to the tap. clean up */
112     g_free(p_camelcounter->filter);
113     g_free(p_camelcounter);
114
115     fprintf(stderr, "tshark: Couldn't register camel,counter tap: %s\n",
116             error_string->str);
117     g_string_free(error_string, TRUE);
118     exit(1);
119   }
120 }
121
122
123 void  /* Next line mandatory */
124 register_tap_listener_camelcounter(void)
125 {
126   register_stat_cmd_arg("camel,counter", camelcounter_init, NULL);
127 }