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