Give it an RCS ID.
[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.8 1999/09/23 05:21:28 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
38 typedef struct _e_ether_aarp {
39         guint16 htype, ptype;
40         guint8  halen, palen;
41         guint16 op;
42         guint8  hsaddr[6];
43         guint8  psaddr[4];
44         guint8  hdaddr[6];
45         guint8  pdaddr[4];
46 } e_ether_aarp;
47
48 #ifndef AARP_REQUEST
49 #define AARP_REQUEST    0x0001
50 #endif
51 #ifndef AARP_REPLY
52 #define AARP_REPLY      0x0002
53 #endif
54 #ifndef AARP_PROBE      
55 #define AARP_PROBE      0x0003
56 #endif
57
58 gchar *
59 atalkid_to_str(guint8 *ad) {
60   gint node;
61   static gchar  str[3][16];
62   static gchar *cur;
63   
64   if (cur == &str[0][0]) {
65     cur = &str[1][0];
66   } else if (cur == &str[1][0]) {  
67     cur = &str[2][0];
68   } else {  
69     cur = &str[0][0];
70   }
71   
72   node=ad[1]<<8|ad[2];
73   sprintf(cur, "%d.%d",node,ad[3]);
74   return cur;
75 }
76     
77 void
78 dissect_aarp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
79   e_ether_aarp  ea;
80   proto_tree    *aarp_tree;
81   proto_item    *ti;
82   gchar                 *op_str;
83   value_string op_vals[] = { {AARP_REQUEST,  "AARP request" },
84                              {AARP_REPLY,    "AARP reply"   },
85                              {AARP_PROBE,    "AARP probe"   },
86                              {0,             NULL           } };
87
88   ea.htype = pntohs(&pd[offset]);
89   ea.ptype = pntohs(&pd[offset + 2]);
90   ea.halen = (guint8) pd[offset + 4];
91   ea.palen = (guint8) pd[offset + 5];
92   ea.op  = pntohs(&pd[offset + 6]);
93   memcpy(&ea.hsaddr, &pd[offset +  8], 6);
94   memcpy(&ea.psaddr, &pd[offset + 14], 4);
95   memcpy(&ea.hdaddr, &pd[offset + 18], 6);
96   memcpy(&ea.pdaddr, &pd[offset + 24], 4);
97   
98   if(check_col(fd, COL_PROTOCOL))
99     col_add_str(fd, COL_PROTOCOL, "AARP");
100   
101   if (tree) {
102     if ((op_str = match_strval(ea.op, op_vals)))
103       ti = proto_tree_add_item_format(tree, proto_aarp, offset, 28, NULL, op_str);
104     else
105       ti = proto_tree_add_item_format(tree, proto_aarp, offset, 28, NULL,
106         "Unknown AARP (opcode 0x%04x)", ea.op);
107     aarp_tree = proto_item_add_subtree(ti, ETT_AARP);
108     proto_tree_add_text(aarp_tree, offset,      2,
109       "Hardware type: 0x%04x", ea.htype);
110     proto_tree_add_text(aarp_tree, offset +  2, 2,
111       "Protocol type: 0x%04x", ea.ptype);
112     proto_tree_add_text(aarp_tree, offset +  4, 1,
113       "Hardware size: 0x%02x", ea.halen);
114     proto_tree_add_text(aarp_tree, offset +  5, 1,
115       "Protocol size: 0x%02x", ea.palen);
116     proto_tree_add_text(aarp_tree, offset +  6, 2,
117       "Opcode: 0x%04x (%s)", ea.op, op_str ? op_str : "Unknown");
118     proto_tree_add_text(aarp_tree, offset +  8, 6,
119       "Sender ether: %s", ether_to_str((guint8 *) ea.hsaddr));
120     proto_tree_add_text(aarp_tree, offset + 14, 4,
121       "Sender ID: %s", atalkid_to_str((guint8 *) ea.psaddr));
122     proto_tree_add_text(aarp_tree, offset + 18, 6,
123       "Target ether: %s", ether_to_str((guint8 *) ea.hdaddr));
124     proto_tree_add_text(aarp_tree, offset + 24, 4,
125       "Target ID: %s", atalkid_to_str((guint8 *) ea.pdaddr));
126   }
127
128   if (ea.ptype != ETHERTYPE_AARP && ea.ptype != ETHERTYPE_ATALK && 
129       check_col(fd, COL_INFO)) {
130     col_add_fstr(fd, COL_INFO, "h/w %d (%d) prot %d (%d) op 0x%04x",
131       ea.htype, ea.halen, ea.ptype, ea.palen, ea.op);
132     return;
133   }
134   if (check_col(fd, COL_INFO)) {
135     switch (ea.op) {
136       case AARP_REQUEST:
137         col_add_fstr(fd, COL_INFO, "Who has %s?  Tell %s",
138           atalkid_to_str((guint8 *) ea.pdaddr), atalkid_to_str((guint8 *) ea.psaddr));
139         break;
140       case AARP_REPLY:
141         col_add_fstr(fd, COL_INFO, "%s is at %s",
142           atalkid_to_str((guint8 *) ea.psaddr),
143           ether_to_str((guint8 *) ea.hsaddr));
144         break;
145       case AARP_PROBE:
146         col_add_fstr(fd, COL_INFO, "Is there a %s",
147           atalkid_to_str((guint8 *) ea.pdaddr));
148         break;
149     }
150   }
151 }
152
153 void
154 proto_register_aarp(void)
155 {
156 /*        static hf_register_info hf[] = {
157                 { &variable,
158                 { "Name",           "aarp.abbreviation", TYPE, VALS_POINTER }},
159         };*/
160
161         proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol", "aarp");
162  /*       proto_register_field_array(proto_aarp, hf, array_length(hf));*/
163 }