Move expert_group_vals expert_severity_vals constant definitions to expert.h
[metze/wireshark/wip.git] / epan / expert.c
1 /* expert.c
2  * Collecting Expert information.
3  *
4  * Implemented as a tap named "expert".
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include "packet.h"
30 #include "expert.h"
31 #include "emem.h"
32 #include "tap.h"
33
34
35 /* proto_expert cannot be static because it's referenced in the
36  * print routines
37  */
38 int proto_expert              = -1;
39
40 static int expert_tap         = -1;
41 static int highest_severity   =  0;
42
43 static int ett_expert         = -1;
44 static int ett_subexpert      = -1;
45
46 static int hf_expert_msg      = -1;
47 static int hf_expert_group    = -1;
48 static int hf_expert_severity = -1;
49
50 void
51 expert_init(void)
52 {
53         static hf_register_info hf[] = {
54                 { &hf_expert_msg,
55                         { "Message", "expert.message", FT_STRING, BASE_NONE, NULL, 0, "Wireshark expert information", HFILL }
56                 },
57                 { &hf_expert_group,
58                         { "Group", "expert.group", FT_UINT32, BASE_HEX, VALS(expert_group_vals), 0, "Wireshark expert group", HFILL }
59                 },
60                 { &hf_expert_severity,
61                         { "Severity level", "expert.severity", FT_UINT32, BASE_HEX, VALS(expert_severity_vals), 0, "Wireshark expert severity level", HFILL }
62                 }
63         };
64         static gint *ett[] = {
65                 &ett_expert,
66                 &ett_subexpert
67         };
68
69         if (expert_tap == -1) {
70                 expert_tap = register_tap("expert");
71         }
72
73         if (proto_expert == -1) {
74                 proto_expert = proto_register_protocol("Expert Info", "Expert", "expert");
75                 proto_register_field_array(proto_expert, hf, array_length(hf));
76                 proto_register_subtree_array(ett, array_length(ett));
77                 proto_set_cant_toggle(proto_expert);
78         }
79
80         highest_severity = 0;
81 }
82
83
84 void
85 expert_cleanup(void)
86 {
87
88 }
89
90
91 int
92 expert_get_highest_severity(void)
93 {
94         return highest_severity;
95 }
96
97
98 /* set's the PI_ flags to a protocol item
99  * (and its parent items till the toplevel) */
100 static void
101 expert_set_item_flags(proto_item *pi, int group, int severity)
102 {
103         if (proto_item_set_expert_flags(pi, group, severity)) {
104                 /* propagate till toplevel item */
105                 pi = proto_item_get_parent(pi);
106                 expert_set_item_flags(pi, group, severity);
107         }
108 }
109
110 static proto_tree*
111 expert_create_tree(proto_item *pi, int group, int severity, const char *msg)
112 {
113         proto_tree *tree;
114         proto_item *ti;
115
116         tree = proto_item_add_subtree(pi, ett_expert);
117         ti = proto_tree_add_protocol_format(tree, proto_expert, NULL, 0, 0, "Expert Info (%s/%s): %s",
118                                             val_to_str(severity, expert_severity_vals, "Unknown (%u)"),
119                                             val_to_str(group, expert_group_vals, "Unknown (%u)"),
120                                             msg);
121         PROTO_ITEM_SET_GENERATED(ti);
122
123         if (group == PI_MALFORMED) {
124                 /* Add hidden malformed protocol filter */
125                 gint proto_malformed = proto_get_id_by_filter_name("malformed");
126                 proto_item *malformed_ti = proto_tree_add_item(tree, proto_malformed, NULL, 0, 0, ENC_NA);
127                 PROTO_ITEM_SET_HIDDEN(malformed_ti);
128         }
129
130         return proto_item_add_subtree(ti, ett_subexpert);
131 }
132
133 static void
134 expert_set_info_vformat(packet_info *pinfo, proto_item *pi, int group, int severity, const char *format, va_list ap)
135 {
136         char            formatted[ITEM_LABEL_LENGTH];
137         int             tap;
138         expert_info_t   *ei;
139         proto_tree      *tree;
140         proto_item      *ti;
141
142         if (pinfo == NULL && pi && pi->tree_data) {
143                 pinfo = PTREE_DATA(pi)->pinfo;
144         }
145
146         /* if this packet isn't loaded because of a read filter, don't output anything */
147         if (pinfo == NULL || PINFO_FD_NUM(pinfo) == 0) {
148                 return;
149         }
150
151         if (severity > highest_severity) {
152                 highest_severity = severity;
153         }
154
155         if (pi != NULL && PITEM_FINFO(pi) != NULL) {
156                 expert_set_item_flags(pi, group, severity);
157         }
158
159         col_add_str(pinfo->cinfo, COL_EXPERT, val_to_str(severity, expert_severity_vals, "Unknown (%u)"));
160
161         g_vsnprintf(formatted, ITEM_LABEL_LENGTH, format, ap);
162
163         tree = expert_create_tree(pi, group, severity, formatted);
164
165         ti = proto_tree_add_string(tree, hf_expert_msg, NULL, 0, 0, formatted);
166         PROTO_ITEM_SET_GENERATED(ti);
167         ti = proto_tree_add_uint_format_value(tree, hf_expert_severity, NULL, 0, 0, severity,
168                                               "%s", val_to_str_const(severity, expert_severity_vals, "Unknown"));
169         PROTO_ITEM_SET_GENERATED(ti);
170         ti = proto_tree_add_uint_format_value(tree, hf_expert_group, NULL, 0, 0, group,
171                                               "%s", val_to_str_const(group, expert_group_vals, "Unknown"));
172         PROTO_ITEM_SET_GENERATED(ti);
173
174         tap = have_tap_listener(expert_tap);
175
176         if (!tap)
177                 return;
178
179         ei = ep_alloc(sizeof(expert_info_t));
180
181         ei->packet_num  = PINFO_FD_NUM(pinfo);
182         ei->group       = group;
183         ei->severity    = severity;
184         ei->protocol    = pinfo->current_proto;
185         ei->summary     = ep_strdup(formatted);
186
187         /* if we have a proto_item (not a faked item), set expert attributes to it */
188         if (pi != NULL && PITEM_FINFO(pi) != NULL) {
189                 ei->pitem = pi;
190         } else {
191                 ei->pitem = NULL;
192         }
193
194         tap_queue_packet(expert_tap, pinfo, ei);
195 }
196
197
198 void
199 expert_add_info_format(packet_info *pinfo, proto_item *pi, int group, int severity, const char *format, ...)
200 {
201         va_list ap;
202
203         va_start(ap, format);
204         expert_set_info_vformat(pinfo, pi, group, severity, format, ap);
205         va_end(ap);
206 }
207
208 void
209 expert_add_undecoded_item(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int length, const int severity)
210 {
211
212         proto_item *expert_item;
213
214         expert_item = proto_tree_add_text(tree, tvb, offset, length, "Not dissected yet");
215
216         expert_add_info_format(pinfo, expert_item, PI_UNDECODED, severity, "Not dissected yet(report to wireshark.org)"); \
217         PROTO_ITEM_SET_GENERATED(expert_item); \
218
219 }