Get rid of -Wshadow warning - I guess we're including something that
[metze/wireshark/wip.git] / ui / cli / tap-bootpstat.c
1 /* tap-bootpstat.c
2  * boop_stat   2003 Jean-Michel FAYARD
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "epan/packet_info.h"
31 #include <epan/tap.h>
32 #include <epan/stat_cmd_args.h>
33
34
35 typedef const char* bootp_info_value_t;
36
37 /* used to keep track of the statictics for an entire program interface */
38 typedef struct _dhcp_stats_t {
39         char            *filter;
40         GHashTable      *hash;
41         guint            index; /* Number of  to display */
42 } dhcpstat_t;
43 /* used to keep track of a single DHCP message type */
44 typedef struct _dhcp_message_type_t {
45         const char      *name;
46         guint32          packets;
47         dhcpstat_t      *sp;    /* entire program interface */
48 } dhcp_message_type_t;
49
50
51 /* Not used anywhere at this moment */
52 /*
53 static void
54 dhcp_free_hash( gpointer key _U_ , gpointer value, gpointer user_data _U_ )
55 {
56         g_free(value);
57 }
58 */
59
60 static void
61 dhcp_reset_hash(gchar *key _U_ , dhcp_message_type_t *data, gpointer ptr _U_ )
62 {
63         data->packets = 0;
64 }
65
66 /* Update the entry corresponding to the number of packets of a special DHCP Message Type
67  * or create it if it don't exist.
68  */
69 static void
70 dhcp_draw_message_type(gchar *key _U_, dhcp_message_type_t *data, gchar * format )
71 {
72         if ((data==NULL) || (data->packets==0))
73                 return;
74         printf( format, data->name, data->packets);
75 }
76 static void
77 dhcpstat_reset(void *psp)
78 {
79         dhcpstat_t *sp=(dhcpstat_t *)psp;
80         g_hash_table_foreach( sp->hash, (GHFunc)dhcp_reset_hash, NULL);
81 }
82 static int
83 dhcpstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
84 {
85         dhcpstat_t *sp=(dhcpstat_t *)psp;
86         const bootp_info_value_t value=(const bootp_info_value_t)pri;
87         dhcp_message_type_t *sc;
88
89         if (sp==NULL)
90                 return 0;
91         sc = (dhcp_message_type_t *)g_hash_table_lookup(
92                         sp->hash,
93                         value);
94         if (!sc) {
95                 sc = g_new(dhcp_message_type_t,1);
96                 sc -> packets = 1;
97                 sc -> name = value;
98                 sc -> sp = sp;
99                 g_hash_table_insert(
100                                 sp->hash,
101                                 (gpointer) value,
102                                 sc);
103         } else {
104                 /*g_warning("sc(%s)->packets++", sc->name);*/
105                 sc->packets++;
106         }
107         return 1;
108 }
109
110
111 static void
112 dhcpstat_draw(void *psp)
113 {
114         dhcpstat_t *sp=(dhcpstat_t *)psp;
115
116         printf("\n");
117         printf("===================================================================\n");
118
119         if (sp->filter==NULL)
120                 printf("BOOTP Statistics\n");
121         else
122                 printf("BOOTP Statistics with filter %s\n",  sp->filter);
123         printf("BOOTP Option 53: DHCP Messages Types:\n");
124         printf("DHCP Message Type      Packets nb\n" );
125         g_hash_table_foreach( sp->hash, (GHFunc) dhcp_draw_message_type,
126                         (gpointer)"%23s %-9d\n" );
127         printf("===================================================================\n");
128
129 }
130
131
132
133
134 /* When called, this function will create a new instance of tap-boopstat.
135  */
136 static void
137 dhcpstat_init(const char *opt_arg, void* userdata _U_)
138 {
139         dhcpstat_t *sp;
140         const char      *filter=NULL;
141         GString         *error_string;
142
143         if (!strncmp (opt_arg, "bootp,stat,", 11)){
144                 filter=opt_arg+11;
145         } else {
146                 filter=NULL;
147         }
148
149         sp = g_new(dhcpstat_t,1);
150         sp->hash = g_hash_table_new( g_str_hash, g_str_equal);
151         if(filter){
152                 sp->filter=g_strdup(filter);
153         } else {
154                 sp->filter=NULL;
155         }
156         sp->index = 0;          /* Nothing to display yet */
157
158         error_string = register_tap_listener(
159                         "bootp",
160                         sp,
161                         filter,
162                         0,
163                         dhcpstat_reset,
164                         dhcpstat_packet,
165                         dhcpstat_draw);
166         if (error_string){
167                 /* error, we failed to attach to the tap. clean up */
168                 g_free(sp->filter);
169                 g_free(sp);
170                 fprintf(stderr, "tshark: Couldn't register dhcp,stat tap: %s\n",
171                                 error_string->str);
172                 g_string_free(error_string, TRUE);
173                 exit(1);
174         }
175 }
176
177
178
179 void
180 register_tap_listener_gtkdhcpstat(void)
181 {
182         register_stat_cmd_arg("bootp,stat,", dhcpstat_init,NULL);
183 }
184