Don't let the user specify a maximum capture file size if they're not
[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.33 2001/12/10 00:25:25 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 "packet.h"
38 #include "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_ether = -1;
48 static int hf_aarp_src_id = -1;
49 static int hf_aarp_dst_ether = -1;
50 static int hf_aarp_dst_id = -1;
51
52 static gint ett_aarp = -1;
53
54 #ifndef AARP_REQUEST
55 #define AARP_REQUEST    0x0001
56 #endif
57 #ifndef AARP_REPLY
58 #define AARP_REPLY      0x0002
59 #endif
60 #ifndef AARP_PROBE      
61 #define AARP_PROBE      0x0003
62 #endif
63
64 /* The following is screwed up shit to deal with the fact that
65    the linux kernel edits the packet inline. */
66 #define AARP_REQUEST_SWAPPED    0x0100
67 #define AARP_REPLY_SWAPPED  0x0200
68 #define AARP_PROBE_SWAPPED  0x0300
69
70 static const value_string op_vals[] = {
71   {AARP_REQUEST,  "request" },
72   {AARP_REPLY,    "reply"   },
73   {AARP_PROBE,    "probe"   },
74   {AARP_REQUEST_SWAPPED,  "request" },
75   {AARP_REPLY_SWAPPED,    "reply"   },
76   {AARP_PROBE_SWAPPED,    "probe"   },
77   {0,             NULL           } };
78
79 /* AARP protocol HARDWARE identifiers. */
80 #define AARPHRD_ETHER   1               /* Ethernet 10Mbps              */
81 #define AARPHRD_TR      2               /* Token Ring                   */
82
83 static const value_string hrd_vals[] = {
84   {AARPHRD_ETHER,   "Ethernet"       },
85   {AARPHRD_TR,      "Token Ring"     },
86   {0,               NULL             } };
87
88 static gchar *
89 atalkid_to_str(const guint8 *ad) {
90   gint node;
91   static gchar  str[3][16];
92   static gchar *cur;
93   
94   if (cur == &str[0][0]) {
95     cur = &str[1][0];
96   } else if (cur == &str[1][0]) {  
97     cur = &str[2][0];
98   } else {  
99     cur = &str[0][0];
100   }
101   
102   node=ad[1]<<8|ad[2];
103   sprintf(cur, "%d.%d",node,ad[3]);
104   return cur;
105 }
106
107 static gchar *
108 aarphrdaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
109   if ((type == AARPHRD_ETHER || type == AARPHRD_TR) && ad_len == 6) {
110     /* Ethernet address (or Token Ring address, which is the same type
111        of address). */
112     return ether_to_str(ad);
113   }
114   return bytes_to_str(ad, ad_len);
115 }
116
117 static gchar *
118 aarpproaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
119   if (type == ETHERTYPE_ATALK && ad_len == 4) {
120     /* IP address.  */
121     return atalkid_to_str(ad);
122   }
123   return bytes_to_str(ad, ad_len);
124 }
125     
126 /* Offsets of fields within an AARP packet. */
127 #define AR_HRD          0
128 #define AR_PRO          2
129 #define AR_HLN          4
130 #define AR_PLN          5
131 #define AR_OP           6
132 #define MIN_AARP_HEADER_SIZE    8
133
134 static void
135 dissect_aarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
136   guint16     ar_hrd;
137   guint16     ar_pro;
138   guint8      ar_hln;
139   guint8      ar_pln;
140   guint16     ar_op;
141   proto_tree  *aarp_tree;
142   proto_item  *ti;
143   gchar       *op_str;
144   int         sha_offset, spa_offset, tha_offset, tpa_offset;
145   const guint8      *sha_val, *spa_val, *tha_val, *tpa_val;
146   gchar       *sha_str, *spa_str, *tha_str, *tpa_str;
147
148   if(check_col(pinfo->cinfo, COL_PROTOCOL))
149     col_set_str(pinfo->cinfo, COL_PROTOCOL, "AARP");
150   if(check_col(pinfo->cinfo, COL_INFO))
151     col_clear(pinfo->cinfo, COL_INFO);
152
153   ar_hrd = tvb_get_ntohs(tvb, AR_HRD);
154   ar_pro = tvb_get_ntohs(tvb, AR_PRO);
155   ar_hln = tvb_get_guint8(tvb, AR_HLN);
156   ar_pln = tvb_get_guint8(tvb, AR_PLN);
157   ar_op  = tvb_get_ntohs(tvb, AR_OP);
158
159   /* Extract the addresses.  */
160   sha_offset = MIN_AARP_HEADER_SIZE;
161   sha_val = tvb_get_ptr(tvb, sha_offset, ar_hln);
162   sha_str = aarphrdaddr_to_str(sha_val, ar_hln, ar_hrd);
163
164   spa_offset = sha_offset + ar_hln;
165   spa_val = tvb_get_ptr(tvb, spa_offset, ar_pln);
166   spa_str = aarpproaddr_to_str(spa_val, ar_pln, ar_pro);
167
168   tha_offset = spa_offset + ar_pln;
169   tha_val = tvb_get_ptr(tvb, tha_offset, ar_hln);
170   tha_str = aarphrdaddr_to_str(tha_val, ar_hln, ar_hrd);
171
172   tpa_offset = tha_offset + ar_hln;
173   tpa_val = tvb_get_ptr(tvb, tpa_offset, ar_pln);
174   tpa_str = aarpproaddr_to_str(tpa_val, ar_pln, ar_pro);
175   
176   if (check_col(pinfo->cinfo, COL_INFO)) {
177     switch (ar_op) {
178       case AARP_REQUEST:
179       case AARP_REQUEST_SWAPPED:
180         col_add_fstr(pinfo->cinfo, COL_INFO, "Who has %s?  Tell %s", tpa_str, spa_str);
181         break;
182       case AARP_REPLY:
183       case AARP_REPLY_SWAPPED:
184         col_add_fstr(pinfo->cinfo, COL_INFO, "%s is at %s", spa_str, sha_str);
185         break;
186       case AARP_PROBE:
187       case AARP_PROBE_SWAPPED:
188         col_add_fstr(pinfo->cinfo, COL_INFO, "Is there a %s", tpa_str);
189         break;
190       default:
191         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
192         break;
193     }
194   }
195
196   if (tree) {
197     if ((op_str = match_strval(ar_op, op_vals)))
198       ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
199                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
200                                       2*ar_pln, "AppleTalk Address Resolution Protocol (%s)", op_str);
201     else
202       ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
203                                       MIN_AARP_HEADER_SIZE + 2*ar_hln + 
204                                       2*ar_pln,
205                                       "AppleTalk Address Resolution Protocol (opcode 0x%04x)", ar_op);
206     aarp_tree = proto_item_add_subtree(ti, ett_aarp);
207     proto_tree_add_uint(aarp_tree, hf_aarp_hard_type, tvb, AR_HRD, 2,
208                                ar_hrd);
209     proto_tree_add_uint(aarp_tree, hf_aarp_proto_type, tvb, AR_PRO, 2, 
210                                ar_pro);
211     proto_tree_add_uint(aarp_tree, hf_aarp_hard_size, tvb, AR_HLN, 1,
212                                ar_hln);
213     proto_tree_add_uint(aarp_tree, hf_aarp_proto_size, tvb, AR_PLN, 1,
214                                ar_pln);
215     proto_tree_add_uint(aarp_tree, hf_aarp_opcode, tvb, AR_OP, 2,
216                                ar_op);
217     proto_tree_add_bytes_format(aarp_tree, hf_aarp_src_ether, tvb, sha_offset, ar_hln,
218                                sha_val,
219                                "Sender hardware address: %s", sha_str);
220     proto_tree_add_bytes_format(aarp_tree, hf_aarp_src_id, tvb, spa_offset, ar_pln,
221                                spa_val,
222                                "Sender ID: %s", spa_str);
223     proto_tree_add_bytes_format(aarp_tree, hf_aarp_dst_ether, tvb, tha_offset, ar_hln,
224                                tha_val,
225                                "Target hardware address: %s", tha_str);
226     proto_tree_add_bytes_format(aarp_tree, hf_aarp_dst_id, tvb, tpa_offset, ar_pln,
227                                tpa_val,
228                                "Target ID: %s", tpa_str);
229   }
230 }
231
232 void
233 proto_register_aarp(void)
234 {
235   static hf_register_info hf[] = {
236     { &hf_aarp_hard_type,
237       { "Hardware type",        "aarp.hard.type",       
238         FT_UINT16,      BASE_HEX,       VALS(hrd_vals), 0x0,
239         "", HFILL }},
240
241     { &hf_aarp_proto_type,
242       { "Protocol type",        "aarp.proto.type",      
243         FT_UINT16,      BASE_HEX,       VALS(etype_vals),       0x0,
244         "", HFILL }},    
245
246     { &hf_aarp_hard_size,
247       { "Hardware size",        "aarp.hard.size",       
248         FT_UINT8,       BASE_DEC,       NULL,   0x0,
249         "", HFILL }},
250
251     { &hf_aarp_proto_size,
252       { "Protocol size",        "aarp.proto.size",      
253         FT_UINT8,       BASE_DEC,       NULL,   0x0,
254         "", HFILL }},
255
256     { &hf_aarp_opcode,
257       { "Opcode",               "aarp.opcode",
258         FT_UINT16,      BASE_DEC,       VALS(op_vals),  0x0,
259         "", HFILL }},
260
261     { &hf_aarp_src_ether,
262       { "Sender ether",         "aarp.src.ether",
263         FT_BYTES,       BASE_NONE,      NULL,   0x0,
264         "", HFILL }},
265
266     { &hf_aarp_src_id,
267       { "Sender ID",            "aarp.src.id",
268         FT_BYTES,       BASE_HEX,       NULL,   0x0,
269         "", HFILL }},
270
271     { &hf_aarp_dst_ether,
272       { "Target ether",         "aarp.dst.ether",
273         FT_BYTES,       BASE_NONE,      NULL,   0x0,
274         "", HFILL }},
275
276     { &hf_aarp_dst_id,
277       { "Target ID",            "aarp.dst.id",          
278         FT_BYTES,       BASE_HEX,       NULL,   0x0,
279         "", HFILL }},
280   };
281   static gint *ett[] = {
282     &ett_aarp,
283   };
284
285   proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
286                                        "AARP",
287                                        "aarp");
288   proto_register_field_array(proto_aarp, hf, array_length(hf));
289   proto_register_subtree_array(ett, array_length(ett));
290 }
291
292 void
293 proto_reg_handoff_aarp(void)
294 {
295   dissector_handle_t aarp_handle;
296
297   aarp_handle = create_dissector_handle(dissect_aarp, proto_aarp);
298   dissector_add("ethertype", ETHERTYPE_AARP, aarp_handle);
299   dissector_add("chdlctype", ETHERTYPE_AARP, aarp_handle);
300 }