From Pascal Quantin:
[obnox/wireshark/wip.git] / tap-megacostat.c
1 /* tap-megacostat.c
2  * mgcpstat   2003 Lars Roland
3  * Copyright 2008, Ericsson AB
4  * By Balint Reczey <balint.reczey@ericsson.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #include <string.h>
38 #include "epan/packet_info.h"
39 #include <epan/tap.h>
40 #include <epan/stat_cmd_args.h>
41 #include "epan/value_string.h"
42 #include "epan/gcp.h"
43 #include "timestats.h"
44 #include <epan/prefs-int.h>
45
46 #include "tap-megaco-common.h"
47
48
49
50 static void
51 megacostat_draw(void *pms)
52 {
53         megacostat_t *ms=(megacostat_t *)pms;
54         int i;
55
56         /* printing results */
57         printf("\n");
58         printf("=====================================================================================================\n");
59         printf("MEGACO Response Time Delay (RTD) Statistics:\n");
60         printf("Filter for statistics: %s\n",ms->filter?ms->filter:"");
61         printf("Duplicate requests: %u\n",ms->req_dup_num);
62         printf("Duplicate responses: %u\n",ms->rsp_dup_num);
63         printf("Open requests: %u\n",ms->open_req_num);
64         printf("Discarded responses: %u\n",ms->disc_rsp_num);
65         printf(" Type   | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
66         for(i=0;i<NUM_TIMESTATS;i++) {
67                 if(ms->rtd[i].num) {
68                         printf("%5s   | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
69                                 val_to_str(i,megaco_message_type,"Other"),ms->rtd[i].num,
70                                 nstime_to_msec(&(ms->rtd[i].min)), nstime_to_msec(&(ms->rtd[i].max)),
71                                 get_average(&(ms->rtd[i].tot), ms->rtd[i].num),
72                                 ms->rtd[i].min_num, ms->rtd[i].max_num
73                         );
74                 }
75         }
76         printf("=====================================================================================================\n");
77 }
78
79
80 static void
81 megacostat_init(const char *optarg, void* userdata _U_)
82 {
83         megacostat_t *ms;
84         int i;
85         GString *error_string;
86         pref_t *megaco_ctx_track,*h248_ctx_track;
87
88         megaco_ctx_track = prefs_find_preference(prefs_find_module("megaco"),"ctx_info");
89         h248_ctx_track = prefs_find_preference(prefs_find_module("h248"),"ctx_info");
90
91         if (!megaco_ctx_track || !h248_ctx_track) {
92                 /* No such preferences */
93                 return;
94         }
95
96         if (!*megaco_ctx_track->varp.boolp || !*h248_ctx_track->varp.boolp) {
97                 printf("Track Context option at Protocols -> MEGACO and Protocols -> H248 preferences\n");
98                 printf("has to be set to true to enable measurement of service response times.\n");
99                 exit(1);
100         }
101
102         ms=g_malloc(sizeof(megacostat_t));
103         if(!strncmp(optarg,"megaco,rtd,",11)){
104                 ms->filter=g_strdup(optarg+11);
105         } else {
106                 ms->filter=NULL;
107         }
108
109         for(i=0;i<NUM_TIMESTATS;i++) {
110                 ms->rtd[i].num=0;
111                 ms->rtd[i].min_num=0;
112                 ms->rtd[i].max_num=0;
113                 ms->rtd[i].min.secs=0;
114                 ms->rtd[i].min.nsecs=0;
115                 ms->rtd[i].max.secs=0;
116                 ms->rtd[i].max.nsecs=0;
117                 ms->rtd[i].tot.secs=0;
118                 ms->rtd[i].tot.nsecs=0;
119         }
120
121         ms->open_req_num=0;
122         ms->disc_rsp_num=0;
123         ms->req_dup_num=0;
124         ms->rsp_dup_num=0;
125
126         error_string=register_tap_listener("megaco", ms, ms->filter, 0, NULL, megacostat_packet, megacostat_draw);
127         if(error_string){
128                 /* error, we failed to attach to the tap. clean up */
129                 g_free(ms->filter);
130                 g_free(ms);
131
132                 fprintf(stderr, "tshark: Couldn't register megaco,rtd tap: %s\n",
133                     error_string->str);
134                 g_string_free(error_string, TRUE);
135                 exit(1);
136         }
137 }
138
139
140 void
141 register_tap_listener_megacostat(void)
142 {
143         /* We don't register this tap, if we don't have the megaco plugin loaded.*/
144         if (find_tap_id("megaco")) {
145                 register_stat_cmd_arg("megaco,rtd", megacostat_init, NULL);
146         }
147 }
148