2 * Routines for DCERPC over SMB packet disassembly
3 * Copyright 2001, Tim Potter <tpot@samba.org>
5 * $Id: packet-dcerpc-nt.c,v 1.1 2001/12/16 20:17:10 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #include "packet-dcerpc.h"
33 #include "packet-dcerpc-nt.h"
37 * This file contains helper routines that are used by the DCERPC over SMB
38 * dissectors for ethereal.
41 /* Align offset to a n-byte boundary */
43 int prs_align(int offset, int n)
46 offset += n - (offset % n);
51 /* Parse a 8-bit integer */
53 int prs_uint8(tvbuff_t *tvb, int offset, packet_info *pinfo,
54 proto_tree *tree, guint8 *data, char *name)
58 /* No alignment required */
60 i = tvb_get_guint8(tvb, offset);
64 proto_tree_add_text(tree, tvb, offset - 1, 1,
73 int prs_uint8s(tvbuff_t *tvb, int offset, packet_info *pinfo,
74 proto_tree *tree, int count, guint8 **data, char *name)
78 /* No alignment required */
80 ptr = tvb_get_ptr(tvb, offset, count);
83 proto_tree_add_text(tree, tvb, offset, count, "%s", name);
86 *data = (guint8 *)ptr;
93 /* Parse a 16-bit integer */
95 int prs_uint16(tvbuff_t *tvb, int offset, packet_info *pinfo,
96 proto_tree *tree, guint16 *data, char *name)
100 offset = prs_align(offset, 2);
102 i = tvb_get_letohs(tvb, offset);
106 proto_tree_add_text(tree, tvb, offset - 2, 2,
114 /* Parse a number of uint16's */
116 int prs_uint16s(tvbuff_t *tvb, int offset, packet_info *pinfo,
117 proto_tree *tree, int count, guint16 **data, char *name)
121 offset = prs_align(offset, 2);
123 ptr = tvb_get_ptr(tvb, offset, count * 2);
126 proto_tree_add_text(tree, tvb, offset, count * 2,
129 *data = (guint16 *)ptr;
136 /* Parse a 32-bit integer */
138 int prs_uint32(tvbuff_t *tvb, int offset, packet_info *pinfo,
139 proto_tree *tree, guint32 *data, char *name)
143 offset = prs_align(offset, 4);
145 i = tvb_get_letohl(tvb, offset);
149 proto_tree_add_text(tree, tvb, offset - 4, 4,
158 /* Parse a number of 32-bit integers */
160 int prs_uint32s(tvbuff_t *tvb, int offset, packet_info *pinfo,
161 proto_tree *tree, int count, guint32 **data, char *name)
165 offset = prs_align(offset, 4);
167 ptr = tvb_get_ptr(tvb, offset, count * 4);
170 proto_tree_add_text(tree, tvb, offset - 4, 4,
173 *data = (guint32 *)ptr;
180 /* Parse a NT status code */
182 int prs_ntstatus(tvbuff_t *tvb, int offset, packet_info *pinfo,
187 offset = prs_uint32(tvb, offset, pinfo, tree, &status, NULL);
190 proto_tree_add_text(tree, tvb, offset - 4, 4, "Status: %s",
191 val_to_str(status, NT_errors, "???"));
197 * We need to keep track of deferred referrents as they appear in the
198 * packet after all the non-pointer objects.
199 * to keep track of pointers as they are parsed as scalars and need to be
200 * remembered for the next call to the prs function.
202 * Pointers are stored in a linked list and pushed in the PARSE_SCALARS
203 * section of the prs function and popped in the PARSE_BUFFERS section. If
204 * we try to pop off a referrent that has a different name then we are
205 * expecting then something has gone wrong.
215 /* Create a new pointer */
217 static struct ptr *new_ptr(char *name, guint32 value)
221 p = g_malloc(sizeof(struct ptr));
223 p->name = g_strdup(name);
231 static void free_ptr(struct ptr *p)
239 /* Parse a pointer and store it's value in a linked list */
241 int prs_push_ptr(tvbuff_t *tvb, int offset, packet_info *pinfo,
242 proto_tree *tree, GList **ptr_list, char *name)
247 offset = prs_uint32(tvb, offset, pinfo, tree, &value, NULL);
250 proto_tree_add_text(tree, tvb, offset - 4, 4,
251 "%s pointer: 0x%08x", name, value);
253 p = new_ptr(name, value);
255 *ptr_list = g_list_append(*ptr_list, p);
258 fprintf(stderr, "DEBUG_PTRS: pushing %s ptr = 0x%08x, %d ptrs in "
259 "list\n", name, value, g_list_length(*ptr_list));
265 /* Pop a pointer of a given name. Return it's value. */
267 guint32 prs_pop_ptr(GList **ptr_list, char *name)
273 g_assert(g_list_length(*ptr_list) != 0); /* List too short */
275 /* Get pointer at head of list */
277 elt = g_list_first(*ptr_list);
278 p = (struct ptr *)elt->data;
282 if (strcmp(p->name, name) != 0) {
283 fprintf(stderr, "DEBUG_PTRS: wrong pointer (%s != %s)\n",
288 /* Free pointer record */
290 *ptr_list = g_list_remove_link(*ptr_list, elt);
293 fprintf(stderr, "DEBUG_PTRS: popping %s ptr = 0x%08x, %d ptrs in "
294 "list\n", p->name, p->value, g_list_length(*ptr_list));
303 * Parse a UNISTR2 structure
308 * [size_is(size/2)] [length_is(length/2)] [unique] wchar_t *string;
313 /* Convert a string from little-endian unicode to ascii. At the moment we
314 fake it by taking every odd byte. )-: The caller must free the
317 static char *fake_unicode(guint16 *data, int len)
322 buffer = malloc(len + 1);
324 for (i = 0; i < len; i++)
325 buffer[i] = data[i] & 0xff;
332 /* Parse a UNISTR2 structure */
334 int prs_UNISTR2(tvbuff_t *tvb, int offset, packet_info *pinfo,
335 proto_tree *tree, int flags, char **data, char *name)
337 guint32 len = 0, unknown = 0, max_len = 0;
339 if (flags & PARSE_SCALARS) {
340 offset = prs_uint32(tvb, offset, pinfo, tree, &len, "Length");
341 offset = prs_uint32(tvb, offset, pinfo, tree, &unknown,
343 offset = prs_uint32(tvb, offset, pinfo, tree, &max_len,
347 if (flags & PARSE_BUFFERS) {
350 offset = prs_uint16s(tvb, offset, pinfo, tree, max_len,
354 *data = fake_unicode(data16, max_len);
360 /* Parse a policy handle. */
362 int prs_policy_hnd(tvbuff_t *tvb, int offset, packet_info *pinfo,
365 offset = prs_align(offset, 4);
367 proto_tree_add_text(tree, tvb, offset, 20, "Policy Handle");