Squelch more const warnings (and fix some memory leaks that found).
[obnox/wireshark/wip.git] / tap-gsm_astat.c
1 /* tap-gsm_astat.c
2  *
3  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 /*
28  * This TAP provides statistics for the GSM A Interface:
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #include <string.h>
42 #include "epan/packet_info.h"
43 #include "epan/value_string.h"
44 #include <epan/tap.h>
45 #include <epan/dissectors/packet-bssap.h>
46 #include <epan/dissectors/packet-gsm_a.h>
47 #include "register.h"
48
49
50 typedef struct _gsm_a_stat_t {
51     int         bssmap_message_type[0xff];
52     int         dtap_mm_message_type[0xff];
53     int         dtap_rr_message_type[0xff];
54     int         dtap_cc_message_type[0xff];
55     int         dtap_gmm_message_type[0xff];
56     int         dtap_sms_message_type[0xff];
57     int         dtap_sm_message_type[0xff];
58     int         dtap_ss_message_type[0xff];
59 } gsm_a_stat_t;
60
61
62 static int
63 gsm_a_stat_packet(
64     void                        *tapdata,
65     packet_info                 *pinfo _U_,
66     epan_dissect_t              *edt _U_,
67     const void                  *data)
68 {
69     gsm_a_stat_t                *stat_p = tapdata;
70     const gsm_a_tap_rec_t       *tap_p = data;
71
72     switch (tap_p->pdu_type)
73     {
74     case BSSAP_PDU_TYPE_BSSMAP:
75         stat_p->bssmap_message_type[tap_p->message_type]++;
76         break;
77
78     case BSSAP_PDU_TYPE_DTAP:
79         switch (tap_p->protocol_disc)
80         {
81         case PD_CC:
82             stat_p->dtap_cc_message_type[tap_p->message_type]++;
83             break;
84         case PD_MM:
85             stat_p->dtap_mm_message_type[tap_p->message_type]++;
86             break;
87         case PD_RR:
88             stat_p->dtap_rr_message_type[tap_p->message_type]++;
89             break;
90         case PD_GMM:
91             stat_p->dtap_gmm_message_type[tap_p->message_type]++;
92             break;
93         case PD_SMS:
94             stat_p->dtap_sms_message_type[tap_p->message_type]++;
95             break;
96         case PD_SM:
97             stat_p->dtap_sm_message_type[tap_p->message_type]++;
98             break;
99         case PD_SS:
100             stat_p->dtap_ss_message_type[tap_p->message_type]++;
101             break;
102         default:
103             /*
104              * unsupported PD
105              */
106             return(0);
107         }
108         break;
109
110     default:
111         /*
112          * unknown PDU type !!!
113          */
114         return(0);
115     }
116
117     return(1);
118 }
119
120
121 static void
122 gsm_a_stat_draw(
123     void                *tapdata)
124 {
125     gsm_a_stat_t        *stat_p = tapdata;
126     guint8              i;
127
128
129     printf("\n");
130     printf("=========== GS=M A-i/f Statistics ============================\n");
131     printf("BSSMAP\n");
132     printf("Message (ID)Type                                        Number\n");
133
134     i = 0;
135     while (gsm_a_bssmap_msg_strings[i].strptr)
136     {
137         if (stat_p->bssmap_message_type[gsm_a_bssmap_msg_strings[i].value] > 0)
138         {
139             printf("0x%02x  %-50s%d\n",
140                 gsm_a_bssmap_msg_strings[i].value,
141                 gsm_a_bssmap_msg_strings[i].strptr,
142                 stat_p->bssmap_message_type[gsm_a_bssmap_msg_strings[i].value]);
143         }
144
145         i++;
146     }
147
148     printf("\nDTAP %s\n", gsm_a_pd_str[PD_MM]);
149     printf("Message (ID)Type                                        Number\n");
150
151     i = 0;
152     while (gsm_a_dtap_msg_mm_strings[i].strptr)
153     {
154         if (stat_p->dtap_mm_message_type[gsm_a_dtap_msg_mm_strings[i].value] > 0)
155         {
156             printf("0x%02x  %-50s%d\n",
157                 gsm_a_dtap_msg_mm_strings[i].value,
158                 gsm_a_dtap_msg_mm_strings[i].strptr,
159                 stat_p->dtap_mm_message_type[gsm_a_dtap_msg_mm_strings[i].value]);
160         }
161
162         i++;
163     }
164
165     printf("\nDTAP %s\n", gsm_a_pd_str[PD_RR]);
166     printf("Message (ID)Type                                        Number\n");
167
168     i = 0;
169     while (gsm_a_dtap_msg_rr_strings[i].strptr)
170     {
171         if (stat_p->dtap_rr_message_type[gsm_a_dtap_msg_rr_strings[i].value] > 0)
172         {
173             printf("0x%02x  %-50s%d\n",
174                 gsm_a_dtap_msg_rr_strings[i].value,
175                 gsm_a_dtap_msg_rr_strings[i].strptr,
176                 stat_p->dtap_rr_message_type[gsm_a_dtap_msg_rr_strings[i].value]);
177         }
178
179         i++;
180     }
181
182     printf("\nDTAP %s\n", gsm_a_pd_str[PD_CC]);
183     printf("Message (ID)Type                                        Number\n");
184
185     i = 0;
186     while (gsm_a_dtap_msg_cc_strings[i].strptr)
187     {
188         if (stat_p->dtap_cc_message_type[gsm_a_dtap_msg_cc_strings[i].value] > 0)
189         {
190             printf("0x%02x  %-50s%d\n",
191                 gsm_a_dtap_msg_cc_strings[i].value,
192                 gsm_a_dtap_msg_cc_strings[i].strptr,
193                 stat_p->dtap_cc_message_type[gsm_a_dtap_msg_cc_strings[i].value]);
194         }
195
196         i++;
197     }
198
199     printf("\nDTAP %s\n", gsm_a_pd_str[PD_GMM]);
200     printf("Message (ID)Type                                        Number\n");
201
202     i = 0;
203     while (gsm_a_dtap_msg_gmm_strings[i].strptr)
204     {
205         if (stat_p->dtap_gmm_message_type[gsm_a_dtap_msg_gmm_strings[i].value] > 0)
206         {
207             printf("0x%02x  %-50s%d\n",
208                 gsm_a_dtap_msg_gmm_strings[i].value,
209                 gsm_a_dtap_msg_gmm_strings[i].strptr,
210                 stat_p->dtap_gmm_message_type[gsm_a_dtap_msg_gmm_strings[i].value]);
211         }
212
213         i++;
214     }
215
216     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SMS]);
217     printf("Message (ID)Type                                        Number\n");
218
219     i = 0;
220     while (gsm_a_dtap_msg_sms_strings[i].strptr)
221     {
222         if (stat_p->dtap_sms_message_type[gsm_a_dtap_msg_sms_strings[i].value] > 0)
223         {
224             printf("0x%02x  %-50s%d\n",
225                 gsm_a_dtap_msg_sms_strings[i].value,
226                 gsm_a_dtap_msg_sms_strings[i].strptr,
227                 stat_p->dtap_sms_message_type[gsm_a_dtap_msg_sms_strings[i].value]);
228         }
229
230         i++;
231     }
232
233     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SM]);
234     printf("Message (ID)Type                                        Number\n");
235
236     i = 0;
237     while (gsm_a_dtap_msg_sm_strings[i].strptr)
238     {
239         if (stat_p->dtap_sm_message_type[gsm_a_dtap_msg_sm_strings[i].value] > 0)
240         {
241             printf("0x%02x  %-50s%d\n",
242                 gsm_a_dtap_msg_sm_strings[i].value,
243                 gsm_a_dtap_msg_sm_strings[i].strptr,
244                 stat_p->dtap_sm_message_type[gsm_a_dtap_msg_sm_strings[i].value]);
245         }
246
247         i++;
248     }
249
250     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SS]);
251     printf("Message (ID)Type                                        Number\n");
252
253     i = 0;
254     while (gsm_a_dtap_msg_ss_strings[i].strptr)
255     {
256         if (stat_p->dtap_ss_message_type[gsm_a_dtap_msg_ss_strings[i].value] > 0)
257         {
258             printf("0x%02x  %-50s%d\n",
259                 gsm_a_dtap_msg_ss_strings[i].value,
260                 gsm_a_dtap_msg_ss_strings[i].strptr,
261                 stat_p->dtap_ss_message_type[gsm_a_dtap_msg_ss_strings[i].value]);
262         }
263
264         i++;
265     }
266
267     printf("==============================================================\n");
268 }
269
270
271 static void
272 gsm_a_stat_init(const char *optarg _U_)
273 {
274     gsm_a_stat_t        *stat_p;
275     GString             *err_p;
276
277     stat_p = g_malloc(sizeof(gsm_a_stat_t));
278
279     memset(stat_p, 0, sizeof(gsm_a_stat_t));
280
281     err_p =
282         register_tap_listener("gsm_a", stat_p, NULL,
283             NULL,
284             gsm_a_stat_packet,
285             gsm_a_stat_draw);
286
287     if (err_p != NULL)
288     {
289         g_free(stat_p);
290         g_string_free(err_p, TRUE);
291
292         exit(1);
293     }
294 }
295
296
297 void
298 register_tap_listener_gsm_astat(void)
299 {
300     register_tap_listener_cmd_arg("gsm_a,", gsm_a_stat_init);
301 }