Add a "capture_arcnet()" routine and use it when capturing.
[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.5 2003/01/23 06:57:37 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_split_flag = -1;
48 static int hf_arcnet_sequence = -1;
49
50 /* Initialize the subtree pointers */
51 static gint ett_arcnet = -1;
52
53 static dissector_table_t arcnet_dissector_table;
54 static dissector_handle_t data_handle;
55
56 void
57 capture_arcnet (const guchar *pd, int len, packet_counts *ld,
58                 gboolean has_offset)
59 {
60   int offset = has_offset ? 2 : 4;
61
62   if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
63     ld->other++;
64     return;
65   }
66
67   switch (pd[offset]) {
68
69   case ARCNET_PROTO_IP_1051:
70     /* No fragmentation stuff in the header */
71     capture_ip(pd, offset + 1, len, ld);
72     break;
73
74   case ARCNET_PROTO_IP_1201:
75     /* There's fragmentation stuff in the header */
76     capture_ip(pd, offset + 4, len, ld);
77     break;
78
79   case ARCNET_PROTO_ARP_1051:
80   case ARCNET_PROTO_ARP_1201:
81     ld->arp++;
82     break;
83
84   default:
85     ld->other++;
86     break;
87   }
88 }
89
90 static void
91 dissect_arcnet_common (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
92                        gboolean has_offset)
93 {
94   int offset = 0;
95   guint8 dst, src, protID;
96   tvbuff_t *next_tvb;
97   proto_item *ti = NULL;
98   proto_tree *arcnet_tree = NULL;
99
100   if (check_col (pinfo->cinfo, COL_PROTOCOL))
101     col_set_str (pinfo->cinfo, COL_PROTOCOL, "ARCNET");
102
103   if (check_col (pinfo->cinfo, COL_INFO))
104     col_set_str (pinfo->cinfo, COL_INFO, "ARCNET");
105
106   src = tvb_get_guint8 (tvb, 0);
107   dst = tvb_get_guint8 (tvb, 1);
108   SET_ADDRESS(&pinfo->dl_src,   AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
109   SET_ADDRESS(&pinfo->src,      AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
110   SET_ADDRESS(&pinfo->dl_dst,   AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
111   SET_ADDRESS(&pinfo->dst,      AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
112
113   if (tree)
114     {
115       ti =
116         proto_tree_add_item (tree, proto_arcnet, tvb, 0, -1, FALSE);
117
118       arcnet_tree = proto_item_add_subtree (ti, ett_arcnet);
119
120       proto_tree_add_uint (tree, hf_arcnet_src, tvb, offset, 1, src);
121     }
122   offset++;
123
124   if (tree)
125       proto_tree_add_uint (tree, hf_arcnet_dst, tvb, offset, 1, dst);
126   offset++;
127
128   if (has_offset) {
129     if (tree)
130         proto_tree_add_item (tree, hf_arcnet_offset, tvb, offset, 2, FALSE);
131     offset += 2;
132   }
133
134   protID = tvb_get_guint8 (tvb, offset);
135   if (tree)
136       proto_tree_add_uint (tree, hf_arcnet_protID, tvb, offset, 1, protID);
137   offset++;
138
139   switch (protID) {
140
141   case ARCNET_PROTO_IP_1051:
142   case ARCNET_PROTO_ARP_1051:
143   case ARCNET_PROTO_DIAGNOSE:
144     /* No fragmentation stuff in the header */
145     break;
146
147   default:
148     /* Show the fragmentation stuff - flag and sequence ID */
149     if (tree) {
150       proto_tree_add_item (tree, hf_arcnet_split_flag, tvb, offset, 1, FALSE);
151       proto_tree_add_item (tree, hf_arcnet_sequence, tvb, offset, 2, FALSE);
152     }
153     offset += 3;
154     break;
155   }
156
157   /* Set the length of the ARCNET header protocol tree item. */
158   if (tree)
159     proto_item_set_len(ti, offset);
160   
161   next_tvb = tvb_new_subset (tvb, offset, -1, -1);
162
163   if (!dissector_try_port (arcnet_dissector_table, protID,
164                            next_tvb, pinfo, tree))
165     {
166       if (check_col (pinfo->cinfo, COL_PROTOCOL))
167         {
168           col_add_fstr (pinfo->cinfo, COL_PROTOCOL, "0x%04x", protID);
169         }
170       call_dissector (data_handle, next_tvb, pinfo, tree);
171     }
172
173 }
174
175 /*
176  * BSD-style ARCNET headers - they don't have the offset field from the
177  * ARCNET hardware packet.
178  */
179 static void
180 dissect_arcnet (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
181 {
182         dissect_arcnet_common (tvb, pinfo, tree, FALSE);
183 }
184
185 /*
186  * Linux-style ARCNET headers - they *do* have the offset field from the
187  * ARCNET hardware packet.
188  */
189 static void
190 dissect_arcnet_linux (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
191 {
192         dissect_arcnet_common (tvb, pinfo, tree, TRUE);
193 }
194
195 static const value_string arcnet_prot_id_vals[] = {
196   {ARCNET_PROTO_IP_1051,          "RFC 1051 IP"},
197   {ARCNET_PROTO_ARP_1051,         "RFC 1051 ARP"},
198   {ARCNET_PROTO_IP_1201,          "RFC 1201 IP"},
199   {ARCNET_PROTO_ARP_1201,         "RFC 1201 ARP"},
200   {ARCNET_PROTO_RARP_1201,        "RFC 1201 RARP"},
201   {ARCNET_PROTO_IPX,              "IPX"},
202   {ARCNET_PROTO_NOVELL_EC,        "Novell of some sort"},
203   {ARCNET_PROTO_IPv6,             "IPv6"},
204   {ARCNET_PROTO_ETHERNET,         "Encapsulated Ethernet"},
205   {ARCNET_PROTO_DATAPOINT_BOOT,   "Datapoint boot"},
206   {ARCNET_PROTO_DATAPOINT_MOUNT,  "Datapoint mount"},
207   {ARCNET_PROTO_POWERLAN_BEACON,  "PowerLAN beacon"},
208   {ARCNET_PROTO_POWERLAN_BEACON2, "PowerLAN beacon2"},
209   {ARCNET_PROTO_APPLETALK,        "Appletalk"},
210   {ARCNET_PROTO_BANYAN,           "Banyan VINES"},
211   {ARCNET_PROTO_DIAGNOSE,         "Diagnose"},
212   {0,                             NULL}
213 };
214
215 void
216 proto_register_arcnet (void)
217 {
218
219 /* Setup list of header fields  See Section 1.6.1 for details*/
220   static hf_register_info hf[] = {
221     {&hf_arcnet_src,
222      {"Source", "arcnet.src",
223       FT_UINT8, BASE_HEX, NULL, 0,
224       "Source ID", HFILL}
225      },
226     {&hf_arcnet_dst,
227      {"Dest", "arcnet.dst",
228       FT_UINT8, BASE_HEX, NULL, 0,
229       "Dest ID", HFILL}
230      },
231     {&hf_arcnet_offset,
232      {"Offset", "arcnet.offset",
233       FT_BYTES, BASE_NONE, NULL, 0,
234       "Offset", HFILL}
235      },
236     {&hf_arcnet_protID,
237      {"Protocol ID", "arcnet.protID",
238       FT_UINT8, BASE_HEX, VALS(arcnet_prot_id_vals), 0,
239       "Proto type", HFILL}
240      },
241     {&hf_arcnet_split_flag,
242      {"Split Flag", "arcnet.split_flag",
243       FT_UINT8, BASE_DEC, NULL, 0,
244       "Split flag", HFILL}
245      },
246     {&hf_arcnet_sequence,
247      {"Sequence", "arcnet.sequence",
248       FT_UINT16, BASE_DEC, NULL, 0,
249       "Sequence number", HFILL}
250      },
251   };
252
253 /* Setup protocol subtree array */
254   static gint *ett[] = {
255     &ett_arcnet,
256   };
257
258   arcnet_dissector_table = register_dissector_table ("arcnet.protocol_id",
259                                                      "ARCNET Protocol ID",
260                                                      FT_UINT8, BASE_HEX);
261
262 /* Register the protocol name and description */
263   proto_arcnet = proto_register_protocol ("ARCNET", "ARCNET", "arcnet");
264
265 /* Required function calls to register the header fields and subtrees used */
266   proto_register_field_array (proto_arcnet, hf, array_length (hf));
267   proto_register_subtree_array (ett, array_length (ett));
268 }
269
270
271 void
272 proto_reg_handoff_arcnet (void)
273 {
274   dissector_handle_t arcnet_handle, arcnet_linux_handle;
275
276   arcnet_handle = create_dissector_handle (dissect_arcnet, proto_arcnet);
277   dissector_add ("wtap_encap", WTAP_ENCAP_ARCNET, arcnet_handle);
278
279   arcnet_linux_handle = create_dissector_handle (dissect_arcnet_linux,
280                                                  proto_arcnet);
281   dissector_add ("wtap_encap", WTAP_ENCAP_ARCNET_LINUX, arcnet_linux_handle);
282   data_handle = find_dissector ("data");
283 }