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