http://bugs.ethereal.com/bugzilla/show_bug.cgi?id=377
[obnox/wireshark/wip.git] / tap-sipstat.c
1 /* tap_sipstat.c
2  * sip message counter for ethereal
3  *
4  * $Id$
5  * Copied from gtk/sip_stat.c and tap-httpstat.c
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37 #include "epan/packet_info.h"
38 #include <epan/tap.h>
39 #include <epan/stat_cmd_args.h>
40 #include "epan/value_string.h"
41 #include "register.h"
42 #include <epan/dissectors/packet-sip.h>
43
44 /* used to keep track of the statictics for an entire program interface */
45 typedef struct _sip_stats_t {
46         char            *filter;
47         guint32         packets;        /* number of sip packets, including continuations */
48         guint32         resent_packets;
49         GHashTable      *hash_responses;
50         GHashTable      *hash_requests;
51 } sipstat_t;
52
53 /* used to keep track of the stats for a specific response code
54  * for example it can be { 3, 404, "Not Found" ,...}
55  * which means we captured 3 reply sip/1.1 404 Not Found */
56 typedef struct _sip_response_code_t {
57         guint32          packets;               /* 3 */
58         guint            response_code;         /* 404 */
59         const gchar     *name;                  /* Not Found */
60         sipstat_t       *sp;
61 } sip_response_code_t;
62
63 /* used to keep track of the stats for a specific request string */
64 typedef struct _sip_request_method_t {
65         gchar           *response;      /* eg. : INVITE */
66         guint32          packets;
67         sipstat_t       *sp;
68 } sip_request_method_t;
69
70 /* TODO: extra codes to be added from SIP extensions? */
71 static const value_string vals_status_code[] = {
72     { 100, "Trying"},
73     { 180, "Ringing"},
74     { 181, "Call Is Being Forwarded"},
75     { 182, "Queued"},
76     { 183, "Session Progress"},
77     { 199, "Informational - Others" },
78
79     { 200, "OK"},
80     { 202, "Accepted"},
81     { 299, "Success - Others"}, /* used to keep track of other Success packets */
82
83     { 300, "Multiple Choices"},
84     { 301, "Moved Permanently"},
85     { 302, "Moved Temporarily"},
86     { 305, "Use Proxy"},
87     { 380, "Alternative Service"},
88     { 399, "Redirection - Others"},
89
90     { 400, "Bad Request"},
91     { 401, "Unauthorized"},
92     { 402, "Payment Required"},
93     { 403, "Forbidden"},
94     { 404, "Not Found"},
95     { 405, "Method Not Allowed"},
96     { 406, "Not Acceptable"},
97     { 407, "Proxy Authentication Required"},
98     { 408, "Request Timeout"},
99     { 410, "Gone"},
100     { 413, "Request Entity Too Large"},
101     { 414, "Request-URI Too Long"},
102     { 415, "Unsupported Media Type"},
103     { 416, "Unsupported URI Scheme"},
104     { 420, "Bad Extension"},
105     { 421, "Extension Required"},
106     { 423, "Interval Too Brief"},
107     { 480, "Temporarily Unavailable"},
108     { 481, "Call/Transaction Does Not Exist"},
109     { 482, "Loop Detected"},
110     { 483, "Too Many Hops"},
111     { 484, "Address Incomplete"},
112     { 485, "Ambiguous"},
113     { 486, "Busy Here"},
114     { 487, "Request Terminated"},
115     { 488, "Not Acceptable Here"},
116     { 489, "Bad Event"},
117     { 491, "Request Pending"},
118     { 493, "Undecipherable"},
119     { 499, "Client Error - Others"},
120
121     { 500, "Server Internal Error"},
122     { 501, "Not Implemented"},
123     { 502, "Bad Gateway"},
124     { 503, "Service Unavailable"},
125     { 504, "Server Time-out"},
126     { 505, "Version Not Supported"},
127     { 513, "Message Too Large"},
128     { 599, "Server Error - Others"},
129
130     { 600, "Busy Everywhere"},
131     { 603, "Decline"},
132     { 604, "Does Not Exist Anywhere"},
133     { 606, "Not Acceptable"},
134     { 699, "Global Failure - Others"},
135
136     { 0,        NULL}
137 };
138
139 /* Create tables for responses and requests */
140 static void
141 sip_init_hash(sipstat_t *sp)
142 {
143     int i;
144
145     /* Create responses table */
146     sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
147
148     /* Add all response codes */
149     for (i=0 ; vals_status_code[i].strptr ; i++)
150     {
151         gint *key = g_malloc (sizeof(gint));
152         sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
153         *key = vals_status_code[i].value;
154         sc->packets=0;
155         sc->response_code =  *key;
156         sc->name=vals_status_code[i].strptr;
157         sc->sp = sp;
158         g_hash_table_insert(sc->sp->hash_responses, key, sc);
159     }
160
161     /* Create empty requests table */
162     sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
163 }
164
165 static void
166 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
167 {
168         if (data->packets==0)
169                 return;
170         printf( format, data->response, data->packets);
171 }
172
173 static void
174 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
175 {
176         if (data==NULL) {
177                 g_warning("C'est quoi ce borderl key=%d\n", *key);
178                 exit(EXIT_FAILURE);
179         }
180         if (data->packets==0)
181                 return;
182         printf(format,  data->response_code, data->name, data->packets );
183 }
184
185 /* NOT USED at this moment */
186 /*
187 static void
188 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
189 {
190         g_free(key);
191         g_free(value);
192 }
193 */
194
195 static void
196 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
197 {
198         data->packets = 0;
199 }
200 static void
201 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
202 {
203         data->packets = 0;
204 }
205
206 static void
207 sipstat_reset(void *psp  )
208 {
209         sipstat_t *sp=psp;
210         if (sp) {
211                 sp->packets = 0;
212                 sp->resent_packets = 0;
213                 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
214                 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
215         }
216 }
217
218
219 /* Main entry point to SIP tap */
220 static int
221 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
222 {
223     const sip_info_value_t *value=pri;
224     sipstat_t *sp = (sipstat_t *)psp;
225     
226     /* Total number of packets, including continuation packets */
227     sp->packets++;
228     
229     /* Update resent count if flag set */
230     if (value->resend)
231     {
232         sp->resent_packets++;
233     }
234
235     
236     /* Looking at both requests and responses */
237     if (value->response_code != 0)
238     {
239         /* Responses */
240         guint *key = g_malloc(sizeof(guint));
241         sip_response_code_t *sc;
242
243         /* Look up response code in hash table */
244         *key = value->response_code;
245         sc = g_hash_table_lookup(sp->hash_responses, key);
246         if (sc==NULL)
247         {
248             /* Non-standard status code ; we classify it as others
249              * in the relevant category
250              * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
251              */
252             int i = value->response_code;
253             if ((i<100) || (i>=700))
254             {
255                 /* Forget about crazy values */
256                 return 0;
257             }
258             else if (i<200)
259             {
260                 *key=199;       /* Hopefully, this status code will never be used */
261             }
262             else if (i<300)
263             {
264                 *key=299;
265             }
266             else if (i<400)
267             {
268                 *key=399;
269             }
270             else if (i<500)
271             {
272                 *key=499;
273             }
274             else if (i<600)
275             {
276                 *key=599;
277             }
278             else
279             {
280                 *key = 699;
281             }
282
283             /* Now look up this fallback code to get its text description */
284             sc = g_hash_table_lookup(sp->hash_responses, key);
285             if (sc==NULL)
286             {
287                 return 0;
288             }
289         }
290         sc->packets++;
291     }
292     else if (value->request_method)
293     {
294         /* Requests */
295         sip_request_method_t *sc;
296
297         /* Look up the request method in the table */
298         sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
299         if (sc == NULL)
300         {
301             /* First of this type. Create structure and initialise */
302             sc=g_malloc(sizeof(sip_request_method_t));
303             sc->response = g_strdup(value->request_method);
304             sc->packets = 1;
305             sc->sp = sp;
306             /* Insert it into request table */
307             g_hash_table_insert(sp->hash_requests, sc->response, sc);
308         }
309         else
310         {
311             /* Already existed, just update count for that method */
312             sc->packets++;
313         }
314         /* g_free(value->request_method); */
315     }
316     else
317     {
318         /* No request method set. Just ignore */
319         return 0;
320     }
321
322     return 1;
323 }
324
325 static void
326 sipstat_draw(void *psp  )
327 {
328         sipstat_t *sp=psp;
329         printf("\n");
330         printf("===================================================================\n");
331         if (sp->filter == NULL)
332                 printf("SIP Statistics\n");
333         else
334                 printf("SIP Statistics with filter %s\n", sp->filter);
335
336         printf("\nNumber of SIP messages: %d", sp->packets);
337         printf("\nNumber of resent SIP messages: %d\n", sp->resent_packets);
338         printf( "\n* SIP Status Codes in reply packets\n");
339         g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_draw_hash_responses,
340                 "  SIP %3d %-15s : %5d Packets\n");
341         printf("\n* List of SIP Request methods\n");
342         g_hash_table_foreach( sp->hash_requests,  (GHFunc)sip_draw_hash_requests,
343                 "  %-15s : %5d Packets\n");
344         printf("===================================================================\n");
345 }
346
347 static void
348 sipstat_init(const char *optarg)
349 {
350         sipstat_t *sp;
351         const char *filter=NULL;
352         GString *error_string;
353
354         if (strncmp (optarg, "sip,stat,", 9) == 0){
355                 filter=optarg+9;
356         } else {
357                 filter=NULL;
358         }
359
360         sp = g_malloc( sizeof(sipstat_t) );
361         if(filter){
362                 sp->filter=g_strdup(filter);
363         } else {
364                 sp->filter=NULL;
365         }
366         /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
367
368
369         error_string = register_tap_listener(
370                         "sip",
371                         sp,
372                         filter,
373                         sipstat_reset,
374                         sipstat_packet,
375                         sipstat_draw);
376         if (error_string){
377                 /* error, we failed to attach to the tap. clean up */
378                 g_free(sp->filter);
379                 g_free(sp);
380                 fprintf (stderr, "tethereal: Couldn't register sip,stat tap: %s\n",
381                                 error_string->str);
382                 g_string_free(error_string, TRUE);
383                 exit(1);
384         }
385
386         sp->packets = 0;
387         sp->resent_packets = 0;
388         sip_init_hash(sp);
389 }
390
391 void
392 register_tap_listener_sipstat(void)
393 {
394         register_stat_cmd_arg("sip,stat", sipstat_init);
395 }