Get rid of -Wshadow warning - I guess we're including something that
[metze/wireshark/wip.git] / ui / cli / tap-protocolinfo.c
1 /* tap-protocolinfo.c
2  * protohierstat   2002 Ronnie Sahlberg
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 /* This module provides Protocol Column Info tap for tshark */
26
27 #include "config.h"
28
29 #include <stdio.h>
30
31 #include <string.h>
32 #include "epan/epan_dissect.h"
33 #include "epan/column-utils.h"
34 #include "epan/proto.h"
35 #include <epan/tap.h>
36 #include <epan/stat_cmd_args.h>
37 #include <epan/strutil.h>
38
39 typedef struct _pci_t {
40         char *filter;
41         int hf_index;
42 } pci_t;
43
44
45 static int
46 protocolinfo_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
47 {
48         pci_t *rs=(pci_t *)prs;
49         GPtrArray *gp;
50         guint i;
51         char *str;
52
53         /*
54          * XXX - there needs to be a way for "protocolinfo_init()" to
55          * find out whether the columns are being generated and, if not,
56          * to report an error and exit, as the whole point of this tap
57          * is to modify the columns, and if the columns aren't being
58          * displayed, that makes this tap somewhat pointless.
59          *
60          * To prevent a crash, we check whether INFO column is writable
61          * and, if not, we report that error and exit.
62          */
63         if (!check_col(pinfo->cinfo, COL_INFO)) {
64                 fprintf(stderr, "tshark: the proto,colinfo tap doesn't work if the INFO column isn't being printed.\n");
65                 exit(1);
66         }
67         gp=proto_get_finfo_ptr_array(edt->tree, rs->hf_index);
68         if(!gp){
69                 return 0;
70         }
71
72         for(i=0;i<gp->len;i++){
73                 str=(char *)proto_construct_match_selected_string((field_info *)gp->pdata[i], NULL);
74                 if(str){
75                         col_append_fstr(pinfo->cinfo, COL_INFO, "  %s",str);
76                 }
77         }
78         return 0;
79 }
80
81
82
83 static void
84 protocolinfo_init(const char *opt_arg, void* userdata _U_)
85 {
86         pci_t *rs;
87         const char *field=NULL;
88         const char *filter=NULL;
89         header_field_info *hfi;
90         GString *error_string;
91
92         if(!strncmp("proto,colinfo,",opt_arg,14)){
93                 filter=opt_arg+14;
94                 field=strchr(filter,',');
95                 if(field){
96                         field+=1;  /* skip the ',' */
97                 }
98         }
99         if(!field){
100                 fprintf(stderr, "tshark: invalid \"-z proto,colinfo,<filter>,<field>\" argument\n");
101                 exit(1);
102         }
103
104         hfi=proto_registrar_get_byname(field);
105         if(!hfi){
106                 fprintf(stderr, "tshark: Field \"%s\" doesn't exist.\n", field);
107                 exit(1);
108         }
109
110         rs=g_new(pci_t,1);
111         rs->hf_index=hfi->id;
112         if((field-filter)>1){
113                 rs->filter=(char *)g_malloc(field-filter);
114                 g_strlcpy(rs->filter,filter,(field-filter));
115         } else {
116                 rs->filter=NULL;
117         }
118
119         error_string=register_tap_listener("frame", rs, rs->filter, TL_REQUIRES_PROTO_TREE, NULL, protocolinfo_packet, NULL);
120         if(error_string){
121                 /* error, we failed to attach to the tap. complain and clean up */
122                 fprintf(stderr, "tshark: Couldn't register proto,colinfo tap: %s\n",
123                     error_string->str);
124                 g_string_free(error_string, TRUE);
125                 g_free(rs->filter);
126                 g_free(rs);
127
128                 exit(1);
129         }
130 }
131
132
133 void
134 register_tap_listener_protocolinfo(void)
135 {
136         register_stat_cmd_arg("proto,colinfo,", protocolinfo_init,NULL);
137 }
138