It's the AppleTalk Session Protocol, not the AppleTalk Stream Protocol.
[obnox/wireshark/wip.git] / packet-dcerpc-ndr.c
1 /* packet-dcerpc-ndr.c
2  * Routines for DCERPC NDR dissection
3  * Copyright 2001, Todd Sabin <tas@webspan.net>
4  *
5  * $Id: packet-dcerpc-ndr.c,v 1.5 2002/01/29 09:13:28 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <string.h>
35 #include <ctype.h>
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-dcerpc.h"
40
41
42 /*
43  * The NDR routines are for use by dcerpc subdissetors.  They're
44  * primarily for making sure things are aligned properly according
45  * to the rules of NDR.
46  */
47
48 int
49 dissect_ndr_uint8 (tvbuff_t *tvb, gint offset, packet_info *pinfo,
50                    proto_tree *tree, char *drep, 
51                    int hfindex, guint8 *pdata)
52 {
53     dcerpc_info *di;
54
55     di=pinfo->private_data;
56     if(di->conformant_run){
57       /* just a run to handle conformant arrays, no scalars to dissect */
58       return offset;
59     }
60
61     /* no alignment needed */
62     return dissect_dcerpc_uint8 (tvb, offset, pinfo, 
63                                  tree, drep, hfindex, pdata);
64 }
65
66 int
67 dissect_ndr_uint16 (tvbuff_t *tvb, gint offset, packet_info *pinfo,
68                     proto_tree *tree, char *drep, 
69                     int hfindex, guint16 *pdata)
70 {
71     dcerpc_info *di;
72
73     di=pinfo->private_data;
74     if(di->conformant_run){
75       /* just a run to handle conformant arrays, no scalars to dissect */
76       return offset;
77     }
78
79
80     if (offset % 2) {
81         offset++;
82     }
83     return dissect_dcerpc_uint16 (tvb, offset, pinfo, 
84                                   tree, drep, hfindex, pdata);
85 }
86
87 int
88 dissect_ndr_uint32 (tvbuff_t *tvb, gint offset, packet_info *pinfo,
89                     proto_tree *tree, char *drep, 
90                     int hfindex, guint32 *pdata)
91 {
92     dcerpc_info *di;
93
94     di=pinfo->private_data;
95     if(di->conformant_run){
96       /* just a run to handle conformant arrays, no scalars to dissect */
97       return offset;
98     }
99
100
101     if (offset % 4) {
102         offset += 4 - (offset % 4);
103     }
104     return dissect_dcerpc_uint32 (tvb, offset, pinfo, 
105                                   tree, drep, hfindex, pdata);
106 }
107
108 int
109 dissect_ndr_uint64 (tvbuff_t *tvb, gint offset, packet_info *pinfo,
110                     proto_tree *tree, char *drep, 
111                     int hfindex, unsigned char *pdata)
112 {
113     dcerpc_info *di;
114
115     di=pinfo->private_data;
116     if(di->conformant_run){
117       /* just a run to handle conformant arrays, no scalars to dissect */
118       return offset;
119     }
120
121     if (offset % 4) {
122         offset += 4 - (offset % 4);
123     }
124     return dissect_dcerpc_uint64 (tvb, offset, pinfo, 
125                                   tree, drep, hfindex, pdata);
126 }
127
128 int
129 dissect_ndr_uuid_t (tvbuff_t *tvb, gint offset, packet_info *pinfo,
130                     proto_tree *tree, char *drep, 
131                     int hfindex, e_uuid_t *pdata)
132 {
133     e_uuid_t uuid;
134     dcerpc_info *di;
135
136     di=pinfo->private_data;
137     if(di->conformant_run){
138       /* just a run to handle conformant arrays, no scalars to dissect */
139       return offset;
140     }
141
142     /* uuid's are aligned to 4 bytes, due to initial uint32 in struct */
143     if (offset % 4) {
144         offset += 4 - (offset % 4);
145     }
146     dcerpc_tvb_get_uuid (tvb, offset, drep, &uuid);
147     if (tree) {
148         proto_tree_add_string_format (tree, hfindex, tvb, offset, 16, "",
149                                       "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
150                                       uuid.Data1, uuid.Data2, uuid.Data3,
151                                       uuid.Data4[0], uuid.Data4[1],
152                                       uuid.Data4[2], uuid.Data4[3],
153                                       uuid.Data4[4], uuid.Data4[5],
154                                       uuid.Data4[6], uuid.Data4[7]);
155     }
156     if (pdata) {
157         *pdata = uuid;
158     }
159     return offset + 16;
160 }
161
162
163 int
164 dissect_ndr_ctx_hnd (tvbuff_t *tvb, gint offset, packet_info *pinfo,
165                      proto_tree *tree, char *drep, 
166                      int hfindex, e_ctx_hnd *pdata)
167 {
168     e_ctx_hnd ctx_hnd;
169     dcerpc_info *di;
170
171     di=pinfo->private_data;
172     if(di->conformant_run){
173       /* just a run to handle conformant arrays, no scalars to dissect */
174       return offset;
175     }
176
177     if (offset % 4) {
178         offset += 4 - (offset % 4);
179     }
180     ctx_hnd.Data1 = dcerpc_tvb_get_ntohl (tvb, offset, drep);
181     dcerpc_tvb_get_uuid (tvb, offset+4, drep, &ctx_hnd.uuid);
182     if (tree) {
183         proto_tree_add_bytes (tree, hfindex, tvb, offset, 20,
184                               tvb_get_ptr (tvb, offset, 20));
185     }
186     if (pdata) {
187         *pdata = ctx_hnd;
188     }
189     return offset + 20;
190 }