Replace the ETT_ "enum" members, declared in "packet.h", with
[obnox/wireshark/wip.git] / packet-atalk.c
1 /* packet-atalk.c
2  * Routines for Appletalk packet disassembly (DDP, currently).
3  *
4  * $Id: packet-atalk.c,v 1.22 1999/11/16 11:42:25 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 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
33 #endif
34
35 #include <glib.h>
36 #include "globals.h"
37 #include "packet.h"
38 #include "packet-atalk.h"
39
40 static int proto_ddp = -1;
41 static int hf_ddp_hopcount = -1;
42 static int hf_ddp_len = -1;
43 static int hf_ddp_checksum = -1;
44 static int hf_ddp_dst_net = -1;
45 static int hf_ddp_src_net = -1;
46 static int hf_ddp_dst_node = -1;
47 static int hf_ddp_src_node = -1;
48 static int hf_ddp_dst_socket = -1;
49 static int hf_ddp_src_socket = -1;
50 static int hf_ddp_type = -1;
51
52 static gint ett_ddp = -1;
53
54 /* P = Padding, H = Hops, L = Len */
55 #if BYTE_ORDER == BIG_ENDIAN
56  /* PPHHHHLL LLLLLLLL */
57 # define ddp_hops(x)    ( ( x >> 10) & 0x3C )
58 # define ddp_len(x)             ( x & 0x03ff )
59 #else
60  /* LLLLLLLL PPHHHHLL*/
61 # define ddp_hops(x)    ( x & 0x3C )
62 # define ddp_len(x)             ( ntohs(x) & 0x03ff )
63 #endif
64 typedef struct _e_ddp {
65   guint16       hops_len; /* combines pad, hops, and len */
66   guint16       sum,dnet,snet;
67   guint8        dnode,snode;
68   guint8        dport,sport;
69   guint8        type;
70 } e_ddp;
71
72 #define DDP_RTMPDATA    0x01
73 #define DDP_NBP         0x02
74 #define DDP_ATP         0x03
75 #define DDP_AEP         0x04
76 #define DDP_RTMPREQ     0x05
77 #define DDP_ZIP         0x06
78 #define DDP_ADSP        0x07
79 #define DDP_HEADER_SIZE 13
80
81 gchar *
82 atalk_addr_to_str(const struct atalk_ddp_addr *addrp)
83 {
84   static gchar  str[3][14];
85   static gchar  *cur;
86
87   if (cur == &str[0][0]) {
88     cur = &str[1][0];
89   } else if (cur == &str[1][0]) {
90     cur = &str[2][0];
91   } else {
92     cur = &str[0][0];
93   }
94
95   sprintf(cur, "%u.%u:%u", addrp->net, addrp->node, addrp->port);
96   return cur;
97 }
98
99 static const value_string op_vals[] = {
100   {DDP_RTMPDATA, "AppleTalk Routing Table response or data" },
101   {DDP_NBP, "AppleTalk Name Binding Protocol packet"},
102   {DDP_ATP, "AppleTalk Transaction Protocol packet"},
103   {DDP_AEP, "AppleTalk Echo Protocol packet"},
104   {DDP_RTMPREQ, "AppleTalk Routing Table request"},
105   {DDP_ZIP, "AppleTalk Zone Information Protocol packet"},
106   {DDP_ADSP, "AppleTalk Data Stream Protocol"},
107   {0, NULL}
108 };
109
110 void
111 dissect_ddp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
112   e_ddp       ddp;
113   proto_tree *ddp_tree;
114   proto_item *ti;
115   static struct atalk_ddp_addr src, dst;
116
117   if (!BYTES_ARE_IN_FRAME(offset, DDP_HEADER_SIZE)) {
118     dissect_data(pd, offset, fd, tree);
119     return;
120   }
121
122   memcpy(&ddp, &pd[offset], sizeof(e_ddp));
123   ddp.dnet=ntohs(ddp.dnet);
124   ddp.snet=ntohs(ddp.snet);
125   ddp.sum=ntohs(ddp.sum);
126   
127   src.net = ddp.snet;
128   src.node = ddp.snode;
129   src.port = ddp.sport;
130   dst.net = ddp.dnet;
131   dst.node = ddp.dnode;
132   dst.port = ddp.dport;
133   SET_ADDRESS(&pi.net_src, AT_ATALK, sizeof src, (guint8 *)&src);
134   SET_ADDRESS(&pi.src, AT_ATALK, sizeof src, (guint8 *)&src);
135   SET_ADDRESS(&pi.net_dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
136   SET_ADDRESS(&pi.dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
137
138   if (check_col(fd, COL_PROTOCOL))
139     col_add_str(fd, COL_PROTOCOL, "DDP");
140   if (check_col(fd, COL_INFO))
141     col_add_str(fd, COL_INFO,
142       val_to_str(ddp.type, op_vals, "Unknown DDP protocol (%02x)"));
143   
144   if (tree) {
145     ti = proto_tree_add_item(tree, proto_ddp, offset, DDP_HEADER_SIZE, NULL);
146     ddp_tree = proto_item_add_subtree(ti, ett_ddp);
147     proto_tree_add_item(ddp_tree, hf_ddp_hopcount, offset,      1, 
148                         ddp_hops(ddp.hops_len));
149     proto_tree_add_item(ddp_tree, hf_ddp_len, offset,       2, 
150                         ddp_len(ddp.hops_len));
151     proto_tree_add_item(ddp_tree, hf_ddp_checksum, offset + 2,  2, ddp.sum);
152     proto_tree_add_item(ddp_tree, hf_ddp_dst_net, offset + 4,  2, ddp.dnet);
153     proto_tree_add_item(ddp_tree, hf_ddp_src_net,  offset + 6,  2, ddp.snet);
154     proto_tree_add_item(ddp_tree, hf_ddp_dst_node, offset + 8,  1, ddp.dnode);
155     proto_tree_add_item(ddp_tree, hf_ddp_src_node, offset + 9,  1, ddp.snode);
156     proto_tree_add_item(ddp_tree, hf_ddp_dst_socket, offset + 10, 1, ddp.dport);
157     proto_tree_add_item(ddp_tree, hf_ddp_src_socket, offset + 11, 1, ddp.sport);
158     proto_tree_add_item(ddp_tree, hf_ddp_type, offset + 12, 1, ddp.type);  
159   }
160
161   offset += DDP_HEADER_SIZE;
162
163   dissect_data(pd, offset, fd, tree);
164 }
165
166 void
167 proto_register_atalk(void)
168 {
169   static hf_register_info hf[] = {
170     { &hf_ddp_hopcount,
171       { "Hop count",            "ddp.hopcount", FT_UINT8,  BASE_DEC, NULL, 0x0,
172         "" }},
173
174     { &hf_ddp_len,
175       { "Datagram length",      "ddp.len",      FT_UINT16, BASE_DEC, NULL, 0x0,
176         "" }},
177
178     { &hf_ddp_checksum,
179       { "Checksum",             "ddp.checksum", FT_UINT16, BASE_DEC, NULL, 0x0,
180         "" }},
181
182     { &hf_ddp_dst_net,
183       { "Destination Net",      "ddp.dst.net",  FT_UINT16, BASE_DEC, NULL, 0x0,
184         "" }},
185
186     { &hf_ddp_src_net,
187       { "Source Net",           "ddp.src.net",  FT_UINT16, BASE_DEC, NULL, 0x0,
188         "" }},
189
190     { &hf_ddp_dst_node,
191       { "Destination Node",     "ddp.dst.node", FT_UINT8,  BASE_DEC, NULL, 0x0,
192         "" }},
193
194     { &hf_ddp_src_node,
195       { "Source Node",          "ddp.src.node", FT_UINT8,  BASE_DEC, NULL, 0x0,
196         "" }},
197
198     { &hf_ddp_dst_socket,
199       { "Destination Socket",   "ddp.dst.socket", FT_UINT8,  BASE_DEC, NULL, 0x0,
200         "" }},
201
202     { &hf_ddp_src_socket,
203       { "Source Socket",        "ddp.src.socket", FT_UINT8,  BASE_DEC, NULL, 0x0,
204         "" }},
205
206     { &hf_ddp_type,
207       { "Protocol type",        "ddp.type",     FT_UINT8,  BASE_DEC, VALS(op_vals), 0x0,
208         "" }},
209   };
210   static gint *ett[] = {
211     &ett_ddp,
212   };
213
214   proto_ddp = proto_register_protocol("Datagram Delivery Protocol", "ddp");
215   proto_register_field_array(proto_ddp, hf, array_length(hf));
216   proto_register_subtree_array(ett, array_length(ett));
217 }