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