Handle exception frames.
[obnox/wireshark/wip.git] / packet-arcnet.c
1 /* packet-arcnet.c
2  * Routines for arcnet dissection
3  * Copyright 2001-2002, Peter Fales <ethereal@fales-lorenz.net>
4  *
5  * $Id: packet-arcnet.c,v 1.6 2003/01/23 07:55:28 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include "packet-arcnet.h"
38 #include "arcnet_pids.h"
39 #include "packet-ip.h"
40
41 /* Initialize the protocol and registered fields */
42 static int proto_arcnet = -1;
43 static int hf_arcnet_src = -1;
44 static int hf_arcnet_dst = -1;
45 static int hf_arcnet_offset = -1;
46 static int hf_arcnet_protID = -1;
47 static int hf_arcnet_exception_flag = -1;
48 static int hf_arcnet_split_flag = -1;
49 static int hf_arcnet_sequence = -1;
50
51 /* Initialize the subtree pointers */
52 static gint ett_arcnet = -1;
53
54 static dissector_table_t arcnet_dissector_table;
55 static dissector_handle_t data_handle;
56
57 void
58 capture_arcnet (const guchar *pd, int len, packet_counts *ld,
59                 gboolean has_offset)
60 {
61   int offset = has_offset ? 2 : 4;
62
63   if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
64     ld->other++;
65     return;
66   }
67
68   switch (pd[offset]) {
69
70   case ARCNET_PROTO_IP_1051:
71     /* No fragmentation stuff in the header */
72     capture_ip(pd, offset + 1, len, ld);
73     break;
74
75   case ARCNET_PROTO_IP_1201:
76     /* There's fragmentation stuff in the header */
77     offset++;
78     if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
79       ld->other++;
80       return;
81     }
82     if (pd[offset] == 0xff) {
83       /* This is an exception packet.  The flag value there is the
84          "this is an exception flag" packet; the next two bytes
85          after it are padding, and another copy of the packet
86          type appears after the padding. */
87       offset += 4;
88     }
89     capture_ip(pd, offset + 3, len, ld);
90     break;
91
92   case ARCNET_PROTO_ARP_1051:
93   case ARCNET_PROTO_ARP_1201:
94     ld->arp++;
95     break;
96
97   default:
98     ld->other++;
99     break;
100   }
101 }
102
103 static void
104 dissect_arcnet_common (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
105                        gboolean has_offset)
106 {
107   int offset = 0;
108   guint8 dst, src, protID, split_flag;
109   tvbuff_t *next_tvb;
110   proto_item *ti = NULL;
111   proto_tree *arcnet_tree = NULL;
112
113   if (check_col (pinfo->cinfo, COL_PROTOCOL))
114     col_set_str (pinfo->cinfo, COL_PROTOCOL, "ARCNET");
115
116   if (check_col (pinfo->cinfo, COL_INFO))
117     col_set_str (pinfo->cinfo, COL_INFO, "ARCNET");
118
119   src = tvb_get_guint8 (tvb, 0);
120   dst = tvb_get_guint8 (tvb, 1);
121   SET_ADDRESS(&pinfo->dl_src,   AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
122   SET_ADDRESS(&pinfo->src,      AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
123   SET_ADDRESS(&pinfo->dl_dst,   AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
124   SET_ADDRESS(&pinfo->dst,      AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
125
126   if (tree)
127     {
128       ti =
129         proto_tree_add_item (tree, proto_arcnet, tvb, 0, -1, FALSE);
130
131       arcnet_tree = proto_item_add_subtree (ti, ett_arcnet);
132
133       proto_tree_add_uint (tree, hf_arcnet_src, tvb, offset, 1, src);
134     }
135   offset++;
136
137   if (tree)
138       proto_tree_add_uint (tree, hf_arcnet_dst, tvb, offset, 1, dst);
139   offset++;
140
141   if (has_offset) {
142     if (tree)
143         proto_tree_add_item (tree, hf_arcnet_offset, tvb, offset, 2, FALSE);
144     offset += 2;
145   }
146
147   protID = tvb_get_guint8 (tvb, offset);
148   if (tree)
149       proto_tree_add_uint (tree, hf_arcnet_protID, tvb, offset, 1, protID);
150   offset++;
151
152   switch (protID) {
153
154   case ARCNET_PROTO_IP_1051:
155   case ARCNET_PROTO_ARP_1051:
156   case ARCNET_PROTO_DIAGNOSE:
157     /* No fragmentation stuff in the header */
158     break;
159
160   default:
161     /* Show the fragmentation stuff - flag and sequence ID */
162     split_flag = tvb_get_guint8 (tvb, offset);
163     if (split_flag == 0xff) {
164       /* This is an exception packet.  The flag value there is the
165          "this is an exception flag" packet; the next two bytes
166          after it are padding. */
167       if (tree) {
168         proto_tree_add_uint (tree, hf_arcnet_exception_flag, tvb, offset, 1,
169                              split_flag);
170       }
171       offset++;  
172
173       if (tree)
174         proto_tree_add_text (tree, tvb, offset, 2, "Padding");
175       offset += 2;
176
177       /* Another copy of the packet type appears after the padding. */
178       if (tree)
179         proto_tree_add_item (tree, hf_arcnet_protID, tvb, offset, 1, FALSE);
180       offset++;
181
182       /* And after that comes the real split flag. */
183       split_flag = tvb_get_guint8 (tvb, offset);
184     } 
185     if (tree) {
186       proto_tree_add_uint (tree, hf_arcnet_split_flag, tvb, offset, 1,
187                            split_flag);
188     }
189     offset++;
190                           
191     if (tree)
192       proto_tree_add_item (tree, hf_arcnet_sequence, tvb, offset, 2, FALSE);
193     offset += 2;
194
195     break;
196   }
197
198   /* Set the length of the ARCNET header protocol tree item. */
199   if (tree)
200     proto_item_set_len(ti, offset);
201   
202   next_tvb = tvb_new_subset (tvb, offset, -1, -1);
203
204   if (!dissector_try_port (arcnet_dissector_table, protID,
205                            next_tvb, pinfo, tree))
206     {
207       if (check_col (pinfo->cinfo, COL_PROTOCOL))
208         {
209           col_add_fstr (pinfo->cinfo, COL_PROTOCOL, "0x%04x", protID);
210         }
211       call_dissector (data_handle, next_tvb, pinfo, tree);
212     }
213
214 }
215
216 /*
217  * BSD-style ARCNET headers - they don't have the offset field from the
218  * ARCNET hardware packet.
219  */
220 static void
221 dissect_arcnet (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
222 {
223         dissect_arcnet_common (tvb, pinfo, tree, FALSE);
224 }
225
226 /*
227  * Linux-style ARCNET headers - they *do* have the offset field from the
228  * ARCNET hardware packet.
229  */
230 static void
231 dissect_arcnet_linux (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
232 {
233         dissect_arcnet_common (tvb, pinfo, tree, TRUE);
234 }
235
236 static const value_string arcnet_prot_id_vals[] = {
237   {ARCNET_PROTO_IP_1051,          "RFC 1051 IP"},
238   {ARCNET_PROTO_ARP_1051,         "RFC 1051 ARP"},
239   {ARCNET_PROTO_IP_1201,          "RFC 1201 IP"},
240   {ARCNET_PROTO_ARP_1201,         "RFC 1201 ARP"},
241   {ARCNET_PROTO_RARP_1201,        "RFC 1201 RARP"},
242   {ARCNET_PROTO_IPX,              "IPX"},
243   {ARCNET_PROTO_NOVELL_EC,        "Novell of some sort"},
244   {ARCNET_PROTO_IPv6,             "IPv6"},
245   {ARCNET_PROTO_ETHERNET,         "Encapsulated Ethernet"},
246   {ARCNET_PROTO_DATAPOINT_BOOT,   "Datapoint boot"},
247   {ARCNET_PROTO_DATAPOINT_MOUNT,  "Datapoint mount"},
248   {ARCNET_PROTO_POWERLAN_BEACON,  "PowerLAN beacon"},
249   {ARCNET_PROTO_POWERLAN_BEACON2, "PowerLAN beacon2"},
250   {ARCNET_PROTO_APPLETALK,        "Appletalk"},
251   {ARCNET_PROTO_BANYAN,           "Banyan VINES"},
252   {ARCNET_PROTO_DIAGNOSE,         "Diagnose"},
253   {0,                             NULL}
254 };
255
256 void
257 proto_register_arcnet (void)
258 {
259
260 /* Setup list of header fields  See Section 1.6.1 for details*/
261   static hf_register_info hf[] = {
262     {&hf_arcnet_src,
263      {"Source", "arcnet.src",
264       FT_UINT8, BASE_HEX, NULL, 0,
265       "Source ID", HFILL}
266      },
267     {&hf_arcnet_dst,
268      {"Dest", "arcnet.dst",
269       FT_UINT8, BASE_HEX, NULL, 0,
270       "Dest ID", HFILL}
271      },
272     {&hf_arcnet_offset,
273      {"Offset", "arcnet.offset",
274       FT_BYTES, BASE_NONE, NULL, 0,
275       "Offset", HFILL}
276      },
277     {&hf_arcnet_protID,
278      {"Protocol ID", "arcnet.protID",
279       FT_UINT8, BASE_HEX, VALS(arcnet_prot_id_vals), 0,
280       "Proto type", HFILL}
281      },
282     {&hf_arcnet_split_flag,
283      {"Split Flag", "arcnet.split_flag",
284       FT_UINT8, BASE_DEC, NULL, 0,
285       "Split flag", HFILL}
286      },
287     {&hf_arcnet_exception_flag,
288      {"Exception Flag", "arcnet.exception_flag",
289       FT_UINT8, BASE_HEX, NULL, 0,
290       "Exception flag", HFILL}
291      },
292     {&hf_arcnet_sequence,
293      {"Sequence", "arcnet.sequence",
294       FT_UINT16, BASE_DEC, NULL, 0,
295       "Sequence number", HFILL}
296      },
297   };
298
299 /* Setup protocol subtree array */
300   static gint *ett[] = {
301     &ett_arcnet,
302   };
303
304   arcnet_dissector_table = register_dissector_table ("arcnet.protocol_id",
305                                                      "ARCNET Protocol ID",
306                                                      FT_UINT8, BASE_HEX);
307
308 /* Register the protocol name and description */
309   proto_arcnet = proto_register_protocol ("ARCNET", "ARCNET", "arcnet");
310
311 /* Required function calls to register the header fields and subtrees used */
312   proto_register_field_array (proto_arcnet, hf, array_length (hf));
313   proto_register_subtree_array (ett, array_length (ett));
314 }
315
316
317 void
318 proto_reg_handoff_arcnet (void)
319 {
320   dissector_handle_t arcnet_handle, arcnet_linux_handle;
321
322   arcnet_handle = create_dissector_handle (dissect_arcnet, proto_arcnet);
323   dissector_add ("wtap_encap", WTAP_ENCAP_ARCNET, arcnet_handle);
324
325   arcnet_linux_handle = create_dissector_handle (dissect_arcnet_linux,
326                                                  proto_arcnet);
327   dissector_add ("wtap_encap", WTAP_ENCAP_ARCNET_LINUX, arcnet_linux_handle);
328   data_handle = find_dissector ("data");
329 }