Fix build by #if 0 out unused de_sgsap_tmsi() function.
[obnox/wireshark/wip.git] / tap-dcerpcstat.c
1 /* tap-dcerpcstat.c
2  * dcerpcstat   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 #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 <epan/tap.h>
38 #include <epan/stat_cmd_args.h>
39 #include <epan/dissectors/packet-dcerpc.h>
40
41 /* used to keep track of statistics for a specific procedure */
42 typedef struct _rpc_procedure_t {
43         const 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         const 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_, const void *pri)
64 {
65         const 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->ptype != PDU_RESP){
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         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->call_data->req_time);
107
108         if(rp->num==0){
109                 rp->max.secs=delta.secs;
110                 rp->max.nsecs=delta.nsecs;
111         }
112
113         if(rp->num==0){
114                 rp->min.secs=delta.secs;
115                 rp->min.nsecs=delta.nsecs;
116         }
117
118         if( (delta.secs<rp->min.secs)
119         ||( (delta.secs==rp->min.secs)
120           &&(delta.nsecs<rp->min.nsecs) ) ){
121                 rp->min.secs=delta.secs;
122                 rp->min.nsecs=delta.nsecs;
123         }
124
125         if( (delta.secs>rp->max.secs)
126         ||( (delta.secs==rp->max.secs)
127           &&(delta.nsecs>rp->max.nsecs) ) ){
128                 rp->max.secs=delta.secs;
129                 rp->max.nsecs=delta.nsecs;
130         }
131
132         rp->tot.secs += delta.secs;
133         rp->tot.nsecs += delta.nsecs;
134         if(rp->tot.nsecs>1000000000){
135                 rp->tot.nsecs-=1000000000;
136                 rp->tot.secs++;
137         }
138
139         rp->num++;
140
141         return 1;
142 }
143
144 static void
145 dcerpcstat_draw(void *prs)
146 {
147         rpcstat_t *rs=prs;
148         guint32 i;
149         guint64 td;
150         printf("\n");
151         printf("===================================================================\n");
152         printf("%s Major Version %u RTT Statistics:\n", rs->prog, rs->ver);
153         printf("Filter: %s\n",rs->filter?rs->filter:"");
154         printf("Procedure                  Calls   Min RTT   Max RTT   Avg RTT\n");
155         for(i=0;i<rs->num_procedures;i++){
156                 /* scale it to units of 10us.*/
157                 td=rs->procedures[i].tot.secs;
158                 td=td*100000+(int)rs->procedures[i].tot.nsecs/10000;
159                 if(rs->procedures[i].num){
160                         td/=rs->procedures[i].num;
161                 } else {
162                         td=0;
163                 }
164
165                 printf("%-25s %6d %3d.%05d %3d.%05d %3" G_GINT64_MODIFIER "u.%05" G_GINT64_MODIFIER "u\n",
166                         rs->procedures[i].proc,
167                         rs->procedures[i].num,
168                         (int)rs->procedures[i].min.secs,rs->procedures[i].min.nsecs/10000,
169                         (int)rs->procedures[i].max.secs,rs->procedures[i].max.nsecs/10000,
170                         td/100000, td%100000
171                 );
172         }
173         printf("===================================================================\n");
174 }
175
176
177
178 static void
179 dcerpcstat_init(const char *optarg, void* userdata _U_)
180 {
181         rpcstat_t *rs;
182         guint32 i, max_procs;
183         dcerpc_sub_dissector *procs;
184         e_uuid_t uuid;
185         guint d1,d2,d3,d40,d41,d42,d43,d44,d45,d46,d47;
186         int major, minor;
187         guint16 ver;
188         int pos=0;
189         const char *filter=NULL;
190         GString *error_string;
191
192         /*
193          * XXX - DCE RPC statistics are maintained only by major version,
194          * not by major and minor version, so the minor version number is
195          * ignored.
196          *
197          * Should we just stop supporting minor version numbers here?
198          * Or should we allow it to be omitted?  Or should we keep
199          * separate statistics for different minor version numbers,
200          * and allow the minor version number to be omitted, and
201          * report aggregate statistics for all minor version numbers
202          * if it's omitted?
203          *
204          * XXX - should this be called "srt" rather than "rtt"?  The
205          * equivalent tap for Wireshark calls it "srt", for "Service
206          * Response Time", rather than "rtt" for "Round-Trip Time".
207          */
208         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){
209                 uuid.Data1=d1;
210                 uuid.Data2=d2;
211                 uuid.Data3=d3;
212                 uuid.Data4[0]=d40;
213                 uuid.Data4[1]=d41;
214                 uuid.Data4[2]=d42;
215                 uuid.Data4[3]=d43;
216                 uuid.Data4[4]=d44;
217                 uuid.Data4[5]=d45;
218                 uuid.Data4[6]=d46;
219                 uuid.Data4[7]=d47;
220                 if(pos){
221                         filter=optarg+pos;
222                 } else {
223                         filter=NULL;
224                 }
225         } else {
226                 fprintf(stderr, "tshark: invalid \"-z dcerpc,rtt,<uuid>,<major version>.<minor version>[,<filter>]\" argument\n");
227                 exit(1);
228         }
229         if (major < 0 || major > 65535) {
230                 fprintf(stderr,"tshark: dcerpcstat_init() Major version number %d is invalid - must be positive and <= 65535\n", major);
231                 exit(1);
232         }
233         if (minor < 0 || minor > 65535) {
234                 fprintf(stderr,"tshark: dcerpcstat_init() Minor version number %d is invalid - must be positive and <= 65535\n", minor);
235                 exit(1);
236         }
237         ver = major;
238
239         rs=g_malloc(sizeof(rpcstat_t));
240         rs->prog=dcerpc_get_proto_name(&uuid, ver);
241         if(!rs->prog){
242                 g_free(rs);
243                 fprintf(stderr,"tshark: dcerpcstat_init() Protocol with uuid:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x v%u 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],ver);
244                 exit(1);
245         }
246         procs=dcerpc_get_proto_sub_dissector(&uuid, ver);
247         rs->uuid=uuid;
248         rs->ver=ver;
249
250         if(filter){
251                 rs->filter=g_strdup(filter);
252         } else {
253                 rs->filter=NULL;
254         }
255
256         for(i=0,max_procs=0;procs[i].name;i++){
257                 if(procs[i].num>max_procs){
258                         max_procs=procs[i].num;
259                 }
260         }
261         rs->num_procedures=max_procs+1;
262         rs->procedures=g_malloc(sizeof(rpc_procedure_t)*(rs->num_procedures+1));
263         for(i=0;i<rs->num_procedures;i++){
264                 int j;
265                 rs->procedures[i].proc="unknown";
266                 for(j=0;procs[j].name;j++){
267                         if(procs[j].num==i){
268                                 rs->procedures[i].proc=procs[j].name;
269                         }
270                 }
271                 rs->procedures[i].num=0;
272                 rs->procedures[i].min.secs=0;
273                 rs->procedures[i].min.nsecs=0;
274                 rs->procedures[i].max.secs=0;
275                 rs->procedures[i].max.nsecs=0;
276                 rs->procedures[i].tot.secs=0;
277                 rs->procedures[i].tot.nsecs=0;
278         }
279
280         error_string=register_tap_listener("dcerpc", rs, filter, 0, NULL, dcerpcstat_packet, dcerpcstat_draw);
281         if(error_string){
282                 /* error, we failed to attach to the tap. clean up */
283                 g_free(rs->procedures);
284                 g_free(rs->filter);
285                 g_free(rs);
286
287                 fprintf(stderr, "tshark: Couldn't register dcerpc,rtt tap: %s\n",
288                     error_string->str);
289                 g_string_free(error_string, TRUE);
290                 exit(1);
291         }
292 }
293
294 void
295 register_tap_listener_dcerpcstat(void)
296 {
297         register_stat_cmd_arg("dcerpc,rtt,", dcerpcstat_init,NULL);
298 }