Fix #ifndef line whose symbol had been omitted.
[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.16 1999/12/10 00:45:15 nneul Exp $
5  *
6  * Simon Wilkinson <sxw@dcs.ed.ac.uk>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 #endif
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include "packet.h"
34 #include "etypes.h"
35
36 static int proto_aarp = -1;
37 static int hf_aarp_hard_type = -1;
38 static int hf_aarp_proto_type = -1;
39 static int hf_aarp_hard_size = -1;
40 static int hf_aarp_proto_size = -1;
41 static int hf_aarp_opcode = -1;
42 static int hf_aarp_src_ether = -1;
43 static int hf_aarp_src_id = -1;
44 static int hf_aarp_dst_ether = -1;
45 static int hf_aarp_dst_id = -1;
46
47 static gint ett_aarp = -1;
48
49 #ifndef AARP_REQUEST
50 #define AARP_REQUEST    0x0001
51 #endif
52 #ifndef AARP_REPLY
53 #define AARP_REPLY      0x0002
54 #endif
55 #ifndef AARP_PROBE      
56 #define AARP_PROBE      0x0003
57 #endif
58
59 /* The following is screwed up shit to deal with the fact that
60    the linux kernel edits the packet inline. */
61 #define AARP_REQUEST_SWAPPED    0x0100
62 #define AARP_REPLY_SWAPPED  0x0200
63 #define AARP_PROBE_SWAPPED  0x0300
64
65 static const value_string op_vals[] = {
66   {AARP_REQUEST,  "request" },
67   {AARP_REPLY,    "reply"   },
68   {AARP_PROBE,    "probe"   },
69   {AARP_REQUEST_SWAPPED,  "request" },
70   {AARP_REPLY_SWAPPED,    "reply"   },
71   {AARP_PROBE_SWAPPED,    "probe"   },
72   {0,             NULL           } };
73
74 /* AARP protocol HARDWARE identifiers. */
75 #define AARPHRD_ETHER   1               /* Ethernet 10Mbps              */
76 #define AARPHRD_TR      2               /* Token Ring                   */
77
78 static const value_string hrd_vals[] = {
79   {AARPHRD_ETHER,   "Ethernet"       },
80   {AARPHRD_TR,      "Token Ring"     },
81   {0,               NULL             } };
82
83 static gchar *
84 atalkid_to_str(const guint8 *ad) {
85   gint node;
86   static gchar  str[3][16];
87   static gchar *cur;
88   
89   if (cur == &str[0][0]) {
90     cur = &str[1][0];
91   } else if (cur == &str[1][0]) {  
92     cur = &str[2][0];
93   } else {  
94     cur = &str[0][0];
95   }
96   
97   node=ad[1]<<8|ad[2];
98   sprintf(cur, "%d.%d",node,ad[3]);
99   return cur;
100 }
101
102 static gchar *
103 aarphrdaddr_to_str(guint8 *ad, int ad_len, guint16 type) {
104   if ((type == AARPHRD_ETHER || type == AARPHRD_TR) && ad_len == 6) {
105     /* Ethernet address (or Token Ring address, which is the same type
106        of address). */
107     return ether_to_str(ad);
108   }
109   return bytes_to_str(ad, ad_len);
110 }
111
112 static gchar *
113 aarpproaddr_to_str(guint8 *ad, int ad_len, guint16 type) {
114   if (type == ETHERTYPE_ATALK && ad_len == 4) {
115     /* IP address.  */
116     return atalkid_to_str(ad);
117   }
118   return bytes_to_str(ad, ad_len);
119 }
120     
121 /* Offsets of fields within an AARP packet. */
122 #define AR_HRD          0
123 #define AR_PRO          2
124 #define AR_HLN          4
125 #define AR_PLN          5
126 #define AR_OP           6
127 #define MIN_AARP_HEADER_SIZE    8
128
129 void
130 dissect_aarp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
131   guint16     ar_hrd;
132   guint16     ar_pro;
133   guint8      ar_hln;
134   guint8      ar_pln;
135   guint16     ar_op;
136   proto_tree  *aarp_tree;
137   proto_item  *ti;
138   gchar       *op_str;
139   int         sha_offset, spa_offset, tha_offset, tpa_offset;
140   gchar       *sha_str, *spa_str, *tha_str, *tpa_str;
141
142   if (!BYTES_ARE_IN_FRAME(offset, MIN_AARP_HEADER_SIZE)) {
143     dissect_data(pd, offset, fd, tree);
144     return;
145   }
146
147   ar_hrd = pntohs(&pd[offset + AR_HRD]);
148   ar_pro = pntohs(&pd[offset + AR_PRO]);
149   ar_hln = (guint8) pd[offset + AR_HLN];
150   ar_pln = (guint8) pd[offset + AR_PLN];
151   ar_op  = pntohs(&pd[offset + AR_OP]);
152
153   if (!BYTES_ARE_IN_FRAME(offset, 
154                           MIN_AARP_HEADER_SIZE + ar_hln*2 + ar_pln*2)) {
155     dissect_data(pd, offset, fd, tree);
156     return;
157   }
158   
159   /* Extract the addresses.  */
160   sha_offset = offset + MIN_AARP_HEADER_SIZE;
161   sha_str = aarphrdaddr_to_str((guint8 *) &pd[sha_offset], ar_hln, ar_hrd);
162   spa_offset = sha_offset + ar_hln;
163   spa_str = aarpproaddr_to_str((guint8 *) &pd[spa_offset], ar_pln, ar_pro);
164   tha_offset = spa_offset + ar_pln;
165   tha_str = aarphrdaddr_to_str((guint8 *) &pd[tha_offset], ar_hln, ar_hrd);
166   tpa_offset = tha_offset + ar_hln;
167   tpa_str = aarpproaddr_to_str((guint8 *) &pd[tpa_offset], ar_pln, ar_pro);
168   
169   if(check_col(fd, COL_PROTOCOL))
170     col_add_str(fd, COL_PROTOCOL, "AARP");
171
172   if (check_col(fd, COL_INFO)) {
173     switch (ar_op) {
174       case AARP_REQUEST:
175       case AARP_REQUEST_SWAPPED:
176         col_add_fstr(fd, COL_INFO, "Who has %s?  Tell %s", tpa_str, spa_str);
177         break;
178       case AARP_REPLY:
179       case AARP_REPLY_SWAPPED:
180         col_add_fstr(fd, COL_INFO, "%s is at %s", spa_str, sha_str);
181         break;
182       case AARP_PROBE:
183       case AARP_PROBE_SWAPPED:
184         col_add_fstr(fd, COL_INFO, "Is there a %s", tpa_str);
185         break;
186       default:
187         col_add_fstr(fd, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
188         break;
189     }
190   }
191
192   if (tree) {
193     if ((op_str = match_strval(ar_op, op_vals)))
194       ti = proto_tree_add_item_format(tree, proto_aarp, offset,
195                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
196                                       2*ar_pln, NULL, "AppleTalk Address Resolution Protocol (%s)", op_str);
197     else
198       ti = proto_tree_add_item_format(tree, proto_aarp, offset,
199                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
200                                       2*ar_pln, NULL,
201                                       "AppleTalk Address Resolution Protocol (opcode 0x%04x)", ar_op);
202     aarp_tree = proto_item_add_subtree(ti, ett_aarp);
203     proto_tree_add_item(aarp_tree, hf_aarp_hard_type, offset + AR_HRD, 2,
204                                ar_hrd);
205     proto_tree_add_item(aarp_tree, hf_aarp_proto_type, offset + AR_PRO, 2, 
206                                ar_pro);
207     proto_tree_add_item(aarp_tree, hf_aarp_hard_size, offset + AR_HLN, 1,
208                                ar_hln);
209     proto_tree_add_item(aarp_tree, hf_aarp_proto_size, offset + AR_PLN, 1,
210                                ar_pln);
211     proto_tree_add_item(aarp_tree, hf_aarp_opcode, offset + AR_OP, 2,
212                                ar_op);
213     proto_tree_add_item_format(aarp_tree, hf_aarp_src_ether, sha_offset, ar_hln,
214                                &pd[sha_offset],
215                                "Sender hardware address: %s", sha_str);
216     proto_tree_add_item_format(aarp_tree, hf_aarp_src_id, spa_offset, ar_pln,
217                                &pd[spa_offset],
218                                "Sender ID: %s", spa_str);
219     proto_tree_add_item_format(aarp_tree, hf_aarp_dst_ether, tha_offset, ar_hln,
220                                &pd[tha_offset],
221                                "Target hardware address: %s", tha_str);
222     proto_tree_add_item_format(aarp_tree, hf_aarp_dst_id, tpa_offset, ar_pln,
223                                &pd[tpa_offset],
224                                "Target ID: %s", tpa_str);
225   }
226 }
227
228 void
229 proto_register_aarp(void)
230 {
231   static hf_register_info hf[] = {
232     { &hf_aarp_hard_type,
233       { "Hardware type",        "aarp.hard.type",       
234         FT_UINT16,      BASE_HEX,       VALS(hrd_vals), 0x0,
235         "" }},
236
237     { &hf_aarp_proto_type,
238       { "Protocol type",        "aarp.proto.type",      
239         FT_UINT16,      BASE_HEX,       VALS(etype_vals),       0x0,
240         "" }},    
241
242     { &hf_aarp_hard_size,
243       { "Hardware size",        "aarp.hard.size",       
244         FT_UINT8,       BASE_DEC,       NULL,   0x0,
245         "" }},
246
247     { &hf_aarp_proto_size,
248       { "Protocol size",        "aarp.proto.size",      
249         FT_UINT8,       BASE_DEC,       NULL,   0x0,
250         "" }},
251
252     { &hf_aarp_opcode,
253       { "Opcode",               "aarp.opcode",
254         FT_UINT16,      BASE_DEC,       VALS(op_vals),  0x0,
255         "" }},
256
257     { &hf_aarp_src_ether,
258       { "Sender ether",         "aarp.src.ether",
259         FT_BYTES,       BASE_NONE,      NULL,   0x0,
260         "" }},
261
262     { &hf_aarp_src_id,
263       { "Sender ID",            "aarp.src.id",
264         FT_BYTES,       BASE_HEX,       NULL,   0x0,
265         "" }},
266
267     { &hf_aarp_dst_ether,
268       { "Target ether",         "aarp.dst.ether",
269         FT_BYTES,       BASE_NONE,      NULL,   0x0,
270         "" }},
271
272     { &hf_aarp_dst_id,
273       { "Target ID",            "aarp.dst.id",          
274         FT_BYTES,       BASE_HEX,       NULL,   0x0,
275         "" }},
276   };
277   static gint *ett[] = {
278     &ett_aarp,
279   };
280
281   proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
282                                        "aarp");
283   proto_register_field_array(proto_aarp, hf, array_length(hf));
284   proto_register_subtree_array(ett, array_length(ett));
285 }