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