From Charlie Lenahan: support for some HP switch protocol.
[obnox/wireshark/wip.git] / tap-rpcprogs.c
1 /* tap-rpcprogs.c
2  * rpcstat   2002 Ronnie Sahlberg
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 rpc call/reply SRT statistics to tethereal.
26  * It is only used by tethereal and not ethereal
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #include <string.h>
40 #include "epan/packet_info.h"
41 #include <epan/tap.h>
42 #include <epan/dissectors/packet-rpc.h>
43 #include "register.h"
44
45 /* used to keep track of statistics for a specific program/version */
46 typedef struct _rpc_program_t {
47         struct _rpc_program_t *next;
48         guint32 program;
49         guint32 version;
50         int num;
51         nstime_t min;
52         nstime_t max;
53         nstime_t tot;
54 } rpc_program_t;
55
56 static rpc_program_t *prog_list=NULL;
57 static int already_enabled=0;
58
59 static int
60 rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
61 {
62         const rpc_call_info_value *ri=pri;
63         nstime_t delta;
64         rpc_program_t *rp=NULL;
65
66         if(!prog_list){
67                 /* the list was empty */
68                 rp=g_malloc(sizeof(rpc_program_t));
69                 rp->next=NULL;
70                 rp->program=ri->prog;
71                 rp->version=ri->vers;
72                 rp->num=0;
73                 rp->min.secs=0;
74                 rp->min.nsecs=0;
75                 rp->max.secs=0;
76                 rp->max.nsecs=0;
77                 rp->tot.secs=0;
78                 rp->tot.nsecs=0;
79                 prog_list=rp;
80         } else if((ri->prog==prog_list->program)
81                 &&(ri->vers==prog_list->version)){
82                 rp=prog_list;
83         } else if( (ri->prog<prog_list->program)
84                 ||((ri->prog==prog_list->program)&&(ri->vers<prog_list->version))){
85                 /* we should be first entry in list */
86                 rp=g_malloc(sizeof(rpc_program_t));
87                 rp->next=prog_list;
88                 rp->program=ri->prog;
89                 rp->version=ri->vers;
90                 rp->num=0;
91                 rp->min.secs=0;
92                 rp->min.nsecs=0;
93                 rp->max.secs=0;
94                 rp->max.nsecs=0;
95                 rp->tot.secs=0;
96                 rp->tot.nsecs=0;
97                 prog_list=rp;
98         } else {
99                 /* we go somewhere else in the list */
100                 for(rp=prog_list;rp;rp=rp->next){
101                         if((rp->next)
102                         && (rp->next->program==ri->prog)
103                         && (rp->next->version==ri->vers)){
104                                 rp=rp->next;
105                                 break;
106                         }
107                         if((!rp->next)
108                         || (rp->next->program>ri->prog)
109                         || (  (rp->next->program==ri->prog)
110                             &&(rp->next->version>ri->vers))){
111                                 rpc_program_t *trp;
112                                 trp=g_malloc(sizeof(rpc_program_t));
113                                 trp->next=rp->next;
114                                 trp->program=ri->prog;
115                                 trp->version=ri->vers;
116                                 trp->num=0;
117                                 trp->min.secs=0;
118                                 trp->min.nsecs=0;
119                                 trp->max.secs=0;
120                                 trp->max.nsecs=0;
121                                 trp->tot.secs=0;
122                                 trp->tot.nsecs=0;
123                                 rp->next=trp;
124                                 rp=trp;
125                                 break;
126                         }
127                 }
128         }
129
130         
131         /* we are only interested in reply packets */
132         if(ri->request){
133                 return 0;
134         }
135
136         /* calculate time delta between request and reply */
137         delta.secs=pinfo->fd->abs_secs-ri->req_time.secs;
138         delta.nsecs=pinfo->fd->abs_usecs*1000-ri->req_time.nsecs;
139         if(delta.nsecs<0){
140                 delta.nsecs+=1000000000;
141                 delta.secs--;
142         }
143
144         if((rp->max.secs==0)
145         && (rp->max.nsecs==0) ){
146                 rp->max.secs=delta.secs;
147                 rp->max.nsecs=delta.nsecs;
148         }
149
150         if((rp->min.secs==0)
151         && (rp->min.nsecs==0) ){
152                 rp->min.secs=delta.secs;
153                 rp->min.nsecs=delta.nsecs;
154         }
155
156         if( (delta.secs<rp->min.secs)
157         ||( (delta.secs==rp->min.secs)
158           &&(delta.nsecs<rp->min.nsecs) ) ){
159                 rp->min.secs=delta.secs;
160                 rp->min.nsecs=delta.nsecs;
161         }
162
163         if( (delta.secs>rp->max.secs)
164         ||( (delta.secs==rp->max.secs)
165           &&(delta.nsecs>rp->max.nsecs) ) ){
166                 rp->max.secs=delta.secs;
167                 rp->max.nsecs=delta.nsecs;
168         }
169         
170         rp->tot.secs += delta.secs;
171         rp->tot.nsecs += delta.nsecs;
172         if(rp->tot.nsecs>1000000000){
173                 rp->tot.nsecs-=1000000000;
174                 rp->tot.secs++;
175         }
176         rp->num++;
177
178         return 1;
179 }
180
181
182 static void
183 rpcprogs_draw(void *dummy _U_)
184 {
185 #ifdef G_HAVE_UINT64
186         guint64 td;
187 #else
188         guint32 td;
189 #endif
190         rpc_program_t *rp;
191         char str[64];
192
193         printf("\n");
194         printf("===================================================================\n");
195         printf("ONC-RPC Program Statistics:\n");
196         printf("Program    Version  Calls   Min SRT   Max SRT   Avg SRT\n");
197         for(rp=prog_list;rp;rp=rp->next){
198                 /* scale it to units of 10us.*/
199                 /* for long captures with a large tot time, this can overflow on 32bit */
200                 td=(int)rp->tot.secs;
201                 td=td*100000+(int)rp->tot.nsecs/10000;
202                 if(rp->num){
203                         td/=rp->num;
204                 } else {
205                         td=0;
206                 }
207
208                 sprintf(str,"%s(%d)",rpc_prog_name(rp->program),rp->program);
209                 printf("%-15s %2d %6d %3d.%05d %3d.%05d %3d.%05d\n",
210                         str,
211                         rp->version,
212                         rp->num,
213                         (int)rp->min.secs,rp->min.nsecs/10000,
214                         (int)rp->max.secs,rp->max.nsecs/10000,
215                         td/100000, td%100000
216                 );
217         }
218         printf("===================================================================\n");
219 }
220
221
222 static void
223 rpcprogs_init(char *optarg _U_)
224 {
225         GString *error_string;
226
227         if(already_enabled){
228                 return;
229         }
230         already_enabled=1;
231
232         error_string=register_tap_listener("rpc", NULL, NULL, NULL, rpcprogs_packet, rpcprogs_draw);
233         if(error_string){
234                 fprintf(stderr,"tethereal: Couldn't register rpc,programs tap: %s\n",
235                     error_string->str);
236                 g_string_free(error_string, TRUE);
237                 exit(1);
238         }
239 }
240
241
242 void
243 register_tap_listener_rpcprogs(void)
244 {
245         register_tap_listener_cmd_arg("rpc,programs", rpcprogs_init);
246 }
247
248