c2025a3ae8fe931b44845e7ceaa93fab04493ae9
[obnox/wireshark/wip.git] / tap-protocolinfo.c
1 /* tap-protocolinfo.c
2  * protohierstat   2002 Ronnie Sahlberg
3  *
4  * $Id: tap-protocolinfo.c,v 1.3 2003/04/23 08:20:01 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 tethereal */
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 "tap.h"
42 #include "register.h"
43
44 typedef struct _pci_t {
45         char *filter;
46         int hf_index;
47 } pci_t;
48
49
50 static int
51 protocolinfo_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, void *dummy _U_)
52 {
53         pci_t *rs=prs;
54         GPtrArray *gp;
55         guint i;
56         char *str;
57
58         gp=proto_get_finfo_ptr_array(edt->tree, rs->hf_index);
59         if(!gp){
60                 return 0;
61         }
62
63         for(i=0;i<gp->len;i++){
64                 str=proto_alloc_dfilter_string(gp->pdata[i], NULL);
65                 col_append_fstr(pinfo->cinfo, COL_INFO, "  %s",str);
66                 g_free(str);
67         }
68         return 0;
69 }
70
71
72
73 static void
74 protocolinfo_init(char *optarg)
75 {
76         pci_t *rs;
77         char *field=NULL;
78         char *filter=NULL;
79         header_field_info *hfi;
80         GString *error_string;
81
82         if(!strncmp("proto,colinfo,",optarg,14)){
83                 filter=optarg+14;
84                 field=strchr(filter,',');
85                 if(field){
86                         field+=1;  /* skip the ',' */
87                 }
88         }
89         if(!field){
90                 fprintf(stderr, "tethereal: invalid \"-z proto,colinfo,<filter>,<field>\" argument\n");
91                 exit(1);
92         }
93
94         hfi=proto_registrar_get_byname(field);
95         if(!hfi){
96                 fprintf(stderr, "tethereal: Field \"%s\" does not exist.\n", field);
97                 exit(1);
98         }
99
100         rs=g_malloc(sizeof(pci_t));
101         rs->hf_index=hfi->id;
102         if((field-filter)>1){
103                 rs->filter=g_malloc(field-filter);
104                 strncpy(rs->filter,filter,(field-filter)-1);
105                 rs->filter[(field-filter)-1]=0;
106         } else {
107                 rs->filter=NULL;
108         }
109
110         error_string=register_tap_listener("frame", rs, rs->filter, NULL, protocolinfo_packet, NULL);
111         if(error_string){
112                 /* error, we failed to attach to the tap. complain and clean up */
113                 fprintf(stderr, "tethereal: Couldn't register proto,colinfo tap: %s\n",
114                     error_string->str);
115                 g_string_free(error_string, TRUE);
116                 if(rs->filter){
117                         g_free(rs->filter);
118                 }
119                 g_free(rs);
120
121                 exit(1);
122         }
123 }
124
125
126 void
127 register_tap_listener_protocolinfo(void)
128 {
129         register_ethereal_tap("proto,colinfo,", protocolinfo_init);
130 }
131