Update documentation information.
[obnox/wireshark/wip.git] / tap-mgcpstat.c
1 /* tap-mgcpstat.c
2  * mgcpstat   2003 Lars Roland
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/packet_info.h"
37 #include <epan/tap.h>
38 #include <epan/stat_cmd_args.h>
39 #include "epan/value_string.h"
40 #include "register.h"
41 #include "epan/dissectors/packet-mgcp.h"
42 #include "timestats.h"
43
44 #ifdef NEED_G_ASCII_STRCASECMP_H
45 #include "epan/g_ascii_strcasecmp.h"
46 #endif
47
48 #define NUM_TIMESTATS 11
49
50 /* used to keep track of the statistics for an entire program interface */
51 typedef struct _mgcpstat_t {
52         char *filter;
53         timestat_t rtd[NUM_TIMESTATS];
54         guint32 open_req_num;
55         guint32 disc_rsp_num;
56         guint32 req_dup_num;
57         guint32 rsp_dup_num;
58 } mgcpstat_t;
59
60 static const value_string mgcp_mesage_type[] = {
61   {  0, "Overall"},
62   {  1, "EPCF   "},
63   {  2, "CRCX   "},
64   {  3, "MDCX   "},
65   {  4, "DLCX   "},
66   {  5, "RQNT   "},
67   {  6, "NTFY   "},
68   {  7, "AUEP   "},
69   {  8, "AUCX   "},
70   {  9, "RSIP   "},
71 };
72
73 static int
74 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pmi)
75 {
76         mgcpstat_t *ms=(mgcpstat_t *)pms;
77         const mgcp_info_t *mi=pmi;
78         nstime_t delta;
79         int ret = 0;
80
81         switch (mi->mgcp_type) {
82
83         case MGCP_REQUEST:
84                 if(mi->is_duplicate){
85                         /* Duplicate is ignored */
86                         ms->req_dup_num++;
87                 }
88                 else {
89                         ms->open_req_num++;
90                 }
91                 break;
92
93         case MGCP_RESPONSE:
94                 if(mi->is_duplicate){
95                         /* Duplicate is ignored */
96                         ms->rsp_dup_num++;
97                 }
98                 else if (!mi->request_available) {
99                         /* no request was seen */
100                         ms->disc_rsp_num++;
101                 }
102                 else {
103                         ms->open_req_num--;
104                         /* calculate time delta between request and response */
105                         nstime_delta(&delta, &pinfo->fd->abs_ts, &mi->req_time);
106
107                         time_stat_update(&(ms->rtd[0]),&delta, pinfo);
108
109                         if (g_ascii_strncasecmp(mi->code, "EPCF", 4) == 0 ) {
110                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
111                         }
112                         else if (g_ascii_strncasecmp(mi->code, "CRCX", 4) == 0 ) {
113                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
114                         }
115                         else if (g_ascii_strncasecmp(mi->code, "MDCX", 4) == 0 ) {
116                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
117                         }
118                         else if (g_ascii_strncasecmp(mi->code, "DLCX", 4) == 0 ) {
119                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
120                         }
121                         else if (g_ascii_strncasecmp(mi->code, "RQNT", 4) == 0 ) {
122                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
123                         }
124                         else if (g_ascii_strncasecmp(mi->code, "NTFY", 4) == 0 ) {
125                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
126                         }
127                         else if (g_ascii_strncasecmp(mi->code, "AUEP", 4) == 0 ) {
128                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
129                         }
130                         else if (g_ascii_strncasecmp(mi->code, "AUCX", 4) == 0 ) {
131                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
132                         }
133                         else if (g_ascii_strncasecmp(mi->code, "RSIP", 4) == 0 ) {
134                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
135                         }
136                         else {
137                                 time_stat_update(&(ms->rtd[10]),&delta, pinfo);
138                         }
139
140                         ret = 1;
141                 }
142                 break;
143
144         default:
145                 break;
146         }
147
148         return ret;
149 }
150
151 static void
152 mgcpstat_draw(void *pms)
153 {
154         mgcpstat_t *ms=(mgcpstat_t *)pms;
155         int i;
156
157         /* printing results */
158         printf("\n");
159         printf("=====================================================================================================\n");
160         printf("MGCP Response Time Delay (RTD) Statistics:\n");
161         printf("Filter for statistics: %s\n",ms->filter?ms->filter:"");
162         printf("Duplicate requests: %u\n",ms->req_dup_num);
163         printf("Duplicate responses: %u\n",ms->rsp_dup_num);
164         printf("Open requests: %u\n",ms->open_req_num);
165         printf("Discarded responses: %u\n",ms->disc_rsp_num);
166         printf("Type    | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
167         for(i=0;i<NUM_TIMESTATS;i++) {
168                 if(ms->rtd[i].num) {
169                         printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
170                                 val_to_str(i,mgcp_mesage_type,"Other  "),ms->rtd[i].num,
171                                 nstime_to_msec(&(ms->rtd[i].min)), nstime_to_msec(&(ms->rtd[i].max)),
172                                 get_average(&(ms->rtd[i].tot), ms->rtd[i].num),
173                                 ms->rtd[i].min_num, ms->rtd[i].max_num
174                         );
175                 }
176         }
177         printf("=====================================================================================================\n");
178 }
179
180
181 static void
182 mgcpstat_init(const char *optarg, void* userdata _U_)
183 {
184         mgcpstat_t *ms;
185         int i;
186         const char *filter=NULL;
187         GString *error_string;
188
189         if(!strncmp(optarg,"mgcp,rtd,",9)){
190                 filter=optarg+9;
191         } else {
192                 filter="";
193         }
194
195         ms=g_malloc(sizeof(mgcpstat_t));
196         ms->filter=g_strdup(filter);
197
198         for(i=0;i<NUM_TIMESTATS;i++) {
199                 ms->rtd[i].num=0;
200                 ms->rtd[i].min_num=0;
201                 ms->rtd[i].max_num=0;
202                 ms->rtd[i].min.secs=0;
203                 ms->rtd[i].min.nsecs=0;
204                 ms->rtd[i].max.secs=0;
205                 ms->rtd[i].max.nsecs=0;
206                 ms->rtd[i].tot.secs=0;
207                 ms->rtd[i].tot.nsecs=0;
208         }
209
210         ms->open_req_num=0;
211         ms->disc_rsp_num=0;
212         ms->req_dup_num=0;
213         ms->rsp_dup_num=0;
214
215         error_string=register_tap_listener("mgcp", ms, filter, NULL, mgcpstat_packet, mgcpstat_draw);
216         if(error_string){
217                 /* error, we failed to attach to the tap. clean up */
218                 g_free(ms->filter);
219                 g_free(ms);
220
221                 fprintf(stderr, "tshark: Couldn't register mgcp,rtd tap: %s\n",
222                     error_string->str);
223                 g_string_free(error_string, TRUE);
224                 exit(1);
225         }
226 }
227
228
229 void
230 register_tap_listener_mgcpstat(void)
231 {
232         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
233         if (find_tap_id("mgcp")) {
234                 register_stat_cmd_arg("mgcp,rtd", mgcpstat_init, NULL);
235         }
236 }
237