#include <sys/types.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <assert.h>
#include <libmlrpc.h>
#define NDOBUFSZ 128
#define NDR_PDU_BLOCK_SIZE (4*1024)
#define NDR_PDU_BLOCK_MASK (NDR_PDU_BLOCK_SIZE - 1)
#define NDR_PDU_ALIGN(N) \
(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
#define NDR_PDU_MAX_SIZE (64*1024*1024)
static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
ndr_ref_t *);
static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
char *, int, ndr_ref_t *);
static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
char *, int, ndr_ref_t *);
static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
static int ndo_reset(ndr_stream_t *);
static void ndo_destruct(ndr_stream_t *);
static void ndo_hexfmt(uint8_t *, int, int, char *, int);
static ndr_stream_ops_t nds_ops = {
ndo_malloc,
ndo_free,
ndo_grow_pdu,
ndo_pad_pdu,
ndo_get_pdu,
ndo_put_pdu,
ndo_tattle,
ndo_tattle_error,
ndo_reset,
ndo_destruct
};
void
nds_bswap(void *srcbuf, void *dstbuf, size_t len)
{
uint8_t *src = (uint8_t *)srcbuf;
uint8_t *dst = (uint8_t *)dstbuf;
if ((len != 0) && ((len & (len - 1)) == 0)) {
src += len;
while (len--)
*dst++ = *(--src);
}
}
int
nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
int composite_op, ndr_heap_t *heap)
{
unsigned size;
assert(nds);
assert(heap);
bzero(nds, sizeof (*nds));
nds->ndo = &nds_ops;
nds->heap = (struct ndr_heap *)heap;
if (pdu_size_hint > NDR_PDU_MAX_SIZE) {
nds->error = NDR_ERR_BOUNDS_CHECK;
nds->error_ref = __LINE__;
NDS_TATTLE_ERROR(nds, NULL, NULL);
return (NDR_DRC_FAULT_RESOURCE_1);
}
size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
if ((nds->pdu_base_addr = malloc(size)) == NULL) {
nds->error = NDR_ERR_MALLOC_FAILED;
nds->error_ref = __LINE__;
NDS_TATTLE_ERROR(nds, NULL, NULL);
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
}
nds->pdu_max_size = size;
nds->pdu_size = 0;
nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
nds->m_op = NDR_MODE_TO_M_OP(composite_op);
nds->dir = NDR_MODE_TO_DIR(composite_op);
nds->outer_queue_tailp = &nds->outer_queue_head;
return (0);
}
void
nds_destruct(ndr_stream_t *nds)
{
if ((nds == NULL) || (nds->ndo == NULL))
return;
NDS_DESTRUCT(nds);
}
void
nds_show_state(ndr_stream_t *nds)
{
if (nds == NULL) {
ndo_printf(NULL, NULL, "nds: <null");
return;
}
ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d, "
"hdr_size=%d, body_size=%d, body_offset=%d",
nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
nds->pdu_scan_offset, nds->pdu_hdr_size, nds->pdu_body_size,
nds->pdu_body_offset);
}
static char *
ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
{
return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
}
static int
ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
{
return (1);
}
static int
ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
{
unsigned char *pdu_addr;
unsigned pdu_max_size;
ndo_printf(nds, ref, "grow %d", want_end_offset);
pdu_max_size = nds->pdu_max_size;
if (want_end_offset > pdu_max_size) {
pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
if (pdu_max_size >= NDR_PDU_MAX_SIZE)
return (0);
pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
if (pdu_addr == 0)
return (0);
nds->pdu_max_size = pdu_max_size;
nds->pdu_base_addr = pdu_addr;
nds->pdu_base_offset = (unsigned long)pdu_addr;
}
nds->pdu_size = want_end_offset;
return (1);
}
static int
ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, ndr_ref_t *ref)
{
unsigned char *data;
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
bzero(data, n_bytes);
return (1);
}
static int
ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
ndo_printf(nds, ref, "get %d@%-3d = %s",
n_bytes, pdu_offset, hexbuf);
if (!swap_bytes)
bcopy(data, buf, n_bytes);
else
nds_bswap(data, (unsigned char *)buf, n_bytes);
return (1);
}
static int
ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
ndo_printf(nds, ref, "put %d@%-3d = %s",
n_bytes, pdu_offset, hexbuf);
bcopy(buf, data, n_bytes);
return (1);
}
static void
ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
{
ndo_printf(nds, ref, what);
}
static void
ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
if (nds->pdu_base_addr != NULL) {
data = (unsigned char *)nds->pdu_base_offset;
if (ref)
data += ref->pdu_offset;
else
data += nds->pdu_scan_offset;
ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
} else {
bzero(hexbuf, NDOBUFSZ);
}
ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
nds->error, nds->error_ref, nds->pdu_scan_offset,
nds->pdu_size, nds->pdu_max_size);
ndo_printf(nds, ref, " %s", hexbuf);
}
static int
ndo_reset(ndr_stream_t *nds)
{
ndo_printf(nds, 0, "reset");
nds->pdu_size = 0;
nds->pdu_scan_offset = 0;
nds->outer_queue_head = 0;
nds->outer_current = 0;
nds->outer_queue_tailp = &nds->outer_queue_head;
return (1);
}
static void
ndo_destruct(ndr_stream_t *nds)
{
ndo_printf(nds, 0, "destruct");
if (nds == NULL)
return;
if (nds->pdu_base_addr != NULL) {
free(nds->pdu_base_addr);
nds->pdu_base_addr = NULL;
nds->pdu_base_offset = 0;
}
nds->outer_queue_head = 0;
nds->outer_current = 0;
nds->outer_queue_tailp = &nds->outer_queue_head;
}
void
ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
{
va_list ap;
char buf[NDOBUFSZ];
va_start(ap, fmt);
(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
va_end(ap);
if (nds)
ndo_fmt(nds, ref, buf);
else
ndo_trace(buf);
}
void
ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
{
ndr_ref_t *p;
int indent;
char ref_name[NDOBUFSZ];
char buf[NDOBUFSZ];
int m_op_c = '?', dir_c = '?';
switch (nds->m_op) {
case 0: m_op_c = '-'; break;
case NDR_M_OP_MARSHALL: m_op_c = 'M'; break;
case NDR_M_OP_UNMARSHALL: m_op_c = 'U'; break;
default: m_op_c = '?'; break;
}
switch (nds->dir) {
case 0: dir_c = '-'; break;
case NDR_DIR_IN: dir_c = 'I'; break;
case NDR_DIR_OUT: dir_c = 'O'; break;
default: dir_c = '?'; break;
}
for (indent = 0, p = ref; p; p = p->enclosing)
indent++;
if (ref && ref->name) {
if (*ref->name == '[' && ref->enclosing) {
indent--;
(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
ref->enclosing->name, ref->name);
} else {
(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
}
} else {
(void) strlcpy(ref_name, "----", NDOBUFSZ);
}
(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s %s",
m_op_c, dir_c, indent,
"....+....+....+....+....+....",
20 - indent, ref_name, note);
ndo_trace(buf);
}
void
ndo_trace(const char *s)
{
}
static void
ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
{
char *p = buf;
int interp = 1;
uint32_t c;
int n;
int i;
n = (size > 10) ? 10 : size;
if (n > len-1)
n = len-1;
switch (size) {
case 1:
c = *(uint8_t *)data;
break;
case 2:
if (swap_bytes == 0)
c = *(uint16_t *)data;
else
c = (data[0] << 8) | data[1];
break;
case 4:
if (swap_bytes == 0) {
c = *(uint32_t *)data;
} else {
c = (data[0] << 24) | (data[1] << 16)
| (data[2] << 8) | data[3];
}
break;
default:
c = 0;
interp = 0;
break;
}
if (interp)
p += sprintf(p, "%4u {", c);
else
p += sprintf(p, " {");
p += sprintf(p, "%02x", data[0]);
for (i = 1; i < n; i++)
p += sprintf(p, " %02x", data[i]);
if (size > 10)
p += sprintf(p, " ...}");
else
p += sprintf(p, "}");
if (size < 4 && isprint((uint8_t)c))
(void) sprintf(p, " %c", (uint8_t)c);
}