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