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