Batch of filterable expert infos.
[metze/wireshark/wip.git] / epan / dissectors / packet-frame.c
1 /* packet-frame.c
2  *
3  * Top-most dissector. Decides dissector based on Wiretap Encapsulation Type.
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 #ifdef _MSC_VER
29 #include <windows.h>
30 #endif
31
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/show_exception.h>
35 #include <epan/timestamp.h>
36 #include <epan/prefs.h>
37 #include <epan/tap.h>
38 #include <epan/expert.h>
39 #include <epan/crypt/md5.h>
40
41 #include "packet-frame.h"
42
43 #include "color.h"
44 #include "color_filters.h"
45
46 int proto_frame = -1;
47 int proto_pkt_comment = -1;
48 int hf_frame_arrival_time = -1;
49 int hf_frame_shift_offset = -1;
50 int hf_frame_arrival_time_epoch = -1;
51 static int hf_frame_time_invalid = -1;
52 static int hf_frame_time_delta = -1;
53 static int hf_frame_time_delta_displayed = -1;
54 static int hf_frame_time_relative = -1;
55 static int hf_frame_time_reference = -1;
56 int hf_frame_number = -1;
57 int hf_frame_len = -1;
58 int hf_frame_capture_len = -1;
59 static int hf_frame_p2p_dir = -1;
60 static int hf_frame_file_off = -1;
61 static int hf_frame_md5_hash = -1;
62 static int hf_frame_marked = -1;
63 static int hf_frame_ignored = -1;
64 static int hf_link_number = -1;
65 static int hf_frame_protocols = -1;
66 static int hf_frame_color_filter_name = -1;
67 static int hf_frame_color_filter_text = -1;
68 static int hf_frame_interface_id = -1;
69 static int hf_frame_pack_flags = -1;
70 static int hf_frame_pack_direction = -1;
71 static int hf_frame_pack_reception_type = -1;
72 static int hf_frame_pack_fcs_length = -1;
73 static int hf_frame_pack_reserved = -1;
74 static int hf_frame_pack_crc_error = -1;
75 static int hf_frame_pack_wrong_packet_too_long_error = -1;
76 static int hf_frame_pack_wrong_packet_too_short_error = -1;
77 static int hf_frame_pack_wrong_inter_frame_gap_error = -1;
78 static int hf_frame_pack_unaligned_frame_error = -1;
79 static int hf_frame_pack_start_frame_delimiter_error = -1;
80 static int hf_frame_pack_preamble_error = -1;
81 static int hf_frame_pack_symbol_error = -1;
82 static int hf_frame_wtap_encap = -1;
83 static int hf_comments_text = -1;
84 static int hf_frame_num_p_prot_data = -1; 
85
86 static gint ett_frame = -1;
87 static gint ett_flags = -1;
88 static gint ett_comments = -1;
89
90 static expert_field ei_comments_text = EI_INIT;
91 static expert_field ei_arrive_time_out_of_range = EI_INIT;
92
93 static int frame_tap = -1;
94
95 static dissector_handle_t data_handle;
96 static dissector_handle_t docsis_handle;
97
98 /* Preferences */
99 static gboolean show_file_off       = FALSE;
100 static gboolean force_docsis_encap  = FALSE;
101 static gboolean generate_md5_hash   = FALSE;
102 static gboolean generate_epoch_time = TRUE;
103 static gboolean generate_bits_field = TRUE;
104
105 static const value_string p2p_dirs[] = {
106         { P2P_DIR_UNKNOWN, "Unknown" },
107         { P2P_DIR_SENT, "Sent" },
108         { P2P_DIR_RECV, "Received" },
109         { 0, NULL }
110 };
111
112 #define PACKET_WORD_DIRECTION_MASK                        0x00000003
113 #define PACKET_WORD_RECEPTION_TYPE_MASK                   0x0000001C
114 #define PACKET_WORD_FCS_LENGTH_MASK                       0x000001E0
115 #define PACKET_WORD_RESERVED_MASK                         0x0000FE00
116 #define PACKET_WORD_CRC_ERR_MASK                          0x01000000
117 #define PACKET_WORD_PACKET_TOO_LONG_ERR_MASK              0x02000000
118 #define PACKET_WORD_PACKET_TOO_SHORT_ERR_MASK             0x04000000
119 #define PACKET_WORD_WRONG_INTER_FRAME_GAP_ERR_MASK        0x08000000
120 #define PACKET_WORD_UNALIGNED_FRAME_ERR_MASK              0x10000000
121 #define PACKET_WORD_START_FRAME_DELIMITER_ERR_MASK        0x20000000
122 #define PACKET_WORD_PREAMBLE_ERR_MASK                     0x40000000
123 #define PACKET_WORD_SYMBOL_ERR_MASK                       0x80000000
124
125 static const value_string packet_word_directions[] = {
126         { 0x00, "Not available" },
127         { 0x01, "Inbound" },
128         { 0x02, "Outbound" },
129         { 0x03, "Undefined" },
130         { 0, NULL }
131 };
132
133 static const value_string packet_word_reception_types[] = {
134         { 0x00, "Not specified" },
135         { 0x01, "Unicast" },
136         { 0x02, "Multicast" },
137         { 0x03, "Broadcast" },
138         { 0x04, "Promiscuous" },
139         { 0x05, "Undefined" },
140         { 0x06, "Undefined" },
141         { 0x07, "Undefined" },
142         { 0, NULL }
143 };
144
145 dissector_table_t wtap_encap_dissector_table;
146
147 /*
148  * Routine used to register frame end routine.  The routine should only
149  * be registered when the dissector is used in the frame, not in the
150  * proto_register_XXX function.
151  */
152 void
153 register_frame_end_routine(packet_info *pinfo, void (*func)(void))
154 {
155         pinfo->frame_end_routines = g_slist_append(pinfo->frame_end_routines, (gpointer)func);
156 }
157
158 typedef void (*void_func_t)(void);
159
160 static void
161 call_frame_end_routine(gpointer routine, gpointer dummy _U_)
162 {
163         void_func_t func = (void_func_t)routine;
164         (*func)();
165 }
166
167 static void
168 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
169 {
170         proto_item  *volatile ti = NULL, *comment_item;
171         guint        cap_len = 0, frame_len = 0;
172         proto_tree  *volatile tree;
173         proto_tree  *comments_tree;
174         proto_item  *item;
175         const gchar *cap_plurality, *frame_plurality;
176
177         tree=parent_tree;
178
179         pinfo->current_proto = "Frame";
180
181         if (pinfo->pseudo_header != NULL) {
182                 switch (pinfo->fd->lnk_t) {
183
184                 case WTAP_ENCAP_WFLEET_HDLC:
185                 case WTAP_ENCAP_CHDLC_WITH_PHDR:
186                 case WTAP_ENCAP_PPP_WITH_PHDR:
187                 case WTAP_ENCAP_SDLC:
188                 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
189                         pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ?
190                             P2P_DIR_SENT : P2P_DIR_RECV;
191                         break;
192
193                 case WTAP_ENCAP_BLUETOOTH_HCI:
194                         pinfo->p2p_dir = pinfo->pseudo_header->bthci.sent;
195                         break;
196
197                 case WTAP_ENCAP_LAPB:
198                 case WTAP_ENCAP_FRELAY_WITH_PHDR:
199                         pinfo->p2p_dir =
200                             (pinfo->pseudo_header->x25.flags & FROM_DCE) ?
201                             P2P_DIR_RECV : P2P_DIR_SENT;
202                         break;
203
204                 case WTAP_ENCAP_ISDN:
205                 case WTAP_ENCAP_V5_EF:
206                 case WTAP_ENCAP_DPNSS:
207                 case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
208                         pinfo->p2p_dir = pinfo->pseudo_header->isdn.uton ?
209                             P2P_DIR_SENT : P2P_DIR_RECV;
210                         break;
211
212                 case WTAP_ENCAP_LINUX_LAPD:
213                         pinfo->p2p_dir = (pinfo->pseudo_header->lapd.pkttype == 3 ||
214                                 pinfo->pseudo_header->lapd.pkttype == 4) ?
215                                 P2P_DIR_SENT : P2P_DIR_RECV;
216                         break;
217
218                 case WTAP_ENCAP_MTP2_WITH_PHDR:
219                         pinfo->p2p_dir = pinfo->pseudo_header->mtp2.sent ?
220                             P2P_DIR_SENT : P2P_DIR_RECV;
221                         pinfo->link_number  = pinfo->pseudo_header->mtp2.link_number;
222                         pinfo->annex_a_used = pinfo->pseudo_header->mtp2.annex_a_used;
223                         break;
224
225                 case WTAP_ENCAP_GSM_UM:
226                         pinfo->p2p_dir = pinfo->pseudo_header->gsm_um.uplink ?
227                             P2P_DIR_SENT : P2P_DIR_RECV;
228                         break;
229
230                 }
231         }
232
233         if(pinfo->fd->opt_comment){
234                 item = proto_tree_add_item(tree, proto_pkt_comment, tvb, 0, -1, ENC_NA);
235                 comments_tree = proto_item_add_subtree(item, ett_comments);
236                 comment_item = proto_tree_add_string_format(comments_tree, hf_comments_text, tvb, 0, -1,
237                                                                            pinfo->fd->opt_comment, "%s",
238                                                                            pinfo->fd->opt_comment);
239                 expert_add_info_format_text(pinfo, comment_item, &ei_comments_text,
240                                                                "%s",  pinfo->fd->opt_comment);
241
242
243         }
244
245         /* if FRAME is not referenced from any filters we dont need to worry about
246            generating any tree items.  */
247         if(!proto_field_is_referenced(tree, proto_frame)) {
248                 tree=NULL;
249                 if(pinfo->fd->flags.has_ts) {
250                         if(pinfo->fd->abs_ts.nsecs < 0 || pinfo->fd->abs_ts.nsecs >= 1000000000)
251                                 expert_add_info(pinfo, NULL, &ei_arrive_time_out_of_range);
252                 }
253         } else {
254                 proto_tree *fh_tree;
255                 gboolean old_visible;
256
257                 /* Put in frame header information. */
258                 cap_len = tvb_length(tvb);
259                 frame_len = tvb_reported_length(tvb);
260
261                 cap_plurality = plurality(cap_len, "", "s");
262                 frame_plurality = plurality(frame_len, "", "s");
263
264                 ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, -1,
265                     "Frame %u: %u byte%s on wire",
266                     pinfo->fd->num, frame_len, frame_plurality);
267                 if (generate_bits_field)
268                         proto_item_append_text(ti, " (%u bits)", frame_len * 8);
269                 proto_item_append_text(ti, ", %u byte%s captured",
270                     cap_len, cap_plurality);
271                 if (generate_bits_field) {
272                         proto_item_append_text(ti, " (%u bits)",
273                             cap_len * 8);
274                 }
275                 if (pinfo->fd->flags.has_if_id) {
276                         proto_item_append_text(ti, " on interface %u",
277                             pinfo->fd->interface_id);
278                 }
279                 if (pinfo->fd->flags.has_pack_flags) {
280                         if (pinfo->fd->pack_flags & 0x00000001) {
281                                 proto_item_append_text(ti, " (inbound)");
282                                 pinfo->p2p_dir = P2P_DIR_RECV;
283                         }
284                         if (pinfo->fd->pack_flags & 0x00000002) {
285                                 proto_item_append_text(ti, " (outbound)");
286                                 pinfo->p2p_dir = P2P_DIR_SENT;
287                         }
288                 }
289
290                 fh_tree = proto_item_add_subtree(ti, ett_frame);
291
292                 if (pinfo->fd->flags.has_if_id)
293                         proto_tree_add_uint(fh_tree, hf_frame_interface_id, tvb, 0, 0, pinfo->fd->interface_id);
294
295                 if (pinfo->fd->flags.has_pack_flags) {
296                         proto_tree *flags_tree;
297                         proto_item *flags_item;
298
299                         flags_item = proto_tree_add_uint(fh_tree, hf_frame_pack_flags, tvb, 0, 0, pinfo->fd->pack_flags);
300                         flags_tree = proto_item_add_subtree(flags_item, ett_flags);
301                         proto_tree_add_uint(flags_tree, hf_frame_pack_direction, tvb, 0, 0, pinfo->fd->pack_flags);
302                         proto_tree_add_uint(flags_tree, hf_frame_pack_reception_type, tvb, 0, 0, pinfo->fd->pack_flags);
303                         proto_tree_add_uint(flags_tree, hf_frame_pack_fcs_length, tvb, 0, 0, pinfo->fd->pack_flags);
304                         proto_tree_add_uint(flags_tree, hf_frame_pack_reserved, tvb, 0, 0, pinfo->fd->pack_flags);
305                         proto_tree_add_boolean(flags_tree, hf_frame_pack_crc_error, tvb, 0, 0, pinfo->fd->pack_flags);
306                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_packet_too_long_error, tvb, 0, 0, pinfo->fd->pack_flags);
307                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_packet_too_short_error, tvb, 0, 0, pinfo->fd->pack_flags);
308                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_inter_frame_gap_error, tvb, 0, 0, pinfo->fd->pack_flags);
309                         proto_tree_add_boolean(flags_tree, hf_frame_pack_unaligned_frame_error, tvb, 0, 0, pinfo->fd->pack_flags);
310                         proto_tree_add_boolean(flags_tree, hf_frame_pack_start_frame_delimiter_error, tvb, 0, 0, pinfo->fd->pack_flags);
311                         proto_tree_add_boolean(flags_tree, hf_frame_pack_preamble_error, tvb, 0, 0, pinfo->fd->pack_flags);
312                         proto_tree_add_boolean(flags_tree, hf_frame_pack_symbol_error, tvb, 0, 0, pinfo->fd->pack_flags);
313                 }
314
315                 proto_tree_add_int(fh_tree, hf_frame_wtap_encap, tvb, 0, 0, pinfo->fd->lnk_t);
316
317                 if (pinfo->fd->flags.has_ts) {
318                         proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
319                                             0, 0, &(pinfo->fd->abs_ts));
320                         if(pinfo->fd->abs_ts.nsecs < 0 || pinfo->fd->abs_ts.nsecs >= 1000000000) {
321                                 item = proto_tree_add_none_format(fh_tree, hf_frame_time_invalid, tvb, 0, 0,
322                                                                   "Arrival Time: Fractional second %09ld is invalid,"
323                                                                   " the valid range is 0-1000000000",
324                                                                   (long) pinfo->fd->abs_ts.nsecs);
325                                 PROTO_ITEM_SET_GENERATED(item);
326                                 expert_add_info(pinfo, item, &ei_arrive_time_out_of_range);
327                         }
328                         item = proto_tree_add_time(fh_tree, hf_frame_shift_offset, tvb,
329                                             0, 0, &(pinfo->fd->shift_offset));
330                         PROTO_ITEM_SET_GENERATED(item);
331
332                         if(generate_epoch_time) {
333                                 proto_tree_add_time(fh_tree, hf_frame_arrival_time_epoch, tvb,
334                                                     0, 0, &(pinfo->fd->abs_ts));
335                         }
336
337                         if (proto_field_is_referenced(tree, hf_frame_time_delta)) {
338                                 nstime_t     del_cap_ts;
339
340                                 frame_delta_abs_time(pinfo->fd, pinfo->fd->prev_cap, &del_cap_ts);
341
342                                 item = proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
343                                                            0, 0, &(del_cap_ts));
344                                 PROTO_ITEM_SET_GENERATED(item);
345                         }
346
347                         if (proto_field_is_referenced(tree, hf_frame_time_delta_displayed)) {
348                                 nstime_t del_dis_ts;
349
350                                 frame_delta_abs_time(pinfo->fd, pinfo->fd->prev_dis, &del_dis_ts);
351
352                                 item = proto_tree_add_time(fh_tree, hf_frame_time_delta_displayed, tvb,
353                                                            0, 0, &(del_dis_ts));
354                                 PROTO_ITEM_SET_GENERATED(item);
355                         }
356
357                         item = proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
358                                                    0, 0, &(pinfo->fd->rel_ts));
359                         PROTO_ITEM_SET_GENERATED(item);
360
361                         if(pinfo->fd->flags.ref_time){
362                                 ti = proto_tree_add_item(fh_tree, hf_frame_time_reference, tvb, 0, 0, ENC_NA);
363                                 PROTO_ITEM_SET_GENERATED(ti);
364                         }
365                 }
366
367                 proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
368                                     0, 0, pinfo->fd->num);
369
370                 proto_tree_add_uint_format(fh_tree, hf_frame_len, tvb,
371                                            0, 0, frame_len, "Frame Length: %u byte%s (%u bits)",
372                                            frame_len, frame_plurality, frame_len * 8);
373
374                 proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
375                                            0, 0, cap_len, "Capture Length: %u byte%s (%u bits)",
376                                            cap_len, cap_plurality, cap_len * 8);
377
378                 if (generate_md5_hash) {
379                         const guint8 *cp;
380                         md5_state_t   md_ctx;
381                         md5_byte_t    digest[16];
382                         const gchar  *digest_string;
383
384                         cp = tvb_get_ptr(tvb, 0, cap_len);
385
386                         md5_init(&md_ctx);
387                         md5_append(&md_ctx, cp, cap_len);
388                         md5_finish(&md_ctx, digest);
389
390                         digest_string = bytestring_to_str(digest, 16, '\0');
391                         ti = proto_tree_add_string(fh_tree, hf_frame_md5_hash, tvb, 0, 0, digest_string);
392                         PROTO_ITEM_SET_GENERATED(ti);
393                 }
394
395                 ti = proto_tree_add_boolean(fh_tree, hf_frame_marked, tvb, 0, 0,pinfo->fd->flags.marked);
396                 PROTO_ITEM_SET_GENERATED(ti);
397
398                 ti = proto_tree_add_boolean(fh_tree, hf_frame_ignored, tvb, 0, 0,pinfo->fd->flags.ignored);
399                 PROTO_ITEM_SET_GENERATED(ti);
400
401                 pinfo->curr_layer_num = 0;
402                 if(proto_field_is_referenced(tree, hf_frame_protocols)) {
403                         /* we are going to be using proto_item_append_string() on
404                          * hf_frame_protocols, and we must therefore disable the
405                          * TRY_TO_FAKE_THIS_ITEM() optimisation for the tree by
406                          * setting it as visible.
407                          *
408                          * See proto.h for details.
409                          */
410                         old_visible = proto_tree_set_visible(fh_tree, TRUE);
411                         ti = proto_tree_add_string(fh_tree, hf_frame_protocols, tvb, 0, 0, "");
412                         PROTO_ITEM_SET_GENERATED(ti);
413                         proto_tree_set_visible(fh_tree, old_visible);
414
415                         pinfo->layer_names = g_string_new("");
416                 }
417                 else
418                         pinfo->layer_names = NULL;
419
420                 if(pinfo->fd->pfd != 0){
421                         proto_item *ppd_item;
422                         guint num_entries = g_slist_length(pinfo->fd->pfd);
423                         guint i;
424                         ppd_item = proto_tree_add_uint(fh_tree, hf_frame_num_p_prot_data, tvb, 0, 0, num_entries);
425                         PROTO_ITEM_SET_GENERATED(ppd_item);
426                         for(i=0; i<num_entries; i++){
427                                 proto_tree_add_text (fh_tree, tvb, 0, 0, "%s",p_get_proto_name_and_key(pinfo->fd, i));
428                         }
429                 }
430                 /* Check for existences of P2P pseudo header */
431                 if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
432                         proto_tree_add_int(fh_tree, hf_frame_p2p_dir, tvb,
433                                            0, 0, pinfo->p2p_dir);
434                 }
435
436                 /* Check for existences of MTP2 link number */
437                 if ((pinfo->pseudo_header != NULL ) && (pinfo->fd->lnk_t == WTAP_ENCAP_MTP2_WITH_PHDR)) {
438                         proto_tree_add_uint(fh_tree, hf_link_number, tvb,
439                                             0, 0, pinfo->link_number);
440                 }
441
442                 if (show_file_off) {
443                         proto_tree_add_int64_format(fh_tree, hf_frame_file_off, tvb,
444                                                     0, 0, pinfo->fd->file_off,
445                                                     "File Offset: %" G_GINT64_MODIFIER "d (0x%" G_GINT64_MODIFIER "x)",
446                                                     pinfo->fd->file_off, pinfo->fd->file_off);
447                 }
448
449                 if(pinfo->fd->color_filter != NULL) {
450                         const color_filter_t *color_filter = (const color_filter_t *)pinfo->fd->color_filter;
451                         item = proto_tree_add_string(fh_tree, hf_frame_color_filter_name, tvb,
452                                                      0, 0, color_filter->filter_name);
453                         PROTO_ITEM_SET_GENERATED(item);
454                         item = proto_tree_add_string(fh_tree, hf_frame_color_filter_text, tvb,
455                                                      0, 0, color_filter->filter_text);
456                         PROTO_ITEM_SET_GENERATED(item);
457                 }
458         }
459
460         if (pinfo->fd->flags.ignored) {
461                 /* Ignored package, stop handling here */
462                 col_set_str(pinfo->cinfo, COL_INFO, "<Ignored>");
463                 proto_tree_add_text (tree, tvb, 0, -1, "This frame is marked as ignored");
464                 return;
465         }
466
467         /* Portable Exception Handling to trap Wireshark specific exceptions like BoundsError exceptions */
468         TRY {
469 #ifdef _MSC_VER
470                 /* Win32: Visual-C Structured Exception Handling (SEH) to trap hardware exceptions
471                    like memory access violations.
472                    (a running debugger will be called before the except part below) */
473                 /* Note: A Windows "exceptional exception" may leave the kazlib's (Portable Exception Handling)
474                    stack in an inconsistent state thus causing a crash at some point in the
475                    handling of the exception.
476                    See: https://www.wireshark.org/lists/wireshark-dev/200704/msg00243.html
477                 */
478                 __try {
479 #endif
480                         if ((force_docsis_encap) && (docsis_handle)) {
481                                 call_dissector(docsis_handle, tvb, pinfo, parent_tree);
482                         } else {
483                                 if (!dissector_try_uint(wtap_encap_dissector_table, pinfo->fd->lnk_t,
484                                                         tvb, pinfo, parent_tree)) {
485
486                                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
487                                         col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %d",
488                                                      pinfo->fd->lnk_t);
489                                         call_dissector(data_handle,tvb, pinfo, parent_tree);
490                                 }
491                         }
492 #ifdef _MSC_VER
493                 } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
494                         switch(GetExceptionCode()) {
495                         case(STATUS_ACCESS_VIOLATION):
496                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
497                                                "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
498                                 break;
499                         case(STATUS_INTEGER_DIVIDE_BY_ZERO):
500                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
501                                                "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
502                                 break;
503                         case(STATUS_STACK_OVERFLOW):
504                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
505                                                "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
506                                 /* XXX - this will have probably corrupted the stack,
507                                    which makes problems later in the exception code */
508                                 break;
509                                 /* XXX - add other hardware exception codes as required */
510                         default:
511                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
512                                                g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
513                         }
514                 }
515 #endif
516         }
517         CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
518                 show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
519         }
520         ENDTRY;
521
522         if (tree && pinfo->layer_names) {
523                 proto_item_append_string(ti, pinfo->layer_names->str);
524                 g_string_free(pinfo->layer_names, TRUE);
525                 pinfo->layer_names = NULL;
526         }
527
528         /*  Call postdissectors if we have any (while trying to avoid another
529          *  TRY/CATCH)
530          */
531         if (have_postdissector()) {
532                 TRY {
533 #ifdef _MSC_VER
534                         /* Win32: Visual-C Structured Exception Handling (SEH)
535                            to trap hardware exceptions like memory access violations */
536                         /* (a running debugger will be called before the except part below) */
537                         /* Note: A Windows "exceptional exception" may leave the kazlib's (Portable Exception Handling)
538                            stack in an inconsistent state thus causing a crash at some point in the
539                            handling of the exception.
540                            See: https://www.wireshark.org/lists/wireshark-dev/200704/msg00243.html
541                         */
542                         __try {
543 #endif
544                                 call_all_postdissectors(tvb, pinfo, parent_tree);
545 #ifdef _MSC_VER
546                         } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
547                                 switch(GetExceptionCode()) {
548                                 case(STATUS_ACCESS_VIOLATION):
549                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
550                                                        "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
551                                         break;
552                                 case(STATUS_INTEGER_DIVIDE_BY_ZERO):
553                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
554                                                        "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
555                                         break;
556                                 case(STATUS_STACK_OVERFLOW):
557                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
558                                                        "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
559                                         /* XXX - this will have probably corrupted the stack,
560                                            which makes problems later in the exception code */
561                                         break;
562                                         /* XXX - add other hardware exception codes as required */
563                                 default:
564                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
565                                                        g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
566                                 }
567                         }
568 #endif
569                 }
570                 CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
571                         show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
572                 }
573                 ENDTRY;
574         }
575
576         tap_queue_packet(frame_tap, pinfo, NULL);
577
578
579         if (pinfo->frame_end_routines) {
580                 g_slist_foreach(pinfo->frame_end_routines, &call_frame_end_routine, NULL);
581                 g_slist_free(pinfo->frame_end_routines);
582                 pinfo->frame_end_routines = NULL;
583         }
584 }
585
586 void
587 proto_register_frame(void)
588 {
589         static hf_register_info hf[] = {
590                 { &hf_frame_arrival_time,
591                   { "Arrival Time", "frame.time",
592                     FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0,
593                     "Absolute time when this frame was captured", HFILL }},
594
595                 { &hf_frame_shift_offset,
596                   { "Time shift for this packet", "frame.offset_shift",
597                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
598                     "Time shift applied to this packet", HFILL }},
599
600                 { &hf_frame_arrival_time_epoch,
601                   { "Epoch Time", "frame.time_epoch",
602                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
603                     "Epoch time when this frame was captured", HFILL }},
604
605                 { &hf_frame_time_invalid,
606                   { "Arrival Timestamp invalid", "frame.time_invalid",
607                     FT_NONE, BASE_NONE, NULL, 0x0,
608                     "The timestamp from the capture is out of the valid range", HFILL }},
609
610                 { &hf_frame_time_delta,
611                   { "Time delta from previous captured frame", "frame.time_delta",
612                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
613                     NULL, HFILL }},
614
615                 { &hf_frame_time_delta_displayed,
616                   { "Time delta from previous displayed frame", "frame.time_delta_displayed",
617                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
618                     NULL, HFILL }},
619
620                 { &hf_frame_time_relative,
621                   { "Time since reference or first frame", "frame.time_relative",
622                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
623                     "Time relative to time reference or first frame", HFILL }},
624
625                 { &hf_frame_time_reference,
626                   { "This is a Time Reference frame", "frame.ref_time",
627                     FT_NONE, BASE_NONE, NULL, 0x0,
628                     "This frame is a Time Reference frame", HFILL }},
629
630                 { &hf_frame_number,
631                   { "Frame Number", "frame.number",
632                     FT_UINT32, BASE_DEC, NULL, 0x0,
633                     NULL, HFILL }},
634
635                 { &hf_frame_len,
636                   { "Frame length on the wire", "frame.len",
637                     FT_UINT32, BASE_DEC, NULL, 0x0,
638                     NULL, HFILL }},
639
640                 { &hf_frame_capture_len,
641                   { "Frame length stored into the capture file", "frame.cap_len",
642                     FT_UINT32, BASE_DEC, NULL, 0x0,
643                     NULL, HFILL }},
644
645                 { &hf_frame_md5_hash,
646                   { "Frame MD5 Hash", "frame.md5_hash",
647                     FT_STRING, BASE_NONE, NULL, 0x0,
648                     NULL, HFILL }},
649
650                 { &hf_frame_p2p_dir,
651                   { "Point-to-Point Direction", "frame.p2p_dir",
652                     FT_INT8, BASE_DEC, VALS(p2p_dirs), 0x0,
653                     NULL, HFILL }},
654
655                 { &hf_link_number,
656                   { "Link Number", "frame.link_nr",
657                     FT_UINT16, BASE_DEC, NULL, 0x0,
658                     NULL, HFILL }},
659
660                 { &hf_frame_file_off,
661                   { "File Offset", "frame.file_off",
662                     FT_INT64, BASE_DEC, NULL, 0x0,
663                     NULL, HFILL }},
664
665                 { &hf_frame_marked,
666                   { "Frame is marked", "frame.marked",
667                     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
668                     "Frame is marked in the GUI", HFILL }},
669
670                 { &hf_frame_ignored,
671                   { "Frame is ignored", "frame.ignored",
672                     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
673                     "Frame is ignored by the dissectors", HFILL }},
674
675                 { &hf_frame_protocols,
676                   { "Protocols in frame", "frame.protocols",
677                     FT_STRING, BASE_NONE, NULL, 0x0,
678                     "Protocols carried by this frame", HFILL }},
679
680                 { &hf_frame_color_filter_name,
681                   { "Coloring Rule Name", "frame.coloring_rule.name",
682                     FT_STRING, BASE_NONE, NULL, 0x0,
683                     "The frame matched the coloring rule with this name", HFILL }},
684
685                 { &hf_frame_color_filter_text,
686                   { "Coloring Rule String", "frame.coloring_rule.string",
687                     FT_STRING, BASE_NONE, NULL, 0x0,
688                     "The frame matched this coloring rule string", HFILL }},
689
690                 { &hf_frame_interface_id,
691                   { "Interface id", "frame.interface_id",
692                     FT_UINT32, BASE_DEC, NULL, 0x0,
693                     NULL, HFILL }},
694
695                 { &hf_frame_pack_flags,
696                   { "Packet flags", "frame.packet_flags",
697                     FT_UINT32, BASE_HEX, NULL, 0x0,
698                     NULL, HFILL }},
699
700                 { &hf_frame_pack_direction,
701                   { "Direction", "frame.packet_flags_direction",
702                     FT_UINT32, BASE_HEX, VALS(&packet_word_directions), PACKET_WORD_DIRECTION_MASK,
703                     NULL, HFILL }},
704
705                 { &hf_frame_pack_reception_type,
706                   { "Reception type", "frame.packet_flags_reception_type",
707                     FT_UINT32, BASE_DEC, VALS(&packet_word_reception_types), PACKET_WORD_RECEPTION_TYPE_MASK,
708                     NULL, HFILL }},
709
710                 { &hf_frame_pack_fcs_length,
711                   { "FCS length", "frame.packet_flags_fcs_length",
712                     FT_UINT32, BASE_DEC, NULL, PACKET_WORD_FCS_LENGTH_MASK,
713                     NULL, HFILL }},
714
715                 { &hf_frame_pack_reserved,
716                   { "Reserved", "frame.packet_flags_reserved",
717                     FT_UINT32, BASE_DEC, NULL, PACKET_WORD_RESERVED_MASK,
718                     NULL, HFILL }},
719
720                 { &hf_frame_pack_crc_error,
721                   { "CRC error", "frame.packet_flags_crc_error",
722                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_CRC_ERR_MASK,
723                     NULL, HFILL }},
724
725                 { &hf_frame_pack_wrong_packet_too_long_error,
726                   { "Packet too long error", "frame.packet_flags_packet_too_error",
727                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PACKET_TOO_LONG_ERR_MASK,
728                     NULL, HFILL }},
729
730                 { &hf_frame_pack_wrong_packet_too_short_error,
731                   { "Packet too short error", "frame.packet_flags_packet_too_short_error",
732                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PACKET_TOO_SHORT_ERR_MASK,
733                     NULL, HFILL }},
734
735                 { &hf_frame_pack_wrong_inter_frame_gap_error,
736                   { "Wrong interframe gap error", "frame.packet_flags_wrong_inter_frame_gap_error",
737                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_WRONG_INTER_FRAME_GAP_ERR_MASK,
738                     NULL, HFILL }},
739
740                 { &hf_frame_pack_unaligned_frame_error,
741                   { "Unaligned frame error", "frame.packet_flags_unaligned_frame_error",
742                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_UNALIGNED_FRAME_ERR_MASK,
743                     NULL, HFILL }},
744
745                 { &hf_frame_pack_start_frame_delimiter_error,
746                   { "Start frame delimiter error", "frame.packet_flags_start_frame_delimiter_error",
747                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_START_FRAME_DELIMITER_ERR_MASK,
748                     NULL, HFILL }},
749
750                 { &hf_frame_pack_preamble_error,
751                   { "Preamble error", "frame.packet_flags_preamble_error",
752                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PREAMBLE_ERR_MASK,
753                     NULL, HFILL }},
754
755                 { &hf_frame_pack_symbol_error,
756                   { "Symbol error", "frame.packet_flags_symbol_error",
757                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_SYMBOL_ERR_MASK,
758                     NULL, HFILL }},
759
760                 { &hf_comments_text,
761                   { "Comment", "frame.comment",
762                     FT_STRING, BASE_NONE, NULL, 0x0,
763                     NULL, HFILL }},
764
765                 { &hf_frame_num_p_prot_data,
766                   { "Number of per-protocol-data", "frame.p_prot_data",
767                     FT_UINT32, BASE_DEC, NULL, 0x0,
768                     NULL, HFILL }},
769         };
770         
771         static hf_register_info hf_encap =
772                 { &hf_frame_wtap_encap,
773                   { "Encapsulation type", "frame.encap_type",
774                     FT_INT16, BASE_DEC, NULL, 0x0,
775                     NULL, HFILL }};
776         
777         static gint *ett[] = {
778                 &ett_frame,
779                 &ett_flags,
780                 &ett_comments
781         };
782
783         static ei_register_info ei[] = {
784                 { &ei_comments_text, { "frame.comment.expert", PI_COMMENTS_GROUP, PI_COMMENT, "Formatted comment", EXPFILL }},
785                 { &ei_arrive_time_out_of_range, { "frame.time_invalid.expert", PI_SEQUENCE, PI_NOTE, "Arrival Time: Fractional second out of range (0-1000000000)", EXPFILL }},
786         };
787
788         module_t *frame_module;
789         expert_module_t* expert_frame;
790
791         if (hf_encap.hfinfo.strings == NULL) {
792                 int encap_count = wtap_get_num_encap_types();
793                 value_string *arr;
794                 int i;
795                 
796                 hf_encap.hfinfo.strings = arr = g_new(value_string, encap_count+1); 
797                 
798                 for (i = 0; i < encap_count; i++) {
799                         arr[i].value = i;
800                         arr[i].strptr = wtap_encap_string(i);
801                 }
802                 arr[encap_count].value = 0;
803                 arr[encap_count].strptr = NULL;
804         }
805
806         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
807             "Wiretap encapsulation type", FT_UINT32, BASE_DEC);
808
809         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
810         proto_pkt_comment = proto_register_protocol("Packet comments", "Pkt_Comment", "pkt_comment");
811         proto_register_field_array(proto_frame, hf, array_length(hf));
812         proto_register_field_array(proto_frame, &hf_encap, 1);
813         proto_register_subtree_array(ett, array_length(ett));
814         expert_frame = expert_register_protocol(proto_frame);
815         expert_register_field_array(expert_frame, ei, array_length(ei));
816         register_dissector("frame",dissect_frame,proto_frame);
817
818         /* You can't disable dissection of "Frame", as that would be
819            tantamount to not doing any dissection whatsoever. */
820         proto_set_cant_toggle(proto_frame);
821
822         /* Our preferences */
823         frame_module = prefs_register_protocol(proto_frame, NULL);
824         prefs_register_bool_preference(frame_module, "show_file_off",
825             "Show File Offset", "Show offset of frame in capture file", &show_file_off);
826         prefs_register_bool_preference(frame_module, "force_docsis_encap",
827             "Treat all frames as DOCSIS frames", "Treat all frames as DOCSIS Frames", &force_docsis_encap);
828         prefs_register_bool_preference(frame_module, "generate_md5_hash",
829             "Generate an MD5 hash of each frame",
830             "Whether or not MD5 hashes should be generated for each frame, useful for finding duplicate frames.",
831             &generate_md5_hash);
832         prefs_register_bool_preference(frame_module, "generate_epoch_time",
833             "Generate an epoch time entry for each frame",
834             "Whether or not an Epoch time entry should be generated for each frame.",
835             &generate_epoch_time);
836         prefs_register_bool_preference(frame_module, "generate_bits_field",
837             "Show the number of bits in the frame",
838             "Whether or not the number of bits in the frame should be shown.",
839             &generate_bits_field);
840
841         frame_tap=register_tap("frame");
842 }
843
844 void
845 proto_reg_handoff_frame(void)
846 {
847         data_handle = find_dissector("data");
848         docsis_handle = find_dissector("docsis");
849 }