From David Richards via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8871 :
[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  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 2000 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29 #include <epan/packet.h>
30 #include <epan/expert.h>
31 #include <epan/show_exception.h>
32
33 static int proto_short = -1;
34 int proto_malformed = -1;
35 static int proto_unreassembled = -1;
36
37 void
38 register_show_exception(void)
39 {
40         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
41         proto_malformed = proto_register_protocol("Malformed Packet",
42             "Malformed packet", "malformed");
43         proto_unreassembled = proto_register_protocol(
44             "Unreassembled Fragmented Packet",
45             "Unreassembled fragmented packet", "unreassembled");
46
47         /* "Short Frame", "Malformed Packet", and "Unreassembled Fragmented
48            Packet" aren't really protocols, they're error indications;
49            disabling them makes no sense. */
50         proto_set_cant_toggle(proto_short);
51         proto_set_cant_toggle(proto_malformed);
52         proto_set_cant_toggle(proto_unreassembled);
53 }
54
55 void
56 show_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
57                unsigned long exception, const char *exception_message)
58 {
59         static const char dissector_error_nomsg[] =
60                 "Dissector writer didn't bother saying what the error was";
61         proto_item *item;
62
63         if (exception == ReportedBoundsError && pinfo->fragmented)
64                 exception = FragmentBoundsError;
65
66         switch (exception) {
67
68         case ScsiBoundsError:
69                 col_append_str(pinfo->cinfo, COL_INFO, "[SCSI transfer limited due to allocation_length too small]");
70                 /*item =*/ proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
71                                 "SCSI transfer limited due to allocation_length too small: %s truncated]", pinfo->current_proto);
72                 /* Don't record ScsiBoundsError exceptions as expert events - they merely
73                  * reflect a normal SCSI condition.
74                  * (any case where it's caused by something else is a bug). */
75                 /* expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Packet size limited");*/
76                 break;
77
78         case BoundsError:
79                 col_append_str(pinfo->cinfo, COL_INFO, "[Packet size limited during capture]");
80                 /*item =*/ proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
81                                 "[Packet size limited during capture: %s truncated]", pinfo->current_proto);
82                 /* Don't record BoundsError exceptions as expert events - they merely
83                  * reflect a capture done with a snapshot length too short to capture
84                  * all of the packet
85                  * (any case where it's caused by something else is a bug). */
86                 /* expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Packet size limited");*/
87                 break;
88
89         case FragmentBoundsError:
90                 col_append_fstr(pinfo->cinfo, COL_INFO, "[Unreassembled Packet%s]", pinfo->noreassembly_reason);
91                 /*item =*/ proto_tree_add_protocol_format(tree, proto_unreassembled,
92                     tvb, 0, 0, "[Unreassembled Packet%s: %s]",
93                     pinfo->noreassembly_reason, pinfo->current_proto);
94                 /* Don't record FragmentBoundsError exceptions as expert events - they merely
95                  * reflect dissection done with reassembly turned off
96                  * (any case where it's caused by something else is a bug). */
97                 /* expert_add_info_format(pinfo, item, PI_REASSEMBLE, PI_WARN, "Unreassembled Packet (Exception occured)");*/
98                 break;
99
100         case ReportedBoundsError:
101                 show_reported_bounds_error(tvb, pinfo, tree);
102                 break;
103
104         case DissectorError:
105                 col_append_fstr(pinfo->cinfo, COL_INFO,
106                     "[Dissector bug, protocol %s: %s]",
107                     pinfo->current_proto,
108                     exception_message == NULL ?
109                         dissector_error_nomsg : exception_message);
110                 item = proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
111                     "[Dissector bug, protocol %s: %s]",
112                     pinfo->current_proto,
113                     exception_message == NULL ?
114                         dissector_error_nomsg : exception_message);
115                 g_warning("Dissector bug, protocol %s, in packet %u: %s",
116                     pinfo->current_proto, pinfo->fd->num,
117                     exception_message == NULL ?
118                         dissector_error_nomsg : exception_message);
119                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
120                     "%s",
121                     exception_message == NULL ?
122                         dissector_error_nomsg : exception_message);
123                 break;
124
125         case ReassemblyError:
126                 col_append_fstr(pinfo->cinfo, COL_INFO,
127                     "[Reassembly error, protocol %s: %s]",
128                     pinfo->current_proto,
129                     exception_message == NULL ?
130                         dissector_error_nomsg : exception_message);
131                 item = proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
132                     "[Reassembly error, protocol %s: %s]",
133                     pinfo->current_proto,
134                     exception_message == NULL ?
135                         dissector_error_nomsg : exception_message);
136                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
137                     "%s",
138                     exception_message == NULL ?
139                         dissector_error_nomsg : exception_message);
140                 break;
141
142         default:
143                 /* XXX - we want to know, if an unknown exception passed until here, don't we? */
144                 g_assert_not_reached();
145         }
146 }
147
148 void
149 show_reported_bounds_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
150 {
151         proto_item *item;
152
153         col_append_str(pinfo->cinfo, COL_INFO,
154             "[Malformed Packet]");
155         item = proto_tree_add_protocol_format(tree, proto_malformed,
156             tvb, 0, 0, "[Malformed Packet: %s]", pinfo->current_proto);
157         expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Malformed Packet (Exception occurred)");
158 }