2 * sip message counter for wireshark
4 * Copied from ui/gtk/sip_stat.c and tap-httpstat.c
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include <epan/packet_info.h>
23 #include <epan/stat_tap_ui.h>
24 #include <epan/value_string.h>
25 #include <epan/dissectors/packet-sip.h>
27 #include <wsutil/wslog.h>
29 #include <wsutil/cmdarg_err.h>
31 void register_tap_listener_sipstat(void);
33 /* used to keep track of the statictics for an entire program interface */
34 typedef struct _sip_stats_t
{
36 uint32_t packets
; /* number of sip packets, including continuations */
37 uint32_t resent_packets
;
38 uint32_t average_setup_time
;
39 uint32_t max_setup_time
;
40 uint32_t min_setup_time
;
41 uint32_t no_of_completed_calls
;
42 uint64_t total_setup_time
;
43 GHashTable
*hash_responses
;
44 GHashTable
*hash_requests
;
47 /* used to keep track of the stats for a specific response code
48 * for example it can be { 3, 404, "Not Found" ,...}
49 * which means we captured 3 reply sip/1.1 404 Not Found */
50 typedef struct _sip_response_code_t
{
51 uint32_t packets
; /* 3 */
52 unsigned response_code
; /* 404 */
53 const char *name
; /* Not Found */
55 } sip_response_code_t
;
57 /* used to keep track of the stats for a specific request string */
58 typedef struct _sip_request_method_t
{
59 char *response
; /* eg. : INVITE */
62 } sip_request_method_t
;
65 /* Create tables for responses and requests */
67 sip_init_hash(sipstat_t
*sp
)
71 /* Create responses table */
72 sp
->hash_responses
= g_hash_table_new(g_int_hash
, g_int_equal
);
74 /* Add all response codes */
75 for (i
=0; sip_response_code_vals
[i
].strptr
; i
++)
77 int *key
= g_new (int, 1);
78 sip_response_code_t
*sc
= g_new (sip_response_code_t
, 1);
79 *key
= sip_response_code_vals
[i
].value
;
81 sc
->response_code
= *key
;
82 sc
->name
= sip_response_code_vals
[i
].strptr
;
84 g_hash_table_insert(sc
->sp
->hash_responses
, key
, sc
);
87 /* Create empty requests table */
88 sp
->hash_requests
= g_hash_table_new(g_str_hash
, g_str_equal
);
92 sip_draw_hash_requests( char *key _U_
, sip_request_method_t
*data
, char *format
)
94 if (data
->packets
== 0)
96 printf( format
, data
->response
, data
->packets
);
100 sip_draw_hash_responses( int *key _U_
, sip_response_code_t
*data
, char *format
)
103 ws_warning("C'est quoi ce borderl key=%d\n", *key
);
106 if (data
->packets
== 0)
108 printf(format
, data
->response_code
, data
->name
, data
->packets
);
111 /* NOT USED at this moment */
114 sip_free_hash( void *key, void *value, void *user_data _U_ )
122 sip_reset_hash_responses(char *key _U_
, sip_response_code_t
*data
, void *ptr _U_
)
127 sip_reset_hash_requests(char *key _U_
, sip_request_method_t
*data
, void *ptr _U_
)
133 sipstat_reset(void *psp
)
135 sipstat_t
*sp
= (sipstat_t
*)psp
;
138 sp
->resent_packets
= 0;
139 sp
->average_setup_time
= 0;
140 sp
->max_setup_time
= 0;
141 sp
->min_setup_time
= 0;
142 sp
->no_of_completed_calls
= 0;
143 sp
->total_setup_time
= 0;
145 g_hash_table_foreach( sp
->hash_responses
, (GHFunc
)sip_reset_hash_responses
, NULL
);
146 g_hash_table_foreach( sp
->hash_requests
, (GHFunc
)sip_reset_hash_requests
, NULL
);
151 /* Main entry point to SIP tap */
152 static tap_packet_status
153 sipstat_packet(void *psp
, packet_info
*pinfo _U_
, epan_dissect_t
*edt _U_
, const void *pri
, tap_flags_t flags _U_
)
155 const sip_info_value_t
*value
= (const sip_info_value_t
*)pri
;
156 sipstat_t
*sp
= (sipstat_t
*)psp
;
158 /* Total number of packets, including continuation packets */
161 /* Calculate average setup time */
162 if (value
->setup_time
) {
163 sp
->no_of_completed_calls
++;
164 /* Check if it's the first value */
165 if ( sp
->total_setup_time
== 0 ) {
166 sp
->average_setup_time
= value
->setup_time
;
167 sp
->total_setup_time
= value
->setup_time
;
168 sp
->max_setup_time
= value
->setup_time
;
169 sp
->min_setup_time
= value
->setup_time
;
171 sp
->total_setup_time
= sp
->total_setup_time
+ value
->setup_time
;
172 if (sp
->max_setup_time
< value
->setup_time
) {
173 sp
->max_setup_time
= value
->setup_time
;
175 if (sp
->min_setup_time
> value
->setup_time
) {
176 sp
->min_setup_time
= value
->setup_time
;
178 /* Calculate average */
179 sp
->average_setup_time
= (uint32_t)(sp
->total_setup_time
/ sp
->no_of_completed_calls
);
183 /* Update resent count if flag set */
186 sp
->resent_packets
++;
190 /* Looking at both requests and responses */
191 if (value
->response_code
!= 0)
195 sip_response_code_t
*sc
;
197 /* Look up response code in hash table */
198 key
= value
->response_code
;
199 sc
= (sip_response_code_t
*)g_hash_table_lookup(sp
->hash_responses
, &key
);
202 /* Non-standard status code ; we classify it as others
203 * in the relevant category
204 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
206 int i
= value
->response_code
;
207 if ((i
< 100) || (i
>= 700))
209 /* Forget about crazy values */
210 return TAP_PACKET_DONT_REDRAW
;
214 key
= 199; /* Hopefully, this status code will never be used */
237 /* Now look up this fallback code to get its text description */
238 sc
= (sip_response_code_t
*)g_hash_table_lookup(sp
->hash_responses
, &key
);
241 return TAP_PACKET_DONT_REDRAW
;
246 else if (value
->request_method
)
249 sip_request_method_t
*sc
;
251 /* Look up the request method in the table */
252 sc
= (sip_request_method_t
*)g_hash_table_lookup(sp
->hash_requests
, value
->request_method
);
255 /* First of this type. Create structure and initialise */
256 sc
= g_new(sip_request_method_t
, 1);
257 sc
->response
= g_strdup(value
->request_method
);
260 /* Insert it into request table */
261 g_hash_table_insert(sp
->hash_requests
, sc
->response
, sc
);
265 /* Already existed, just update count for that method */
268 /* g_free(value->request_method); */
272 /* No request method set. Just ignore */
273 return TAP_PACKET_DONT_REDRAW
;
276 return TAP_PACKET_REDRAW
;
280 sipstat_draw(void *psp
)
282 sipstat_t
*sp
= (sipstat_t
*)psp
;
284 printf("===================================================================\n");
285 if (sp
->filter
== NULL
)
286 printf("SIP Statistics\n");
288 printf("SIP Statistics with filter %s\n", sp
->filter
);
290 printf("\nNumber of SIP messages: %u", sp
->packets
);
291 printf("\nNumber of resent SIP messages: %u\n", sp
->resent_packets
);
292 printf( "\n* SIP Status Codes in reply packets\n");
293 g_hash_table_foreach(sp
->hash_responses
, (GHFunc
)sip_draw_hash_responses
,
294 (void *)" SIP %3d %-15s : %5d Packets\n");
295 printf("\n* List of SIP Request methods\n");
296 g_hash_table_foreach(sp
->hash_requests
, (GHFunc
)sip_draw_hash_requests
,
297 (void *)" %-15s : %5d Packets\n");
298 printf( "\n* Average setup time %u ms\n Min %u ms\n Max %u ms\n", sp
->average_setup_time
, sp
->min_setup_time
, sp
->max_setup_time
);
299 printf("===================================================================\n");
303 sipstat_init(const char *opt_arg
, void *userdata _U_
)
306 const char *filter
= NULL
;
307 GString
*error_string
;
309 if (strncmp (opt_arg
, "sip,stat,", 9) == 0) {
315 sp
= g_new0(sipstat_t
, 1);
316 sp
->filter
= g_strdup(filter
);
317 /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
320 error_string
= register_tap_listener(
330 /* error, we failed to attach to the tap. clean up */
333 cmdarg_err("Couldn't register sip,stat tap: %s",
335 g_string_free(error_string
, TRUE
);
340 sp
->resent_packets
= 0;
344 static stat_tap_ui sipstat_ui
= {
345 REGISTER_STAT_GROUP_GENERIC
,
354 register_tap_listener_sipstat(void)
356 register_stat_tap_ui(&sipstat_ui
, NULL
);
360 * Editor modelines - https://www.wireshark.org/tools/modelines.html
365 * indent-tabs-mode: t
368 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
369 * :indentSize=8:tabSize=8:noTabs=false: