Rename samr_dissect_LOGON_HOURS() to dissect_ndr_nt_LOGON_HOURS() and
[obnox/wireshark/wip.git] / packet-ieee8023.c
1 /* packet-ieee8023.c
2  * Routine for dissecting 802.3 (as opposed to D/I/X Ethernet) packets.
3  *
4  * $Id: packet-ieee8023.c,v 1.2 2002/01/21 07:36:35 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "packet-ieee8023.h"
36
37 static dissector_handle_t ipx_handle;
38 static dissector_handle_t llc_handle;
39
40 static void add_trailer(proto_tree *fh_tree, int trailer_id,
41     tvbuff_t *trailer_tvb);
42
43 void
44 dissect_802_3(int length, gboolean is_802_2, tvbuff_t *tvb,
45               int offset_after_length, packet_info *pinfo, proto_tree *tree,
46               proto_tree *fh_tree, int length_id, int trailer_id)
47 {
48   tvbuff_t              *volatile next_tvb;
49   tvbuff_t              *volatile trailer_tvb;
50
51   if (fh_tree)
52     proto_tree_add_uint(fh_tree, length_id, tvb, offset_after_length - 2, 2,
53                         length);
54
55   /* Give the next dissector only 'length' number of bytes */
56   TRY {
57     next_tvb = tvb_new_subset(tvb, offset_after_length, length, length);
58     trailer_tvb = tvb_new_subset(tvb, offset_after_length + length, -1, -1);
59   }
60   CATCH2(BoundsError, ReportedBoundsError) {
61     /* Either:
62
63           the packet doesn't have "length" bytes worth of
64           captured data left in it - or it may not even have
65           "length" bytes worth of data in it, period -
66           so the "tvb_new_subset()" creating "next_tvb"
67           threw an exception
68
69        or
70
71           the packet has exactly "length" bytes worth of
72           captured data left in it, so the "tvb_new_subset()"
73           creating "trailer_tvb" threw an exception.
74
75        In either case, this means that all the data in the frame
76        is within the length value, so we give all the data to the
77        next protocol and have no trailer. */
78     next_tvb = tvb_new_subset(tvb, offset_after_length, -1, length);
79     trailer_tvb = NULL;
80   }
81   ENDTRY;
82
83   /* Dissect the payload either as IPX or as an LLC frame.
84      Catch BoundsError and ReportedBoundsError, so that if the
85      reported length of "next_tvb" was reduced by some dissector
86      before an exception was thrown, we can still put in an item
87      for the trailer. */
88   TRY {
89     if (is_802_2)
90       call_dissector(llc_handle, next_tvb, pinfo, tree);
91     else
92       call_dissector(ipx_handle, next_tvb, pinfo, tree);
93   }
94   CATCH2(BoundsError, ReportedBoundsError) {
95     /* Well, somebody threw an exception.  Add the trailer, if appropriate. */
96     add_trailer(fh_tree, trailer_id, trailer_tvb);
97
98     /* Rethrow the exception, so the "Short Frame" or "Mangled Frame"
99        indication can be put into the tree. */
100     RETHROW;
101
102     /* XXX - RETHROW shouldn't return. */
103     g_assert_not_reached();
104   }
105   ENDTRY;
106
107   add_trailer(fh_tree, trailer_id, trailer_tvb);
108 }
109
110 static void
111 add_trailer(proto_tree *fh_tree, int trailer_id, tvbuff_t *trailer_tvb)
112 {
113   /* If there's some bytes left over, mark them. */
114   if (trailer_tvb && fh_tree) {
115     guint trailer_length;
116
117     trailer_length = tvb_length(trailer_tvb);
118     if (trailer_length != 0) {
119       proto_tree_add_item(fh_tree, trailer_id, trailer_tvb, 0,
120                           trailer_length, FALSE);
121     }
122   }
123 }
124
125 void
126 proto_reg_handoff_ieee802_3(void)
127 {
128         /*
129          * Get handles for the IPX and LLC dissectors.
130          */
131         ipx_handle = find_dissector("ipx");
132         llc_handle = find_dissector("llc");
133 }