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