Fix mask when extracting FACH Indicator.
[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 "epan/dissectors/packet-mgcp.h"
41 #include "timestats.h"
42
43 #define NUM_TIMESTATS 11
44
45 /* used to keep track of the statistics for an entire program interface */
46 typedef struct _mgcpstat_t {
47         char *filter;
48         timestat_t rtd[NUM_TIMESTATS];
49         guint32 open_req_num;
50         guint32 disc_rsp_num;
51         guint32 req_dup_num;
52         guint32 rsp_dup_num;
53 } mgcpstat_t;
54
55 static const value_string mgcp_mesage_type[] = {
56   {  0, "Overall"},
57   {  1, "EPCF   "},
58   {  2, "CRCX   "},
59   {  3, "MDCX   "},
60   {  4, "DLCX   "},
61   {  5, "RQNT   "},
62   {  6, "NTFY   "},
63   {  7, "AUEP   "},
64   {  8, "AUCX   "},
65   {  9, "RSIP   "},
66   {  0, NULL}
67 };
68
69 static int
70 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pmi)
71 {
72         mgcpstat_t *ms=(mgcpstat_t *)pms;
73         const mgcp_info_t *mi=pmi;
74         nstime_t delta;
75         int ret = 0;
76
77         switch (mi->mgcp_type) {
78
79         case MGCP_REQUEST:
80                 if(mi->is_duplicate){
81                         /* Duplicate is ignored */
82                         ms->req_dup_num++;
83                 }
84                 else {
85                         ms->open_req_num++;
86                 }
87                 break;
88
89         case MGCP_RESPONSE:
90                 if(mi->is_duplicate){
91                         /* Duplicate is ignored */
92                         ms->rsp_dup_num++;
93                 }
94                 else if (!mi->request_available) {
95                         /* no request was seen */
96                         ms->disc_rsp_num++;
97                 }
98                 else {
99                         ms->open_req_num--;
100                         /* calculate time delta between request and response */
101                         nstime_delta(&delta, &pinfo->fd->abs_ts, &mi->req_time);
102
103                         time_stat_update(&(ms->rtd[0]),&delta, pinfo);
104
105                         if (g_ascii_strncasecmp(mi->code, "EPCF", 4) == 0 ) {
106                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
107                         }
108                         else if (g_ascii_strncasecmp(mi->code, "CRCX", 4) == 0 ) {
109                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
110                         }
111                         else if (g_ascii_strncasecmp(mi->code, "MDCX", 4) == 0 ) {
112                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
113                         }
114                         else if (g_ascii_strncasecmp(mi->code, "DLCX", 4) == 0 ) {
115                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
116                         }
117                         else if (g_ascii_strncasecmp(mi->code, "RQNT", 4) == 0 ) {
118                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
119                         }
120                         else if (g_ascii_strncasecmp(mi->code, "NTFY", 4) == 0 ) {
121                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
122                         }
123                         else if (g_ascii_strncasecmp(mi->code, "AUEP", 4) == 0 ) {
124                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
125                         }
126                         else if (g_ascii_strncasecmp(mi->code, "AUCX", 4) == 0 ) {
127                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
128                         }
129                         else if (g_ascii_strncasecmp(mi->code, "RSIP", 4) == 0 ) {
130                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
131                         }
132                         else {
133                                 time_stat_update(&(ms->rtd[10]),&delta, pinfo);
134                         }
135
136                         ret = 1;
137                 }
138                 break;
139
140         default:
141                 break;
142         }
143
144         return ret;
145 }
146
147 static void
148 mgcpstat_draw(void *pms)
149 {
150         mgcpstat_t *ms=(mgcpstat_t *)pms;
151         int i;
152
153         /* printing results */
154         printf("\n");
155         printf("=====================================================================================================\n");
156         printf("MGCP Response Time Delay (RTD) Statistics:\n");
157         printf("Filter for statistics: %s\n",ms->filter?ms->filter:"");
158         printf("Duplicate requests: %u\n",ms->req_dup_num);
159         printf("Duplicate responses: %u\n",ms->rsp_dup_num);
160         printf("Open requests: %u\n",ms->open_req_num);
161         printf("Discarded responses: %u\n",ms->disc_rsp_num);
162         printf("Type    | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
163         for(i=0;i<NUM_TIMESTATS;i++) {
164                 if(ms->rtd[i].num) {
165                         printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
166                                 val_to_str(i,mgcp_mesage_type,"Other  "),ms->rtd[i].num,
167                                 nstime_to_msec(&(ms->rtd[i].min)), nstime_to_msec(&(ms->rtd[i].max)),
168                                 get_average(&(ms->rtd[i].tot), ms->rtd[i].num),
169                                 ms->rtd[i].min_num, ms->rtd[i].max_num
170                         );
171                 }
172         }
173         printf("=====================================================================================================\n");
174 }
175
176
177 static void
178 mgcpstat_init(const char *optarg, void* userdata _U_)
179 {
180         mgcpstat_t *ms;
181         int i;
182         GString *error_string;
183
184         ms=g_malloc(sizeof(mgcpstat_t));
185         if(!strncmp(optarg,"mgcp,rtd,",9)){
186                 ms->filter=g_strdup(optarg+9);
187         } else {
188                 ms->filter=NULL;
189         }
190
191         for(i=0;i<NUM_TIMESTATS;i++) {
192                 ms->rtd[i].num=0;
193                 ms->rtd[i].min_num=0;
194                 ms->rtd[i].max_num=0;
195                 ms->rtd[i].min.secs=0;
196                 ms->rtd[i].min.nsecs=0;
197                 ms->rtd[i].max.secs=0;
198                 ms->rtd[i].max.nsecs=0;
199                 ms->rtd[i].tot.secs=0;
200                 ms->rtd[i].tot.nsecs=0;
201         }
202
203         ms->open_req_num=0;
204         ms->disc_rsp_num=0;
205         ms->req_dup_num=0;
206         ms->rsp_dup_num=0;
207
208         error_string=register_tap_listener("mgcp", ms, ms->filter, 0, NULL, mgcpstat_packet, mgcpstat_draw);
209         if(error_string){
210                 /* error, we failed to attach to the tap. clean up */
211                 g_free(ms->filter);
212                 g_free(ms);
213
214                 fprintf(stderr, "tshark: Couldn't register mgcp,rtd tap: %s\n",
215                     error_string->str);
216                 g_string_free(error_string, TRUE);
217                 exit(1);
218         }
219 }
220
221
222 void
223 register_tap_listener_mgcpstat(void)
224 {
225         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
226         if (find_tap_id("mgcp")) {
227                 register_stat_cmd_arg("mgcp,rtd", mgcpstat_init, NULL);
228         }
229 }
230