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