From Sylvain Munaut:
[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/stat_cmd_args.h>
39 #include "epan/value_string.h"
40 #include <epan/dissectors/packet-smb.h>
41 #include "timestats.h"
42
43 #define MICROSECS_PER_SEC   1000000
44 #define NANOSECS_PER_SEC    1000000000
45
46 /* used to keep track of the statistics for an entire program interface */
47 typedef struct _smbstat_t {
48         char *filter;
49         timestat_t proc[256];
50         timestat_t trans2[256];
51         timestat_t nt_trans[256];
52 } smbstat_t;
53
54
55
56 static int
57 smbstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
58 {
59         smbstat_t *ss=(smbstat_t *)pss;
60         const smb_info_t *si=psi;
61         nstime_t t, deltat;
62         timestat_t *sp=NULL;
63
64         /* we are only interested in reply packets */
65         if(si->request){
66                 return 0;
67         }
68         /* if we havnt seen the request, just ignore it */
69         if(!si->sip){
70                 return 0;
71         }
72
73         if(si->cmd==0xA0 && si->sip->extra_info_type == SMB_EI_NTI){
74                 smb_nt_transact_info_t *sti=(smb_nt_transact_info_t *)si->sip->extra_info;
75
76                 /*nt transaction*/
77                 if(sti){
78                         sp=&(ss->nt_trans[sti->subcmd]);
79                 }
80         } else if(si->cmd==0x32 && si->sip->extra_info_type == SMB_EI_T2I){
81                 smb_transact2_info_t *st2i=(smb_transact2_info_t *)si->sip->extra_info;
82
83                 /*transaction2*/
84                 if(st2i){
85                         sp=&(ss->trans2[st2i->subcmd]);
86                 }
87         } else {
88                 sp=&(ss->proc[si->cmd]);
89         }
90
91         /* calculate time delta between request and reply */
92         t=pinfo->fd->abs_ts;
93         nstime_delta(&deltat, &t, &si->sip->req_time);
94
95         if(sp){
96                 time_stat_update(sp,&deltat, pinfo);
97         }
98
99         return 1;
100 }
101
102 static void
103 smbstat_draw(void *pss)
104 {
105         smbstat_t *ss=(smbstat_t *)pss;
106         guint32 i;
107         guint64 td;
108         printf("\n");
109         printf("=================================================================\n");
110         printf("SMB SRT Statistics:\n");
111         printf("Filter: %s\n",ss->filter?ss->filter:"");
112         printf("Commands                   Calls    Min SRT    Max SRT    Avg SRT\n");
113         for(i=0;i<256;i++){
114                 /* nothing seen, nothing to do */
115                 if(ss->proc[i].num==0){
116                         continue;
117                 }
118
119                 /* we deal with transaction2 later */
120                 if(i==0x32){
121                         continue;
122                 }
123
124                 /* we deal with nt transaction later */
125                 if(i==0xA0){
126                         continue;
127                 }
128
129                 /* Scale the average SRT in units of 1us and round to the nearest us. */
130                 td = ((guint64)(ss->proc[i].tot.secs)) * NANOSECS_PER_SEC + ss->proc[i].tot.nsecs;
131
132                 td = ((td / ss->proc[i].num) + 500) / 1000;
133
134                 printf("%-25s %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
135                         val_to_str_ext(i, &smb_cmd_vals_ext, "Unknown (0x%02x)"),
136                         ss->proc[i].num,
137                         (int)(ss->proc[i].min.secs),(ss->proc[i].min.nsecs+500)/1000,
138                         (int)(ss->proc[i].max.secs),(ss->proc[i].max.nsecs+500)/1000,
139                         td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
140                 );
141         }
142
143         printf("\n");
144         printf("Transaction2 Commands      Calls    Min SRT    Max SRT    Avg SRT\n");
145         for(i=0;i<256;i++){
146                 /* nothing seen, nothing to do */
147                 if(ss->trans2[i].num==0){
148                         continue;
149                 }
150
151                 /* Scale the average SRT in units of 1us and round to the nearest us. */
152                 td = ((guint64)(ss->trans2[i].tot.secs)) * NANOSECS_PER_SEC + ss->trans2[i].tot.nsecs;
153                 td = ((td / ss->trans2[i].num) + 500) / 1000;
154
155                 printf("%-25s %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
156                         val_to_str_ext(i, &trans2_cmd_vals_ext, "Unknown (0x%02x)"),
157                         ss->trans2[i].num,
158                         (int)(ss->trans2[i].min.secs),(ss->trans2[i].min.nsecs+500)/1000,
159                         (int)(ss->trans2[i].max.secs),(ss->trans2[i].max.nsecs+500)/1000,
160                         td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
161                 );
162         }
163
164         printf("\n");
165         printf("NT Transaction Commands    Calls    Min SRT    Max SRT    Avg SRT\n");
166         for(i=0;i<256;i++){
167                 /* nothing seen, nothing to do */
168                 if(ss->nt_trans[i].num==0){
169                         continue;
170                 }
171                 /* Scale the average SRT in units of 1us and round to the nearest us. */
172                 td = ((guint64)(ss->nt_trans[i].tot.secs)) * NANOSECS_PER_SEC + ss->nt_trans[i].tot.nsecs;
173                 td = ((td / ss->nt_trans[i].num) + 500) / 1000;
174
175                 printf("%-25s %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
176                         val_to_str_ext(i, &nt_cmd_vals_ext, "Unknown (0x%02x)"),
177                         ss->nt_trans[i].num,
178                         (int)(ss->nt_trans[i].min.secs),(ss->nt_trans[i].min.nsecs+500)/1000,
179                         (int)(ss->nt_trans[i].max.secs),(ss->nt_trans[i].max.nsecs+500)/1000,
180                         td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
181                 );
182         }
183
184         printf("=================================================================\n");
185 }
186
187
188 static void
189 smbstat_init(const char *optarg,void* userdata _U_)
190 {
191         smbstat_t *ss;
192         guint32 i;
193         const char *filter=NULL;
194         GString *error_string;
195
196         if(!strncmp(optarg,"smb,srt,",8)){
197                 filter=optarg+8;
198         } else {
199                 filter=NULL;
200         }
201
202         ss=g_malloc(sizeof(smbstat_t));
203         if(filter){
204                 ss->filter=g_strdup(filter);
205         } else {
206                 ss->filter=NULL;
207         }
208
209         for(i=0;i<256;i++){
210                 ss->proc[i].num=0;
211                 ss->proc[i].min_num=0;
212                 ss->proc[i].max_num=0;
213                 ss->proc[i].min.secs=0;
214                 ss->proc[i].min.nsecs=0;
215                 ss->proc[i].max.secs=0;
216                 ss->proc[i].max.nsecs=0;
217                 ss->proc[i].tot.secs=0;
218                 ss->proc[i].tot.nsecs=0;
219
220                 ss->trans2[i].num=0;
221                 ss->trans2[i].min_num=0;
222                 ss->trans2[i].max_num=0;
223                 ss->trans2[i].min.secs=0;
224                 ss->trans2[i].min.nsecs=0;
225                 ss->trans2[i].max.secs=0;
226                 ss->trans2[i].max.nsecs=0;
227                 ss->trans2[i].tot.secs=0;
228                 ss->trans2[i].tot.nsecs=0;
229
230                 ss->nt_trans[i].num=0;
231                 ss->nt_trans[i].min_num=0;
232                 ss->nt_trans[i].max_num=0;
233                 ss->nt_trans[i].min.secs=0;
234                 ss->nt_trans[i].min.nsecs=0;
235                 ss->nt_trans[i].max.secs=0;
236                 ss->nt_trans[i].max.nsecs=0;
237                 ss->nt_trans[i].tot.secs=0;
238                 ss->nt_trans[i].tot.nsecs=0;
239         }
240
241         error_string=register_tap_listener("smb", ss, filter, 0, NULL, smbstat_packet, smbstat_draw);
242         if(error_string){
243                 /* error, we failed to attach to the tap. clean up */
244                 g_free(ss->filter);
245                 g_free(ss);
246
247                 fprintf(stderr, "tshark: Couldn't register smb,srt tap: %s\n",
248                     error_string->str);
249                 g_string_free(error_string, TRUE);
250                 exit(1);
251         }
252 }
253
254
255 void
256 register_tap_listener_smbstat(void)
257 {
258         register_stat_cmd_arg("smb,srt", smbstat_init,NULL);
259 }
260