xref: /onnv-gate/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision 8334:5f1c6a3b0fad)
15772Sas200622 /*
25772Sas200622  * CDDL HEADER START
35772Sas200622  *
45772Sas200622  * The contents of this file are subject to the terms of the
55772Sas200622  * Common Development and Distribution License (the "License").
65772Sas200622  * You may not use this file except in compliance with the License.
75772Sas200622  *
85772Sas200622  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95772Sas200622  * or http://www.opensolaris.org/os/licensing.
105772Sas200622  * See the License for the specific language governing permissions
115772Sas200622  * and limitations under the License.
125772Sas200622  *
135772Sas200622  * When distributing Covered Code, include this CDDL HEADER in each
145772Sas200622  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155772Sas200622  * If applicable, add the following below this CDDL HEADER, with the
165772Sas200622  * fields enclosed by brackets "[]" replaced with your own identifying
175772Sas200622  * information: Portions Copyright [yyyy] [name of copyright owner]
185772Sas200622  *
195772Sas200622  * CDDL HEADER END
205772Sas200622  */
215772Sas200622 /*
226482Samw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235772Sas200622  * Use is subject to license terms.
245772Sas200622  */
255772Sas200622 
265772Sas200622 /*
27*8334SJose.Borrego@Sun.COM  * Server-side NDR stream (PDU) operations. Stream operations should
28*8334SJose.Borrego@Sun.COM  * return TRUE (non-zero) on success or FALSE (zero or a null pointer)
29*8334SJose.Borrego@Sun.COM  * on failure. When an operation returns FALSE, including ndo_malloc()
30*8334SJose.Borrego@Sun.COM  * returning NULL, it should set the nds->error to indicate what went
31*8334SJose.Borrego@Sun.COM  * wrong.
325772Sas200622  *
33*8334SJose.Borrego@Sun.COM  * When available, the relevant ndr reference is passed to the
345772Sas200622  * operation but keep in mind that it may be a null pointer.
355772Sas200622  *
36*8334SJose.Borrego@Sun.COM  * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu()
375772Sas200622  * must never grow the PDU data. A request for out-of-bounds data is
385772Sas200622  * an error. The swap_bytes flag is 1 if NDR knows that the byte-
395772Sas200622  * order in the PDU is different from the local system.
405772Sas200622  */
415772Sas200622 
425772Sas200622 #include <sys/types.h>
435772Sas200622 #include <stdarg.h>
445772Sas200622 #include <ctype.h>
455772Sas200622 #include <stdio.h>
465772Sas200622 #include <stdlib.h>
475772Sas200622 #include <strings.h>
485772Sas200622 #include <string.h>
495772Sas200622 #include <assert.h>
505772Sas200622 
515772Sas200622 #include <smbsrv/libsmb.h>
52*8334SJose.Borrego@Sun.COM #include <smbsrv/libmlrpc.h>
535772Sas200622 #include <smbsrv/ntstatus.h>
545772Sas200622 
555772Sas200622 #define	NDOBUFSZ		128
565772Sas200622 
575772Sas200622 #define	NDR_PDU_BLOCK_SIZE	(4*1024)
585772Sas200622 #define	NDR_PDU_BLOCK_MASK	(NDR_PDU_BLOCK_SIZE - 1)
595772Sas200622 #define	NDR_PDU_ALIGN(N) \
605772Sas200622 	(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
615772Sas200622 #define	NDR_PDU_MAX_SIZE		(64*1024*1024)
625772Sas200622 
63*8334SJose.Borrego@Sun.COM static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
64*8334SJose.Borrego@Sun.COM static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
65*8334SJose.Borrego@Sun.COM static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
66*8334SJose.Borrego@Sun.COM static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
67*8334SJose.Borrego@Sun.COM     ndr_ref_t *);
68*8334SJose.Borrego@Sun.COM static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
69*8334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
70*8334SJose.Borrego@Sun.COM static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
71*8334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
72*8334SJose.Borrego@Sun.COM static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
73*8334SJose.Borrego@Sun.COM static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
74*8334SJose.Borrego@Sun.COM static int ndo_reset(ndr_stream_t *);
75*8334SJose.Borrego@Sun.COM static void ndo_destruct(ndr_stream_t *);
76*8334SJose.Borrego@Sun.COM static void ndo_hexfmt(uint8_t *, int, int, char *, int);
775772Sas200622 
785772Sas200622 /*
79*8334SJose.Borrego@Sun.COM  * The ndr stream operations table.
805772Sas200622  */
81*8334SJose.Borrego@Sun.COM static ndr_stream_ops_t nds_ops = {
82*8334SJose.Borrego@Sun.COM     ndo_malloc,
83*8334SJose.Borrego@Sun.COM     ndo_free,
84*8334SJose.Borrego@Sun.COM     ndo_grow_pdu,
85*8334SJose.Borrego@Sun.COM     ndo_pad_pdu,
86*8334SJose.Borrego@Sun.COM     ndo_get_pdu,
87*8334SJose.Borrego@Sun.COM     ndo_put_pdu,
88*8334SJose.Borrego@Sun.COM     ndo_tattle,
89*8334SJose.Borrego@Sun.COM     ndo_tattle_error,
90*8334SJose.Borrego@Sun.COM     ndo_reset,
91*8334SJose.Borrego@Sun.COM     ndo_destruct
925772Sas200622 };
935772Sas200622 
945772Sas200622 /*
95*8334SJose.Borrego@Sun.COM  * nds_bswap
965772Sas200622  *
975772Sas200622  * Copies len bytes from src to dst such that dst contains the bytes
985772Sas200622  * from src in reverse order.
995772Sas200622  *
1005772Sas200622  * We expect to be dealing with bytes, words, dwords etc. So the
1015772Sas200622  * length must be non-zero and a power of 2.
1025772Sas200622  */
1035772Sas200622 void
104*8334SJose.Borrego@Sun.COM nds_bswap(void *srcbuf, void *dstbuf, size_t len)
1055772Sas200622 {
1065772Sas200622 	uint8_t *src = (uint8_t *)srcbuf;
1075772Sas200622 	uint8_t *dst = (uint8_t *)dstbuf;
1085772Sas200622 
1095772Sas200622 	if ((len != 0) && ((len & (len - 1)) == 0)) {
1105772Sas200622 		src += len;
1115772Sas200622 
1125772Sas200622 		while (len--)
1135772Sas200622 			*dst++ = *(--src);
1145772Sas200622 	}
1155772Sas200622 }
1165772Sas200622 
1175772Sas200622 /*
118*8334SJose.Borrego@Sun.COM  * nds_initialize
1195772Sas200622  *
1205772Sas200622  * Initialize a stream. Sets up the PDU parameters and assigns the stream
1215772Sas200622  * operations and the reference to the heap. An external heap is provided
1225772Sas200622  * to the stream, rather than each stream creating its own heap.
1235772Sas200622  */
1247052Samw void
125*8334SJose.Borrego@Sun.COM nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
126*8334SJose.Borrego@Sun.COM     int composite_op, ndr_heap_t *heap)
1275772Sas200622 {
1285772Sas200622 	unsigned size;
1295772Sas200622 
130*8334SJose.Borrego@Sun.COM 	assert(nds);
1315772Sas200622 	assert(heap);
1325772Sas200622 
133*8334SJose.Borrego@Sun.COM 	bzero(nds, sizeof (*nds));
1345772Sas200622 
1355772Sas200622 	if (pdu_size_hint > NDR_PDU_MAX_SIZE)
1367052Samw 		return;
1375772Sas200622 
1385772Sas200622 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
139*8334SJose.Borrego@Sun.COM 	nds->pdu_base_addr = malloc(size);
140*8334SJose.Borrego@Sun.COM 	assert(nds->pdu_base_addr);
1415772Sas200622 
142*8334SJose.Borrego@Sun.COM 	nds->pdu_max_size = size;
143*8334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
144*8334SJose.Borrego@Sun.COM 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
1455772Sas200622 
146*8334SJose.Borrego@Sun.COM 	nds->ndo = &nds_ops;
147*8334SJose.Borrego@Sun.COM 	nds->heap = (struct ndr_heap *)heap;
1485772Sas200622 
149*8334SJose.Borrego@Sun.COM 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
150*8334SJose.Borrego@Sun.COM 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
1515772Sas200622 
152*8334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
1535772Sas200622 }
1545772Sas200622 
1557052Samw void
156*8334SJose.Borrego@Sun.COM nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
1576482Samw {
1587052Samw 	iovec_t *iov;
1596482Samw 	ndr_frag_t *frag;
1606482Samw 	uint32_t size = 0;
1616482Samw 
1627052Samw 	bzero(frags, sizeof (ndr_fraglist_t));
1637052Samw 
164*8334SJose.Borrego@Sun.COM 	for (frag = nds->frags.head; frag; frag = frag->next)
1656482Samw 		size += frag->len;
1666482Samw 
1677052Samw 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
1687052Samw 		return;
1697052Samw 
170*8334SJose.Borrego@Sun.COM 	frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
1717052Samw 	if (frags->iov == NULL)
1727052Samw 		return;
1737052Samw 
174*8334SJose.Borrego@Sun.COM 	frags->head = nds->frags.head;
175*8334SJose.Borrego@Sun.COM 	frags->tail = nds->frags.tail;
176*8334SJose.Borrego@Sun.COM 	frags->nfrag = nds->frags.nfrag;
177*8334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
1786482Samw 
1797052Samw 	frags->uio.uio_iov = frags->iov;
1807052Samw 	frags->uio.uio_iovcnt = frags->nfrag;
1817052Samw 	frags->uio.uio_offset = 0;
1827052Samw 	frags->uio.uio_segflg = UIO_USERSPACE;
1837052Samw 	frags->uio.uio_resid = size;
1847052Samw 
1857052Samw 	iov = frags->uio.uio_iov;
1867052Samw 	for (frag = frags->head; frag; frag = frag->next) {
1877052Samw 		iov->iov_base = (caddr_t)frag->buf;
1887052Samw 		iov->iov_len = frag->len;
1897052Samw 		++iov;
1906482Samw 	}
1916482Samw }
1926482Samw 
1935772Sas200622 /*
194*8334SJose.Borrego@Sun.COM  * nds_destruct
1955772Sas200622  *
1965772Sas200622  * Destroy a stream. This is an external interface to provide access to
1975772Sas200622  * the stream's destruct operation.
1985772Sas200622  */
1995772Sas200622 void
200*8334SJose.Borrego@Sun.COM nds_destruct(ndr_stream_t *nds)
2015772Sas200622 {
202*8334SJose.Borrego@Sun.COM 	NDS_DESTRUCT(nds);
2035772Sas200622 }
2045772Sas200622 
2055772Sas200622 /*
206*8334SJose.Borrego@Sun.COM  * ndo_malloc
2075772Sas200622  *
2085772Sas200622  * Allocate memory from the stream heap.
2095772Sas200622  */
2105772Sas200622 /*ARGSUSED*/
2115772Sas200622 static char *
212*8334SJose.Borrego@Sun.COM ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
2135772Sas200622 {
214*8334SJose.Borrego@Sun.COM 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
2155772Sas200622 }
2165772Sas200622 
2175772Sas200622 /*
218*8334SJose.Borrego@Sun.COM  * ndo_free
2195772Sas200622  *
2205772Sas200622  * Always succeeds: cannot free individual stream allocations.
2215772Sas200622  */
2225772Sas200622 /*ARGSUSED*/
2235772Sas200622 static int
224*8334SJose.Borrego@Sun.COM ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
2255772Sas200622 {
2265772Sas200622 	return (1);
2275772Sas200622 }
2285772Sas200622 
2295772Sas200622 /*
230*8334SJose.Borrego@Sun.COM  * ndo_grow_pdu
2315772Sas200622  *
2325772Sas200622  * This is the only place that should change the size of the PDU. If the
2335772Sas200622  * desired offset is beyond the current PDU size, we realloc the PDU
2345772Sas200622  * buffer to accommodate the request. For efficiency, the PDU is always
2355772Sas200622  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2365772Sas200622  * beyond NDR_PDU_MAX_SIZE are rejected.
2375772Sas200622  *
2385772Sas200622  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2395772Sas200622  */
2405772Sas200622 static int
241*8334SJose.Borrego@Sun.COM ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
2425772Sas200622 {
2435772Sas200622 	unsigned char *pdu_addr;
2445772Sas200622 	unsigned pdu_max_size;
2455772Sas200622 
246*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "grow %d", want_end_offset);
2475772Sas200622 
248*8334SJose.Borrego@Sun.COM 	pdu_max_size = nds->pdu_max_size;
2495772Sas200622 
2505772Sas200622 	if (want_end_offset > pdu_max_size) {
2515772Sas200622 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2525772Sas200622 
2535772Sas200622 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2545772Sas200622 			return (0);
2555772Sas200622 
256*8334SJose.Borrego@Sun.COM 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
2575772Sas200622 		if (pdu_addr == 0)
2585772Sas200622 			return (0);
2595772Sas200622 
260*8334SJose.Borrego@Sun.COM 		nds->pdu_max_size = pdu_max_size;
261*8334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = pdu_addr;
262*8334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = (unsigned long)pdu_addr;
2635772Sas200622 	}
2645772Sas200622 
265*8334SJose.Borrego@Sun.COM 	nds->pdu_size = want_end_offset;
2665772Sas200622 	return (1);
2675772Sas200622 }
2685772Sas200622 
2695772Sas200622 static int
270*8334SJose.Borrego@Sun.COM ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
271*8334SJose.Borrego@Sun.COM     unsigned long n_bytes, ndr_ref_t *ref)
2725772Sas200622 {
2735772Sas200622 	unsigned char *data;
2745772Sas200622 
275*8334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
2765772Sas200622 	data += pdu_offset;
2775772Sas200622 
278*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
2795772Sas200622 
2805772Sas200622 	bzero(data, n_bytes);
2815772Sas200622 	return (1);
2825772Sas200622 }
2835772Sas200622 
2845772Sas200622 /*
285*8334SJose.Borrego@Sun.COM  * ndo_get_pdu
2865772Sas200622  *
2875772Sas200622  * The swap flag is 1 if NDR knows that the byte-order in the PDU
2885772Sas200622  * is different from the local system.
2895772Sas200622  *
2905772Sas200622  * Returns 1 on success or 0 to indicate failure.
2915772Sas200622  */
2925772Sas200622 static int
293*8334SJose.Borrego@Sun.COM ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
294*8334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
2955772Sas200622 {
2965772Sas200622 	unsigned char *data;
2975772Sas200622 	char hexbuf[NDOBUFSZ];
2985772Sas200622 
299*8334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3005772Sas200622 	data += pdu_offset;
3015772Sas200622 
302*8334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
3035772Sas200622 
304*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "get %d@%-3d = %s",
3055772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3065772Sas200622 
3075772Sas200622 	if (!swap_bytes)
3085772Sas200622 		bcopy(data, buf, n_bytes);
3095772Sas200622 	else
310*8334SJose.Borrego@Sun.COM 		nds_bswap(data, (unsigned char *)buf, n_bytes);
3115772Sas200622 
3125772Sas200622 	return (1);
3135772Sas200622 }
3145772Sas200622 
3155772Sas200622 /*
316*8334SJose.Borrego@Sun.COM  * ndo_put_pdu
3175772Sas200622  *
3185772Sas200622  * This is a receiver makes right protocol. So we do not need
3195772Sas200622  * to be concerned about the byte-order of an outgoing PDU.
3205772Sas200622  */
3215772Sas200622 /*ARGSUSED*/
3225772Sas200622 static int
323*8334SJose.Borrego@Sun.COM ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
324*8334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3255772Sas200622 {
3265772Sas200622 	unsigned char *data;
3275772Sas200622 	char hexbuf[NDOBUFSZ];
3285772Sas200622 
329*8334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3305772Sas200622 	data += pdu_offset;
3315772Sas200622 
332*8334SJose.Borrego@Sun.COM 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3335772Sas200622 
334*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "put %d@%-3d = %s",
3355772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3365772Sas200622 
3375772Sas200622 	bcopy(buf, data, n_bytes);
3385772Sas200622 	return (1);
3395772Sas200622 }
3405772Sas200622 
3415772Sas200622 static void
342*8334SJose.Borrego@Sun.COM ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
3435772Sas200622 {
344*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, what);
3455772Sas200622 }
3465772Sas200622 
3475772Sas200622 static void
348*8334SJose.Borrego@Sun.COM ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
3495772Sas200622 {
3505772Sas200622 	unsigned char *data;
3515772Sas200622 	char hexbuf[NDOBUFSZ];
3525772Sas200622 
353*8334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3545772Sas200622 	if (ref)
3555772Sas200622 		data += ref->pdu_offset;
3565772Sas200622 	else
357*8334SJose.Borrego@Sun.COM 		data += nds->pdu_scan_offset;
3585772Sas200622 
359*8334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
3605772Sas200622 
361*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
362*8334SJose.Borrego@Sun.COM 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
363*8334SJose.Borrego@Sun.COM 	    nds->pdu_size, nds->pdu_max_size);
364*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "      %s", hexbuf);
3655772Sas200622 }
3665772Sas200622 
3675772Sas200622 /*
368*8334SJose.Borrego@Sun.COM  * ndo_reset
3695772Sas200622  *
3705772Sas200622  * Reset a stream: zap the outer_queue. We don't need to tamper
3715772Sas200622  * with the stream heap: it's handled externally to the stream.
3725772Sas200622  */
3735772Sas200622 static int
374*8334SJose.Borrego@Sun.COM ndo_reset(ndr_stream_t *nds)
3755772Sas200622 {
376*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "reset");
3775772Sas200622 
378*8334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
379*8334SJose.Borrego@Sun.COM 	nds->pdu_scan_offset = 0;
380*8334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
381*8334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
382*8334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
3835772Sas200622 
3845772Sas200622 	return (1);
3855772Sas200622 }
3865772Sas200622 
3875772Sas200622 /*
388*8334SJose.Borrego@Sun.COM  * ndo_destruct
3895772Sas200622  *
3906482Samw  * Destruct a stream: zap the outer_queue.
3916482Samw  * Note: heap management (creation/destruction) is external to the stream.
3925772Sas200622  */
3935772Sas200622 static void
394*8334SJose.Borrego@Sun.COM ndo_destruct(ndr_stream_t *nds)
3955772Sas200622 {
3966482Samw 	ndr_frag_t *frag;
3976482Samw 
398*8334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "destruct");
399*8334SJose.Borrego@Sun.COM 
400*8334SJose.Borrego@Sun.COM 	if (nds == NULL)
401*8334SJose.Borrego@Sun.COM 		return;
4025772Sas200622 
403*8334SJose.Borrego@Sun.COM 	if (nds->pdu_base_addr != NULL) {
404*8334SJose.Borrego@Sun.COM 		free(nds->pdu_base_addr);
405*8334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = NULL;
406*8334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = 0;
4075772Sas200622 	}
4085772Sas200622 
409*8334SJose.Borrego@Sun.COM 	while ((frag = nds->frags.head) != NULL) {
410*8334SJose.Borrego@Sun.COM 		nds->frags.head = frag->next;
4116482Samw 		free(frag);
4126482Samw 	}
4136482Samw 
414*8334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
4157052Samw 
416*8334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
417*8334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
418*8334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
4195772Sas200622 }
4205772Sas200622 
4215772Sas200622 /*
4225772Sas200622  * Printf style formatting for NDR operations.
4235772Sas200622  */
4245772Sas200622 void
425*8334SJose.Borrego@Sun.COM ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
4265772Sas200622 {
4275772Sas200622 	va_list ap;
4285772Sas200622 	char buf[NDOBUFSZ];
4295772Sas200622 
4305772Sas200622 	va_start(ap, fmt);
4315772Sas200622 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4325772Sas200622 	va_end(ap);
4335772Sas200622 
434*8334SJose.Borrego@Sun.COM 	if (nds)
435*8334SJose.Borrego@Sun.COM 		ndo_fmt(nds, ref, buf);
4365772Sas200622 	else
437*8334SJose.Borrego@Sun.COM 		ndo_trace(buf);
4385772Sas200622 }
4395772Sas200622 
4405772Sas200622 /*
4415772Sas200622  * Main output formatter for NDR operations.
4425772Sas200622  *
4435772Sas200622  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
4445772Sas200622  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
4455772Sas200622  *
4465772Sas200622  *	U       Marshalling flag (M=marshal, U=unmarshal)
4475772Sas200622  *	I       Direction flag (I=in, O=out)
4485772Sas200622  *	...     Field name
4495772Sas200622  *	get     PDU operation (get or put)
4505772Sas200622  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
4515772Sas200622  *	{05}    Value
4525772Sas200622  */
4535772Sas200622 void
454*8334SJose.Borrego@Sun.COM ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
4555772Sas200622 {
456*8334SJose.Borrego@Sun.COM 	ndr_ref_t	*p;
457*8334SJose.Borrego@Sun.COM 	int		indent;
458*8334SJose.Borrego@Sun.COM 	char		ref_name[NDOBUFSZ];
459*8334SJose.Borrego@Sun.COM 	char		buf[NDOBUFSZ];
460*8334SJose.Borrego@Sun.COM 	int		m_op_c = '?', dir_c = '?';
4615772Sas200622 
462*8334SJose.Borrego@Sun.COM 	switch (nds->m_op) {
4635772Sas200622 	case 0:				m_op_c = '-';	break;
4645772Sas200622 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
4655772Sas200622 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
4665772Sas200622 	default:			m_op_c = '?';	break;
4675772Sas200622 	}
4685772Sas200622 
469*8334SJose.Borrego@Sun.COM 	switch (nds->dir) {
4705772Sas200622 	case 0:				dir_c = '-';	break;
4715772Sas200622 	case NDR_DIR_IN:		dir_c = 'I';	break;
4725772Sas200622 	case NDR_DIR_OUT:		dir_c = 'O';	break;
4735772Sas200622 	default:			dir_c = '?';	break;
4745772Sas200622 	}
4755772Sas200622 
4765772Sas200622 	for (indent = 0, p = ref; p; p = p->enclosing)
4775772Sas200622 		indent++;
4785772Sas200622 
4795772Sas200622 	if (ref && ref->name) {
4805772Sas200622 		if (*ref->name == '[' && ref->enclosing) {
4815772Sas200622 			indent--;
4825772Sas200622 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
4835772Sas200622 			    ref->enclosing->name, ref->name);
4845772Sas200622 		} else {
4855772Sas200622 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
4865772Sas200622 		}
4875772Sas200622 	} else {
4885772Sas200622 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
4895772Sas200622 	}
4905772Sas200622 
4915772Sas200622 	(void) snprintf(buf, NDOBUFSZ, "%c%c %02d %-.*s %-*s  %s",
4925772Sas200622 	    m_op_c, dir_c, indent, indent,
4935772Sas200622 	    "....+....+....+....+....+....",
4945772Sas200622 	    20 - indent, ref_name, note);
4955772Sas200622 
496*8334SJose.Borrego@Sun.COM 	ndo_trace(buf);
4975772Sas200622 }
4985772Sas200622 
4995772Sas200622 /*ARGSUSED*/
5005772Sas200622 void
501*8334SJose.Borrego@Sun.COM ndo_trace(const char *s)
5025772Sas200622 {
5035772Sas200622 	/*
5045772Sas200622 	 * Temporary fbt for dtrace until user space sdt enabled.
5055772Sas200622 	 */
5065772Sas200622 }
5075772Sas200622 
5085772Sas200622 /*
5095772Sas200622  * Format data as hex bytes (limit is 10 bytes):
5105772Sas200622  *
5115772Sas200622  *	1188689424 {10 f6 d9 46}
5125772Sas200622  *
5135772Sas200622  * If the input data is greater than 10 bytes, an ellipsis will
5145772Sas200622  * be inserted before the closing brace.
5155772Sas200622  */
5165772Sas200622 static void
517*8334SJose.Borrego@Sun.COM ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5185772Sas200622 {
5195772Sas200622 	char *p = buf;
5205772Sas200622 	int interp = 1;
5215772Sas200622 	uint32_t c;
5225772Sas200622 	int n;
5235772Sas200622 	int i;
5245772Sas200622 
5255772Sas200622 	n = (size > 10) ? 10 : size;
5265772Sas200622 	if (n > len-1)
5275772Sas200622 		n = len-1;
5285772Sas200622 
5295772Sas200622 	switch (size) {
5305772Sas200622 	case 1:
5315772Sas200622 		c = *(uint8_t *)data;
5325772Sas200622 		break;
5335772Sas200622 	case 2:
5345772Sas200622 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5355772Sas200622 			c = *(uint16_t *)data;
5365772Sas200622 		else
5375772Sas200622 			c = (data[0] << 8) | data[1];
5385772Sas200622 		break;
5395772Sas200622 	case 4:
5405772Sas200622 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5415772Sas200622 			c = *(uint32_t *)data;
5425772Sas200622 		} else {
5435772Sas200622 			c = (data[0] << 24) | (data[1] << 16)
5445772Sas200622 			    | (data[2] << 8) | data[3];
5455772Sas200622 		}
5465772Sas200622 		break;
5475772Sas200622 	default:
5485772Sas200622 		c = 0;
5495772Sas200622 		interp = 0;
5505772Sas200622 		break;
5515772Sas200622 	}
5525772Sas200622 
5535772Sas200622 	if (interp)
5545772Sas200622 		p += sprintf(p, "%4u {", c);
5555772Sas200622 	else
5565772Sas200622 		p += sprintf(p, " {");
5575772Sas200622 
5585772Sas200622 	p += sprintf(p, "%02x", data[0]);
5595772Sas200622 	for (i = 1; i < n; i++)
5605772Sas200622 		p += sprintf(p, " %02x", data[i]);
5615772Sas200622 	if (size > 10)
5625772Sas200622 		p += sprintf(p, " ...}");
5635772Sas200622 	else
5645772Sas200622 		p += sprintf(p, "}");
5655772Sas200622 
5665772Sas200622 	/*
5675772Sas200622 	 * Show c if it's a printable character or wide-char.
5685772Sas200622 	 */
5695772Sas200622 	if (size < 4 && isprint((uint8_t)c))
5705772Sas200622 		(void) sprintf(p, " %c", (uint8_t)c);
5715772Sas200622 }
572