Make various lengths unsigned in "dissect_fhandle_data_unknown()", so
[obnox/wireshark/wip.git] / tap-rpcstat.c
1 /* tap-rpcstat.c
2  * rpcstat   2002 Ronnie Sahlberg
3  *
4  * $Id: tap-rpcstat.c,v 1.8 2003/04/23 08:20:02 guy Exp $
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 rpc call/reply RTT statistics to tethereal.
26  * It is only used by tethereal and not ethereal
27  *
28  * It serves as an example on how to use the tap api.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #include <string.h>
42 #include "epan/packet_info.h"
43 #include "tap.h"
44 #include "packet-rpc.h"
45 #include "register.h"
46
47 /* used to keep track of statistics for a specific procedure */
48 typedef struct _rpc_procedure_t {
49         char *proc;
50         int num;
51         nstime_t min;
52         nstime_t max;
53         nstime_t tot;
54 } rpc_procedure_t;
55
56 /* used to keep track of the statistics for an entire program interface */
57 typedef struct _rpcstat_t {
58         char *prog;
59         char *filter;
60         guint32 program;
61         guint32 version;
62         guint32 num_procedures;
63         rpc_procedure_t *procedures;
64 } rpcstat_t;
65
66
67
68 /* This callback is never used by tethereal but it is here for completeness.
69  * When registering below, we could just have left this function as NULL.
70  *
71  * When used by ethereal, this function will be called whenever we would need
72  * to reset all state. Such as when ethereal opens a new file, when it
73  * starts a new capture, when it rescans the packetlist after some prefs have
74  * changed etc.
75  * So if your aplication has some state it needs to clean up in those
76  * situations, here is a good place to put that code.
77  */
78 static void
79 rpcstat_reset(void *prs)
80 {
81         rpcstat_t *rs=prs;
82         guint32 i;
83
84         for(i=0;i<rs->num_procedures;i++){
85                 rs->procedures[i].num=0;        
86                 rs->procedures[i].min.secs=0;
87                 rs->procedures[i].min.nsecs=0;
88                 rs->procedures[i].max.secs=0;
89                 rs->procedures[i].max.nsecs=0;
90                 rs->procedures[i].tot.secs=0;
91                 rs->procedures[i].tot.nsecs=0;
92         }
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  * This function should be as lightweight as possible since it executes together
104  * with the normal ethereal dissectors. Try to push as much processing as
105  * possible into (*draw) instead since that function executes asynchronously
106  * and does not affect the main threads performance.
107  *
108  * If it is possible, try to do all "filtering" explicitely as we do below in 
109  * this example since you will get MUCH better performance than applying
110  * a similar display-filter in the register call.
111  *
112  * The third parameter is tap dependant. Since we register this one to the "rpc"
113  * tap the third parameters type is rpc_call_info_value.
114  *
115  *
116  * The filtering we do is just to check the rpc_call_info_value struct that
117  * we were called for the proper program and version. We didnt apply a filter
118  * when we registered so we will be called for ALL rpc packets and not just
119  * the ones we are collecting stats for.
120  * 
121  *
122  * function returns :
123  *  0: no updates, no need to call (*draw) later
124  * !0: state has changed, call (*draw) sometime later
125  */
126 static int
127 rpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *pri)
128 {
129         rpcstat_t *rs=prs;
130         rpc_call_info_value *ri=pri;
131         nstime_t delta;
132         rpc_procedure_t *rp;
133
134         if(ri->proc>=rs->num_procedures){
135                 /* dont handle this since its outside of known table */
136                 return 0;
137         }
138         /* we are only interested in reply packets */
139         if(ri->request){
140                 return 0;
141         }
142         /* we are only interested in certain program/versions */
143         if( (ri->prog!=rs->program) || (ri->vers!=rs->version) ){
144                 return 0;
145         }
146
147         rp=&(rs->procedures[ri->proc]);
148
149         /* calculate time delta between request and reply */
150         delta.secs=pinfo->fd->abs_secs-ri->req_time.secs;
151         delta.nsecs=pinfo->fd->abs_usecs*1000-ri->req_time.nsecs;
152         if(delta.nsecs<0){
153                 delta.nsecs+=1000000000;
154                 delta.secs--;
155         }
156
157         if((rp->max.secs==0)
158         && (rp->max.nsecs==0) ){
159                 rp->max.secs=delta.secs;
160                 rp->max.nsecs=delta.nsecs;
161         }
162
163         if((rp->min.secs==0)
164         && (rp->min.nsecs==0) ){
165                 rp->min.secs=delta.secs;
166                 rp->min.nsecs=delta.nsecs;
167         }
168
169         if( (delta.secs<rp->min.secs)
170         ||( (delta.secs==rp->min.secs)
171           &&(delta.nsecs<rp->min.nsecs) ) ){
172                 rp->min.secs=delta.secs;
173                 rp->min.nsecs=delta.nsecs;
174         }
175
176         if( (delta.secs>rp->max.secs)
177         ||( (delta.secs==rp->max.secs)
178           &&(delta.nsecs>rp->max.nsecs) ) ){
179                 rp->max.secs=delta.secs;
180                 rp->max.nsecs=delta.nsecs;
181         }
182         
183         rp->tot.secs += delta.secs;
184         rp->tot.nsecs += delta.nsecs;
185         if(rp->tot.nsecs>1000000000){
186                 rp->tot.nsecs-=1000000000;
187                 rp->tot.secs++;
188         }
189         rp->num++;
190
191         return 1;
192 }
193
194 /* This callback is used when tethereal wants us to draw/update our
195  * data to the output device. Since this is tethereal only output is
196  * stdout.
197  * Tethereal will only call this callback once, which is when tethereal has
198  * finished reading all packets and exists.
199  * If used with ethereal this may be called any time, perhaps once every 3 
200  * seconds or so.
201  * This function may even be called in parallell with (*reset) or (*draw)
202  * so make sure there are no races. The data in the rpcstat_t can thus change
203  * beneath us. Beware.
204  */
205 static void
206 rpcstat_draw(void *prs)
207 {
208         rpcstat_t *rs=prs;
209         guint32 i;
210 #ifdef G_HAVE_UINT64
211         guint64 td;
212 #else
213         guint32 td;
214 #endif
215         printf("\n");
216         printf("===================================================================\n");
217         printf("%s Version %d RTT Statistics:\n", rs->prog, rs->version);
218         printf("Filter: %s\n",rs->filter?rs->filter:"");
219         printf("Procedure        Calls   Min RTT   Max RTT   Avg RTT\n");
220         for(i=0;i<rs->num_procedures;i++){
221                 /* scale it to units of 10us.*/
222                 /* for long captures with a large tot time, this can overflow on 32bit */
223                 td=(int)rs->procedures[i].tot.secs;
224                 td=td*100000+(int)rs->procedures[i].tot.nsecs/10000;
225                 if(rs->procedures[i].num){
226                         td/=rs->procedures[i].num;
227                 } else {
228                         td=0;
229                 }
230
231                 printf("%-15s %6d %3d.%05d %3d.%05d %3d.%05d\n",
232                         rs->procedures[i].proc,
233                         rs->procedures[i].num,
234                         (int)rs->procedures[i].min.secs,rs->procedures[i].min.nsecs/10000,
235                         (int)rs->procedures[i].max.secs,rs->procedures[i].max.nsecs/10000,
236                         td/100000, td%100000
237                 );
238         }
239         printf("===================================================================\n");
240 }
241
242 static guint32 rpc_program=0;
243 static guint32 rpc_version=0;
244 static gint32 rpc_min_proc=-1;
245 static gint32 rpc_max_proc=-1;
246
247 static void *
248 rpcstat_find_procs(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
249 {
250         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
251
252         if(k->prog!=rpc_program){
253                 return NULL;
254         }
255         if(k->vers!=rpc_version){
256                 return NULL;
257         }
258         if(rpc_min_proc==-1){
259                 rpc_min_proc=k->proc;
260                 rpc_max_proc=k->proc;
261         }
262         if((gint32)k->proc<rpc_min_proc){
263                 rpc_min_proc=k->proc;
264         }
265         if((gint32)k->proc>rpc_max_proc){
266                 rpc_max_proc=k->proc;
267         }
268
269         return NULL;
270 }
271
272
273 /* When called, this function will create a new instance of rpcstat.
274  * program and version are whick onc-rpc program/version we want to
275  * collect statistics for.
276  * This function is called from tethereal when it parses the -Z rpc, arguments
277  * and it creates a new instance to store statistics in and registers this
278  * new instance for the rpc tap.
279  */
280 static void
281 rpcstat_init(char *optarg)
282 {
283         rpcstat_t *rs;
284         guint32 i;
285         int program, version;
286         int pos=0;
287         char *filter=NULL;
288         GString *error_string;
289
290         if(sscanf(optarg,"rpc,rtt,%d,%d,%n",&program,&version,&pos)==2){
291                 if(pos){
292                         filter=optarg+pos;
293                 } else {
294                         filter=NULL;
295                 }
296         } else {
297                 fprintf(stderr, "tethereal: invalid \"-z rpc,rtt,<program>,<version>[,<filter>]\" argument\n");
298                 exit(1);
299         }
300
301         rs=g_malloc(sizeof(rpcstat_t));
302         rs->prog=rpc_prog_name(program);
303         rs->program=program;
304         rs->version=version;
305         if(filter){
306                 rs->filter=g_malloc(strlen(filter)+1);
307                 strcpy(rs->filter, filter);
308         } else {
309                 rs->filter=NULL;
310         }
311         rpc_program=program;
312         rpc_version=version;
313         rpc_min_proc=-1;
314         rpc_max_proc=-1;
315         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_procs, NULL);
316         if(rpc_min_proc==-1){
317                 fprintf(stderr,"tethereal: Invalid -Z rpc,rrt,%d,%d\n",rpc_program,rpc_version);
318                 fprintf(stderr,"   Program:%d version:%d is not supported by tethereal.\n", rpc_program, rpc_version);
319                 exit(1);
320         }
321
322
323         rs->num_procedures=rpc_max_proc+1;
324         rs->procedures=g_malloc(sizeof(rpc_procedure_t)*(rs->num_procedures+1));
325         for(i=0;i<rs->num_procedures;i++){
326                 rs->procedures[i].proc=rpc_proc_name(program, version, i);
327                 rs->procedures[i].num=0;        
328                 rs->procedures[i].min.secs=0;
329                 rs->procedures[i].min.nsecs=0;
330                 rs->procedures[i].max.secs=0;
331                 rs->procedures[i].max.nsecs=0;
332                 rs->procedures[i].tot.secs=0;
333                 rs->procedures[i].tot.nsecs=0;
334         }
335
336 /* It is possible to create a filter and attach it to the callbacks. Then the
337  * callbacks would only be invoked if the filter matched.
338  * Evaluating filters is expensive and if we can avoid it and not use them
339  * we gain performance. 
340  * In this case we do the filtering for protocol and version inside the 
341  * callback itself but use whatever filter the user provided.
342  * (Perhaps the user only want the stats for nis+ traffic for certain objects?)
343  *
344  */
345
346         error_string=register_tap_listener("rpc", rs, filter, rpcstat_reset, rpcstat_packet, rpcstat_draw);
347         if(error_string){
348                 /* error, we failed to attach to the tap. clean up */
349                 g_free(rs->procedures);
350                 g_free(rs->filter);
351                 g_free(rs);
352
353                 fprintf(stderr, "tethereal: Couldn't register rpc,rtt tap: %s\n",
354                     error_string->str);
355                 g_string_free(error_string, TRUE);
356                 exit(1);
357         }
358 }
359
360
361 void
362 register_tap_listener_rpcstat(void)
363 {
364         register_ethereal_tap("rpc,rtt,", rpcstat_init);
365 }
366