Add Windows version info resource.
[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 "register.h"
43 #include "timestats.h"
44
45 /* used to keep track of the statistics for an entire program interface */
46 typedef struct _afpstat_t {
47         char *filter;
48         timestat_t proc[256];
49 } afpstat_t;
50
51 static int
52 afpstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *prv)
53 {
54         afpstat_t *ss=(afpstat_t *)pss;
55         const afp_request_val *request_val=prv;
56         nstime_t t, deltat;
57         timestat_t *sp=NULL;
58
59         /* if we havnt seen the request, just ignore it */
60         if(!request_val){
61                 return 0;
62         }
63
64         sp=&(ss->proc[request_val->command]);
65
66         /* calculate time delta between request and reply */
67         t=pinfo->fd->abs_ts;
68         nstime_delta(&deltat, &t, &request_val->req_time);
69
70         if(sp){
71                 time_stat_update(sp,&deltat, pinfo);
72         }
73
74         return 1;
75 }
76
77 static void
78 afpstat_draw(void *pss)
79 {
80         afpstat_t *ss=(afpstat_t *)pss;
81         guint32 i;
82 #ifdef G_HAVE_UINT64
83         guint64 td;
84 #else
85         guint32 td;
86 #endif
87         printf("\n");
88         printf("===================================================================\n");
89         printf("AFP RTT Statistics:\n");
90         printf("Filter: %s\n",ss->filter?ss->filter:"");
91         printf("Commands                   Calls   Min RTT   Max RTT   Avg RTT\n");
92         for(i=0;i<256;i++){
93                 /* nothing seen, nothing to do */
94                 if(ss->proc[i].num==0){
95                         continue;
96                 }
97
98                 /* scale it to units of 10us.*/
99                 /* for long captures with a large tot time, this can overflow on 32bit */
100                 td=(int)ss->proc[i].tot.secs;
101                 td=td*100000+(int)ss->proc[i].tot.nsecs/10000;
102                 if(ss->proc[i].num){
103                         td/=ss->proc[i].num;
104                 } else {
105                         td=0;
106                 }
107
108                 printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
109                         val_to_str(i, CommandCode_vals, "Unknown (%u)"),
110                         ss->proc[i].num,
111                         (int)ss->proc[i].min.secs,ss->proc[i].min.nsecs/10000,
112                         (int)ss->proc[i].max.secs,ss->proc[i].max.nsecs/10000,
113                         td/100000, td%100000
114                 );
115         }
116         printf("===================================================================\n");
117 }
118
119
120 static void
121 afpstat_init(const char *optarg, void* userdata _U_)
122 {
123         afpstat_t *ss;
124         guint32 i;
125         const char *filter=NULL;
126         GString *error_string;
127
128         if(!strncmp(optarg,"afp,rtt,",8)){
129                 filter=optarg+8;
130         } else {
131                 filter=NULL;
132         }
133
134         ss=g_malloc(sizeof(afpstat_t));
135         if(filter){
136                 ss->filter=g_malloc(strlen(filter)+1);
137                 strcpy(ss->filter, filter);
138         } else {
139                 ss->filter=NULL;
140         }
141
142         for(i=0;i<256;i++){
143                 ss->proc[i].num=0;
144                 ss->proc[i].min_num=0;
145                 ss->proc[i].max_num=0;
146                 ss->proc[i].min.secs=0;
147                 ss->proc[i].min.nsecs=0;
148                 ss->proc[i].max.secs=0;
149                 ss->proc[i].max.nsecs=0;
150                 ss->proc[i].tot.secs=0;
151                 ss->proc[i].tot.nsecs=0;
152         }
153
154         error_string=register_tap_listener("afp", ss, filter, NULL, afpstat_packet, afpstat_draw);
155         if(error_string){
156                 /* error, we failed to attach to the tap. clean up */
157                 g_free(ss->filter);
158                 g_free(ss);
159
160                 fprintf(stderr, "tshark: Couldn't register afp,rtt tap: %s\n",
161                     error_string->str);
162                 g_string_free(error_string, TRUE);
163                 exit(1);
164         }
165 }
166
167 void
168 register_tap_listener_afpstat(void)
169 {
170         register_stat_cmd_arg("afp,rtt", afpstat_init,NULL);
171 }