Get rid of -Wshadow warning - I guess we're including something that
[metze/wireshark/wip.git] / ui / cli / tap-wspstat.c
1 /* tap-rpcstat.c
2  * wspstat   2003 Jean-Michel FAYARD
3  *
4  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /* This module provides WSP  statistics to tshark.
26  * It is only used by tshark and not wireshark
27  *
28  */
29
30 #include "config.h"
31
32 #include <stdio.h>
33
34 #include <string.h>
35 #include "epan/packet_info.h"
36 #include <epan/tap.h>
37 #include <epan/stat_cmd_args.h>
38 #include "epan/value_string.h"
39 #include <epan/dissectors/packet-wsp.h>
40
41 /* used to keep track of the stats for a specific PDU type*/
42 typedef struct _wsp_pdu_t {
43         const gchar     *type;
44         guint32          packets;
45 } wsp_pdu_t;
46 /* used to keep track of SRT statistics */
47 typedef struct _wsp_status_code_t {
48         const gchar     *name;
49         guint32          packets;
50 } wsp_status_code_t;
51 /* used to keep track of the statictics for an entire program interface */
52 typedef struct _wsp_stats_t {
53         char            *filter;
54         wsp_pdu_t       *pdu_stats;
55         guint32 num_pdus;
56         GHashTable      *hash;
57 } wspstat_t;
58
59 static void
60 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ )
61 {
62         data->packets = 0;
63 }
64 static void
65 wsp_print_statuscode(gint *key, wsp_status_code_t *data, char* format)
66 {
67         if (data && (data->packets!=0))
68                 printf(format, *key, data->packets ,data->name);
69 }
70 static void
71 wsp_free_hash_table( gpointer key, gpointer value, gpointer user_data _U_ )
72 {
73         g_free(key);
74         g_free(value);
75 }
76 static void
77 wspstat_reset(void *psp)
78 {
79         wspstat_t *sp=(wspstat_t *)psp;
80         guint32 i;
81
82         for(i=1;i<=sp->num_pdus;i++)
83         {
84                 sp->pdu_stats[i].packets=0;
85         }
86         g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
87 }
88
89
90 /* This callback is invoked whenever the tap system has seen a packet
91  * we might be interested in.
92  * The function is to be used to only update internal state information
93  * in the *tapdata structure, and if there were state changes which requires
94  * the window to be redrawn, return 1 and (*draw) will be called sometime
95  * later.
96  *
97  * We didnt apply a filter when we registered so we will be called for
98  * ALL packets and not just the ones we are collecting stats for.
99  *
100  */
101 static gint
102 pdut2index(gint pdut)
103 {
104         if (pdut<=0x09)         return pdut;
105         if (pdut>=0x40){
106                 if (pdut <= 0x44){
107                         return pdut-54;
108                 } else if (pdut==0x60||pdut==0x61){
109                         return pdut-81;
110                 }
111         }
112         return 0;
113 }
114 static gint
115 index2pdut(gint pdut)
116 {
117         if (pdut<=0x09)
118                 return pdut;
119         if (pdut<=14)
120                 return pdut+54;
121         if (pdut<=16)
122                 return pdut+81;
123         return 0;
124 }
125 static int
126 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
127 {
128         wspstat_t *sp=(wspstat_t *)psp;
129         const wsp_info_value_t *value=(const wsp_info_value_t *)pri;
130         gint idx = pdut2index(value->pdut);
131         int retour=0;
132
133         if (value->status_code != 0) {
134                 gint *key=g_new(gint,1);
135                 wsp_status_code_t *sc;
136                 *key=value->status_code ;
137                 sc = (wsp_status_code_t *)g_hash_table_lookup(
138                                 sp->hash,
139                                 key);
140                 if (!sc) {
141                         sc = g_new(wsp_status_code_t,1);
142                         sc -> packets = 1;
143                         sc -> name = NULL;
144                         g_hash_table_insert(
145                                 sp->hash,
146                                 key,
147                                 sc);
148                 } else {
149                         sc->packets++;
150                 }
151                 retour=1;
152         }
153
154
155
156         if (idx!=0) {
157                 sp->pdu_stats[ idx ].packets++;
158                 retour = 1;
159         }
160         return retour;
161 }
162
163
164 /* This callback is used when tshark wants us to draw/update our
165  * data to the output device. Since this is tshark only output is
166  * stdout.
167  * TShark will only call this callback once, which is when tshark has
168  * finished reading all packets and exists.
169  * If used with wireshark this may be called any time, perhaps once every 3
170  * seconds or so.
171  * This function may even be called in parallell with (*reset) or (*draw)
172  * so make sure there are no races. The data in the rpcstat_t can thus change
173  * beneath us. Beware.
174  */
175 static void
176 wspstat_draw(void *psp)
177 {
178         wspstat_t *sp=(wspstat_t *)psp;
179         guint32 i;
180
181         printf("\n");
182         printf("===================================================================\n");
183         printf("WSP Statistics:\n");
184         printf("%-23s %9s || %-23s %9s\n","PDU Type", "Packets", "PDU Type", "Packets");
185         for(i=1; i<= ((sp->num_pdus+1)/2) ; i++)
186         {
187                 guint32 ii=i+sp->num_pdus/2;
188                 printf("%-23s %9d", sp->pdu_stats[i ].type, sp->pdu_stats[i ].packets);
189                 printf(" || ");
190                 if (ii< (sp->num_pdus) )
191                         printf("%-23s %9d\n", sp->pdu_stats[ii].type, sp->pdu_stats[ii].packets);
192                 else
193                         printf("\n");
194                 }
195         printf("\nStatus code in reply packets\n");
196         printf(         "Status Code    Packets  Description\n");
197         g_hash_table_foreach( sp->hash, (GHFunc) wsp_print_statuscode,
198                         (gpointer)"       0x%02X  %9d  %s\n" ) ;
199         printf("===================================================================\n");
200 }
201
202 /* When called, this function will create a new instance of wspstat.
203  * program and version are whick onc-rpc program/version we want to
204  * collect statistics for.
205  * This function is called from tshark when it parses the -z wsp, arguments
206  * and it creates a new instance to store statistics in and registers this
207  * new instance for the wsp tap.
208  */
209 static void
210 wspstat_init(const char *opt_arg, void* userdata _U_)
211 {
212         wspstat_t          *sp;
213         const char         *filter=NULL;
214         guint32             i;
215         GString            *error_string;
216         wsp_status_code_t  *sc;
217         const value_string *wsp_vals_status_p;
218
219         if (!strncmp (opt_arg, "wsp,stat," , 9)){
220                 filter=opt_arg+9;
221         } else {
222                 filter=NULL;
223         }
224
225
226         sp = g_new(wspstat_t,1);
227         sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
228         wsp_vals_status_p = VALUE_STRING_EXT_VS_P(&wsp_vals_status_ext);
229         for (i=0 ; wsp_vals_status_p[i].strptr ; i++ )
230         {
231                 gint *key;
232                 sc=g_new(wsp_status_code_t,1);
233                 key=g_new(gint,1);
234                 sc->packets=0;
235                 sc->name=wsp_vals_status_p[i].strptr;
236                 *key=wsp_vals_status_p[i].value;
237                 g_hash_table_insert(
238                                 sp->hash,
239                                 key,
240                                 sc);
241         }
242         sp->num_pdus = 16;
243         sp->pdu_stats=g_new(wsp_pdu_t,(sp->num_pdus+1));
244         if(filter){
245                 sp->filter=g_strdup(filter);
246         } else {
247                 sp->filter=NULL;
248         }
249         for (i=0;i<sp->num_pdus; i++)
250         {
251                 sp->pdu_stats[i].packets=0;
252                 sp->pdu_stats[i].type = try_val_to_str_ext( index2pdut( i ), &wsp_vals_pdu_type_ext) ;
253         }
254
255         error_string = register_tap_listener(
256                         "wsp",
257                         sp,
258                         filter,
259                         0,
260                         wspstat_reset,
261                         wspstat_packet,
262                         wspstat_draw);
263         if (error_string){
264                 /* error, we failed to attach to the tap. clean up */
265                 g_free(sp->pdu_stats);
266                 g_free(sp->filter);
267                 g_free(sp);
268                 g_hash_table_foreach( sp->hash, (GHFunc) wsp_free_hash_table, NULL ) ;
269                 g_hash_table_destroy( sp->hash );
270                 fprintf(stderr, "tshark: Couldn't register wsp,stat tap: %s\n",
271                                 error_string->str);
272                 g_string_free(error_string, TRUE);
273                 exit(1);
274         }
275 }
276 void
277 register_tap_listener_wspstat(void)
278 {
279         register_stat_cmd_arg("wsp,stat,", wspstat_init,NULL);
280 }