Get rid of -Wshadow warning - I guess we're including something that
[metze/wireshark/wip.git] / ui / cli / tap-afpstat.c
1 /* tap-afpstat.c
2  * Based on
3  * smbstat   2003 Ronnie Sahlberg
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29
30 #include <string.h>
31 #include <epan/packet_info.h>
32 #include <epan/tap.h>
33 #include <epan/stat_cmd_args.h>
34 #include <epan/value_string.h>
35 #include <epan/dissectors/packet-afp.h>
36 #include "epan/timestats.h"
37
38 /* used to keep track of the statistics for an entire program interface */
39 typedef struct _afpstat_t {
40         char *filter;
41         timestat_t proc[256];
42 } afpstat_t;
43
44 static int
45 afpstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *prv)
46 {
47         afpstat_t *ss=(afpstat_t *)pss;
48         const afp_request_val *request_val=(const afp_request_val *)prv;
49         nstime_t t, deltat;
50         timestat_t *sp=NULL;
51
52         /* if we havnt seen the request, just ignore it */
53         if(!request_val){
54                 return 0;
55         }
56
57         sp=&(ss->proc[request_val->command]);
58
59         /* calculate time delta between request and reply */
60         t=pinfo->fd->abs_ts;
61         nstime_delta(&deltat, &t, &request_val->req_time);
62
63         if(sp){
64                 time_stat_update(sp,&deltat, pinfo);
65         }
66
67         return 1;
68 }
69
70 static void
71 afpstat_draw(void *pss)
72 {
73         afpstat_t *ss=(afpstat_t *)pss;
74         guint32 i;
75         guint64 td;
76         printf("\n");
77         printf("===================================================================\n");
78         printf("AFP SRT Statistics:\n");
79         printf("Filter: %s\n",ss->filter?ss->filter:"");
80         printf("Commands                   Calls   Min SRT   Max SRT   Avg SRT\n");
81         for(i=0;i<256;i++){
82                 /* nothing seen, nothing to do */
83                 if(ss->proc[i].num==0){
84                         continue;
85                 }
86
87                 /* scale it to units of 10us.*/
88                 td=ss->proc[i].tot.secs;
89                 td=td*100000+(int)ss->proc[i].tot.nsecs/10000;
90                 if(ss->proc[i].num){
91                         td/=ss->proc[i].num;
92                 } else {
93                         td=0;
94                 }
95
96                 printf("%-25s %6d %3d.%05d %3d.%05d %3" G_GINT64_MODIFIER "u.%05" G_GINT64_MODIFIER "u\n",
97                         val_to_str_ext(i, &CommandCode_vals_ext, "Unknown (%u)"),
98                         ss->proc[i].num,
99                         (int)ss->proc[i].min.secs,ss->proc[i].min.nsecs/10000,
100                         (int)ss->proc[i].max.secs,ss->proc[i].max.nsecs/10000,
101                         td/100000, td%100000
102                 );
103         }
104         printf("===================================================================\n");
105 }
106
107
108 static void
109 afpstat_init(const char *opt_arg, void* userdata _U_)
110 {
111         afpstat_t *ss;
112         guint32 i;
113         const char *filter=NULL;
114         GString *error_string;
115
116         if(!strncmp(opt_arg,"afp,srt,",8)){
117                 filter=opt_arg+8;
118         } else {
119                 filter=NULL;
120         }
121
122         ss=g_new(afpstat_t,1);
123         if(filter){
124                 ss->filter=g_strdup(filter);
125         } else {
126                 ss->filter=NULL;
127         }
128
129         for(i=0;i<256;i++){
130                 ss->proc[i].num=0;
131                 ss->proc[i].min_num=0;
132                 ss->proc[i].max_num=0;
133                 ss->proc[i].min.secs=0;
134                 ss->proc[i].min.nsecs=0;
135                 ss->proc[i].max.secs=0;
136                 ss->proc[i].max.nsecs=0;
137                 ss->proc[i].tot.secs=0;
138                 ss->proc[i].tot.nsecs=0;
139         }
140
141         error_string=register_tap_listener("afp", ss, filter, 0, NULL, afpstat_packet, afpstat_draw);
142         if(error_string){
143                 /* error, we failed to attach to the tap. clean up */
144                 g_free(ss->filter);
145                 g_free(ss);
146
147                 fprintf(stderr, "tshark: Couldn't register afp,srt tap: %s\n",
148                     error_string->str);
149                 g_string_free(error_string, TRUE);
150                 exit(1);
151         }
152 }
153
154 void
155 register_tap_listener_afpstat(void)
156 {
157         register_stat_cmd_arg("afp,srt", afpstat_init,NULL);
158 }