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