Use MAC address documentation range in filter examples
[metze/wireshark/wip.git] / epan / show_exception.c
1 /* show_exception.c
2  *
3  * Routines to put exception information into the protocol tree
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 2000 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <glib.h>
27 #include <epan/packet.h>
28 #include <epan/exceptions.h>
29 #include <epan/expert.h>
30 #include <epan/prefs.h>
31 #include <epan/prefs-int.h>
32 #include <epan/show_exception.h>
33
34 static int proto_short = -1;
35 static int proto_malformed = -1;
36 static int proto_unreassembled = -1;
37
38 static expert_field ei_malformed_dissector_bug = EI_INIT;
39 static expert_field ei_malformed_reassembly = EI_INIT;
40 static expert_field ei_malformed = EI_INIT;
41
42 void
43 register_show_exception(void)
44 {
45         static ei_register_info ei[] = {
46                 { &ei_malformed_dissector_bug, { "_ws.malformed.dissector_bug", PI_MALFORMED, PI_ERROR, "Dissector bug", EXPFILL }},
47                 { &ei_malformed_reassembly, { "_ws.malformed.reassembly", PI_MALFORMED, PI_ERROR, "Reassembly error", EXPFILL }},
48                 { &ei_malformed, { "_ws.malformed.expert", PI_MALFORMED, PI_ERROR, "Malformed Packet (Exception occurred)", EXPFILL }},
49         };
50
51         expert_module_t* expert_malformed;
52
53         proto_short = proto_register_protocol("Short Frame", "Short frame", "_ws.short");
54         proto_malformed = proto_register_protocol("Malformed Packet",
55             "Malformed packet", "_ws.malformed");
56         proto_unreassembled = proto_register_protocol(
57             "Unreassembled Fragmented Packet",
58             "Unreassembled fragmented packet", "_ws.unreassembled");
59
60         expert_malformed = expert_register_protocol(proto_malformed);
61         expert_register_field_array(expert_malformed, ei, array_length(ei));
62
63         /* "Short Frame", "Malformed Packet", and "Unreassembled Fragmented
64            Packet" aren't really protocols, they're error indications;
65            disabling them makes no sense. */
66         proto_set_cant_toggle(proto_short);
67         proto_set_cant_toggle(proto_malformed);
68         proto_set_cant_toggle(proto_unreassembled);
69 }
70
71 void
72 show_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
73                unsigned long exception, const char *exception_message)
74 {
75         static const char dissector_error_nomsg[] =
76                 "Dissector writer didn't bother saying what the error was";
77         proto_item *item;
78
79         if (exception == ReportedBoundsError && pinfo->fragmented)
80                 exception = FragmentBoundsError;
81
82         switch (exception) {
83
84         case ScsiBoundsError:
85                 col_append_str(pinfo->cinfo, COL_INFO, "[SCSI transfer limited due to allocation_length too small]");
86                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
87                                 "SCSI transfer limited due to allocation_length too small: %s truncated]", pinfo->current_proto);
88                 /* Don't record ScsiBoundsError exceptions as expert events - they merely
89                  * reflect a normal SCSI condition.
90                  * (any case where it's caused by something else is a bug). */
91                 break;
92
93         case BoundsError:
94                 {
95                 gboolean display_info = TRUE;
96                 module_t * frame_module = prefs_find_module("frame");
97                 if (frame_module != NULL)
98                 {
99                         pref_t *display_pref = prefs_find_preference(frame_module, "disable_packet_size_limited_in_summary");
100                         if (display_pref)
101                         {
102                                 if (*display_pref->varp.boolp)
103                                         display_info = FALSE;
104                         }
105                 }
106
107                 if (display_info)
108                         col_append_str(pinfo->cinfo, COL_INFO, "[Packet size limited during capture]");
109                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
110                                 "[Packet size limited during capture: %s truncated]", pinfo->current_proto);
111                 /* Don't record BoundsError exceptions as expert events - they merely
112                  * reflect a capture done with a snapshot length too short to capture
113                  * all of the packet
114                  * (any case where it's caused by something else is a bug). */
115         }
116                 break;
117
118         case FragmentBoundsError:
119                 col_append_fstr(pinfo->cinfo, COL_INFO, "[Unreassembled Packet%s]", pinfo->noreassembly_reason);
120                 proto_tree_add_protocol_format(tree, proto_unreassembled,
121                     tvb, 0, 0, "[Unreassembled Packet%s: %s]",
122                     pinfo->noreassembly_reason, pinfo->current_proto);
123                 /* Don't record FragmentBoundsError exceptions as expert events - they merely
124                  * reflect dissection done with reassembly turned off
125                  * (any case where it's caused by something else is a bug). */
126                 break;
127
128         case ReportedBoundsError:
129                 show_reported_bounds_error(tvb, pinfo, tree);
130                 break;
131
132         case DissectorError:
133                 col_append_fstr(pinfo->cinfo, COL_INFO,
134                     "[Dissector bug, protocol %s: %s]",
135                     pinfo->current_proto,
136                     exception_message == NULL ?
137                         dissector_error_nomsg : exception_message);
138                 item = proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
139                     "[Dissector bug, protocol %s: %s]",
140                     pinfo->current_proto,
141                     exception_message == NULL ?
142                         dissector_error_nomsg : exception_message);
143                 g_warning("Dissector bug, protocol %s, in packet %u: %s",
144                     pinfo->current_proto, pinfo->fd->num,
145                     exception_message == NULL ?
146                         dissector_error_nomsg : exception_message);
147                 expert_add_info_format(pinfo, item, &ei_malformed_dissector_bug, "%s",
148                     exception_message == NULL ?
149                         dissector_error_nomsg : exception_message);
150                 break;
151
152         case ReassemblyError:
153                 col_append_fstr(pinfo->cinfo, COL_INFO,
154                     "[Reassembly error, protocol %s: %s]",
155                     pinfo->current_proto,
156                     exception_message == NULL ?
157                         dissector_error_nomsg : exception_message);
158                 item = proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
159                     "[Reassembly error, protocol %s: %s]",
160                     pinfo->current_proto,
161                     exception_message == NULL ?
162                         dissector_error_nomsg : exception_message);
163                 expert_add_info_format(pinfo, item, &ei_malformed_reassembly, "%s",
164                     exception_message == NULL ?
165                         dissector_error_nomsg : exception_message);
166                 break;
167
168         default:
169                 /* XXX - we want to know, if an unknown exception passed until here, don't we? */
170                 g_assert_not_reached();
171         }
172 }
173
174 void
175 show_reported_bounds_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
176 {
177         proto_item *item;
178
179         col_append_str(pinfo->cinfo, COL_INFO,
180             "[Malformed Packet]");
181         item = proto_tree_add_protocol_format(tree, proto_malformed,
182             tvb, 0, 0, "[Malformed Packet: %s]", pinfo->current_proto);
183         expert_add_info(pinfo, item, &ei_malformed);
184 }
185
186 /*
187  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
188  *
189  * Local variables:
190  * c-basic-offset: 8
191  * tab-width: 8
192  * indent-tabs-mode: t
193  * End:
194  *
195  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
196  * :indentSize=8:tabSize=8:noTabs=false:
197  */