proto_registrar_get_nth(hfinfo->id) == hfinfo, so use the latter rather
[obnox/wireshark/wip.git] / packet-aarp.c
1 /* packet-aarp.c
2  * Routines for Appletalk ARP packet disassembly
3  *
4  * $Id: packet-aarp.c,v 1.35 2002/02/10 23:48:14 guy Exp $
5  *
6  * Simon Wilkinson <sxw@dcs.ed.ac.uk>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998
11  *
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.
16  * 
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.
21  * 
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <stdio.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39 #include "etypes.h"
40
41 static int proto_aarp = -1;
42 static int hf_aarp_hard_type = -1;
43 static int hf_aarp_proto_type = -1;
44 static int hf_aarp_hard_size = -1;
45 static int hf_aarp_proto_size = -1;
46 static int hf_aarp_opcode = -1;
47 static int hf_aarp_src_hw = -1;
48 static int hf_aarp_src_hw_mac = -1;
49 static int hf_aarp_src_proto = -1;
50 static int hf_aarp_src_proto_id = -1;
51 static int hf_aarp_dst_hw = -1;
52 static int hf_aarp_dst_hw_mac = -1;
53 static int hf_aarp_dst_proto = -1;
54 static int hf_aarp_dst_proto_id = -1;
55
56 static gint ett_aarp = -1;
57
58 #ifndef AARP_REQUEST
59 #define AARP_REQUEST    0x0001
60 #endif
61 #ifndef AARP_REPLY
62 #define AARP_REPLY      0x0002
63 #endif
64 #ifndef AARP_PROBE      
65 #define AARP_PROBE      0x0003
66 #endif
67
68 /* The following is screwed up shit to deal with the fact that
69    the linux kernel edits the packet inline. */
70 #define AARP_REQUEST_SWAPPED    0x0100
71 #define AARP_REPLY_SWAPPED  0x0200
72 #define AARP_PROBE_SWAPPED  0x0300
73
74 static const value_string op_vals[] = {
75   {AARP_REQUEST,  "request" },
76   {AARP_REPLY,    "reply"   },
77   {AARP_PROBE,    "probe"   },
78   {AARP_REQUEST_SWAPPED,  "request" },
79   {AARP_REPLY_SWAPPED,    "reply"   },
80   {AARP_PROBE_SWAPPED,    "probe"   },
81   {0,             NULL           } };
82
83 /* AARP protocol HARDWARE identifiers. */
84 #define AARPHRD_ETHER   1               /* Ethernet 10Mbps              */
85 #define AARPHRD_TR      2               /* Token Ring                   */
86
87 static const value_string hrd_vals[] = {
88   {AARPHRD_ETHER,   "Ethernet"       },
89   {AARPHRD_TR,      "Token Ring"     },
90   {0,               NULL             } };
91
92 /*
93  * Given the hardware address type and length, check whether an address
94  * is an Ethernet address - the address must be of type "Ethernet" or
95  * "Token Ring", and the length must be 6 bytes.
96  */
97 #define AARP_HW_IS_ETHER(ar_hrd, ar_hln) \
98         (((ar_hrd) == AARPHRD_ETHER || (ar_hrd) == AARPHRD_TR) \
99                                 && (ar_hln) == 6)
100
101 /*
102  * Given the protocol address type and length, check whether an address
103  * is an Appletalk address - the address must be of type "Appletalk",
104  * and the length must be 4 bytes.
105  */
106 #define AARP_PRO_IS_ATALK(ar_pro, ar_pln) \
107         ((ar_pro) == ETHERTYPE_ATALK && (ar_pln) == 4)
108
109 static gchar *
110 atalkid_to_str(const guint8 *ad) {
111   gint node;
112   static gchar  str[3][16];
113   static gchar *cur;
114   
115   if (cur == &str[0][0]) {
116     cur = &str[1][0];
117   } else if (cur == &str[1][0]) {  
118     cur = &str[2][0];
119   } else {  
120     cur = &str[0][0];
121   }
122   
123   node=ad[1]<<8|ad[2];
124   sprintf(cur, "%d.%d",node,ad[3]);
125   return cur;
126 }
127
128 static gchar *
129 aarphrdaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
130   if (AARP_HW_IS_ETHER(type, ad_len)) {
131     /* Ethernet address (or Token Ring address, which is the same type
132        of address). */
133     return ether_to_str(ad);
134   }
135   return bytes_to_str(ad, ad_len);
136 }
137
138 static gchar *
139 aarpproaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
140   if (AARP_PRO_IS_ATALK(type, ad_len)) {
141     /* Appletalk address.  */
142     return atalkid_to_str(ad);
143   }
144   return bytes_to_str(ad, ad_len);
145 }
146     
147 /* Offsets of fields within an AARP packet. */
148 #define AR_HRD          0
149 #define AR_PRO          2
150 #define AR_HLN          4
151 #define AR_PLN          5
152 #define AR_OP           6
153 #define MIN_AARP_HEADER_SIZE    8
154
155 static void
156 dissect_aarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
157   guint16     ar_hrd;
158   guint16     ar_pro;
159   guint8      ar_hln;
160   guint8      ar_pln;
161   guint16     ar_op;
162   proto_tree  *aarp_tree;
163   proto_item  *ti;
164   gchar       *op_str;
165   int         sha_offset, spa_offset, tha_offset, tpa_offset;
166   const guint8      *sha_val, *spa_val, *tha_val, *tpa_val;
167   gchar       *sha_str, *spa_str, *tha_str, *tpa_str;
168
169   if(check_col(pinfo->cinfo, COL_PROTOCOL))
170     col_set_str(pinfo->cinfo, COL_PROTOCOL, "AARP");
171   if(check_col(pinfo->cinfo, COL_INFO))
172     col_clear(pinfo->cinfo, COL_INFO);
173
174   ar_hrd = tvb_get_ntohs(tvb, AR_HRD);
175   ar_pro = tvb_get_ntohs(tvb, AR_PRO);
176   ar_hln = tvb_get_guint8(tvb, AR_HLN);
177   ar_pln = tvb_get_guint8(tvb, AR_PLN);
178   ar_op  = tvb_get_ntohs(tvb, AR_OP);
179
180   /* Get the offsets of the addresses. */
181   sha_offset = MIN_AARP_HEADER_SIZE;
182   spa_offset = sha_offset + ar_hln;
183   tha_offset = spa_offset + ar_pln;
184   tpa_offset = tha_offset + ar_hln;
185
186   /* Extract the addresses.  */
187   sha_val = tvb_get_ptr(tvb, sha_offset, ar_hln);
188   sha_str = aarphrdaddr_to_str(sha_val, ar_hln, ar_hrd);
189
190   spa_val = tvb_get_ptr(tvb, spa_offset, ar_pln);
191   spa_str = aarpproaddr_to_str(spa_val, ar_pln, ar_pro);
192
193   tha_val = tvb_get_ptr(tvb, tha_offset, ar_hln);
194   tha_str = aarphrdaddr_to_str(tha_val, ar_hln, ar_hrd);
195
196   tpa_val = tvb_get_ptr(tvb, tpa_offset, ar_pln);
197   tpa_str = aarpproaddr_to_str(tpa_val, ar_pln, ar_pro);
198
199   if (check_col(pinfo->cinfo, COL_INFO)) {
200     switch (ar_op) {
201       case AARP_REQUEST:
202       case AARP_REQUEST_SWAPPED:
203         col_add_fstr(pinfo->cinfo, COL_INFO, "Who has %s?  Tell %s", tpa_str, spa_str);
204         break;
205       case AARP_REPLY:
206       case AARP_REPLY_SWAPPED:
207         col_add_fstr(pinfo->cinfo, COL_INFO, "%s is at %s", spa_str, sha_str);
208         break;
209       case AARP_PROBE:
210       case AARP_PROBE_SWAPPED:
211         col_add_fstr(pinfo->cinfo, COL_INFO, "Is there a %s", tpa_str);
212         break;
213       default:
214         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
215         break;
216     }
217   }
218
219   if (tree) {
220     if ((op_str = match_strval(ar_op, op_vals)))
221       ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
222                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
223                                       2*ar_pln, "AppleTalk Address Resolution Protocol (%s)", op_str);
224     else
225       ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
226                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
227                                       2*ar_pln,
228                                       "AppleTalk Address Resolution Protocol (opcode 0x%04x)", ar_op);
229     aarp_tree = proto_item_add_subtree(ti, ett_aarp);
230     proto_tree_add_uint(aarp_tree, hf_aarp_hard_type, tvb, AR_HRD, 2,
231                                ar_hrd);
232     proto_tree_add_uint(aarp_tree, hf_aarp_proto_type, tvb, AR_PRO, 2, 
233                                ar_pro);
234     proto_tree_add_uint(aarp_tree, hf_aarp_hard_size, tvb, AR_HLN, 1,
235                                ar_hln);
236     proto_tree_add_uint(aarp_tree, hf_aarp_proto_size, tvb, AR_PLN, 1,
237                                ar_pln);
238     proto_tree_add_uint(aarp_tree, hf_aarp_opcode, tvb, AR_OP, 2,
239                                ar_op);
240     if (ar_hln != 0) {
241       proto_tree_add_item(aarp_tree,
242         AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_src_hw_mac : hf_aarp_src_hw,
243         tvb, sha_offset, ar_hln, FALSE);
244     }
245
246     if (ar_pln != 0) {
247       if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
248         proto_tree_add_bytes_format(aarp_tree, hf_aarp_src_proto_id, tvb, spa_offset, ar_pln,
249                                spa_val,
250                                "Sender ID: %s", spa_str);
251       } else { 
252         proto_tree_add_bytes_format(aarp_tree, hf_aarp_src_proto, tvb, spa_offset, ar_pln,
253                                spa_val,
254                                "Sender protocol address: %s", spa_str);
255       }
256     }
257
258     if (ar_hln != 0) {
259       proto_tree_add_item(aarp_tree,
260         AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_dst_hw_mac : hf_aarp_dst_hw,
261         tvb, tha_offset, ar_hln, FALSE);
262     }
263
264     if (ar_pln != 0) {
265       if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
266         proto_tree_add_bytes_format(aarp_tree, hf_aarp_dst_proto_id, tvb, tpa_offset, ar_pln,
267                                tpa_val,
268                                "Target ID: %s", tpa_str);
269       } else {
270         proto_tree_add_bytes_format(aarp_tree, hf_aarp_dst_proto, tvb, tpa_offset, ar_pln,
271                                tpa_val,
272                                "Target protocol address: %s", tpa_str);
273       }
274     }
275   }
276 }
277
278 void
279 proto_register_aarp(void)
280 {
281   static hf_register_info hf[] = {
282     { &hf_aarp_hard_type,
283       { "Hardware type",                "aarp.hard.type",       
284         FT_UINT16,      BASE_HEX,       VALS(hrd_vals), 0x0,
285         "", HFILL }},
286
287     { &hf_aarp_proto_type,
288       { "Protocol type",                "aarp.proto.type",      
289         FT_UINT16,      BASE_HEX,       VALS(etype_vals),       0x0,
290         "", HFILL }},    
291
292     { &hf_aarp_hard_size,
293       { "Hardware size",                "aarp.hard.size",       
294         FT_UINT8,       BASE_DEC,       NULL,   0x0,
295         "", HFILL }},
296
297     { &hf_aarp_proto_size,
298       { "Protocol size",                "aarp.proto.size",      
299         FT_UINT8,       BASE_DEC,       NULL,   0x0,
300         "", HFILL }},
301
302     { &hf_aarp_opcode,
303       { "Opcode",                       "aarp.opcode",
304         FT_UINT16,      BASE_DEC,       VALS(op_vals),  0x0,
305         "", HFILL }},
306
307     { &hf_aarp_src_hw,
308       { "Sender hardware address",      "aarp.src.hw",
309         FT_BYTES,       BASE_NONE,      NULL,   0x0,
310         "", HFILL }},
311
312     { &hf_aarp_src_hw_mac,
313       { "Sender MAC address",           "aarp.src.hw_mac",
314         FT_ETHER,       BASE_NONE,      NULL,   0x0,
315         "", HFILL }},
316
317     { &hf_aarp_src_proto,
318       { "Sender protocol address",      "aarp.src.proto",
319         FT_BYTES,       BASE_NONE,      NULL,   0x0,
320         "", HFILL }},
321
322     { &hf_aarp_src_proto_id,
323       { "Sender ID",                    "aarp.src.proto_id",
324         FT_BYTES,       BASE_HEX,       NULL,   0x0,
325         "", HFILL }},
326
327     { &hf_aarp_dst_hw,
328       { "Target hardware address",      "aarp.dst.hw",
329         FT_BYTES,       BASE_NONE,      NULL,   0x0,
330         "", HFILL }},
331
332     { &hf_aarp_dst_hw_mac,
333       { "Target MAC address",           "aarp.dst.hw_mac",
334         FT_ETHER,       BASE_NONE,      NULL,   0x0,
335         "", HFILL }},
336
337     { &hf_aarp_dst_proto,
338       { "Target protocol address",      "aarp.dst.proto",
339         FT_BYTES,       BASE_NONE,      NULL,   0x0,
340       "", HFILL }},
341
342     { &hf_aarp_dst_proto_id,
343       { "Target ID",                    "aarp.dst.proto_id",
344         FT_BYTES,       BASE_HEX,       NULL,   0x0,
345         "", HFILL }},
346   };
347   static gint *ett[] = {
348     &ett_aarp,
349   };
350
351   proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
352                                        "AARP",
353                                        "aarp");
354   proto_register_field_array(proto_aarp, hf, array_length(hf));
355   proto_register_subtree_array(ett, array_length(ett));
356 }
357
358 void
359 proto_reg_handoff_aarp(void)
360 {
361   dissector_handle_t aarp_handle;
362
363   aarp_handle = create_dissector_handle(dissect_aarp, proto_aarp);
364   dissector_add("ethertype", ETHERTYPE_AARP, aarp_handle);
365   dissector_add("chdlctype", ETHERTYPE_AARP, aarp_handle);
366 }