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