update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / cli / tap-sipstat.c
blob87717eb3ed61e0126ad76dc98fe17865e8309de0
1 /* tap_sipstat.c
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
13 #include "config.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include <glib.h>
21 #include <epan/packet_info.h>
22 #include <epan/tap.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 {
35 char *filter;
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;
45 } sipstat_t;
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 */
54 sipstat_t *sp;
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 */
60 uint32_t packets;
61 sipstat_t *sp;
62 } sip_request_method_t;
65 /* Create tables for responses and requests */
66 static void
67 sip_init_hash(sipstat_t *sp)
69 int i;
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;
80 sc->packets = 0;
81 sc->response_code = *key;
82 sc->name = sip_response_code_vals[i].strptr;
83 sc->sp = sp;
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);
91 static void
92 sip_draw_hash_requests( char *key _U_, sip_request_method_t *data, char *format)
94 if (data->packets == 0)
95 return;
96 printf( format, data->response, data->packets);
99 static void
100 sip_draw_hash_responses( int *key _U_ , sip_response_code_t *data, char *format)
102 if (data == NULL) {
103 ws_warning("C'est quoi ce borderl key=%d\n", *key);
104 exit(EXIT_FAILURE);
106 if (data->packets == 0)
107 return;
108 printf(format, data->response_code, data->name, data->packets );
111 /* NOT USED at this moment */
113 static void
114 sip_free_hash( void *key, void *value, void *user_data _U_ )
116 g_free(key);
117 g_free(value);
121 static void
122 sip_reset_hash_responses(char *key _U_ , sip_response_code_t *data, void *ptr _U_ )
124 data->packets = 0;
126 static void
127 sip_reset_hash_requests(char *key _U_ , sip_request_method_t *data, void *ptr _U_ )
129 data->packets = 0;
132 static void
133 sipstat_reset(void *psp )
135 sipstat_t *sp = (sipstat_t *)psp;
136 if (sp) {
137 sp->packets = 0;
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 */
159 sp->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;
170 }else{
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 */
184 if (value->resend)
186 sp->resent_packets++;
190 /* Looking at both requests and responses */
191 if (value->response_code != 0)
193 /* Responses */
194 unsigned key;
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);
200 if (sc == NULL)
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;
212 else if (i < 200)
214 key = 199; /* Hopefully, this status code will never be used */
216 else if (i < 300)
218 key = 299;
220 else if (i < 400)
222 key = 399;
224 else if (i < 500)
226 key = 499;
228 else if (i < 600)
230 key = 599;
232 else
234 key = 699;
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);
239 if (sc == NULL)
241 return TAP_PACKET_DONT_REDRAW;
244 sc->packets++;
246 else if (value->request_method)
248 /* Requests */
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);
253 if (sc == NULL)
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);
258 sc->packets = 1;
259 sc->sp = sp;
260 /* Insert it into request table */
261 g_hash_table_insert(sp->hash_requests, sc->response, sc);
263 else
265 /* Already existed, just update count for that method */
266 sc->packets++;
268 /* g_free(value->request_method); */
270 else
272 /* No request method set. Just ignore */
273 return TAP_PACKET_DONT_REDRAW;
276 return TAP_PACKET_REDRAW;
279 static void
280 sipstat_draw(void *psp )
282 sipstat_t *sp = (sipstat_t *)psp;
283 printf("\n");
284 printf("===================================================================\n");
285 if (sp->filter == NULL)
286 printf("SIP Statistics\n");
287 else
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");
302 static void
303 sipstat_init(const char *opt_arg, void *userdata _U_)
305 sipstat_t *sp;
306 const char *filter = NULL;
307 GString *error_string;
309 if (strncmp (opt_arg, "sip,stat,", 9) == 0) {
310 filter = opt_arg+9;
311 } else {
312 filter = NULL;
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(
321 "sip",
323 filter,
325 sipstat_reset,
326 sipstat_packet,
327 sipstat_draw,
328 NULL);
329 if (error_string) {
330 /* error, we failed to attach to the tap. clean up */
331 g_free(sp->filter);
332 g_free(sp);
333 cmdarg_err("Couldn't register sip,stat tap: %s",
334 error_string->str);
335 g_string_free(error_string, TRUE);
336 exit(1);
339 sp->packets = 0;
340 sp->resent_packets = 0;
341 sip_init_hash(sp);
344 static stat_tap_ui sipstat_ui = {
345 REGISTER_STAT_GROUP_GENERIC,
346 NULL,
347 "sip,stat",
348 sipstat_init,
350 NULL
353 void
354 register_tap_listener_sipstat(void)
356 register_stat_tap_ui(&sipstat_ui, NULL);
360 * Editor modelines - https://www.wireshark.org/tools/modelines.html
362 * Local variables:
363 * c-basic-offset: 8
364 * tab-width: 8
365 * indent-tabs-mode: t
366 * End:
368 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
369 * :indentSize=8:tabSize=8:noTabs=false: