Set the svn:eol-style property on all text files to "native", so that
[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$
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 #include <glib.h>
30 #include <epan/packet.h>
31 #include "packet-ieee8023.h"
32 #include "packet-eth.h"
33 #include "packet-frame.h"
34
35 static dissector_handle_t ipx_handle;
36 static dissector_handle_t llc_handle;
37
38 void
39 dissect_802_3(int length, gboolean is_802_2, tvbuff_t *tvb,
40               int offset_after_length, packet_info *pinfo, proto_tree *tree,
41               proto_tree *fh_tree, int length_id, int trailer_id,
42               int fcs_len)
43 {
44   tvbuff_t              *volatile next_tvb;
45   tvbuff_t              *volatile trailer_tvb;
46   const char            *saved_proto;
47
48   if (fh_tree)
49     proto_tree_add_uint(fh_tree, length_id, tvb, offset_after_length - 2, 2,
50                         length);
51
52   /* Give the next dissector only 'length' number of bytes */
53   TRY {
54     next_tvb = tvb_new_subset(tvb, offset_after_length, length, length);
55     trailer_tvb = tvb_new_subset(tvb, offset_after_length + length, -1, -1);
56   }
57   CATCH2(BoundsError, ReportedBoundsError) {
58     /* Either:
59
60           the packet doesn't have "length" bytes worth of
61           captured data left in it - or it may not even have
62           "length" bytes worth of data in it, period -
63           so the "tvb_new_subset()" creating "next_tvb"
64           threw an exception
65
66        or
67
68           the packet has exactly "length" bytes worth of
69           captured data left in it, so the "tvb_new_subset()"
70           creating "trailer_tvb" threw an exception.
71
72        In either case, this means that all the data in the frame
73        is within the length value, so we give all the data to the
74        next protocol and have no trailer. */
75     next_tvb = tvb_new_subset(tvb, offset_after_length, -1, length);
76     trailer_tvb = NULL;
77   }
78   ENDTRY;
79
80   /* Dissect the payload either as IPX or as an LLC frame.
81      Catch BoundsError and ReportedBoundsError, so that if the
82      reported length of "next_tvb" was reduced by some dissector
83      before an exception was thrown, we can still put in an item
84      for the trailer. */
85   saved_proto = pinfo->current_proto;
86   TRY {
87     if (is_802_2)
88       call_dissector(llc_handle, next_tvb, pinfo, tree);
89     else
90       call_dissector(ipx_handle, next_tvb, pinfo, tree);
91   }
92   CATCH(BoundsError) {
93    /* Somebody threw BoundsError, which means that dissecting the payload
94       found that the packet was cut off by a snapshot length before the
95       end of the payload.  The trailer comes after the payload, so *all*
96       of the trailer is cut off - don't bother adding the trailer, just
97       rethrow the exception so it gets reported. */
98    RETHROW;
99   }
100   CATCH_ALL {
101     /* Well, somebody threw an exception other than BoundsError.
102        Show the exception, and then drive on to show the trailer,
103        restoring the protocol value that was in effect before we
104        called the subdissector. */
105     show_exception(next_tvb, pinfo, tree, EXCEPT_CODE);
106     pinfo->current_proto = saved_proto;
107   }
108   ENDTRY;
109
110   add_ethernet_trailer(fh_tree, trailer_id, tvb, trailer_tvb, fcs_len);
111 }
112
113 void
114 proto_reg_handoff_ieee802_3(void)
115 {
116   /*
117    * Get handles for the IPX and LLC dissectors.
118    */
119   ipx_handle = find_dissector("ipx");
120   llc_handle = find_dissector("llc");
121 }