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