MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-aarp.c
blob5ed3b09d211d83c65a525b803d32f5925efb6e19
1 /* packet-aarp.c
2 * Routines for Appletalk ARP packet disassembly
4 * $Id$
6 * Simon Wilkinson <sxw@dcs.ed.ac.uk>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #include <epan/packet.h>
30 #include <epan/strutil.h>
31 #include <epan/wmem/wmem.h>
32 #include <epan/etypes.h>
33 #include <epan/to_str.h>
35 /* Forward declarations */
36 void proto_register_aarp(void);
37 void proto_reg_handoff_aarp(void);
39 static int proto_aarp = -1;
40 static int hf_aarp_hard_type = -1;
41 static int hf_aarp_proto_type = -1;
42 static int hf_aarp_hard_size = -1;
43 static int hf_aarp_proto_size = -1;
44 static int hf_aarp_opcode = -1;
45 static int hf_aarp_src_hw = -1;
46 static int hf_aarp_src_hw_mac = -1;
47 static int hf_aarp_src_proto = -1;
48 static int hf_aarp_src_proto_id = -1;
49 static int hf_aarp_dst_hw = -1;
50 static int hf_aarp_dst_hw_mac = -1;
51 static int hf_aarp_dst_proto = -1;
52 static int hf_aarp_dst_proto_id = -1;
54 static gint ett_aarp = -1;
56 #ifndef AARP_REQUEST
57 #define AARP_REQUEST 0x0001
58 #endif
59 #ifndef AARP_REPLY
60 #define AARP_REPLY 0x0002
61 #endif
62 #ifndef AARP_PROBE
63 #define AARP_PROBE 0x0003
64 #endif
66 /* The following is screwed up shit to deal with the fact that
67 the linux kernel edits the packet inline. */
68 #define AARP_REQUEST_SWAPPED 0x0100
69 #define AARP_REPLY_SWAPPED 0x0200
70 #define AARP_PROBE_SWAPPED 0x0300
72 static const value_string op_vals[] = {
73 {AARP_REQUEST, "request" },
74 {AARP_REPLY, "reply" },
75 {AARP_PROBE, "probe" },
76 {AARP_REQUEST_SWAPPED, "request" },
77 {AARP_REPLY_SWAPPED, "reply" },
78 {AARP_PROBE_SWAPPED, "probe" },
79 {0, NULL } };
81 /* AARP protocol HARDWARE identifiers. */
82 #define AARPHRD_ETHER 1 /* Ethernet 10Mbps */
83 #define AARPHRD_TR 2 /* Token Ring */
85 static const value_string hrd_vals[] = {
86 {AARPHRD_ETHER, "Ethernet" },
87 {AARPHRD_TR, "Token Ring" },
88 {0, NULL } };
91 * Given the hardware address type and length, check whether an address
92 * is an Ethernet address - the address must be of type "Ethernet" or
93 * "Token Ring", and the length must be 6 bytes.
95 #define AARP_HW_IS_ETHER(ar_hrd, ar_hln) \
96 (((ar_hrd) == AARPHRD_ETHER || (ar_hrd) == AARPHRD_TR) \
97 && (ar_hln) == 6)
100 * Given the protocol address type and length, check whether an address
101 * is an Appletalk address - the address must be of type "Appletalk",
102 * and the length must be 4 bytes.
104 #define AARP_PRO_IS_ATALK(ar_pro, ar_pln) \
105 ((ar_pro) == ETHERTYPE_ATALK && (ar_pln) == 4)
107 static gchar *
108 tvb_atalkid_to_str(tvbuff_t *tvb, gint offset)
110 gint node;
111 gchar *cur;
113 cur=(gchar *)wmem_alloc(wmem_packet_scope(), 16);
114 node=tvb_get_guint8(tvb, offset)<<8|tvb_get_guint8(tvb, offset+1);
115 g_snprintf(cur, 16, "%d.%d",node,tvb_get_guint8(tvb, offset+2));
116 return cur;
119 static const gchar *
120 tvb_aarphrdaddr_to_str(tvbuff_t *tvb, gint offset, int ad_len, guint16 type)
122 if (AARP_HW_IS_ETHER(type, ad_len)) {
123 /* Ethernet address (or Token Ring address, which is the same type
124 of address). */
125 return tvb_ether_to_str(tvb, offset);
127 return tvb_bytes_to_str(tvb, offset, ad_len);
130 static gchar *
131 tvb_aarpproaddr_to_str(tvbuff_t *tvb, gint offset, int ad_len, guint16 type)
133 if (AARP_PRO_IS_ATALK(type, ad_len)) {
134 /* Appletalk address. */
135 return tvb_atalkid_to_str(tvb, offset);
137 return tvb_bytes_to_str(tvb, offset, ad_len);
140 /* Offsets of fields within an AARP packet. */
141 #define AR_HRD 0
142 #define AR_PRO 2
143 #define AR_HLN 4
144 #define AR_PLN 5
145 #define AR_OP 6
146 #define MIN_AARP_HEADER_SIZE 8
148 static void
149 dissect_aarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
150 guint16 ar_hrd;
151 guint16 ar_pro;
152 guint8 ar_hln;
153 guint8 ar_pln;
154 guint16 ar_op;
155 proto_tree *aarp_tree;
156 proto_item *ti;
157 const gchar *op_str;
158 int sha_offset, spa_offset, tha_offset, tpa_offset;
159 const gchar *sha_str, *spa_str, /* *tha_str, */ *tpa_str;
161 col_set_str(pinfo->cinfo, COL_PROTOCOL, "AARP");
162 col_clear(pinfo->cinfo, COL_INFO);
164 ar_hrd = tvb_get_ntohs(tvb, AR_HRD);
165 ar_pro = tvb_get_ntohs(tvb, AR_PRO);
166 ar_hln = tvb_get_guint8(tvb, AR_HLN);
167 ar_pln = tvb_get_guint8(tvb, AR_PLN);
168 ar_op = tvb_get_ntohs(tvb, AR_OP);
170 /* Get the offsets of the addresses. */
171 sha_offset = MIN_AARP_HEADER_SIZE;
172 spa_offset = sha_offset + ar_hln;
173 tha_offset = spa_offset + ar_pln;
174 tpa_offset = tha_offset + ar_hln;
176 /* Extract the addresses. */
177 sha_str = tvb_aarphrdaddr_to_str(tvb, sha_offset, ar_hln, ar_hrd);
178 spa_str = tvb_aarpproaddr_to_str(tvb, spa_offset, ar_pln, ar_pro);
179 #if 0
180 /* TODO: tha_str is currently not shown nor parsed */
181 tha_str = tvb_aarphrdaddr_to_str(tvb, tha_offset, ar_hln, ar_hrd);
182 #endif
183 tpa_str = tvb_aarpproaddr_to_str(tvb, tpa_offset, ar_pln, ar_pro);
185 switch (ar_op) {
186 case AARP_REQUEST:
187 case AARP_REQUEST_SWAPPED:
188 col_add_fstr(pinfo->cinfo, COL_INFO, "Who has %s? Tell %s", tpa_str, spa_str);
189 break;
190 case AARP_REPLY:
191 case AARP_REPLY_SWAPPED:
192 col_add_fstr(pinfo->cinfo, COL_INFO, "%s is at %s", spa_str, sha_str);
193 break;
194 case AARP_PROBE:
195 case AARP_PROBE_SWAPPED:
196 col_add_fstr(pinfo->cinfo, COL_INFO, "Is there a %s", tpa_str);
197 break;
198 default:
199 col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
200 break;
203 if (tree) {
204 if ((op_str = try_val_to_str(ar_op, op_vals)))
205 ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
206 MIN_AARP_HEADER_SIZE + 2*ar_hln +
207 2*ar_pln, "AppleTalk Address Resolution Protocol (%s)", op_str);
208 else
209 ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
210 MIN_AARP_HEADER_SIZE + 2*ar_hln +
211 2*ar_pln,
212 "AppleTalk Address Resolution Protocol (opcode 0x%04x)", ar_op);
213 aarp_tree = proto_item_add_subtree(ti, ett_aarp);
214 proto_tree_add_uint(aarp_tree, hf_aarp_hard_type, tvb, AR_HRD, 2,
215 ar_hrd);
216 proto_tree_add_uint(aarp_tree, hf_aarp_proto_type, tvb, AR_PRO, 2,
217 ar_pro);
218 proto_tree_add_uint(aarp_tree, hf_aarp_hard_size, tvb, AR_HLN, 1,
219 ar_hln);
220 proto_tree_add_uint(aarp_tree, hf_aarp_proto_size, tvb, AR_PLN, 1,
221 ar_pln);
222 proto_tree_add_uint(aarp_tree, hf_aarp_opcode, tvb, AR_OP, 2,
223 ar_op);
224 if (ar_hln != 0) {
225 proto_tree_add_item(aarp_tree,
226 AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_src_hw_mac : hf_aarp_src_hw,
227 tvb, sha_offset, ar_hln, ENC_NA);
230 if (ar_pln != 0) {
231 if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
232 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_src_proto_id, tvb,
233 spa_offset, ar_pln, NULL,
234 "%s", spa_str);
235 } else {
236 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_src_proto, tvb,
237 spa_offset, ar_pln, NULL,
238 "%s", spa_str);
242 if (ar_hln != 0) {
243 proto_tree_add_item(aarp_tree,
244 AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_dst_hw_mac : hf_aarp_dst_hw,
245 tvb, tha_offset, ar_hln, ENC_NA);
248 if (ar_pln != 0) {
249 if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
250 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto_id, tvb,
251 tpa_offset, ar_pln,
252 NULL, "%s", tpa_str);
253 } else {
254 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto, tvb,
255 tpa_offset, ar_pln,
256 NULL, "%s", tpa_str);
262 void
263 proto_register_aarp(void)
265 static hf_register_info hf[] = {
266 { &hf_aarp_hard_type,
267 { "Hardware type", "aarp.hard.type",
268 FT_UINT16, BASE_HEX, VALS(hrd_vals), 0x0,
269 NULL, HFILL }},
271 { &hf_aarp_proto_type,
272 { "Protocol type", "aarp.proto.type",
273 FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
274 NULL, HFILL }},
276 { &hf_aarp_hard_size,
277 { "Hardware size", "aarp.hard.size",
278 FT_UINT8, BASE_DEC, NULL, 0x0,
279 NULL, HFILL }},
281 { &hf_aarp_proto_size,
282 { "Protocol size", "aarp.proto.size",
283 FT_UINT8, BASE_DEC, NULL, 0x0,
284 NULL, HFILL }},
286 { &hf_aarp_opcode,
287 { "Opcode", "aarp.opcode",
288 FT_UINT16, BASE_DEC, VALS(op_vals), 0x0,
289 NULL, HFILL }},
291 { &hf_aarp_src_hw,
292 { "Sender hardware address", "aarp.src.hw",
293 FT_BYTES, BASE_NONE, NULL, 0x0,
294 NULL, HFILL }},
296 { &hf_aarp_src_hw_mac,
297 { "Sender MAC address", "aarp.src.hw_mac",
298 FT_ETHER, BASE_NONE, NULL, 0x0,
299 NULL, HFILL }},
301 { &hf_aarp_src_proto,
302 { "Sender protocol address", "aarp.src.proto",
303 FT_BYTES, BASE_NONE, NULL, 0x0,
304 NULL, HFILL }},
306 { &hf_aarp_src_proto_id,
307 { "Sender ID", "aarp.src.proto_id",
308 FT_BYTES, BASE_NONE, NULL, 0x0,
309 NULL, HFILL }},
311 { &hf_aarp_dst_hw,
312 { "Target hardware address", "aarp.dst.hw",
313 FT_BYTES, BASE_NONE, NULL, 0x0,
314 NULL, HFILL }},
316 { &hf_aarp_dst_hw_mac,
317 { "Target MAC address", "aarp.dst.hw_mac",
318 FT_ETHER, BASE_NONE, NULL, 0x0,
319 NULL, HFILL }},
321 { &hf_aarp_dst_proto,
322 { "Target protocol address", "aarp.dst.proto",
323 FT_BYTES, BASE_NONE, NULL, 0x0,
324 NULL, HFILL }},
326 { &hf_aarp_dst_proto_id,
327 { "Target ID", "aarp.dst.proto_id",
328 FT_BYTES, BASE_NONE, NULL, 0x0,
329 NULL, HFILL }},
331 static gint *ett[] = {
332 &ett_aarp,
335 proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
336 "AARP",
337 "aarp");
338 proto_register_field_array(proto_aarp, hf, array_length(hf));
339 proto_register_subtree_array(ett, array_length(ett));
342 void
343 proto_reg_handoff_aarp(void)
345 dissector_handle_t aarp_handle;
347 aarp_handle = create_dissector_handle(dissect_aarp, proto_aarp);
348 dissector_add_uint("ethertype", ETHERTYPE_AARP, aarp_handle);
349 dissector_add_uint("chdlc.protocol", ETHERTYPE_AARP, aarp_handle);