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