2 * sip_stat 2004 Martin Mathieson
4 * $Id: sip_stat.c,v 1.2 2004/03/27 11:13:03 guy Exp $
5 * Copied from http_stat.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.
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
38 #include "simple_dialog.h"
40 #include "dlg_utils.h"
42 #include "../register.h"
43 #include "../packet-sip.h"
44 #include "../globals.h"
45 #include "compat_macros.h"
46 #include "../tap_dfilter_dlg.h"
47 #include "tap_dfilter_dlg.h"
49 #define SUM_STR_MAX 1024
51 /* Used to keep track of the statistics for an entire program interface */
52 typedef struct _sip_stats_t {
55 GHashTable *hash_responses;
56 GHashTable *hash_requests;
57 guint32 packets; /* number of sip packets, including continuations */
58 GtkWidget *packets_label;
60 GtkWidget *request_box; /* container for INVITE, ... */
62 GtkWidget *informational_table; /* Status code between 100 and 199 */
63 GtkWidget *success_table; /* 200 and 299 */
64 GtkWidget *redirection_table; /* 300 and 399 */
65 GtkWidget *client_error_table; /* 400 and 499 */
66 GtkWidget *server_errors_table; /* 500 and 599 */
67 GtkWidget *global_failures_table; /* 600 and 699 */
70 /* Used to keep track of the stats for a specific response code
71 * for example it can be { 3, 404, "Not Found" ,...}
72 * which means we captured 3 reply sip/1.1 404 Not Found */
73 typedef struct _sip_response_code_t {
74 guint32 packets; /* 3 */
75 guint response_code; /* 404 */
76 gchar *name; /* "Not Found" */
77 GtkWidget *widget; /* Label where we display it */
78 GtkWidget *table; /* Table in which we put it,
79 e.g. client_error_table */
80 sipstat_t *sp; /* Pointer back to main struct */
81 } sip_response_code_t;
83 /* Used to keep track of the stats for a specific request string */
84 typedef struct _sip_request_method_t {
85 gchar *response; /* eg. : INVITE */
88 sipstat_t *sp; /* Pointer back to main struct */
89 } sip_request_method_t;
91 /* TODO: extra codes to be added from SIP extensions? */
92 static const value_string vals_status_code[] = {
95 { 181, "Call Is Being Forwarded"},
97 { 183, "Session Progress"},
98 { 199, "Informational - Others" },
102 { 299, "Success - Others"}, /* used to keep track of other Success packets */
104 { 300, "Multiple Choices"},
105 { 301, "Moved Permanently"},
106 { 302, "Moved Temporarily"},
108 { 380, "Alternative Service"},
109 { 399, "Redirection - Others"},
111 { 400, "Bad Request"},
112 { 401, "Unauthorized"},
113 { 402, "Payment Required"},
116 { 405, "Method Not Allowed"},
117 { 406, "Not Acceptable"},
118 { 407, "Proxy Authentication Required"},
119 { 408, "Request Timeout"},
121 { 413, "Request Entity Too Large"},
122 { 414, "Request-URI Too Long"},
123 { 415, "Unsupported Media Type"},
124 { 416, "Unsupported URI Scheme"},
125 { 420, "Bad Extension"},
126 { 421, "Extension Required"},
127 { 423, "Interval Too Brief"},
128 { 480, "Temporarily Unavailable"},
129 { 481, "Call/Transaction Does Not Exist"},
130 { 482, "Loop Detected"},
131 { 483, "Too Many Hops"},
132 { 484, "Address Incomplete"},
135 { 487, "Request Terminated"},
136 { 488, "Not Acceptable Here"},
138 { 491, "Request Pending"},
139 { 493, "Undecipherable"},
140 { 499, "Client Error - Others"},
142 { 500, "Server Internal Error"},
143 { 501, "Not Implemented"},
144 { 502, "Bad Gateway"},
145 { 503, "Service Unavailable"},
146 { 504, "Server Time-out"},
147 { 505, "Version Not Supported"},
148 { 513, "Message Too Large"},
149 { 599, "Server Error - Others"},
151 { 600, "Busy Everywhere"},
153 { 604, "Does Not Exist Anywhere"},
154 { 606, "Not Acceptable"},
155 { 699, "Global Failure - Others"},
160 /* Create tables for responses and requests */
162 sip_init_hash(sipstat_t *sp)
166 /* Create responses table */
167 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
169 /* Add all response codes */
170 for (i=0 ; vals_status_code[i].strptr ; i++)
172 gint *key = g_malloc (sizeof(gint));
173 sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
174 *key = vals_status_code[i].value;
176 sc->response_code = *key;
177 sc->name=vals_status_code[i].strptr;
181 g_hash_table_insert(sc->sp->hash_responses, key, sc);
184 /* Create empty requests table */
185 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
188 /* Draw the entry for an individual request message */
190 sip_draw_hash_requests(gchar *key _U_ , sip_request_method_t *data, gchar * unused _U_)
192 gchar string_buff[SUM_STR_MAX];
194 if (data->packets==0)
199 /* Build string showing method and count */
200 g_snprintf(string_buff, sizeof(string_buff),
201 " %-11s : %3d packets", data->response, data->packets);
202 if (data->widget==NULL)
204 /* Create new label */
205 data->widget=gtk_label_new(string_buff);
206 gtk_misc_set_alignment(GTK_MISC(data->widget), 0.0, 0.5);
207 gtk_box_pack_start(GTK_BOX(data->sp->request_box), data->widget,FALSE,FALSE, 0);
208 gtk_widget_show(data->widget);
212 /* Update existing label */
213 gtk_label_set(GTK_LABEL(data->widget), string_buff);
217 /* Draw an individual response entry */
219 sip_draw_hash_responses(gint * key _U_ , sip_response_code_t *data, gchar * unused _U_)
221 gchar string_buff[SUM_STR_MAX];
225 g_warning("C'est quoi ce borderl key=%d\n", *key);
227 if (data->packets==0)
232 /* Create an entry in the relevant box of the window */
233 if (data->widget==NULL)
237 guint i = data->response_code;
239 /* Out of valid range - ignore */
240 if ((i<100)||(i>=700))
245 /* Find the table matching the code */
248 data->table = data->sp->informational_table;
252 data->table = data->sp->success_table;
256 data->table = data->sp->redirection_table;
260 data->table = data->sp->client_error_table;
264 data->table = data->sp->server_errors_table;
268 data->table = data->sp->global_failures_table;
271 /* Get number of rows in table */
272 x = GTK_TABLE(data->table)->nrows;
274 /* Create a new label with this response, e.g. "SIP 180 Ringing" */
275 g_snprintf(string_buff, sizeof(string_buff),
276 "SIP %3d %s ", data->response_code, data->name);
277 tmp = gtk_label_new(string_buff);
279 /* Insert the label in the correct place in the table */
280 gtk_table_attach_defaults(GTK_TABLE(data->table), tmp, 0, 1, x, x+1);
281 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
282 gtk_widget_show(tmp);
284 /* Show number of packets */
285 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
286 data->widget=gtk_label_new(string_buff);
288 /* Show this widget in the right place */
289 gtk_table_attach_defaults(GTK_TABLE(data->table), data->widget, 1, 2,x,x+1);
290 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_RIGHT);
291 gtk_widget_show(data->widget);
293 gtk_table_resize(GTK_TABLE(data->table), x+1, 4);
297 /* Just update the existing label string */
298 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
299 gtk_label_set(GTK_LABEL(data->widget), string_buff);
306 sip_free_hash(gpointer key, gpointer value, gpointer user_data _U_)
313 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_)
319 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_)
325 sipstat_reset(void *psp)
330 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
331 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_reset_hash_requests, NULL);
335 /* Main entry point to SIP tap */
337 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *pri)
339 sip_info_value_t *value=pri;
340 sipstat_t *sp = (sipstat_t *)psp;
342 /* Total number of packets, including continuation packets */
345 /* Looking at both requests and responses */
346 if (value->response_code != 0)
349 guint *key = g_malloc(sizeof(guint));
350 sip_response_code_t *sc;
352 /* Look up response code in hash table */
353 *key = value->response_code;
354 sc = g_hash_table_lookup(sp->hash_responses, key);
357 /* Non-standard status code ; we classify it as others
358 * in the relevant category
359 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
361 int i = value->response_code;
362 if ((i<100) || (i>=700))
364 /* Forget about crazy values */
369 *key=199; /* Hopefully, this status code will never be used */
392 /* Now look up this fallback code to get its text description */
393 sc = g_hash_table_lookup(sp->hash_responses, key);
401 else if (value->request_method)
404 sip_request_method_t *sc;
406 /* Look up the request method in the table */
407 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
410 /* First of this type. Create structure and initialise */
411 sc=g_malloc(sizeof(sip_request_method_t));
412 sc->response = g_strdup(value->request_method);
416 /* Insert it into request table */
417 g_hash_table_insert(sp->hash_requests, sc->response, sc);
421 /* Already existed, just update count for that method */
424 /* g_free(value->request_method); */
428 /* No request method set. Just ignore */
435 /* Redraw the whole stats window */
437 sipstat_draw(void *psp)
439 gchar string_buff[SUM_STR_MAX];
442 /* Set summary label */
443 g_snprintf(string_buff, sizeof(string_buff),
444 "SIP stats (%d packets)", sp->packets);
445 gtk_label_set(GTK_LABEL(sp->packets_label), string_buff);
447 /* Draw responses and requests from their tables */
448 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_draw_hash_responses, NULL);
449 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_draw_hash_requests, NULL);
453 /* since the gtk2 implementation of tap is multithreaded we must protect
454 * remove_tap_listener() from modifying the list while draw_tap_listener()
455 * is running. the other protected block is in main.c
457 * there should not be any other critical regions in gtk2
459 void protect_thread_critical_region(void);
460 void unprotect_thread_critical_region(void);
462 /* When window is destroyed, clean up */
464 win_destroy_cb(GtkWindow *win _U_, gpointer data)
466 sipstat_t *sp=(sipstat_t *)data;
468 protect_thread_critical_region();
469 remove_tap_listener(sp);
470 unprotect_thread_critical_region();
472 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_free_hash, NULL);
473 g_hash_table_destroy(sp->hash_responses);
474 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_free_hash, NULL);
475 g_hash_table_destroy(sp->hash_requests);
480 /* Create a new instance of gtk_sipstat. */
482 gtk_sipstat_init(char *optarg)
486 GString *error_string;
488 GtkWidget *main_vb, *separator,
489 *informational_fr, *success_fr, *redirection_fr,
490 *client_errors_fr, *server_errors_fr, *global_failures_fr,
494 if (strncmp (optarg, "sip,stat,", 9) == 0)
496 /* Skip those characters from filter to display */
505 /* Create sip stats window structure */
506 sp = g_malloc(sizeof(sipstat_t));
508 /* Set title to include any filter given */
511 sp->filter = g_strdup(filter);
512 title = g_strdup_printf("SIP statistics with filter: %s", filter);
517 title = g_strdup("SIP statistics");
520 /* Create underlying window */
521 sp->win = window_new(GTK_WINDOW_TOPLEVEL, title);
524 /* Set destroy callback for underlying window */
525 SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
528 /* Create container for all widgets */
529 main_vb = gtk_vbox_new(FALSE, 10);
530 gtk_container_border_width(GTK_CONTAINER(main_vb), 10);
531 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
532 gtk_widget_show(main_vb);
534 /* Initialise & show number of packets */
536 sp->packets_label = gtk_label_new("SIP stats (0 SIP packets)");
537 gtk_container_add(GTK_CONTAINER(main_vb), sp->packets_label);
538 gtk_widget_show(sp->packets_label);
541 /* Informational response frame */
542 informational_fr = gtk_frame_new("Informational SIP 1xx");
543 gtk_container_add(GTK_CONTAINER(main_vb), informational_fr);
544 gtk_widget_show(informational_fr);
546 /* Information table (within that frame) */
547 sp->informational_table = gtk_table_new(0, 2, FALSE);
548 gtk_container_add(GTK_CONTAINER(informational_fr), sp->informational_table);
549 gtk_widget_show(sp->informational_table);
552 /* Success table and frame */
553 success_fr = gtk_frame_new ("Success SIP 2xx");
554 gtk_container_add(GTK_CONTAINER(main_vb), success_fr);
555 gtk_widget_show(success_fr);
557 sp->success_table = gtk_table_new(0, 2, FALSE);
558 gtk_container_add(GTK_CONTAINER(success_fr), sp->success_table);
559 gtk_widget_show(sp->success_table);
562 /* Redirection table and frame */
563 redirection_fr = gtk_frame_new ("Redirection SIP 3xx");
564 gtk_container_add(GTK_CONTAINER(main_vb), redirection_fr);
565 gtk_widget_show(redirection_fr);
567 sp->redirection_table = gtk_table_new(0, 2, FALSE);
568 gtk_container_add(GTK_CONTAINER(redirection_fr), sp->redirection_table);
569 gtk_widget_show(sp->redirection_table);
572 /* Client Errors table and frame */
573 client_errors_fr = gtk_frame_new("Client errors SIP 4xx");
574 gtk_container_add(GTK_CONTAINER(main_vb), client_errors_fr);
575 gtk_widget_show(client_errors_fr);
577 sp->client_error_table = gtk_table_new(0, 2, FALSE);
578 gtk_container_add(GTK_CONTAINER(client_errors_fr), sp->client_error_table);
579 gtk_widget_show(sp->client_error_table);
582 /* Server Errors table and frame */
583 server_errors_fr = gtk_frame_new("Server errors SIP 5xx");
584 gtk_container_add(GTK_CONTAINER(main_vb), server_errors_fr);
585 gtk_widget_show(server_errors_fr);
587 sp->server_errors_table = gtk_table_new(0, 2, FALSE);
588 gtk_container_add(GTK_CONTAINER(server_errors_fr), sp->server_errors_table);
589 gtk_widget_show(sp->server_errors_table);
592 /* Global Failures table and frame */
593 global_failures_fr = gtk_frame_new("Global failures SIP 6xx");
594 gtk_container_add(GTK_CONTAINER(main_vb), global_failures_fr);
595 gtk_widget_show(global_failures_fr);
597 sp->global_failures_table = gtk_table_new(0, 2, FALSE);
598 gtk_container_add(GTK_CONTAINER(global_failures_fr), sp->global_failures_table);
599 gtk_widget_show(sp->global_failures_table);
602 /* Separator between requests and responses */
603 separator = gtk_hseparator_new();
604 gtk_container_add(GTK_CONTAINER(main_vb), separator);
607 /* Request table and frame */
608 request_fr = gtk_frame_new("List of request methods");
609 gtk_container_add(GTK_CONTAINER(main_vb), request_fr);
610 gtk_container_border_width(GTK_CONTAINER(request_fr), 0);
611 gtk_widget_show(request_fr);
613 sp->request_box = gtk_vbox_new(FALSE, 10);
614 gtk_container_add(GTK_CONTAINER(request_fr), sp->request_box);
615 gtk_widget_show(sp->request_box);
618 /* Register this tap listener now */
619 error_string = register_tap_listener("sip",
627 /* Error. We failed to attach to the tap. Clean up */
628 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
631 g_string_free(error_string, TRUE);
635 /* Display up-to-date contents */
636 gtk_widget_show_all(sp->win);
638 retap_packets(&cfile);
641 static tap_dfilter_dlg sip_stat_dlg = {
642 "SIP Packet Counter",
648 /* Register this tap listener and add menu item. */
650 register_tap_listener_gtksipstat(void)
652 register_ethereal_tap("sip,stat", gtk_sipstat_init);
654 register_tap_menu_item("SIP", REGISTER_TAP_GROUP_NONE,
655 gtk_tap_dfilter_dlg_cb, NULL, NULL, &(sip_stat_dlg));