2 * Collecting Expert information.
4 * Implemented as a tap named "expert".
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include "wmem/wmem.h"
37 /* proto_expert cannot be static because it's referenced in the
40 int proto_expert = -1;
42 static int proto_malformed = -1;
44 static int expert_tap = -1;
45 static int highest_severity = 0;
47 static int ett_expert = -1;
48 static int ett_subexpert = -1;
50 static int hf_expert_msg = -1;
51 static int hf_expert_group = -1;
52 static int hf_expert_severity = -1;
56 const char *proto_name;
57 int proto_id; /* Cache this for registering hfs */
60 /* List which stores protocols and expert_info that have been registered */
61 typedef struct _gpa_expertinfo_t {
63 guint32 allocated_len;
64 expert_field_info **ei;
66 static gpa_expertinfo_t gpa_expertinfo;
68 /* Hash table of abbreviations and IDs */
69 static GHashTable *gpa_name_map = NULL;
71 const value_string expert_group_vals[] = {
72 { PI_CHECKSUM, "Checksum" },
73 { PI_SEQUENCE, "Sequence" },
74 { PI_RESPONSE_CODE, "Response" },
75 { PI_REQUEST_CODE, "Request" },
76 { PI_UNDECODED, "Undecoded" },
77 { PI_REASSEMBLE, "Reassemble" },
78 { PI_MALFORMED, "Malformed" },
79 { PI_DEBUG, "Debug" },
80 { PI_PROTOCOL, "Protocol" },
81 { PI_SECURITY, "Security" },
82 { PI_COMMENTS_GROUP, "Comment" },
83 { PI_DECRYPTION, "Decryption" },
87 const value_string expert_severity_vals[] = {
88 { PI_ERROR, "Error" },
92 { PI_COMMENT, "Comment" },
97 /* Possible values for a checksum evaluation */
98 const value_string expert_checksum_vals[] = {
99 { EXPERT_CHECKSUM_DISABLED, "Disabled" },
100 { EXPERT_CHECKSUM_UNKNOWN, "Unknown" },
101 { EXPERT_CHECKSUM_GOOD, "Good" },
102 { EXPERT_CHECKSUM_BAD, "Bad" },
106 static expert_field_info *expert_registrar_get_byname(const char *field_name);
108 /*----------------------------------------------------------------------------*/
109 /* UAT for customizing severity levels. */
110 /*----------------------------------------------------------------------------*/
115 } expert_level_entry_t;
117 static expert_level_entry_t *uat_expert_entries = NULL;
118 static guint expert_level_entry_count = 0;
119 /* Array of field names currently in UAT */
120 static GArray *uat_saved_fields = NULL;
122 UAT_CSTRING_CB_DEF(uat_expert_entries, field, expert_level_entry_t)
123 UAT_VS_DEF(uat_expert_entries, severity, expert_level_entry_t, guint32, PI_ERROR, "Error")
125 static gboolean uat_expert_update_cb(void *r, char **err)
127 expert_level_entry_t *rec = (expert_level_entry_t *)r;
129 if (expert_registrar_get_byname(rec->field) == NULL) {
130 *err = g_strdup_printf("Expert Info field doesn't exist");
136 static void *uat_expert_copy_cb(void *n, const void *o, size_t siz _U_)
138 expert_level_entry_t *new_record = (expert_level_entry_t*)n;
139 const expert_level_entry_t *old_record = (const expert_level_entry_t *)o;
141 if (old_record->field) {
142 new_record->field = g_strdup(old_record->field);
144 new_record->field = NULL;
147 new_record->severity = old_record->severity;
152 static void uat_expert_free_cb(void*r)
154 expert_level_entry_t *rec = (expert_level_entry_t *)r;
160 static void uat_expert_post_update_cb(void)
163 expert_field_info *field;
165 /* Reset any of the previous list of expert info fields to their original severity */
166 for ( i = 0 ; i < uat_saved_fields->len; i++ ) {
167 field = g_array_index(uat_saved_fields, expert_field_info*, i);
169 field->severity = field->orig_severity;
173 g_array_set_size(uat_saved_fields, 0);
175 for (i = 0; i < expert_level_entry_count; i++)
177 field = expert_registrar_get_byname(uat_expert_entries[i].field);
180 field->severity = uat_expert_entries[i].severity;
181 g_array_append_val(uat_saved_fields, field);
186 #define EXPERT_REGISTRAR_GET_NTH(eiindex, expinfo) \
187 if((guint)eiindex >= gpa_expertinfo.len && getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG")) \
188 g_error("Unregistered expert info! index=%d", eiindex); \
189 DISSECTOR_ASSERT_HINT((guint)eiindex < gpa_expertinfo.len, "Unregistered expert info!"); \
190 expinfo = gpa_expertinfo.ei[eiindex];
193 expert_packet_init(void)
195 module_t *module_expert;
198 static hf_register_info hf[] = {
200 { "Message", "_ws.expert.message", FT_STRING, BASE_NONE, NULL, 0, "Wireshark expert information", HFILL }
203 { "Group", "_ws.expert.group", FT_UINT32, BASE_HEX, VALS(expert_group_vals), 0, "Wireshark expert group", HFILL }
205 { &hf_expert_severity,
206 { "Severity level", "_ws.expert.severity", FT_UINT32, BASE_HEX, VALS(expert_severity_vals), 0, "Wireshark expert severity level", HFILL }
209 static gint *ett[] = {
214 /* UAT for overriding severity levels */
215 static uat_field_t custom_expert_fields[] = {
216 UAT_FLD_CSTRING(uat_expert_entries, field, "Field name", "Expert Info filter name"),
217 UAT_FLD_VS(uat_expert_entries, severity, "Severity", expert_severity_vals, "Custom severity level"),
221 if (expert_tap == -1) {
222 expert_tap = register_tap("expert");
225 if (proto_expert == -1) {
226 proto_expert = proto_register_protocol("Expert Info", "Expert", "_ws.expert");
227 proto_register_field_array(proto_expert, hf, array_length(hf));
228 proto_register_subtree_array(ett, array_length(ett));
229 proto_set_cant_toggle(proto_expert);
231 module_expert = prefs_register_protocol(proto_expert, NULL);
233 expert_uat = uat_new("Expert Info Severity Level Configuration",
234 sizeof(expert_level_entry_t),
237 (void **)&uat_expert_entries,
238 &expert_level_entry_count,
239 UAT_AFFECTS_DISSECTION,
242 uat_expert_update_cb,
244 uat_expert_post_update_cb,
245 custom_expert_fields);
247 prefs_register_uat_preference(module_expert,
248 "expert_severity_levels",
249 "Severity Level Configuration",
250 "A table that overrides Expert Info field severity levels to user configured levels",
255 highest_severity = 0;
257 proto_malformed = proto_get_id_by_filter_name("_ws.malformed");
263 gpa_expertinfo.len = 0;
264 gpa_expertinfo.allocated_len = 0;
265 gpa_expertinfo.ei = NULL;
266 gpa_name_map = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
267 uat_saved_fields = g_array_new(FALSE, FALSE, sizeof(expert_field_info*));
271 expert_packet_cleanup(void)
278 if (gpa_expertinfo.allocated_len) {
279 gpa_expertinfo.len = 0;
280 gpa_expertinfo.allocated_len = 0;
281 g_free(gpa_expertinfo.ei);
282 gpa_expertinfo.ei = NULL;
285 /* Free the abbrev/ID GTree */
287 g_hash_table_destroy(gpa_name_map);
291 /* Free the UAT saved fields */
292 if (uat_saved_fields) {
293 g_array_free(uat_saved_fields, TRUE);
294 uat_saved_fields = NULL;
300 expert_get_highest_severity(void)
302 return highest_severity;
306 expert_update_comment_count(guint64 count)
308 if (count==0 && highest_severity==PI_COMMENT)
309 highest_severity = 0;
312 expert_module_t *expert_register_protocol(int id)
314 expert_module_t *module;
315 protocol_t *protocol;
317 protocol = find_protocol_by_id(id);
319 module = wmem_new(wmem_epan_scope(), expert_module_t);
320 module->proto_id = id;
321 module->proto_name = proto_get_protocol_short_name(protocol);
326 void expert_deregister_protocol (expert_module_t *module)
328 wmem_free(wmem_epan_scope(), module);
332 expert_register_field_init(expert_field_info *expinfo, expert_module_t *module)
334 expinfo->protocol = module->proto_name;
336 /* if we always add and never delete, then id == len - 1 is correct */
337 if (gpa_expertinfo.len >= gpa_expertinfo.allocated_len) {
338 if (!gpa_expertinfo.ei) {
339 gpa_expertinfo.allocated_len = PRE_ALLOC_EXPERT_FIELDS_MEM;
340 gpa_expertinfo.ei = (expert_field_info **)g_malloc(sizeof(expert_field_info *)*PRE_ALLOC_EXPERT_FIELDS_MEM);
342 gpa_expertinfo.allocated_len += 1000;
343 gpa_expertinfo.ei = (expert_field_info **)g_realloc(gpa_expertinfo.ei,
344 sizeof(expert_field_info *)*gpa_expertinfo.allocated_len);
347 gpa_expertinfo.ei[gpa_expertinfo.len] = expinfo;
348 gpa_expertinfo.len++;
349 expinfo->id = gpa_expertinfo.len - 1;
350 /* Save the original severity so it can be restored by the UAT */
351 expinfo->orig_severity = expinfo->severity;
353 /* save field name for lookup */
354 g_hash_table_insert(gpa_name_map, (gpointer) (expinfo->name), expinfo);
360 /* for use with static arrays only, since we don't allocate our own copies
361 of the expert_field_info struct contained within the exp_register_info struct */
363 expert_register_field_array(expert_module_t *module, ei_register_info *exp, const int num_records)
366 ei_register_info *ptr = exp;
368 for (i = 0; i < num_records; i++, ptr++) {
370 * Make sure we haven't registered this yet.
371 * Most fields have variables associated with them
372 * that are initialized to -1; some have array elements,
373 * or possibly uninitialized variables, so we also allow
374 * 0 (which is unlikely to be the field ID we get back
375 * from "expert_register_field_init()").
377 if (ptr->ids->ei != -1 && ptr->ids->ei != 0) {
379 "Duplicate field detected in call to expert_register_field_array: '%s' is already registered, name=%s\n",
380 ptr->eiinfo.summary, ptr->eiinfo.name);
384 /* Register the field with the experts */
385 ptr->ids->ei = expert_register_field_init(&ptr->eiinfo, module);
387 /* Register with the header field info, so it's display filterable */
388 ptr->eiinfo.hf_info.p_id = &ptr->ids->hf;
389 ptr->eiinfo.hf_info.hfinfo.abbrev = ptr->eiinfo.name;
390 ptr->eiinfo.hf_info.hfinfo.blurb = ptr->eiinfo.summary;
392 proto_register_field_array(module->proto_id, &ptr->eiinfo.hf_info, 1);
396 /* Finds a record in the expert array by name.
397 * For the moment, this function is only used "internally"
398 * but may find a reason to be exported
400 static expert_field_info *
401 expert_registrar_get_byname(const char *field_name)
403 expert_field_info *hfinfo;
408 hfinfo = (expert_field_info*)g_hash_table_lookup(gpa_name_map, field_name);
414 * Get summary text of an expert_info field.
415 * This is intended for use in expert_add_info_format or proto_tree_add_expert_format
416 * to get the "base" string to then append additional information
418 const gchar* expert_get_summary(expert_field *eiindex)
420 expert_field_info *eiinfo;
422 /* Look up the item */
423 EXPERT_REGISTRAR_GET_NTH(eiindex->ei, eiinfo);
425 return eiinfo->summary;
428 /** clear flags according to the mask and set new flag values */
429 #define FI_REPLACE_FLAGS(fi, mask, flags_in) { \
430 (fi->flags = (fi)->flags & ~(mask)); \
431 (fi->flags = (fi)->flags | (flags_in)); \
434 /* set's the PI_ flags to a protocol item
435 * (and its parent items till the toplevel) */
437 expert_set_item_flags(proto_item *pi, const int group, const guint severity)
439 if (pi != NULL && PITEM_FINFO(pi) != NULL && (severity >= FI_GET_FLAG(PITEM_FINFO(pi), PI_SEVERITY_MASK))) {
440 FI_REPLACE_FLAGS(PITEM_FINFO(pi), PI_GROUP_MASK, group);
441 FI_REPLACE_FLAGS(PITEM_FINFO(pi), PI_SEVERITY_MASK, severity);
443 /* propagate till toplevel item */
444 pi = proto_item_get_parent(pi);
445 expert_set_item_flags(pi, group, severity);
450 expert_create_tree(proto_item *pi, int group, int severity, const char *msg)
455 tree = proto_item_add_subtree(pi, ett_expert);
456 ti = proto_tree_add_protocol_format(tree, proto_expert, NULL, 0, 0, "Expert Info (%s/%s): %s",
457 val_to_str(severity, expert_severity_vals, "Unknown (%u)"),
458 val_to_str(group, expert_group_vals, "Unknown (%u)"),
460 PROTO_ITEM_SET_GENERATED(ti);
462 if (group == PI_MALFORMED) {
463 /* Add hidden malformed protocol filter */
464 proto_item *malformed_ti = proto_tree_add_item(tree, proto_malformed, NULL, 0, 0, ENC_NA);
465 PROTO_ITEM_SET_HIDDEN(malformed_ti);
468 return proto_item_add_subtree(ti, ett_subexpert);
472 expert_set_info_vformat(packet_info *pinfo, proto_item *pi, int group, int severity, int hf_index, gboolean use_vaformat,
473 const char *format, va_list ap)
475 char formatted[ITEM_LABEL_LENGTH];
481 if (pinfo == NULL && pi && pi->tree_data) {
482 pinfo = PTREE_DATA(pi)->pinfo;
485 /* if this packet isn't loaded because of a read filter, don't output anything */
486 if (pinfo == NULL || PINFO_FD_NUM(pinfo) == 0) {
490 if (severity > highest_severity) {
491 highest_severity = severity;
494 /* XXX: can we get rid of these checks and make them programming errors instead now? */
495 if (pi != NULL && PITEM_FINFO(pi) != NULL) {
496 expert_set_item_flags(pi, group, severity);
499 if ((pi == NULL) || (PITEM_FINFO(pi) == NULL) ||
500 ((guint)severity >= FI_GET_FLAG(PITEM_FINFO(pi), PI_SEVERITY_MASK))) {
501 col_add_str(pinfo->cinfo, COL_EXPERT, val_to_str(severity, expert_severity_vals, "Unknown (%u)"));
505 g_vsnprintf(formatted, ITEM_LABEL_LENGTH, format, ap);
507 g_strlcpy(formatted, format, ITEM_LABEL_LENGTH);
510 tree = expert_create_tree(pi, group, severity, formatted);
512 if (hf_index == -1) {
513 /* If no filterable expert info, just add the message */
514 ti = proto_tree_add_string(tree, hf_expert_msg, NULL, 0, 0, formatted);
515 PROTO_ITEM_SET_GENERATED(ti);
517 /* If filterable expert info, hide the "generic" form of the message,
518 and generate the formatted filterable expert info */
519 ti = proto_tree_add_none_format(tree, hf_index, NULL, 0, 0, "%s", formatted);
520 PROTO_ITEM_SET_GENERATED(ti);
521 ti = proto_tree_add_string(tree, hf_expert_msg, NULL, 0, 0, formatted);
522 PROTO_ITEM_SET_HIDDEN(ti);
525 ti = proto_tree_add_uint_format_value(tree, hf_expert_severity, NULL, 0, 0, severity,
526 "%s", val_to_str_const(severity, expert_severity_vals, "Unknown"));
527 PROTO_ITEM_SET_GENERATED(ti);
528 ti = proto_tree_add_uint_format_value(tree, hf_expert_group, NULL, 0, 0, group,
529 "%s", val_to_str_const(group, expert_group_vals, "Unknown"));
530 PROTO_ITEM_SET_GENERATED(ti);
532 tap = have_tap_listener(expert_tap);
537 ei = wmem_new(wmem_packet_scope(), expert_info_t);
539 ei->packet_num = PINFO_FD_NUM(pinfo);
541 ei->severity = severity;
542 ei->hf_index = hf_index;
543 ei->protocol = pinfo->current_proto;
544 ei->summary = wmem_strdup(wmem_packet_scope(), formatted);
546 /* if we have a proto_item (not a faked item), set expert attributes to it */
547 if (pi != NULL && PITEM_FINFO(pi) != NULL) {
550 /* XXX: remove this because we don't have an internal-only function now? */
555 tap_queue_packet(expert_tap, pinfo, ei);
558 /* Helper function for expert_add_info() to work around compiler's special needs on ARM */
560 expert_add_info_internal(packet_info *pinfo, proto_item *pi, expert_field *expindex, ...)
562 /* the va_list is ignored */
564 expert_field_info *eiinfo;
566 /* Look up the item */
567 EXPERT_REGISTRAR_GET_NTH(expindex->ei, eiinfo);
569 va_start(unused, expindex);
570 expert_set_info_vformat(pinfo, pi, eiinfo->group, eiinfo->severity, *eiinfo->hf_info.p_id, FALSE, eiinfo->summary, unused);
575 expert_add_info(packet_info *pinfo, proto_item *pi, expert_field *expindex)
577 expert_add_info_internal(pinfo, pi, expindex);
581 expert_add_info_format(packet_info *pinfo, proto_item *pi, expert_field *expindex, const char *format, ...)
584 expert_field_info *eiinfo;
586 /* Look up the item */
587 EXPERT_REGISTRAR_GET_NTH(expindex->ei, eiinfo);
589 va_start(ap, format);
590 expert_set_info_vformat(pinfo, pi, eiinfo->group, eiinfo->severity, *eiinfo->hf_info.p_id, TRUE, format, ap);
594 /* Helper function for expert_add_expert() to work around compiler's special needs on ARM */
595 static inline proto_item *
596 proto_tree_add_expert_internal(proto_tree *tree, packet_info *pinfo, expert_field *expindex,
597 tvbuff_t *tvb, gint start, gint length, ...)
599 expert_field_info *eiinfo;
603 /* Look up the item */
604 EXPERT_REGISTRAR_GET_NTH(expindex->ei, eiinfo);
606 ti = proto_tree_add_text(tree, tvb, start, length, "%s", eiinfo->summary);
607 va_start(unused, length);
608 expert_set_info_vformat(pinfo, ti, eiinfo->group, eiinfo->severity, *eiinfo->hf_info.p_id, FALSE, eiinfo->summary, unused);
614 proto_tree_add_expert(proto_tree *tree, packet_info *pinfo, expert_field *expindex,
615 tvbuff_t *tvb, gint start, gint length)
617 return proto_tree_add_expert_internal(tree, pinfo, expindex, tvb, start, length);
621 proto_tree_add_expert_format(proto_tree *tree, packet_info *pinfo, expert_field *expindex,
622 tvbuff_t *tvb, gint start, gint length, const char *format, ...)
625 expert_field_info *eiinfo;
628 /* Look up the item */
629 EXPERT_REGISTRAR_GET_NTH(expindex->ei, eiinfo);
631 va_start(ap, format);
632 ti = proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
635 va_start(ap, format);
636 expert_set_info_vformat(pinfo, ti, eiinfo->group, eiinfo->severity, *eiinfo->hf_info.p_id, TRUE, format, ap);
643 * Editor modelines - http://www.wireshark.org/tools/modelines.html
648 * indent-tabs-mode: t
651 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
652 * :indentSize=8:tabSize=8:noTabs=false: