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