-B is no longer Windows-only.
[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  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
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/stat_cmd_args.h>
46 #include <epan/dissectors/packet-bssap.h>
47 #include <epan/dissectors/packet-gsm_a_common.h>
48 #include "register.h"
49
50
51 typedef struct _gsm_a_stat_t {
52     int         bssmap_message_type[0xff];
53     int         dtap_mm_message_type[0xff];
54     int         dtap_rr_message_type[0xff];
55     int         dtap_cc_message_type[0xff];
56     int         dtap_gmm_message_type[0xff];
57     int         dtap_sms_message_type[0xff];
58     int         dtap_sm_message_type[0xff];
59     int         dtap_ss_message_type[0xff];
60     int         dtap_tp_message_type[0xff];
61     int         sacch_rr_message_type[0xff];
62 } gsm_a_stat_t;
63
64
65 static int
66 gsm_a_stat_packet(
67     void                        *tapdata,
68     packet_info                 *pinfo _U_,
69     epan_dissect_t              *edt _U_,
70     const void                  *data)
71 {
72     gsm_a_stat_t                *stat_p = tapdata;
73     const gsm_a_tap_rec_t       *tap_p = data;
74
75     switch (tap_p->pdu_type)
76     {
77     case BSSAP_PDU_TYPE_BSSMAP:
78         stat_p->bssmap_message_type[tap_p->message_type]++;
79         break;
80
81     case BSSAP_PDU_TYPE_DTAP:
82         switch (tap_p->protocol_disc)
83         {
84         case PD_CC:
85             stat_p->dtap_cc_message_type[tap_p->message_type]++;
86             break;
87         case PD_MM:
88             stat_p->dtap_mm_message_type[tap_p->message_type]++;
89             break;
90         case PD_RR:
91             stat_p->dtap_rr_message_type[tap_p->message_type]++;
92             break;
93         case PD_GMM:
94             stat_p->dtap_gmm_message_type[tap_p->message_type]++;
95             break;
96         case PD_SMS:
97             stat_p->dtap_sms_message_type[tap_p->message_type]++;
98             break;
99         case PD_SM:
100             stat_p->dtap_sm_message_type[tap_p->message_type]++;
101             break;
102         case PD_SS:
103             stat_p->dtap_ss_message_type[tap_p->message_type]++;
104             break;
105         case PD_TP:
106             stat_p->dtap_tp_message_type[tap_p->message_type]++;
107             break;
108         default:
109             /*
110              * unsupported PD
111              */
112             return(0);
113         }
114         break;
115
116    case GSM_A_PDU_TYPE_SACCH:
117    switch (tap_p->protocol_disc)
118    {
119    case 0:
120       stat_p->sacch_rr_message_type[tap_p->message_type]++;
121       break;
122    default:
123       /* unknown Short PD */
124       break;
125    }
126    break;
127
128
129     default:
130         /*
131          * unknown PDU type !!!
132          */
133         return(0);
134     }
135
136     return(1);
137 }
138
139
140 static void
141 gsm_a_stat_draw(
142     void                *tapdata)
143 {
144     gsm_a_stat_t        *stat_p = tapdata;
145     guint8              i;
146
147
148     printf("\n");
149     printf("=========== GS=M A-i/f Statistics ============================\n");
150     printf("BSSMAP\n");
151     printf("Message (ID)Type                                        Number\n");
152
153     i = 0;
154     while (gsm_a_bssmap_msg_strings[i].strptr)
155     {
156         if (stat_p->bssmap_message_type[gsm_a_bssmap_msg_strings[i].value] > 0)
157         {
158             printf("0x%02x  %-50s%d\n",
159                 gsm_a_bssmap_msg_strings[i].value,
160                 gsm_a_bssmap_msg_strings[i].strptr,
161                 stat_p->bssmap_message_type[gsm_a_bssmap_msg_strings[i].value]);
162         }
163
164         i++;
165     }
166
167     printf("\nDTAP %s\n", gsm_a_pd_str[PD_MM]);
168     printf("Message (ID)Type                                        Number\n");
169
170     i = 0;
171     while (gsm_a_dtap_msg_mm_strings[i].strptr)
172     {
173         if (stat_p->dtap_mm_message_type[gsm_a_dtap_msg_mm_strings[i].value] > 0)
174         {
175             printf("0x%02x  %-50s%d\n",
176                 gsm_a_dtap_msg_mm_strings[i].value,
177                 gsm_a_dtap_msg_mm_strings[i].strptr,
178                 stat_p->dtap_mm_message_type[gsm_a_dtap_msg_mm_strings[i].value]);
179         }
180
181         i++;
182     }
183
184     printf("\nDTAP %s\n", gsm_a_pd_str[PD_RR]);
185     printf("Message (ID)Type                                        Number\n");
186
187     i = 0;
188     while (gsm_a_dtap_msg_rr_strings[i].strptr)
189     {
190         if (stat_p->dtap_rr_message_type[gsm_a_dtap_msg_rr_strings[i].value] > 0)
191         {
192             printf("0x%02x  %-50s%d\n",
193                 gsm_a_dtap_msg_rr_strings[i].value,
194                 gsm_a_dtap_msg_rr_strings[i].strptr,
195                 stat_p->dtap_rr_message_type[gsm_a_dtap_msg_rr_strings[i].value]);
196         }
197
198         i++;
199     }
200
201     printf("\nDTAP %s\n", gsm_a_pd_str[PD_CC]);
202     printf("Message (ID)Type                                        Number\n");
203
204     i = 0;
205     while (gsm_a_dtap_msg_cc_strings[i].strptr)
206     {
207         if (stat_p->dtap_cc_message_type[gsm_a_dtap_msg_cc_strings[i].value] > 0)
208         {
209             printf("0x%02x  %-50s%d\n",
210                 gsm_a_dtap_msg_cc_strings[i].value,
211                 gsm_a_dtap_msg_cc_strings[i].strptr,
212                 stat_p->dtap_cc_message_type[gsm_a_dtap_msg_cc_strings[i].value]);
213         }
214
215         i++;
216     }
217
218     printf("\nDTAP %s\n", gsm_a_pd_str[PD_GMM]);
219     printf("Message (ID)Type                                        Number\n");
220
221     i = 0;
222     while (gsm_a_dtap_msg_gmm_strings[i].strptr)
223     {
224         if (stat_p->dtap_gmm_message_type[gsm_a_dtap_msg_gmm_strings[i].value] > 0)
225         {
226             printf("0x%02x  %-50s%d\n",
227                 gsm_a_dtap_msg_gmm_strings[i].value,
228                 gsm_a_dtap_msg_gmm_strings[i].strptr,
229                 stat_p->dtap_gmm_message_type[gsm_a_dtap_msg_gmm_strings[i].value]);
230         }
231
232         i++;
233     }
234
235     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SMS]);
236     printf("Message (ID)Type                                        Number\n");
237
238     i = 0;
239     while (gsm_a_dtap_msg_sms_strings[i].strptr)
240     {
241         if (stat_p->dtap_sms_message_type[gsm_a_dtap_msg_sms_strings[i].value] > 0)
242         {
243             printf("0x%02x  %-50s%d\n",
244                 gsm_a_dtap_msg_sms_strings[i].value,
245                 gsm_a_dtap_msg_sms_strings[i].strptr,
246                 stat_p->dtap_sms_message_type[gsm_a_dtap_msg_sms_strings[i].value]);
247         }
248
249         i++;
250     }
251
252     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SM]);
253     printf("Message (ID)Type                                        Number\n");
254
255     i = 0;
256     while (gsm_a_dtap_msg_sm_strings[i].strptr)
257     {
258         if (stat_p->dtap_sm_message_type[gsm_a_dtap_msg_sm_strings[i].value] > 0)
259         {
260             printf("0x%02x  %-50s%d\n",
261                 gsm_a_dtap_msg_sm_strings[i].value,
262                 gsm_a_dtap_msg_sm_strings[i].strptr,
263                 stat_p->dtap_sm_message_type[gsm_a_dtap_msg_sm_strings[i].value]);
264         }
265
266         i++;
267     }
268
269     printf("\nDTAP %s\n", gsm_a_pd_str[PD_SS]);
270     printf("Message (ID)Type                                        Number\n");
271
272     i = 0;
273     while (gsm_a_dtap_msg_ss_strings[i].strptr)
274     {
275         if (stat_p->dtap_ss_message_type[gsm_a_dtap_msg_ss_strings[i].value] > 0)
276         {
277             printf("0x%02x  %-50s%d\n",
278                 gsm_a_dtap_msg_ss_strings[i].value,
279                 gsm_a_dtap_msg_ss_strings[i].strptr,
280                 stat_p->dtap_ss_message_type[gsm_a_dtap_msg_ss_strings[i].value]);
281         }
282
283         i++;
284     }
285
286     printf("\nDTAP %s\n", gsm_a_pd_str[PD_TP]);
287     printf("Message (ID)Type                                        Number\n");
288
289     i = 0;
290     while (gsm_a_dtap_msg_tp_strings[i].strptr)
291     {
292         if (stat_p->dtap_tp_message_type[gsm_a_dtap_msg_tp_strings[i].value] > 0)
293         {
294             printf("0x%02x  %-50s%d\n",
295                 gsm_a_dtap_msg_tp_strings[i].value,
296                 gsm_a_dtap_msg_tp_strings[i].strptr,
297                 stat_p->dtap_tp_message_type[gsm_a_dtap_msg_tp_strings[i].value]);
298         }
299
300         i++;
301     }
302
303     printf("\nSACCH Radio Resources Management messages\n");
304     printf("Message (ID)Type                                        Number\n");
305
306     i = 0;
307     while (gsm_a_sacch_msg_rr_strings[i].strptr)
308     {
309         if (stat_p->sacch_rr_message_type[gsm_a_sacch_msg_rr_strings[i].value] > 0)
310         {
311             printf("0x%02x  %-50s%d\n",
312                 gsm_a_sacch_msg_rr_strings[i].value,
313                 gsm_a_sacch_msg_rr_strings[i].strptr,
314                 stat_p->sacch_rr_message_type[gsm_a_sacch_msg_rr_strings[i].value]);
315         }
316
317         i++;
318     }
319
320     printf("==============================================================\n");
321 }
322
323
324 static void
325 gsm_a_stat_init(const char *optarg _U_,void* userdata _U_)
326 {
327     gsm_a_stat_t        *stat_p;
328     GString             *err_p;
329
330     stat_p = g_malloc(sizeof(gsm_a_stat_t));
331
332     memset(stat_p, 0, sizeof(gsm_a_stat_t));
333
334     err_p =
335         register_tap_listener("gsm_a", stat_p, NULL, 0,
336             NULL,
337             gsm_a_stat_packet,
338             gsm_a_stat_draw);
339
340     if (err_p != NULL)
341     {
342         g_free(stat_p);
343         g_string_free(err_p, TRUE);
344
345         exit(1);
346     }
347 }
348
349
350 void
351 register_tap_listener_gsm_astat(void)
352 {
353     register_stat_cmd_arg("gsm_a,", gsm_a_stat_init,NULL);
354 }