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