fix a bug reported by Eric Wedel: Could not save preferences, as "You have not select...
[obnox/wireshark/wip.git] / tap-ansi_astat.c
1 /* tap-ansi_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 ANSI A Interface:
29  */
30
31 /* With MSVC and a libethereal.dll this file needs to import some variables 
32    in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */   
33 #define _NEED_VAR_IMPORT_
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <stdio.h>
40
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44
45 #include <string.h>
46 #include "epan/packet_info.h"
47 #include "epan/value_string.h"
48 #include <epan/tap.h>
49 #include <epan/dissectors/packet-bssap.h>
50 #include <epan/dissectors/packet-ansi_a.h>
51 #include "register.h"
52
53
54 typedef struct _ansi_a_stat_t {
55     int         bsmap_message_type[0xff];
56     int         dtap_message_type[0xff];
57 } ansi_a_stat_t;
58
59
60 static int
61 ansi_a_stat_packet(
62     void                        *tapdata,
63     packet_info                 *pinfo,
64     epan_dissect_t              *edt _U_,
65     void                        *data)
66 {
67     ansi_a_stat_t               *stat_p = tapdata;
68     ansi_a_tap_rec_t            *tap_p = data;
69
70
71     pinfo = pinfo;
72
73     switch (tap_p->pdu_type)
74     {
75     case BSSAP_PDU_TYPE_BSMAP:
76         stat_p->bsmap_message_type[tap_p->message_type]++;
77         break;
78
79     case BSSAP_PDU_TYPE_DTAP:
80         stat_p->dtap_message_type[tap_p->message_type]++;
81         break;
82
83     default:
84         /*
85          * unknown PDU type !!!
86          */
87         return(0);
88     }
89
90     return(1);
91 }
92
93
94 static void
95 ansi_a_stat_draw(
96     void                *tapdata)
97 {
98     ansi_a_stat_t       *stat_p = tapdata;
99     guint8              i;
100
101
102     printf("\n");
103     printf("=========== ANSI A-i/f Statistics ============================\n");
104     printf("BSMAP\n");
105     printf("Message (ID)Type                                        Number\n");
106
107     i = 0;
108     while (ansi_a_ios401_bsmap_strings[i].strptr)
109     {
110         if (stat_p->bsmap_message_type[ansi_a_ios401_bsmap_strings[i].value] > 0)
111         {
112             printf("0x%02x  %-50s%d\n",
113                 ansi_a_ios401_bsmap_strings[i].value,
114                 ansi_a_ios401_bsmap_strings[i].strptr,
115                 stat_p->bsmap_message_type[ansi_a_ios401_bsmap_strings[i].value]);
116         }
117
118         i++;
119     }
120
121     printf("\nDTAP\n");
122     printf("Message (ID)Type                                        Number\n");
123
124     i = 0;
125     while (ansi_a_ios401_dtap_strings[i].strptr)
126     {
127         if (stat_p->dtap_message_type[ansi_a_ios401_dtap_strings[i].value] > 0)
128         {
129             printf("0x%02x  %-50s%d\n",
130                 ansi_a_ios401_dtap_strings[i].value,
131                 ansi_a_ios401_dtap_strings[i].strptr,
132                 stat_p->dtap_message_type[ansi_a_ios401_dtap_strings[i].value]);
133         }
134
135         i++;
136     }
137
138     printf("==============================================================\n");
139 }
140
141
142 static void
143 ansi_a_stat_init(char *optarg)
144 {
145     ansi_a_stat_t       *stat_p;
146     GString             *err_p;
147
148
149     optarg = optarg;
150
151     stat_p = g_malloc(sizeof(ansi_a_stat_t));
152
153     memset(stat_p, 0, sizeof(ansi_a_stat_t));
154
155     err_p =
156         register_tap_listener("ansi_a", stat_p, NULL,
157             NULL,
158             ansi_a_stat_packet,
159             ansi_a_stat_draw);
160
161     if (err_p != NULL)
162     {
163         g_free(stat_p);
164         g_string_free(err_p, TRUE);
165
166         exit(1);
167     }
168 }
169
170
171 void
172 register_tap_listener_ansi_astat(void)
173 {
174     register_ethereal_tap("ansi_a,", ansi_a_stat_init);
175 }