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