From Ronnie Sahlberg: have a registration interface for tap listeners,
[obnox/wireshark/wip.git] / tap-dcerpcstat.c
1 /* tap-dcerpcstat.c
2  * dcerpcstat   2002 Ronnie Sahlberg
3  *
4  * $Id: tap-dcerpcstat.c,v 1.3 2002/10/31 22:16:01 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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/packet_info.h"
37 #include "tap.h"
38 #include "packet-dcerpc.h"
39 #include "register.h"
40
41 /* used to keep track of statistics for a specific procedure */
42 typedef struct _rpc_procedure_t {
43         char *proc;
44         int num;
45         nstime_t min;
46         nstime_t max;
47         nstime_t tot;
48 } rpc_procedure_t;
49
50 /* used to keep track of the statistics for an entire program interface */
51 typedef struct _rpcstat_t {
52         char *prog;
53         char *filter;
54         e_uuid_t uuid;
55         guint16 ver;
56         guint32 num_procedures;
57         rpc_procedure_t *procedures;
58 } rpcstat_t;
59
60
61
62 static int
63 dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *pri)
64 {
65         dcerpc_info *ri=pri;
66         rpcstat_t *rs=prs;
67         nstime_t delta;
68         rpc_procedure_t *rp;
69
70         if(!ri->call_data){
71                 return 0;
72         }
73         if(!ri->call_data->req_frame){
74                 /* we have not seen the request so we dont know the delta*/
75                 return 0;
76         }
77         if(ri->call_data->opnum>=rs->num_procedures){
78                 /* dont handle this since its outside of known table */
79                 return 0;
80         }
81
82         /* we are only interested in reply packets */
83         if(ri->request){
84                 return 0;
85         }
86
87         /* we are only interested in certain program/versions */
88         if( (ri->call_data->uuid.Data1!=rs->uuid.Data1)
89           ||(ri->call_data->uuid.Data2!=rs->uuid.Data2)
90           ||(ri->call_data->uuid.Data3!=rs->uuid.Data3)
91           ||(ri->call_data->uuid.Data4[0]!=rs->uuid.Data4[0])
92           ||(ri->call_data->uuid.Data4[1]!=rs->uuid.Data4[1])
93           ||(ri->call_data->uuid.Data4[2]!=rs->uuid.Data4[2])
94           ||(ri->call_data->uuid.Data4[3]!=rs->uuid.Data4[3])
95           ||(ri->call_data->uuid.Data4[4]!=rs->uuid.Data4[4])
96           ||(ri->call_data->uuid.Data4[5]!=rs->uuid.Data4[5])
97           ||(ri->call_data->uuid.Data4[6]!=rs->uuid.Data4[6])
98           ||(ri->call_data->uuid.Data4[7]!=rs->uuid.Data4[7])
99           ||(ri->call_data->ver!=rs->ver)){
100                 return 0;
101         }
102
103         rp=&(rs->procedures[ri->call_data->opnum]);
104
105         /* calculate time delta between request and reply */
106         delta.secs=pinfo->fd->abs_secs-ri->call_data->req_time.secs;
107         delta.nsecs=pinfo->fd->abs_usecs*1000-ri->call_data->req_time.nsecs;
108         if(delta.nsecs<0){
109                 delta.nsecs+=1000000000;
110                 delta.secs--;
111         }
112
113         if((rp->max.secs==0)
114         && (rp->max.nsecs==0) ){
115                 rp->max.secs=delta.secs;
116                 rp->max.nsecs=delta.nsecs;
117         }
118
119         if((rp->min.secs==0)
120         && (rp->min.nsecs==0) ){
121                 rp->min.secs=delta.secs;
122                 rp->min.nsecs=delta.nsecs;
123         }
124
125         if( (delta.secs<rp->min.secs)
126         ||( (delta.secs==rp->min.secs)
127           &&(delta.nsecs<rp->min.nsecs) ) ){
128                 rp->min.secs=delta.secs;
129                 rp->min.nsecs=delta.nsecs;
130         }
131
132         if( (delta.secs>rp->max.secs)
133         ||( (delta.secs==rp->max.secs)
134           &&(delta.nsecs>rp->max.nsecs) ) ){
135                 rp->max.secs=delta.secs;
136                 rp->max.nsecs=delta.nsecs;
137         }
138         
139         rp->tot.secs += delta.secs;
140         rp->tot.nsecs += delta.nsecs;
141         if(rp->tot.nsecs>1000000000){
142                 rp->tot.nsecs-=1000000000;
143                 rp->tot.secs++;
144         }
145         rp->num++;
146
147         return 1;
148 }
149
150 static void
151 dcerpcstat_draw(void *prs)
152 {
153         rpcstat_t *rs=prs;
154         guint32 i;
155 #ifdef G_HAVE_UINT64
156         guint64 td;
157 #else
158         guint32 td;
159 #endif
160         printf("\n");
161         printf("===================================================================\n");
162         printf("%s Version %d.%d RTT Statistics:\n", rs->prog, rs->ver&0xff,rs->ver>>8);
163         printf("Filter: %s\n",rs->filter?rs->filter:"");
164         printf("Procedure                  Calls   Min RTT   Max RTT   Avg RTT\n");
165         for(i=0;i<rs->num_procedures;i++){
166                 /* scale it to units of 10us.*/
167                 /* for long captures with a large tot time, this can overflow on 32bit */
168                 td=(int)rs->procedures[i].tot.secs;
169                 td=td*100000+(int)rs->procedures[i].tot.nsecs/10000;
170                 if(rs->procedures[i].num){
171                         td/=rs->procedures[i].num;
172                 } else {
173                         td=0;
174                 }
175
176                 printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
177                         rs->procedures[i].proc,
178                         rs->procedures[i].num,
179                         (int)rs->procedures[i].min.secs,rs->procedures[i].min.nsecs/10000,
180                         (int)rs->procedures[i].max.secs,rs->procedures[i].max.nsecs/10000,
181                         td/100000, td%100000
182                 );
183         }
184         printf("===================================================================\n");
185 }
186
187
188
189 static void
190 dcerpcstat_init(char *optarg)
191 {
192         rpcstat_t *rs;
193         guint32 i, max_procs;
194         dcerpc_sub_dissector *procs;
195         e_uuid_t uuid;
196         int d1,d2,d3,d40,d41,d42,d43,d44,d45,d46,d47;
197         int major, minor;
198         int pos=0;
199         char *filter=NULL;
200     
201         if(sscanf(optarg,"dcerpc,rtt,%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x,%d.%d%n", &d1,&d2,&d3,&d40,&d41,&d42,&d43,&d44,&d45,&d46,&d47,&major,&minor,&pos)==13){
202                 uuid.Data1=d1;
203                 uuid.Data2=d2;
204                 uuid.Data3=d3;
205                 uuid.Data4[0]=d40;
206                 uuid.Data4[1]=d41;
207                 uuid.Data4[2]=d42;
208                 uuid.Data4[3]=d43;
209                 uuid.Data4[4]=d44;
210                 uuid.Data4[5]=d45;
211                 uuid.Data4[6]=d46;
212                 uuid.Data4[7]=d47;
213                 if(pos){
214                         filter=optarg+pos;
215                 } else {
216                         filter=NULL;
217                 }
218         } else {
219                 fprintf(stderr, "tethereal: invalid \"-z dcerpc,rtt,<uuid>,<major version>.<minor version>[,<filter>]\" argument\n");
220                 exit(1);
221         }
222
223
224         rs=g_malloc(sizeof(rpcstat_t));
225         rs->prog=dcerpc_get_proto_name(&uuid, (minor<<8)|(major&0xff) );
226         if(!rs->prog){
227                 g_free(rs);
228                 fprintf(stderr,"tethereal: dcerpcstat_init() Protocol with uuid:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x v%d.%d not supported\n",uuid.Data1,uuid.Data2,uuid.Data3,uuid.Data4[0],uuid.Data4[1],uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],uuid.Data4[5],uuid.Data4[6],uuid.Data4[7],major,minor);
229                 exit(1);
230         }
231         procs=dcerpc_get_proto_sub_dissector(&uuid, (minor<<8)|(major&0xff) );
232         rs->uuid=uuid;
233         rs->ver=(minor<<8)|(major&0xff);
234
235         if(filter){
236                 rs->filter=g_malloc(strlen(filter)+1);
237                 strcpy(rs->filter, filter);
238         } else {
239                 rs->filter=NULL;
240         }
241
242         for(i=0,max_procs=0;procs[i].name;i++){
243                 if(procs[i].num>max_procs){
244                         max_procs=procs[i].num;
245                 }
246         }
247         rs->num_procedures=max_procs+1;
248         rs->procedures=g_malloc(sizeof(rpc_procedure_t)*(rs->num_procedures+1));
249         for(i=0;i<rs->num_procedures;i++){
250                 int j;
251                 rs->procedures[i].proc="unknown";
252                 for(j=0;procs[j].name;j++){
253                         if(procs[j].num==i){
254                                 rs->procedures[i].proc=procs[j].name;
255                         }
256                 }
257                 rs->procedures[i].num=0;        
258                 rs->procedures[i].min.secs=0;
259                 rs->procedures[i].min.nsecs=0;
260                 rs->procedures[i].max.secs=0;
261                 rs->procedures[i].max.nsecs=0;
262                 rs->procedures[i].tot.secs=0;
263                 rs->procedures[i].tot.nsecs=0;
264         }
265
266         if(register_tap_listener("dcerpc", rs, filter, NULL, dcerpcstat_packet, dcerpcstat_draw)){
267                 /* error, we failed to attach to the tap. clean up */
268                 g_free(rs->procedures);
269                 g_free(rs->filter);
270                 g_free(rs);
271
272                 fprintf(stderr,"tethereal: dcerpcstat_init() failed to attach to tap.\n");
273                 exit(1);
274         }
275 }
276
277 void
278 register_tap_listener_dcerpcstat(void)
279 {
280         register_ethereal_tap("dcerpc,rtt,", dcerpcstat_init, NULL, NULL);
281 }