Fix lots of warnings of type:
[obnox/wireshark/wip.git] / tap-mgcpstat.c
1 /* tap-mgcpstat.c
2  * mgcpstat   2003 Lars Roland
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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/value_string.h"
39 #include "register.h"
40 #include "plugins/mgcp/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 };
67
68 static int
69 mgcpstat_packet(void *pms, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pmi)
70 {
71         mgcpstat_t *ms=(mgcpstat_t *)pms;
72         const mgcp_info_t *mi=pmi;
73         nstime_t delta;
74
75         switch (mi->mgcp_type) {
76
77         case MGCP_REQUEST:
78                 if(mi->is_duplicate){
79                         /* Duplicate is ignored */
80                         ms->req_dup_num++;
81                         return 0;
82                 }
83                 else {
84                         ms->open_req_num++;
85                         return 0;
86                 }
87         break;
88
89         case MGCP_RESPONSE:
90                 if(mi->is_duplicate){
91                         /* Duplicate is ignored */
92                         ms->rsp_dup_num++;
93                         return 0;
94                 }
95                 else if (!mi->request_available) {
96                         /* no request was seen */
97                         ms->disc_rsp_num++;
98                         return 0;
99                 }
100                 else {
101                         ms->open_req_num--;
102                         /* calculate time delta between request and response */
103                         delta.secs=pinfo->fd->abs_secs-mi->req_time.secs;
104                         delta.nsecs=pinfo->fd->abs_usecs*1000-mi->req_time.nsecs;
105                         if(delta.nsecs<0){
106                                 delta.nsecs+=1000000000;
107                                 delta.secs--;
108                         }
109
110                         time_stat_update(&(ms->rtd[0]),&delta, pinfo);
111
112                         if (strncasecmp(mi->code, "EPCF", 4) == 0 ) {
113                                 time_stat_update(&(ms->rtd[1]),&delta, pinfo);
114                         }
115                         else if (strncasecmp(mi->code, "CRCX", 4) == 0 ) {
116                                 time_stat_update(&(ms->rtd[2]),&delta, pinfo);
117                         }
118                         else if (strncasecmp(mi->code, "MDCX", 4) == 0 ) {
119                                 time_stat_update(&(ms->rtd[3]),&delta, pinfo);
120                         }
121                         else if (strncasecmp(mi->code, "DLCX", 4) == 0 ) {
122                                 time_stat_update(&(ms->rtd[4]),&delta, pinfo);
123                         }
124                         else if (strncasecmp(mi->code, "RQNT", 4) == 0 ) {
125                                 time_stat_update(&(ms->rtd[5]),&delta, pinfo);
126                         }
127                         else if (strncasecmp(mi->code, "NTFY", 4) == 0 ) {
128                                 time_stat_update(&(ms->rtd[6]),&delta, pinfo);
129                         }
130                         else if (strncasecmp(mi->code, "AUEP", 4) == 0 ) {
131                                 time_stat_update(&(ms->rtd[7]),&delta, pinfo);
132                         }
133                         else if (strncasecmp(mi->code, "AUCX", 4) == 0 ) {
134                                 time_stat_update(&(ms->rtd[8]),&delta, pinfo);
135                         }
136                         else if (strncasecmp(mi->code, "RSIP", 4) == 0 ) {
137                                 time_stat_update(&(ms->rtd[9]),&delta, pinfo);
138                         }
139                         else {
140                                 time_stat_update(&(ms->rtd[10]),&delta, pinfo);
141                         }
142
143                         return 1;
144                 }
145         break;
146
147         default:
148                 return 0;
149         break;
150         }
151 }
152
153 static void
154 mgcpstat_draw(void *pms)
155 {
156         mgcpstat_t *ms=(mgcpstat_t *)pms;
157         int i;
158
159         /* printing results */
160         printf("\n");
161         printf("=====================================================================================================\n");
162         printf("MGCP Response Time Delay (RTD) Statistics:\n");
163         printf("Filter for statistics: %s\n",ms->filter?ms->filter:"");
164         printf("Duplicate requests: %u\n",ms->req_dup_num);
165         printf("Duplicate responses: %u\n",ms->rsp_dup_num);
166         printf("Open requests: %u\n",ms->open_req_num);
167         printf("Discarded responses: %u\n",ms->disc_rsp_num);
168         printf("Type    | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
169         for(i=0;i<NUM_TIMESTATS;i++) {
170                 if(ms->rtd[i].num) {
171                         printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
172                                 val_to_str(i,mgcp_mesage_type,"Other  "),ms->rtd[i].num,
173                                 nstime_to_msec(&(ms->rtd[i].min)), nstime_to_msec(&(ms->rtd[i].max)),
174                                 get_average(&(ms->rtd[i].tot), ms->rtd[i].num),
175                                 ms->rtd[i].min_num, ms->rtd[i].max_num
176                         );
177                 }
178         }
179         printf("=====================================================================================================\n");
180 }
181
182
183 static void
184 mgcpstat_init(char *optarg)
185 {
186         mgcpstat_t *ms;
187         int i;
188         char *filter=NULL;
189         GString *error_string;
190
191         if(!strncmp(optarg,"mgcp,rtd,",9)){
192                 filter=optarg+9;
193         } else {
194                 filter=g_malloc(1);
195                 *filter='\0';
196         }
197
198         ms=g_malloc(sizeof(mgcpstat_t));
199         ms->filter=g_malloc(strlen(filter)+1);
200         strcpy(ms->filter, filter);
201
202         for(i=0;i<NUM_TIMESTATS;i++) {
203                 ms->rtd[i].num=0;
204                 ms->rtd[i].min_num=0;
205                 ms->rtd[i].max_num=0;
206                 ms->rtd[i].min.secs=0;
207                 ms->rtd[i].min.nsecs=0;
208                 ms->rtd[i].max.secs=0;
209                 ms->rtd[i].max.nsecs=0;
210                 ms->rtd[i].tot.secs=0;
211                 ms->rtd[i].tot.nsecs=0;
212         }
213
214         ms->open_req_num=0;
215         ms->disc_rsp_num=0;
216         ms->req_dup_num=0;
217         ms->rsp_dup_num=0;
218
219         error_string=register_tap_listener("mgcp", ms, filter, NULL, mgcpstat_packet, mgcpstat_draw);
220         if(error_string){
221                 /* error, we failed to attach to the tap. clean up */
222                 g_free(ms->filter);
223                 g_free(ms);
224
225                 fprintf(stderr, "tethereal: Couldn't register mgcp,rtd tap: %s\n",
226                     error_string->str);
227                 g_string_free(error_string, TRUE);
228                 exit(1);
229         }
230 }
231
232
233 void
234 register_tap_listener_mgcpstat(void)
235 {
236         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
237         if (find_tap_id("mgcp")) {
238                 register_tap_listener_cmd_arg("mgcp,rtd", mgcpstat_init);
239         }
240 }
241