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