Get rid of -Wshadow warning - I guess we're including something that
[metze/wireshark/wip.git] / ui / cli / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28
29 #include <string.h>
30 #include "epan/packet_info.h"
31 #include <epan/tap.h>
32 #include <epan/stat_cmd_args.h>
33 #include <epan/dissectors/packet-dcerpc.h>
34
35 #define MICROSECS_PER_SEC   1000000
36 #define NANOSECS_PER_SEC    1000000000
37
38 /* used to keep track of statistics for a specific procedure */
39 typedef struct _rpc_procedure_t {
40         const char *proc;
41         int num;
42         nstime_t min;
43         nstime_t max;
44         nstime_t tot;
45 } rpc_procedure_t;
46
47 /* used to keep track of the statistics for an entire program interface */
48 typedef struct _rpcstat_t {
49         const char *prog;
50         char *filter;
51         e_uuid_t uuid;
52         guint16 ver;
53         guint32 num_procedures;
54         rpc_procedure_t *procedures;
55 } rpcstat_t;
56
57
58
59 static int
60 dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
61 {
62         const dcerpc_info *ri=(const dcerpc_info *)pri;
63         rpcstat_t *rs=(rpcstat_t *)prs;
64         nstime_t delta;
65         rpc_procedure_t *rp;
66
67         if(!ri->call_data){
68                 return 0;
69         }
70         if(!ri->call_data->req_frame){
71                 /* we have not seen the request so we dont know the delta*/
72                 return 0;
73         }
74         if(ri->call_data->opnum>=rs->num_procedures){
75                 /* dont handle this since its outside of known table */
76                 return 0;
77         }
78
79         /* we are only interested in reply packets */
80         if(ri->ptype != PDU_RESP){
81                 return 0;
82         }
83
84         /* we are only interested in certain program/versions */
85         if( (ri->call_data->uuid.Data1!=rs->uuid.Data1)
86           ||(ri->call_data->uuid.Data2!=rs->uuid.Data2)
87           ||(ri->call_data->uuid.Data3!=rs->uuid.Data3)
88           ||(ri->call_data->uuid.Data4[0]!=rs->uuid.Data4[0])
89           ||(ri->call_data->uuid.Data4[1]!=rs->uuid.Data4[1])
90           ||(ri->call_data->uuid.Data4[2]!=rs->uuid.Data4[2])
91           ||(ri->call_data->uuid.Data4[3]!=rs->uuid.Data4[3])
92           ||(ri->call_data->uuid.Data4[4]!=rs->uuid.Data4[4])
93           ||(ri->call_data->uuid.Data4[5]!=rs->uuid.Data4[5])
94           ||(ri->call_data->uuid.Data4[6]!=rs->uuid.Data4[6])
95           ||(ri->call_data->uuid.Data4[7]!=rs->uuid.Data4[7])
96           ||(ri->call_data->ver!=rs->ver)){
97                 return 0;
98         }
99
100         rp=&(rs->procedures[ri->call_data->opnum]);
101
102         /* calculate time delta between request and reply */
103         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->call_data->req_time);
104
105         if(rp->num==0){
106                 rp->max.secs=delta.secs;
107                 rp->max.nsecs=delta.nsecs;
108         }
109
110         if(rp->num==0){
111                 rp->min.secs=delta.secs;
112                 rp->min.nsecs=delta.nsecs;
113         }
114
115         if( (delta.secs<rp->min.secs)
116         ||( (delta.secs==rp->min.secs)
117           &&(delta.nsecs<rp->min.nsecs) ) ){
118                 rp->min.secs=delta.secs;
119                 rp->min.nsecs=delta.nsecs;
120         }
121
122         if( (delta.secs>rp->max.secs)
123         ||( (delta.secs==rp->max.secs)
124           &&(delta.nsecs>rp->max.nsecs) ) ){
125                 rp->max.secs=delta.secs;
126                 rp->max.nsecs=delta.nsecs;
127         }
128
129         rp->tot.secs += delta.secs;
130         rp->tot.nsecs += delta.nsecs;
131         if(rp->tot.nsecs > NANOSECS_PER_SEC){
132                 rp->tot.nsecs -= NANOSECS_PER_SEC;
133                 rp->tot.secs++;
134         }
135
136         rp->num++;
137
138         return 1;
139 }
140
141 static void
142 dcerpcstat_draw(void *prs)
143 {
144         rpcstat_t *rs=(rpcstat_t *)prs;
145         guint32 i;
146         guint64 td;
147         printf("\n");
148         printf("=======================================================================\n");
149         printf("%s Major Version %u SRT Statistics:\n", rs->prog, rs->ver);
150         printf("Filter: %s\n",rs->filter?rs->filter:"");
151         printf("Procedure                        Calls    Min SRT    Max SRT    Avg SRT\n");
152         
153         for(i=0;i<rs->num_procedures;i++){
154                 /* Only display procs with non-zero calls */
155                 if(rs->procedures[i].num==0){
156                         continue;
157                 }
158                 /* Scale the average SRT in units of 1us and round to the nearest us. */
159                 td = ((guint64)(rs->procedures[i].tot.secs)) * NANOSECS_PER_SEC + rs->procedures[i].tot.nsecs;
160                 td = ((td / rs->procedures[i].num) + 500) / 1000;
161
162                 printf("%-31s %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
163                         rs->procedures[i].proc,
164                         rs->procedures[i].num,
165                         (int)(rs->procedures[i].min.secs),(rs->procedures[i].min.nsecs+500)/1000,
166                         (int)(rs->procedures[i].max.secs),(rs->procedures[i].max.nsecs+500)/1000,
167                         td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
168                 );
169         }
170         printf("=======================================================================\n");
171 }
172
173
174
175 static void
176 dcerpcstat_init(const char *opt_arg, void* userdata _U_)
177 {
178         rpcstat_t *rs;
179         guint32 i, max_procs;
180         dcerpc_sub_dissector *procs;
181         e_uuid_t uuid;
182         guint d1,d2,d3,d40,d41,d42,d43,d44,d45,d46,d47;
183         int major, minor;
184         guint16 ver;
185         int pos=0;
186         const char *filter=NULL;
187         GString *error_string;
188
189         /*
190          * XXX - DCE RPC statistics are maintained only by major version,
191          * not by major and minor version, so the minor version number is
192          * ignored.
193          *
194          * Should we just stop supporting minor version numbers here?
195          * Or should we allow it to be omitted?  Or should we keep
196          * separate statistics for different minor version numbers,
197          * and allow the minor version number to be omitted, and
198          * report aggregate statistics for all minor version numbers
199          * if it's omitted?
200          */
201         if(sscanf(opt_arg,
202                 "dcerpc,srt,%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x,%d.%d%n",
203                 &d1,&d2,&d3,&d40,&d41,&d42,&d43,&d44,&d45,&d46,&d47,
204                 &major,&minor,&pos)==13){
205                 uuid.Data1=d1;
206                 uuid.Data2=d2;
207                 uuid.Data3=d3;
208                 uuid.Data4[0]=d40;
209                 uuid.Data4[1]=d41;
210                 uuid.Data4[2]=d42;
211                 uuid.Data4[3]=d43;
212                 uuid.Data4[4]=d44;
213                 uuid.Data4[5]=d45;
214                 uuid.Data4[6]=d46;
215                 uuid.Data4[7]=d47;
216                 if(pos){
217                         filter=opt_arg+pos;
218                 } else {
219                         filter=NULL;
220                 }
221         } else {
222                 fprintf(stderr, "tshark: invalid \"-z dcerpc,srt,<uuid>,<major version>.<minor version>[,<filter>]\" argument\n");
223                 exit(1);
224         }
225         if (major < 0 || major > 65535) {
226                 fprintf(stderr,"tshark: dcerpcstat_init() Major version number %d is invalid - must be positive and <= 65535\n", major);
227                 exit(1);
228         }
229         if (minor < 0 || minor > 65535) {
230                 fprintf(stderr,"tshark: dcerpcstat_init() Minor version number %d is invalid - must be positive and <= 65535\n", minor);
231                 exit(1);
232         }
233         ver = major;
234
235         rs=g_new(rpcstat_t,1);
236         rs->prog=dcerpc_get_proto_name(&uuid, ver);
237         if(!rs->prog){
238                 g_free(rs);
239                 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);
240                 exit(1);
241         }
242         procs=dcerpc_get_proto_sub_dissector(&uuid, ver);
243         rs->uuid=uuid;
244         rs->ver=ver;
245
246         if(filter){
247                 rs->filter=g_strdup(filter);
248         } else {
249                 rs->filter=NULL;
250         }
251
252         for(i=0,max_procs=0;procs[i].name;i++){
253                 if(procs[i].num>max_procs){
254                         max_procs=procs[i].num;
255                 }
256         }
257         rs->num_procedures=max_procs+1;
258         rs->procedures=(rpc_procedure_t *)g_malloc(sizeof(rpc_procedure_t)*(rs->num_procedures+1));
259         for(i=0;i<rs->num_procedures;i++){
260                 int j;
261                 rs->procedures[i].proc="unknown";
262                 for(j=0;procs[j].name;j++){
263                         if(procs[j].num==i){
264                                 rs->procedures[i].proc=procs[j].name;
265                         }
266                 }
267                 rs->procedures[i].num=0;
268                 rs->procedures[i].min.secs=0;
269                 rs->procedures[i].min.nsecs=0;
270                 rs->procedures[i].max.secs=0;
271                 rs->procedures[i].max.nsecs=0;
272                 rs->procedures[i].tot.secs=0;
273                 rs->procedures[i].tot.nsecs=0;
274         }
275
276         error_string=register_tap_listener("dcerpc", rs, filter, 0, NULL, dcerpcstat_packet, dcerpcstat_draw);
277         if(error_string){
278                 /* error, we failed to attach to the tap. clean up */
279                 g_free(rs->procedures);
280                 g_free(rs->filter);
281                 g_free(rs);
282
283                 fprintf(stderr, "tshark: Couldn't register dcerpc,srt tap: %s\n",
284                     error_string->str);
285                 g_string_free(error_string, TRUE);
286                 exit(1);
287         }
288 }
289
290 void
291 register_tap_listener_dcerpcstat(void)
292 {
293         register_stat_cmd_arg("dcerpc,srt,", dcerpcstat_init,NULL);
294 }