As suggested by Anders: back out 37112.
[obnox/wireshark/wip.git] / 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37 #include <epan/packet_info.h>
38 #include <epan/tap.h>
39 #include <epan/stat_cmd_args.h>
40 #include <epan/value_string.h>
41 #include <epan/dissectors/packet-afp.h>
42 #include "timestats.h"
43
44 /* used to keep track of the statistics for an entire program interface */
45 typedef struct _afpstat_t {
46         char *filter;
47         timestat_t proc[256];
48 } afpstat_t;
49
50 static int
51 afpstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *prv)
52 {
53         afpstat_t *ss=(afpstat_t *)pss;
54         const afp_request_val *request_val=prv;
55         nstime_t t, deltat;
56         timestat_t *sp=NULL;
57
58         /* if we havnt seen the request, just ignore it */
59         if(!request_val){
60                 return 0;
61         }
62
63         sp=&(ss->proc[request_val->command]);
64
65         /* calculate time delta between request and reply */
66         t=pinfo->fd->abs_ts;
67         nstime_delta(&deltat, &t, &request_val->req_time);
68
69         if(sp){
70                 time_stat_update(sp,&deltat, pinfo);
71         }
72
73         return 1;
74 }
75
76 static void
77 afpstat_draw(void *pss)
78 {
79         afpstat_t *ss=(afpstat_t *)pss;
80         guint32 i;
81         guint64 td;
82         printf("\n");
83         printf("===================================================================\n");
84         printf("AFP SRT Statistics:\n");
85         printf("Filter: %s\n",ss->filter?ss->filter:"");
86         printf("Commands                   Calls   Min SRT   Max SRT   Avg SRT\n");
87         for(i=0;i<256;i++){
88                 /* nothing seen, nothing to do */
89                 if(ss->proc[i].num==0){
90                         continue;
91                 }
92
93                 /* scale it to units of 10us.*/
94                 td=ss->proc[i].tot.secs;
95                 td=td*100000+(int)ss->proc[i].tot.nsecs/10000;
96                 if(ss->proc[i].num){
97                         td/=ss->proc[i].num;
98                 } else {
99                         td=0;
100                 }
101
102                 printf("%-25s %6d %3d.%05d %3d.%05d %3" G_GINT64_MODIFIER "u.%05" G_GINT64_MODIFIER "u\n",
103                         val_to_str_ext(i, &CommandCode_vals_ext, "Unknown (%u)"),
104                         ss->proc[i].num,
105                         (int)ss->proc[i].min.secs,ss->proc[i].min.nsecs/10000,
106                         (int)ss->proc[i].max.secs,ss->proc[i].max.nsecs/10000,
107                         td/100000, td%100000
108                 );
109         }
110         printf("===================================================================\n");
111 }
112
113
114 static void
115 afpstat_init(const char *optarg, void* userdata _U_)
116 {
117         afpstat_t *ss;
118         guint32 i;
119         const char *filter=NULL;
120         GString *error_string;
121
122         if(!strncmp(optarg,"afp,srt,",8)){
123                 filter=optarg+8;
124         } else {
125                 filter=NULL;
126         }
127
128         ss=g_malloc(sizeof(afpstat_t));
129         if(filter){
130                 ss->filter=g_strdup(filter);
131         } else {
132                 ss->filter=NULL;
133         }
134
135         for(i=0;i<256;i++){
136                 ss->proc[i].num=0;
137                 ss->proc[i].min_num=0;
138                 ss->proc[i].max_num=0;
139                 ss->proc[i].min.secs=0;
140                 ss->proc[i].min.nsecs=0;
141                 ss->proc[i].max.secs=0;
142                 ss->proc[i].max.nsecs=0;
143                 ss->proc[i].tot.secs=0;
144                 ss->proc[i].tot.nsecs=0;
145         }
146
147         error_string=register_tap_listener("afp", ss, filter, 0, NULL, afpstat_packet, afpstat_draw);
148         if(error_string){
149                 /* error, we failed to attach to the tap. clean up */
150                 g_free(ss->filter);
151                 g_free(ss);
152
153                 fprintf(stderr, "tshark: Couldn't register afp,srt tap: %s\n",
154                     error_string->str);
155                 g_string_free(error_string, TRUE);
156                 exit(1);
157         }
158 }
159
160 void
161 register_tap_listener_afpstat(void)
162 {
163         register_stat_cmd_arg("afp,srt", afpstat_init,NULL);
164 }