change all file offsets from long to gint64 so we can - theoretically - handle files...
[obnox/wireshark/wip.git] / tap-smbstat.c
1 /* tap-smbstat.c
2  * smbstat   2003 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/emem.h>
39 #include <epan/stat_cmd_args.h>
40 #include "epan/value_string.h"
41 #include <epan/dissectors/packet-smb.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 _smbstat_t {
47         char *filter;
48         timestat_t proc[256];
49         timestat_t trans2[256];
50         timestat_t nt_trans[256];
51 } smbstat_t;
52
53
54
55 static int
56 smbstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
57 {
58         smbstat_t *ss=(smbstat_t *)pss;
59         const smb_info_t *si=psi;
60         nstime_t t, deltat;
61         timestat_t *sp=NULL;
62
63         /* we are only interested in reply packets */
64         if(si->request){
65                 return 0;
66         }
67         /* if we havnt seen the request, just ignore it */
68         if(!si->sip){
69                 return 0;
70         }
71
72         if(si->cmd==0xA0 && si->sip->extra_info_type == SMB_EI_NTI){
73                 smb_nt_transact_info_t *sti=(smb_nt_transact_info_t *)si->sip->extra_info;
74
75                 /*nt transaction*/
76                 if(sti){
77                         sp=&(ss->nt_trans[sti->subcmd]);
78                 }
79         } else if(si->cmd==0x32 && si->sip->extra_info_type == SMB_EI_T2I){
80                 smb_transact2_info_t *st2i=(smb_transact2_info_t *)si->sip->extra_info;
81
82                 /*transaction2*/
83                 if(st2i){
84                         sp=&(ss->trans2[st2i->subcmd]);
85                 }
86         } else {
87                 sp=&(ss->proc[si->cmd]);
88         }
89
90         /* calculate time delta between request and reply */
91         t=pinfo->fd->abs_ts;
92         nstime_delta(&deltat, &t, &si->sip->req_time);
93
94         if(sp){
95                 time_stat_update(sp,&deltat, pinfo);
96         }
97
98         return 1;
99 }
100
101 static void
102 smbstat_draw(void *pss)
103 {
104         smbstat_t *ss=(smbstat_t *)pss;
105         guint32 i;
106 #ifdef G_HAVE_UINT64
107         guint64 td;
108 #else
109         guint32 td;
110 #endif
111         printf("\n");
112         printf("===================================================================\n");
113         printf("SMB RTT Statistics:\n");
114         printf("Filter: %s\n",ss->filter?ss->filter:"");
115         printf("Commands                   Calls   Min RTT   Max RTT   Avg RTT\n");
116         for(i=0;i<256;i++){
117                 /* nothing seen, nothing to do */
118                 if(ss->proc[i].num==0){
119                         continue;
120                 }
121
122                 /* we deal with transaction2 later */
123                 if(i==0x32){
124                         continue;
125                 }
126
127                 /* we deal with nt transaction later */
128                 if(i==0xA0){
129                         continue;
130                 }
131
132                 /* scale it to units of 10us.*/
133                 /* for long captures with a large tot time, this can overflow on 32bit */
134                 td=(int)ss->proc[i].tot.secs;
135                 td=td*100000+(int)ss->proc[i].tot.nsecs/10000;
136                 if(ss->proc[i].num){
137                         td/=ss->proc[i].num;
138                 } else {
139                         td=0;
140                 }
141
142                 printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
143                         val_to_str(i, smb_cmd_vals, "Unknown (0x%02x)"),
144                         ss->proc[i].num,
145                         (int)ss->proc[i].min.secs,ss->proc[i].min.nsecs/10000,
146                         (int)ss->proc[i].max.secs,ss->proc[i].max.nsecs/10000,
147                         td/100000, td%100000
148                 );
149         }
150
151         printf("\n");
152         printf("Transaction2 Commands      Calls   Min RTT   Max RTT   Avg RTT\n");
153         for(i=0;i<256;i++){
154                 /* nothing seen, nothing to do */
155                 if(ss->trans2[i].num==0){
156                         continue;
157                 }
158
159                 /* scale it to units of 10us.*/
160                 /* for long captures with a large tot time, this can overflow on 32bit */
161                 td=(int)ss->trans2[i].tot.secs;
162                 td=td*100000+(int)ss->trans2[i].tot.nsecs/10000;
163                 if(ss->trans2[i].num){
164                         td/=ss->trans2[i].num;
165                 } else {
166                         td=0;
167                 }
168
169                 printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
170                         val_to_str(i, trans2_cmd_vals, "Unknown (0x%02x)"),
171                         ss->trans2[i].num,
172                         (int)ss->trans2[i].min.secs,ss->trans2[i].min.nsecs/10000,
173                         (int)ss->trans2[i].max.secs,ss->trans2[i].max.nsecs/10000,
174                         td/100000, td%100000
175                 );
176         }
177
178         printf("\n");
179         printf("NT Transaction Commands    Calls   Min RTT   Max RTT   Avg RTT\n");
180         for(i=0;i<256;i++){
181                 /* nothing seen, nothing to do */
182                 if(ss->nt_trans[i].num==0){
183                         continue;
184                 }
185
186                 /* scale it to units of 10us.*/
187                 /* for long captures with a large tot time, this can overflow on 32bit */
188                 td=(int)ss->nt_trans[i].tot.secs;
189                 td=td*100000+(int)ss->nt_trans[i].tot.nsecs/10000;
190                 if(ss->nt_trans[i].num){
191                         td/=ss->nt_trans[i].num;
192                 } else {
193                         td=0;
194                 }
195
196                 printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
197                         val_to_str(i, nt_cmd_vals, "Unknown (0x%02x)"),
198                         ss->nt_trans[i].num,
199                         (int)ss->nt_trans[i].min.secs,ss->nt_trans[i].min.nsecs/10000,
200                         (int)ss->nt_trans[i].max.secs,ss->nt_trans[i].max.nsecs/10000,
201                         td/100000, td%100000
202                 );
203         }
204
205         printf("===================================================================\n");
206 }
207
208
209 static void
210 smbstat_init(const char *optarg,void* userdata _U_)
211 {
212         smbstat_t *ss;
213         guint32 i;
214         const char *filter=NULL;
215         GString *error_string;
216
217         if(!strncmp(optarg,"smb,rtt,",8)){
218                 filter=optarg+8;
219         } else {
220                 filter=NULL;
221         }
222
223         ss=g_malloc(sizeof(smbstat_t));
224         if(filter){
225                 ss->filter=g_malloc(strlen(filter)+1);
226                 strcpy(ss->filter, filter);
227         } else {
228                 ss->filter=NULL;
229         }
230
231         for(i=0;i<256;i++){
232                 ss->proc[i].num=0;
233                 ss->proc[i].min_num=0;
234                 ss->proc[i].max_num=0;
235                 ss->proc[i].min.secs=0;
236                 ss->proc[i].min.nsecs=0;
237                 ss->proc[i].max.secs=0;
238                 ss->proc[i].max.nsecs=0;
239                 ss->proc[i].tot.secs=0;
240                 ss->proc[i].tot.nsecs=0;
241
242                 ss->trans2[i].num=0;
243                 ss->trans2[i].min_num=0;
244                 ss->trans2[i].max_num=0;
245                 ss->trans2[i].min.secs=0;
246                 ss->trans2[i].min.nsecs=0;
247                 ss->trans2[i].max.secs=0;
248                 ss->trans2[i].max.nsecs=0;
249                 ss->trans2[i].tot.secs=0;
250                 ss->trans2[i].tot.nsecs=0;
251
252                 ss->nt_trans[i].num=0;
253                 ss->nt_trans[i].min_num=0;
254                 ss->nt_trans[i].max_num=0;
255                 ss->nt_trans[i].min.secs=0;
256                 ss->nt_trans[i].min.nsecs=0;
257                 ss->nt_trans[i].max.secs=0;
258                 ss->nt_trans[i].max.nsecs=0;
259                 ss->nt_trans[i].tot.secs=0;
260                 ss->nt_trans[i].tot.nsecs=0;
261         }
262
263         error_string=register_tap_listener("smb", ss, filter, NULL, smbstat_packet, smbstat_draw);
264         if(error_string){
265                 /* error, we failed to attach to the tap. clean up */
266                 g_free(ss->filter);
267                 g_free(ss);
268
269                 fprintf(stderr, "tshark: Couldn't register smb,rtt tap: %s\n",
270                     error_string->str);
271                 g_string_free(error_string, TRUE);
272                 exit(1);
273         }
274 }
275
276
277 void
278 register_tap_listener_smbstat(void)
279 {
280         register_stat_cmd_arg("smb,rtt", smbstat_init,NULL);
281 }
282