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