more handling of etype<max_802_3_len
[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.14 1999/11/16 11:42:23 guy 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 static const value_string op_vals[] = {
60   {AARP_REQUEST,  "AARP request" },
61   {AARP_REPLY,    "AARP reply"   },
62   {AARP_PROBE,    "AARP probe"   },
63   {0,             NULL           } };
64
65 /* AARP protocol HARDWARE identifiers. */
66 #define AARPHRD_ETHER   1               /* Ethernet 10Mbps              */
67 #define AARPHRD_TR      2               /* Token Ring                   */
68
69 static const value_string hrd_vals[] = {
70   {AARPHRD_ETHER,   "Ethernet"       },
71   {AARPHRD_TR,      "Token Ring"     },
72   {0,               NULL             } };
73
74 static gchar *
75 atalkid_to_str(const guint8 *ad) {
76   gint node;
77   static gchar  str[3][16];
78   static gchar *cur;
79   
80   if (cur == &str[0][0]) {
81     cur = &str[1][0];
82   } else if (cur == &str[1][0]) {  
83     cur = &str[2][0];
84   } else {  
85     cur = &str[0][0];
86   }
87   
88   node=ad[1]<<8|ad[2];
89   sprintf(cur, "%d.%d",node,ad[3]);
90   return cur;
91 }
92
93 static gchar *
94 aarphrdaddr_to_str(guint8 *ad, int ad_len, guint16 type) {
95   if ((type == AARPHRD_ETHER || type == AARPHRD_TR) && ad_len == 6) {
96     /* Ethernet address (or Token Ring address, which is the same type
97        of address). */
98     return ether_to_str(ad);
99   }
100   return bytes_to_str(ad, ad_len);
101 }
102
103 static gchar *
104 aarpproaddr_to_str(guint8 *ad, int ad_len, guint16 type) {
105   if (type == ETHERTYPE_ATALK && ad_len == 4) {
106     /* IP address.  */
107     return atalkid_to_str(ad);
108   }
109   return bytes_to_str(ad, ad_len);
110 }
111     
112 /* Offsets of fields within an AARP packet. */
113 #define AR_HRD          0
114 #define AR_PRO          2
115 #define AR_HLN          4
116 #define AR_PLN          5
117 #define AR_OP           6
118 #define MIN_AARP_HEADER_SIZE    8
119
120 void
121 dissect_aarp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
122   guint16     ar_hrd;
123   guint16     ar_pro;
124   guint8      ar_hln;
125   guint8      ar_pln;
126   guint16     ar_op;
127   proto_tree  *aarp_tree;
128   proto_item  *ti;
129   gchar       *op_str;
130   int         sha_offset, spa_offset, tha_offset, tpa_offset;
131   gchar       *sha_str, *spa_str, *tha_str, *tpa_str;
132
133   if (!BYTES_ARE_IN_FRAME(offset, MIN_AARP_HEADER_SIZE)) {
134     dissect_data(pd, offset, fd, tree);
135     return;
136   }
137
138   ar_hrd = pntohs(&pd[offset + AR_HRD]);
139   ar_pro = pntohs(&pd[offset + AR_PRO]);
140   ar_hln = (guint8) pd[offset + AR_HLN];
141   ar_pln = (guint8) pd[offset + AR_PLN];
142   ar_op  = pntohs(&pd[offset + AR_OP]);
143
144   if (!BYTES_ARE_IN_FRAME(offset, 
145                           MIN_AARP_HEADER_SIZE + ar_hln*2 + ar_pln*2)) {
146     dissect_data(pd, offset, fd, tree);
147     return;
148   }
149   
150   /* Extract the addresses.  */
151   sha_offset = offset + MIN_AARP_HEADER_SIZE;
152   sha_str = aarphrdaddr_to_str((guint8 *) &pd[sha_offset], ar_hln, ar_hrd);
153   spa_offset = sha_offset + ar_hln;
154   spa_str = aarpproaddr_to_str((guint8 *) &pd[spa_offset], ar_pln, ar_pro);
155   tha_offset = spa_offset + ar_pln;
156   tha_str = aarphrdaddr_to_str((guint8 *) &pd[tha_offset], ar_hln, ar_hrd);
157   tpa_offset = tha_offset + ar_hln;
158   tpa_str = aarpproaddr_to_str((guint8 *) &pd[tpa_offset], ar_pln, ar_pro);
159   
160   if(check_col(fd, COL_PROTOCOL))
161     col_add_str(fd, COL_PROTOCOL, "AARP");
162
163   if (check_col(fd, COL_INFO)) {
164     switch (ar_op) {
165       case AARP_REQUEST:
166         col_add_fstr(fd, COL_INFO, "Who has %s?  Tell %s", tpa_str, spa_str);
167         break;
168       case AARP_REPLY:
169         col_add_fstr(fd, COL_INFO, "%s is at %s", spa_str, sha_str);
170         break;
171       case AARP_PROBE:
172         col_add_fstr(fd, COL_INFO, "Is there a %s", tpa_str);
173         break;
174       default:
175         col_add_fstr(fd, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
176         break;
177     }
178   }
179
180   if (tree) {
181     if ((op_str = match_strval(ar_op, op_vals)))
182       ti = proto_tree_add_item_format(tree, proto_aarp, offset,
183                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
184                                       2*ar_pln, NULL, op_str);
185     else
186       ti = proto_tree_add_item_format(tree, proto_aarp, offset,
187                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
188                                       2*ar_pln, NULL,
189                                       "Unknown AARP (opcode 0x%04x)", ar_op);
190     aarp_tree = proto_item_add_subtree(ti, ett_aarp);
191     proto_tree_add_item(aarp_tree, hf_aarp_hard_type, offset + AR_HRD, 2,
192                                ar_hrd);
193     proto_tree_add_item(aarp_tree, hf_aarp_proto_type, offset + AR_PRO, 2, 
194                                ar_pro);
195     proto_tree_add_item(aarp_tree, hf_aarp_hard_size, offset + AR_HLN, 1,
196                                ar_hln);
197     proto_tree_add_item(aarp_tree, hf_aarp_proto_size, offset + AR_PLN, 1,
198                                ar_pln);
199     proto_tree_add_item(aarp_tree, hf_aarp_opcode, offset + AR_OP, 2,
200                                ar_op);
201     proto_tree_add_item_format(aarp_tree, hf_aarp_src_ether, sha_offset, ar_hln,
202                                &pd[sha_offset],
203                                "Sender hardware address: %s", sha_str);
204     proto_tree_add_item_format(aarp_tree, hf_aarp_src_id, spa_offset, ar_pln,
205                                &pd[spa_offset],
206                                "Sender ID: %s", spa_str);
207     proto_tree_add_item_format(aarp_tree, hf_aarp_dst_ether, tha_offset, ar_hln,
208                                &pd[tha_offset],
209                                "Target hardware address: %s", tha_str);
210     proto_tree_add_item_format(aarp_tree, hf_aarp_dst_id, tpa_offset, ar_pln,
211                                &pd[tpa_offset],
212                                "Target ID: %s", tpa_str);
213   }
214 }
215
216 void
217 proto_register_aarp(void)
218 {
219   static hf_register_info hf[] = {
220     { &hf_aarp_hard_type,
221       { "Hardware type",        "aarp.hard.type",       
222         FT_UINT16,      BASE_HEX,       VALS(hrd_vals), 0x0,
223         "" }},
224
225     { &hf_aarp_proto_type,
226       { "Protocol type",        "aarp.proto.type",      
227         FT_UINT16,      BASE_HEX,       VALS(etype_vals),       0x0,
228         "" }},    
229
230     { &hf_aarp_hard_size,
231       { "Hardware size",        "aarp.hard.size",       
232         FT_UINT8,       BASE_DEC,       NULL,   0x0,
233         "" }},
234
235     { &hf_aarp_proto_size,
236       { "Protocol size",        "aarp.proto.size",      
237         FT_UINT8,       BASE_DEC,       NULL,   0x0,
238         "" }},
239
240     { &hf_aarp_opcode,
241       { "Opcode",               "aarp.opcode",
242         FT_UINT16,      BASE_DEC,       VALS(op_vals),  0x0,
243         "" }},
244
245     { &hf_aarp_src_ether,
246       { "Sender ether",         "aarp.src.ether",
247         FT_BYTES,       BASE_NONE,      NULL,   0x0,
248         "" }},
249
250     { &hf_aarp_src_id,
251       { "Sender ID",            "aarp.src.id",
252         FT_BYTES,       BASE_HEX,       NULL,   0x0,
253         "" }},
254
255     { &hf_aarp_dst_ether,
256       { "Target ether",         "aarp.dst.ether",
257         FT_BYTES,       BASE_NONE,      NULL,   0x0,
258         "" }},
259
260     { &hf_aarp_dst_id,
261       { "Target ID",            "aarp.dst.id",          
262         FT_BYTES,       BASE_HEX,       NULL,   0x0,
263         "" }},
264   };
265   static gint *ett[] = {
266     &ett_aarp,
267   };
268
269   proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
270                                        "aarp");
271   proto_register_field_array(proto_aarp, hf, array_length(hf));
272   proto_register_subtree_array(ett, array_length(ett));
273 }