xref: /onnv-gate/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision 11337:1f8fe42c7b83)
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 /*
229914Samw@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235772Sas200622  * Use is subject to license terms.
245772Sas200622  */
255772Sas200622 
265772Sas200622 /*
278334SJose.Borrego@Sun.COM  * Server-side NDR stream (PDU) operations. Stream operations should
288334SJose.Borrego@Sun.COM  * return TRUE (non-zero) on success or FALSE (zero or a null pointer)
298334SJose.Borrego@Sun.COM  * on failure. When an operation returns FALSE, including ndo_malloc()
308334SJose.Borrego@Sun.COM  * returning NULL, it should set the nds->error to indicate what went
318334SJose.Borrego@Sun.COM  * wrong.
325772Sas200622  *
338334SJose.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  *
368334SJose.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>
528334SJose.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 
638334SJose.Borrego@Sun.COM static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
648334SJose.Borrego@Sun.COM static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
658334SJose.Borrego@Sun.COM static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
668334SJose.Borrego@Sun.COM static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
678334SJose.Borrego@Sun.COM     ndr_ref_t *);
688334SJose.Borrego@Sun.COM static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
698334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
708334SJose.Borrego@Sun.COM static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
718334SJose.Borrego@Sun.COM     char *, int, ndr_ref_t *);
728334SJose.Borrego@Sun.COM static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
738334SJose.Borrego@Sun.COM static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
748334SJose.Borrego@Sun.COM static int ndo_reset(ndr_stream_t *);
758334SJose.Borrego@Sun.COM static void ndo_destruct(ndr_stream_t *);
768334SJose.Borrego@Sun.COM static void ndo_hexfmt(uint8_t *, int, int, char *, int);
775772Sas200622 
785772Sas200622 /*
798334SJose.Borrego@Sun.COM  * The ndr stream operations table.
805772Sas200622  */
818334SJose.Borrego@Sun.COM static ndr_stream_ops_t nds_ops = {
828334SJose.Borrego@Sun.COM     ndo_malloc,
838334SJose.Borrego@Sun.COM     ndo_free,
848334SJose.Borrego@Sun.COM     ndo_grow_pdu,
858334SJose.Borrego@Sun.COM     ndo_pad_pdu,
868334SJose.Borrego@Sun.COM     ndo_get_pdu,
878334SJose.Borrego@Sun.COM     ndo_put_pdu,
888334SJose.Borrego@Sun.COM     ndo_tattle,
898334SJose.Borrego@Sun.COM     ndo_tattle_error,
908334SJose.Borrego@Sun.COM     ndo_reset,
918334SJose.Borrego@Sun.COM     ndo_destruct
925772Sas200622 };
935772Sas200622 
945772Sas200622 /*
958334SJose.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
1048334SJose.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 /*
1188334SJose.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  */
124*11337SWilliam.Krier@Sun.COM int
1258334SJose.Borrego@Sun.COM nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
1268334SJose.Borrego@Sun.COM     int composite_op, ndr_heap_t *heap)
1275772Sas200622 {
1285772Sas200622 	unsigned size;
1295772Sas200622 
1308334SJose.Borrego@Sun.COM 	assert(nds);
1315772Sas200622 	assert(heap);
1325772Sas200622 
1338334SJose.Borrego@Sun.COM 	bzero(nds, sizeof (*nds));
1345772Sas200622 
1355772Sas200622 	if (pdu_size_hint > NDR_PDU_MAX_SIZE)
136*11337SWilliam.Krier@Sun.COM 		return (0);
1375772Sas200622 
1385772Sas200622 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
139*11337SWilliam.Krier@Sun.COM 
140*11337SWilliam.Krier@Sun.COM 	if ((nds->pdu_base_addr = malloc(size)) == NULL) {
141*11337SWilliam.Krier@Sun.COM 		nds->error = NDR_ERR_MALLOC_FAILED;
142*11337SWilliam.Krier@Sun.COM 		nds->error_ref = __LINE__;
143*11337SWilliam.Krier@Sun.COM 		NDS_TATTLE_ERROR(nds, NULL, NULL);
144*11337SWilliam.Krier@Sun.COM 		return (NDR_DRC_FAULT_OUT_OF_MEMORY);
145*11337SWilliam.Krier@Sun.COM 	}
1465772Sas200622 
1478334SJose.Borrego@Sun.COM 	nds->pdu_max_size = size;
1488334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
1498334SJose.Borrego@Sun.COM 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
1505772Sas200622 
1518334SJose.Borrego@Sun.COM 	nds->ndo = &nds_ops;
1528334SJose.Borrego@Sun.COM 	nds->heap = (struct ndr_heap *)heap;
1535772Sas200622 
1548334SJose.Borrego@Sun.COM 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
1558334SJose.Borrego@Sun.COM 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
1565772Sas200622 
1578334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
158*11337SWilliam.Krier@Sun.COM 	return (0);
1595772Sas200622 }
1605772Sas200622 
1617052Samw void
1628334SJose.Borrego@Sun.COM nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
1636482Samw {
1647052Samw 	iovec_t *iov;
1656482Samw 	ndr_frag_t *frag;
1666482Samw 	uint32_t size = 0;
1676482Samw 
1687052Samw 	bzero(frags, sizeof (ndr_fraglist_t));
1697052Samw 
1708334SJose.Borrego@Sun.COM 	for (frag = nds->frags.head; frag; frag = frag->next)
1716482Samw 		size += frag->len;
1726482Samw 
1737052Samw 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
1747052Samw 		return;
1757052Samw 
1768334SJose.Borrego@Sun.COM 	frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
1777052Samw 	if (frags->iov == NULL)
1787052Samw 		return;
1797052Samw 
1808334SJose.Borrego@Sun.COM 	frags->head = nds->frags.head;
1818334SJose.Borrego@Sun.COM 	frags->tail = nds->frags.tail;
1828334SJose.Borrego@Sun.COM 	frags->nfrag = nds->frags.nfrag;
1838334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
1846482Samw 
1857052Samw 	frags->uio.uio_iov = frags->iov;
1867052Samw 	frags->uio.uio_iovcnt = frags->nfrag;
1877052Samw 	frags->uio.uio_offset = 0;
1887052Samw 	frags->uio.uio_segflg = UIO_USERSPACE;
1897052Samw 	frags->uio.uio_resid = size;
1907052Samw 
1917052Samw 	iov = frags->uio.uio_iov;
1927052Samw 	for (frag = frags->head; frag; frag = frag->next) {
1937052Samw 		iov->iov_base = (caddr_t)frag->buf;
1947052Samw 		iov->iov_len = frag->len;
1957052Samw 		++iov;
1966482Samw 	}
1976482Samw }
1986482Samw 
1995772Sas200622 /*
2008334SJose.Borrego@Sun.COM  * nds_destruct
2015772Sas200622  *
2025772Sas200622  * Destroy a stream. This is an external interface to provide access to
2035772Sas200622  * the stream's destruct operation.
2045772Sas200622  */
2055772Sas200622 void
2068334SJose.Borrego@Sun.COM nds_destruct(ndr_stream_t *nds)
2075772Sas200622 {
20810475Samw@Sun.COM 	if ((nds == NULL) || (nds->ndo == NULL))
20910475Samw@Sun.COM 		return;
21010475Samw@Sun.COM 
2118334SJose.Borrego@Sun.COM 	NDS_DESTRUCT(nds);
2125772Sas200622 }
2135772Sas200622 
2145772Sas200622 /*
21510475Samw@Sun.COM  * Print NDR stream state.
21610475Samw@Sun.COM  */
21710475Samw@Sun.COM void
21810475Samw@Sun.COM nds_show_state(ndr_stream_t *nds)
21910475Samw@Sun.COM {
22010475Samw@Sun.COM 	if (nds == NULL) {
22110475Samw@Sun.COM 		ndo_printf(NULL, NULL, "nds: <null");
22210475Samw@Sun.COM 		return;
22310475Samw@Sun.COM 	}
22410475Samw@Sun.COM 
22510475Samw@Sun.COM 	ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d",
22610475Samw@Sun.COM 	    nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
22710475Samw@Sun.COM 	    nds->pdu_scan_offset);
22810475Samw@Sun.COM }
22910475Samw@Sun.COM 
23010475Samw@Sun.COM /*
2318334SJose.Borrego@Sun.COM  * ndo_malloc
2325772Sas200622  *
2335772Sas200622  * Allocate memory from the stream heap.
2345772Sas200622  */
2355772Sas200622 /*ARGSUSED*/
2365772Sas200622 static char *
2378334SJose.Borrego@Sun.COM ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
2385772Sas200622 {
2398334SJose.Borrego@Sun.COM 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
2405772Sas200622 }
2415772Sas200622 
2425772Sas200622 /*
2438334SJose.Borrego@Sun.COM  * ndo_free
2445772Sas200622  *
2455772Sas200622  * Always succeeds: cannot free individual stream allocations.
2465772Sas200622  */
2475772Sas200622 /*ARGSUSED*/
2485772Sas200622 static int
2498334SJose.Borrego@Sun.COM ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
2505772Sas200622 {
2515772Sas200622 	return (1);
2525772Sas200622 }
2535772Sas200622 
2545772Sas200622 /*
2558334SJose.Borrego@Sun.COM  * ndo_grow_pdu
2565772Sas200622  *
2575772Sas200622  * This is the only place that should change the size of the PDU. If the
2585772Sas200622  * desired offset is beyond the current PDU size, we realloc the PDU
2595772Sas200622  * buffer to accommodate the request. For efficiency, the PDU is always
2605772Sas200622  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
2615772Sas200622  * beyond NDR_PDU_MAX_SIZE are rejected.
2625772Sas200622  *
2635772Sas200622  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
2645772Sas200622  */
2655772Sas200622 static int
2668334SJose.Borrego@Sun.COM ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
2675772Sas200622 {
2685772Sas200622 	unsigned char *pdu_addr;
2695772Sas200622 	unsigned pdu_max_size;
2705772Sas200622 
2718334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "grow %d", want_end_offset);
2725772Sas200622 
2738334SJose.Borrego@Sun.COM 	pdu_max_size = nds->pdu_max_size;
2745772Sas200622 
2755772Sas200622 	if (want_end_offset > pdu_max_size) {
2765772Sas200622 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
2775772Sas200622 
2785772Sas200622 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
2795772Sas200622 			return (0);
2805772Sas200622 
2818334SJose.Borrego@Sun.COM 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
2825772Sas200622 		if (pdu_addr == 0)
2835772Sas200622 			return (0);
2845772Sas200622 
2858334SJose.Borrego@Sun.COM 		nds->pdu_max_size = pdu_max_size;
2868334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = pdu_addr;
2878334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = (unsigned long)pdu_addr;
2885772Sas200622 	}
2895772Sas200622 
2908334SJose.Borrego@Sun.COM 	nds->pdu_size = want_end_offset;
2915772Sas200622 	return (1);
2925772Sas200622 }
2935772Sas200622 
2945772Sas200622 static int
2958334SJose.Borrego@Sun.COM ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
2968334SJose.Borrego@Sun.COM     unsigned long n_bytes, ndr_ref_t *ref)
2975772Sas200622 {
2985772Sas200622 	unsigned char *data;
2995772Sas200622 
3008334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3015772Sas200622 	data += pdu_offset;
3025772Sas200622 
3038334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
3045772Sas200622 
3055772Sas200622 	bzero(data, n_bytes);
3065772Sas200622 	return (1);
3075772Sas200622 }
3085772Sas200622 
3095772Sas200622 /*
3108334SJose.Borrego@Sun.COM  * ndo_get_pdu
3115772Sas200622  *
3125772Sas200622  * The swap flag is 1 if NDR knows that the byte-order in the PDU
3135772Sas200622  * is different from the local system.
3145772Sas200622  *
3155772Sas200622  * Returns 1 on success or 0 to indicate failure.
3165772Sas200622  */
3175772Sas200622 static int
3188334SJose.Borrego@Sun.COM ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3198334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3205772Sas200622 {
3215772Sas200622 	unsigned char *data;
3225772Sas200622 	char hexbuf[NDOBUFSZ];
3235772Sas200622 
3248334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3255772Sas200622 	data += pdu_offset;
3265772Sas200622 
3278334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
3285772Sas200622 
3298334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "get %d@%-3d = %s",
3305772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3315772Sas200622 
3325772Sas200622 	if (!swap_bytes)
3335772Sas200622 		bcopy(data, buf, n_bytes);
3345772Sas200622 	else
3358334SJose.Borrego@Sun.COM 		nds_bswap(data, (unsigned char *)buf, n_bytes);
3365772Sas200622 
3375772Sas200622 	return (1);
3385772Sas200622 }
3395772Sas200622 
3405772Sas200622 /*
3418334SJose.Borrego@Sun.COM  * ndo_put_pdu
3425772Sas200622  *
3435772Sas200622  * This is a receiver makes right protocol. So we do not need
3445772Sas200622  * to be concerned about the byte-order of an outgoing PDU.
3455772Sas200622  */
3465772Sas200622 /*ARGSUSED*/
3475772Sas200622 static int
3488334SJose.Borrego@Sun.COM ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
3498334SJose.Borrego@Sun.COM     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
3505772Sas200622 {
3515772Sas200622 	unsigned char *data;
3525772Sas200622 	char hexbuf[NDOBUFSZ];
3535772Sas200622 
3548334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3555772Sas200622 	data += pdu_offset;
3565772Sas200622 
3578334SJose.Borrego@Sun.COM 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
3585772Sas200622 
3598334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "put %d@%-3d = %s",
3605772Sas200622 	    n_bytes, pdu_offset, hexbuf);
3615772Sas200622 
3625772Sas200622 	bcopy(buf, data, n_bytes);
3635772Sas200622 	return (1);
3645772Sas200622 }
3655772Sas200622 
3665772Sas200622 static void
3678334SJose.Borrego@Sun.COM ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
3685772Sas200622 {
3698334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, what);
3705772Sas200622 }
3715772Sas200622 
3725772Sas200622 static void
3738334SJose.Borrego@Sun.COM ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
3745772Sas200622 {
3755772Sas200622 	unsigned char *data;
3765772Sas200622 	char hexbuf[NDOBUFSZ];
3775772Sas200622 
3788334SJose.Borrego@Sun.COM 	data = (unsigned char *)nds->pdu_base_offset;
3795772Sas200622 	if (ref)
3805772Sas200622 		data += ref->pdu_offset;
3815772Sas200622 	else
3828334SJose.Borrego@Sun.COM 		data += nds->pdu_scan_offset;
3835772Sas200622 
3848334SJose.Borrego@Sun.COM 	ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
3855772Sas200622 
3868334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
3878334SJose.Borrego@Sun.COM 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
3888334SJose.Borrego@Sun.COM 	    nds->pdu_size, nds->pdu_max_size);
3898334SJose.Borrego@Sun.COM 	ndo_printf(nds, ref, "      %s", hexbuf);
3905772Sas200622 }
3915772Sas200622 
3925772Sas200622 /*
3938334SJose.Borrego@Sun.COM  * ndo_reset
3945772Sas200622  *
3955772Sas200622  * Reset a stream: zap the outer_queue. We don't need to tamper
3965772Sas200622  * with the stream heap: it's handled externally to the stream.
3975772Sas200622  */
3985772Sas200622 static int
3998334SJose.Borrego@Sun.COM ndo_reset(ndr_stream_t *nds)
4005772Sas200622 {
4018334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "reset");
4025772Sas200622 
4038334SJose.Borrego@Sun.COM 	nds->pdu_size = 0;
4048334SJose.Borrego@Sun.COM 	nds->pdu_scan_offset = 0;
4058334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
4068334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
4078334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
4085772Sas200622 
4095772Sas200622 	return (1);
4105772Sas200622 }
4115772Sas200622 
4125772Sas200622 /*
4138334SJose.Borrego@Sun.COM  * ndo_destruct
4145772Sas200622  *
4156482Samw  * Destruct a stream: zap the outer_queue.
4166482Samw  * Note: heap management (creation/destruction) is external to the stream.
4175772Sas200622  */
4185772Sas200622 static void
4198334SJose.Borrego@Sun.COM ndo_destruct(ndr_stream_t *nds)
4205772Sas200622 {
4216482Samw 	ndr_frag_t *frag;
4226482Samw 
4238334SJose.Borrego@Sun.COM 	ndo_printf(nds, 0, "destruct");
4248334SJose.Borrego@Sun.COM 
4258334SJose.Borrego@Sun.COM 	if (nds == NULL)
4268334SJose.Borrego@Sun.COM 		return;
4275772Sas200622 
4288334SJose.Borrego@Sun.COM 	if (nds->pdu_base_addr != NULL) {
4298334SJose.Borrego@Sun.COM 		free(nds->pdu_base_addr);
4308334SJose.Borrego@Sun.COM 		nds->pdu_base_addr = NULL;
4318334SJose.Borrego@Sun.COM 		nds->pdu_base_offset = 0;
4325772Sas200622 	}
4335772Sas200622 
4348334SJose.Borrego@Sun.COM 	while ((frag = nds->frags.head) != NULL) {
4358334SJose.Borrego@Sun.COM 		nds->frags.head = frag->next;
4366482Samw 		free(frag);
4376482Samw 	}
4386482Samw 
4398334SJose.Borrego@Sun.COM 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
4407052Samw 
4418334SJose.Borrego@Sun.COM 	nds->outer_queue_head = 0;
4428334SJose.Borrego@Sun.COM 	nds->outer_current = 0;
4438334SJose.Borrego@Sun.COM 	nds->outer_queue_tailp = &nds->outer_queue_head;
4445772Sas200622 }
4455772Sas200622 
4465772Sas200622 /*
4475772Sas200622  * Printf style formatting for NDR operations.
4485772Sas200622  */
4495772Sas200622 void
4508334SJose.Borrego@Sun.COM ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
4515772Sas200622 {
4525772Sas200622 	va_list ap;
4535772Sas200622 	char buf[NDOBUFSZ];
4545772Sas200622 
4555772Sas200622 	va_start(ap, fmt);
4565772Sas200622 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
4575772Sas200622 	va_end(ap);
4585772Sas200622 
4598334SJose.Borrego@Sun.COM 	if (nds)
4608334SJose.Borrego@Sun.COM 		ndo_fmt(nds, ref, buf);
4615772Sas200622 	else
4628334SJose.Borrego@Sun.COM 		ndo_trace(buf);
4635772Sas200622 }
4645772Sas200622 
4655772Sas200622 /*
4665772Sas200622  * Main output formatter for NDR operations.
4675772Sas200622  *
4685772Sas200622  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
4695772Sas200622  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
4705772Sas200622  *
4715772Sas200622  *	U       Marshalling flag (M=marshal, U=unmarshal)
4725772Sas200622  *	I       Direction flag (I=in, O=out)
4735772Sas200622  *	...     Field name
4745772Sas200622  *	get     PDU operation (get or put)
4755772Sas200622  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
4765772Sas200622  *	{05}    Value
4775772Sas200622  */
4785772Sas200622 void
4798334SJose.Borrego@Sun.COM ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
4805772Sas200622 {
4818334SJose.Borrego@Sun.COM 	ndr_ref_t	*p;
4828334SJose.Borrego@Sun.COM 	int		indent;
4838334SJose.Borrego@Sun.COM 	char		ref_name[NDOBUFSZ];
4848334SJose.Borrego@Sun.COM 	char		buf[NDOBUFSZ];
4858334SJose.Borrego@Sun.COM 	int		m_op_c = '?', dir_c = '?';
4865772Sas200622 
4878334SJose.Borrego@Sun.COM 	switch (nds->m_op) {
4885772Sas200622 	case 0:				m_op_c = '-';	break;
4895772Sas200622 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
4905772Sas200622 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
4915772Sas200622 	default:			m_op_c = '?';	break;
4925772Sas200622 	}
4935772Sas200622 
4948334SJose.Borrego@Sun.COM 	switch (nds->dir) {
4955772Sas200622 	case 0:				dir_c = '-';	break;
4965772Sas200622 	case NDR_DIR_IN:		dir_c = 'I';	break;
4975772Sas200622 	case NDR_DIR_OUT:		dir_c = 'O';	break;
4985772Sas200622 	default:			dir_c = '?';	break;
4995772Sas200622 	}
5005772Sas200622 
5015772Sas200622 	for (indent = 0, p = ref; p; p = p->enclosing)
5025772Sas200622 		indent++;
5035772Sas200622 
5045772Sas200622 	if (ref && ref->name) {
5055772Sas200622 		if (*ref->name == '[' && ref->enclosing) {
5065772Sas200622 			indent--;
5075772Sas200622 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
5085772Sas200622 			    ref->enclosing->name, ref->name);
5095772Sas200622 		} else {
5105772Sas200622 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
5115772Sas200622 		}
5125772Sas200622 	} else {
5135772Sas200622 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
5145772Sas200622 	}
5155772Sas200622 
5169914Samw@Sun.COM 	(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s  %s",
5179914Samw@Sun.COM 	    m_op_c, dir_c, indent,
5185772Sas200622 	    "....+....+....+....+....+....",
5195772Sas200622 	    20 - indent, ref_name, note);
5205772Sas200622 
5218334SJose.Borrego@Sun.COM 	ndo_trace(buf);
5225772Sas200622 }
5235772Sas200622 
5245772Sas200622 /*ARGSUSED*/
5255772Sas200622 void
5268334SJose.Borrego@Sun.COM ndo_trace(const char *s)
5275772Sas200622 {
5285772Sas200622 	/*
5295772Sas200622 	 * Temporary fbt for dtrace until user space sdt enabled.
5305772Sas200622 	 */
5315772Sas200622 }
5325772Sas200622 
5335772Sas200622 /*
5345772Sas200622  * Format data as hex bytes (limit is 10 bytes):
5355772Sas200622  *
5365772Sas200622  *	1188689424 {10 f6 d9 46}
5375772Sas200622  *
5385772Sas200622  * If the input data is greater than 10 bytes, an ellipsis will
5395772Sas200622  * be inserted before the closing brace.
5405772Sas200622  */
5415772Sas200622 static void
5428334SJose.Borrego@Sun.COM ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
5435772Sas200622 {
5445772Sas200622 	char *p = buf;
5455772Sas200622 	int interp = 1;
5465772Sas200622 	uint32_t c;
5475772Sas200622 	int n;
5485772Sas200622 	int i;
5495772Sas200622 
5505772Sas200622 	n = (size > 10) ? 10 : size;
5515772Sas200622 	if (n > len-1)
5525772Sas200622 		n = len-1;
5535772Sas200622 
5545772Sas200622 	switch (size) {
5555772Sas200622 	case 1:
5565772Sas200622 		c = *(uint8_t *)data;
5575772Sas200622 		break;
5585772Sas200622 	case 2:
5595772Sas200622 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
5605772Sas200622 			c = *(uint16_t *)data;
5615772Sas200622 		else
5625772Sas200622 			c = (data[0] << 8) | data[1];
5635772Sas200622 		break;
5645772Sas200622 	case 4:
5655772Sas200622 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
5665772Sas200622 			c = *(uint32_t *)data;
5675772Sas200622 		} else {
5685772Sas200622 			c = (data[0] << 24) | (data[1] << 16)
5695772Sas200622 			    | (data[2] << 8) | data[3];
5705772Sas200622 		}
5715772Sas200622 		break;
5725772Sas200622 	default:
5735772Sas200622 		c = 0;
5745772Sas200622 		interp = 0;
5755772Sas200622 		break;
5765772Sas200622 	}
5775772Sas200622 
5785772Sas200622 	if (interp)
5795772Sas200622 		p += sprintf(p, "%4u {", c);
5805772Sas200622 	else
5815772Sas200622 		p += sprintf(p, " {");
5825772Sas200622 
5835772Sas200622 	p += sprintf(p, "%02x", data[0]);
5845772Sas200622 	for (i = 1; i < n; i++)
5855772Sas200622 		p += sprintf(p, " %02x", data[i]);
5865772Sas200622 	if (size > 10)
5875772Sas200622 		p += sprintf(p, " ...}");
5885772Sas200622 	else
5895772Sas200622 		p += sprintf(p, "}");
5905772Sas200622 
5915772Sas200622 	/*
5925772Sas200622 	 * Show c if it's a printable character or wide-char.
5935772Sas200622 	 */
5945772Sas200622 	if (size < 4 && isprint((uint8_t)c))
5955772Sas200622 		(void) sprintf(p, " %c", (uint8_t)c);
5965772Sas200622 }
597