2 * sip message counter for ethereal
5 * Copied from gtk/sip_stat.c and tap-httpstat.c
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
37 #include "epan/packet_info.h"
39 #include "epan/value_string.h"
41 #include <epan/dissectors/packet-sip.h>
43 /* used to keep track of the statictics for an entire program interface */
44 typedef struct _sip_stats_t {
46 guint32 packets; /* number of sip packets, including continuations */
47 guint32 resent_packets;
48 GHashTable *hash_responses;
49 GHashTable *hash_requests;
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 */
60 } sip_response_code_t;
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 */
67 } sip_request_method_t;
69 /* TODO: extra codes to be added from SIP extensions? */
70 static const value_string vals_status_code[] = {
73 { 181, "Call Is Being Forwarded"},
75 { 183, "Session Progress"},
76 { 199, "Informational - Others" },
80 { 299, "Success - Others"}, /* used to keep track of other Success packets */
82 { 300, "Multiple Choices"},
83 { 301, "Moved Permanently"},
84 { 302, "Moved Temporarily"},
86 { 380, "Alternative Service"},
87 { 399, "Redirection - Others"},
89 { 400, "Bad Request"},
90 { 401, "Unauthorized"},
91 { 402, "Payment Required"},
94 { 405, "Method Not Allowed"},
95 { 406, "Not Acceptable"},
96 { 407, "Proxy Authentication Required"},
97 { 408, "Request Timeout"},
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"},
113 { 487, "Request Terminated"},
114 { 488, "Not Acceptable Here"},
116 { 491, "Request Pending"},
117 { 493, "Undecipherable"},
118 { 499, "Client Error - Others"},
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"},
129 { 600, "Busy Everywhere"},
131 { 604, "Does Not Exist Anywhere"},
132 { 606, "Not Acceptable"},
133 { 699, "Global Failure - Others"},
138 /* Create tables for responses and requests */
140 sip_init_hash(sipstat_t *sp)
144 /* Create responses table */
145 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
147 /* Add all response codes */
148 for (i=0 ; vals_status_code[i].strptr ; i++)
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;
154 sc->response_code = *key;
155 sc->name=vals_status_code[i].strptr;
157 g_hash_table_insert(sc->sp->hash_responses, key, sc);
160 /* Create empty requests table */
161 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
165 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
167 if (data->packets==0)
169 printf( format, data->response, data->packets);
173 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
176 g_warning("C'est quoi ce borderl key=%d\n", *key);
179 if (data->packets==0)
181 printf(format, data->response_code, data->name, data->packets );
184 /* NOT USED at this moment */
187 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
195 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
200 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
206 sipstat_reset(void *psp )
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);
218 /* Main entry point to SIP tap */
220 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *pri)
222 sip_info_value_t *value=pri;
223 sipstat_t *sp = (sipstat_t *)psp;
225 /* Total number of packets, including continuation packets */
228 /* Update resent count if flag set */
231 sp->resent_packets++;
235 /* Looking at both requests and responses */
236 if (value->response_code != 0)
239 guint *key = g_malloc(sizeof(guint));
240 sip_response_code_t *sc;
242 /* Look up response code in hash table */
243 *key = value->response_code;
244 sc = g_hash_table_lookup(sp->hash_responses, key);
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)
251 int i = value->response_code;
252 if ((i<100) || (i>=700))
254 /* Forget about crazy values */
259 *key=199; /* Hopefully, this status code will never be used */
282 /* Now look up this fallback code to get its text description */
283 sc = g_hash_table_lookup(sp->hash_responses, key);
291 else if (value->request_method)
294 sip_request_method_t *sc;
296 /* Look up the request method in the table */
297 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
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);
305 /* Insert it into request table */
306 g_hash_table_insert(sp->hash_requests, sc->response, sc);
310 /* Already existed, just update count for that method */
313 /* g_free(value->request_method); */
317 /* No request method set. Just ignore */
325 sipstat_draw(void *psp )
329 printf("===================================================================\n");
330 if (sp->filter == NULL)
331 printf("SIP Statistics\n");
333 printf("SIP Statistics with filter %s\n", sp->filter);
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");
347 sipstat_init(char *optarg)
351 GString *error_string;
353 if (strncmp (optarg, "sip,stat,", 9) == 0){
359 sp = g_malloc( sizeof(sipstat_t) );
361 sp->filter=g_strdup(filter);
365 /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
368 error_string = register_tap_listener(
376 /* error, we failed to attach to the tap. clean up */
379 fprintf (stderr, "tethereal: Coulnd't register sip,stat tap: %s\n",
381 g_string_free(error_string, TRUE);
386 sp->resent_packets = 0;
391 register_tap_listener_sipstat(void)
393 register_ethereal_tap("sip,stat", sipstat_init);