Home | History | Annotate | Line # | Download | only in rpc
xdr_rec.c revision 1.33
      1  1.33      matt /*	$NetBSD: xdr_rec.c,v 1.33 2012/03/20 17:14:50 matt Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5   1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6   1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7   1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8   1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9   1.1       cgd  * program developed by the user.
     10   1.1       cgd  *
     11   1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12   1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13   1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14   1.1       cgd  *
     15   1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16   1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17   1.1       cgd  * modification or enhancement.
     18   1.1       cgd  *
     19   1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20   1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21   1.1       cgd  * OR ANY PART THEREOF.
     22   1.1       cgd  *
     23   1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24   1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25   1.1       cgd  * Sun has been advised of the possibility of such damages.
     26   1.1       cgd  *
     27   1.1       cgd  * Sun Microsystems, Inc.
     28   1.1       cgd  * 2550 Garcia Avenue
     29   1.1       cgd  * Mountain View, California  94043
     30   1.1       cgd  */
     31   1.7  christos 
     32   1.7  christos #include <sys/cdefs.h>
     33   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34   1.7  christos #if 0
     35   1.7  christos static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     36   1.7  christos static char *sccsid = "@(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";
     37   1.7  christos #else
     38  1.33      matt __RCSID("$NetBSD: xdr_rec.c,v 1.33 2012/03/20 17:14:50 matt Exp $");
     39   1.7  christos #endif
     40   1.1       cgd #endif
     41   1.1       cgd 
     42   1.1       cgd /*
     43   1.1       cgd  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
     44   1.1       cgd  * layer above tcp (for rpc's use).
     45   1.1       cgd  *
     46   1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     47   1.1       cgd  *
     48   1.1       cgd  * These routines interface XDRSTREAMS to a tcp/ip connection.
     49   1.1       cgd  * There is a record marking layer between the xdr stream
     50   1.1       cgd  * and the tcp transport level.  A record is composed on one or more
     51   1.1       cgd  * record fragments.  A record fragment is a thirty-two bit header followed
     52   1.1       cgd  * by n bytes of data, where n is contained in the header.  The header
     53  1.11     lukem  * is represented as a htonl(u_long).  Thegh order bit encodes
     54   1.1       cgd  * whether or not the fragment is the last fragment of the record
     55   1.1       cgd  * (1 => fragment is last, 0 => more fragments to follow.
     56   1.1       cgd  * The other 31 bits encode the byte length of the fragment.
     57   1.1       cgd  */
     58   1.1       cgd 
     59   1.8       jtc #include "namespace.h"
     60  1.12     lukem 
     61  1.12     lukem #include <sys/types.h>
     62  1.12     lukem 
     63  1.12     lukem #include <netinet/in.h>
     64  1.12     lukem 
     65  1.32  christos #include <assert.h>
     66  1.12     lukem #include <err.h>
     67  1.19      fvdl #include <stddef.h>
     68   1.1       cgd #include <stdio.h>
     69   1.1       cgd #include <stdlib.h>
     70   1.6       cgd #include <string.h>
     71  1.12     lukem 
     72   1.1       cgd #include <rpc/types.h>
     73   1.1       cgd #include <rpc/xdr.h>
     74  1.19      fvdl #include <rpc/auth.h>
     75  1.19      fvdl #include <rpc/svc.h>
     76  1.19      fvdl #include <rpc/clnt.h>
     77  1.19      fvdl 
     78  1.19      fvdl #include "rpc_internal.h"
     79   1.8       jtc 
     80   1.8       jtc #ifdef __weak_alias
     81  1.17   mycroft __weak_alias(xdrrec_create,_xdrrec_create)
     82  1.17   mycroft __weak_alias(xdrrec_endofrecord,_xdrrec_endofrecord)
     83  1.17   mycroft __weak_alias(xdrrec_eof,_xdrrec_eof)
     84  1.17   mycroft __weak_alias(xdrrec_skiprecord,_xdrrec_skiprecord)
     85   1.8       jtc #endif
     86   1.1       cgd 
     87  1.33      matt static bool_t	xdrrec_getlong(XDR *, long *);
     88  1.33      matt static bool_t	xdrrec_putlong(XDR *, const long *);
     89  1.33      matt static bool_t	xdrrec_getbytes(XDR *, char *, u_int);
     90  1.33      matt 
     91  1.33      matt static bool_t	xdrrec_putbytes(XDR *, const char *, u_int);
     92  1.33      matt static u_int	xdrrec_getpos(XDR *);
     93  1.33      matt static bool_t	xdrrec_setpos(XDR *, u_int);
     94  1.33      matt static int32_t *xdrrec_inline(XDR *, u_int);
     95  1.33      matt static void	xdrrec_destroy(XDR *);
     96   1.1       cgd 
     97  1.13   mycroft static const struct  xdr_ops xdrrec_ops = {
     98   1.1       cgd 	xdrrec_getlong,
     99   1.1       cgd 	xdrrec_putlong,
    100   1.1       cgd 	xdrrec_getbytes,
    101   1.1       cgd 	xdrrec_putbytes,
    102   1.1       cgd 	xdrrec_getpos,
    103   1.1       cgd 	xdrrec_setpos,
    104   1.1       cgd 	xdrrec_inline,
    105  1.27  christos 	xdrrec_destroy,
    106  1.27  christos 	NULL, /* xdrrec_control */
    107   1.1       cgd };
    108   1.1       cgd 
    109   1.1       cgd /*
    110   1.1       cgd  * A record is composed of one or more record fragments.
    111  1.19      fvdl  * A record fragment is a four-byte header followed by zero to
    112   1.1       cgd  * 2**32-1 bytes.  The header is treated as a long unsigned and is
    113   1.1       cgd  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
    114   1.1       cgd  * are a byte count of the fragment.  The highest order bit is a boolean:
    115   1.1       cgd  * 1 => this fragment is the last fragment of the record,
    116   1.1       cgd  * 0 => this fragment is followed by more fragment(s).
    117   1.1       cgd  *
    118   1.1       cgd  * The fragment/record machinery is not general;  it is constructed to
    119   1.1       cgd  * meet the needs of xdr and rpc based on tcp.
    120   1.1       cgd  */
    121   1.1       cgd 
    122  1.33      matt #define LAST_FRAG ((uint32_t)(1 << 31))
    123   1.1       cgd 
    124   1.1       cgd typedef struct rec_strm {
    125  1.14   mycroft 	char *tcp_handle;
    126   1.1       cgd 	/*
    127   1.1       cgd 	 * out-goung bits
    128   1.1       cgd 	 */
    129  1.33      matt 	int (*writeit)(char *, char *, int);
    130  1.14   mycroft 	char *out_base;	/* output buffer (points to frag header) */
    131  1.14   mycroft 	char *out_finger;	/* next output position */
    132  1.14   mycroft 	char *out_boundry;	/* data cannot up to this address */
    133  1.33      matt 	uint32_t *frag_header;	/* beginning of curren fragment */
    134   1.1       cgd 	bool_t frag_sent;	/* true if buffer sent in middle of record */
    135   1.1       cgd 	/*
    136   1.1       cgd 	 * in-coming bits
    137   1.1       cgd 	 */
    138  1.33      matt 	int (*readit)(char *, char *, int);
    139  1.11     lukem 	u_long in_size;	/* fixed size of the input buffer */
    140  1.14   mycroft 	char *in_base;
    141  1.14   mycroft 	char *in_finger;	/* location of next byte to be had */
    142  1.14   mycroft 	char *in_boundry;	/* can read up to this location */
    143  1.11     lukem 	long fbtbc;		/* fragment bytes to be consumed */
    144   1.1       cgd 	bool_t last_frag;
    145  1.11     lukem 	u_int sendsize;
    146  1.11     lukem 	u_int recvsize;
    147  1.19      fvdl 
    148  1.19      fvdl 	bool_t nonblock;
    149  1.19      fvdl 	bool_t in_haveheader;
    150  1.33      matt 	uint32_t in_header;
    151  1.19      fvdl 	char *in_hdrp;
    152  1.19      fvdl 	int in_hdrlen;
    153  1.19      fvdl 	int in_reclen;
    154  1.19      fvdl 	int in_received;
    155  1.19      fvdl 	int in_maxrec;
    156   1.1       cgd } RECSTREAM;
    157   1.1       cgd 
    158  1.33      matt static u_int	fix_buf_size(u_int);
    159  1.33      matt static bool_t	flush_out(RECSTREAM *, bool_t);
    160  1.33      matt static bool_t	fill_input_buf(RECSTREAM *);
    161  1.33      matt static bool_t	get_input_bytes(RECSTREAM *, char *, u_int);
    162  1.33      matt static bool_t	set_input_fragment(RECSTREAM *);
    163  1.33      matt static bool_t	skip_input_bytes(RECSTREAM *, long);
    164  1.33      matt static bool_t	realloc_stream(RECSTREAM *, int);
    165   1.6       cgd 
    166   1.1       cgd 
    167   1.1       cgd /*
    168   1.1       cgd  * Create an xdr handle for xdrrec
    169   1.1       cgd  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
    170   1.1       cgd  * send and recv buffer sizes (0 => use default).
    171   1.1       cgd  * tcp_handle is an opaque handle that is passed as the first parameter to
    172   1.1       cgd  * the procedures readit and writeit.  Readit and writeit are read and
    173   1.1       cgd  * write respectively.   They are like the system
    174   1.1       cgd  * calls expect that they take an opaque handle rather than an fd.
    175   1.1       cgd  */
    176   1.1       cgd void
    177  1.33      matt xdrrec_create(
    178  1.33      matt 	XDR *xdrs,
    179  1.33      matt 	u_int sendsize,
    180  1.33      matt 	u_int recvsize,
    181  1.33      matt 	char *tcp_handle,
    182  1.11     lukem 	/* like read, but pass it a tcp_handle, not sock */
    183  1.33      matt 	int (*readit)(char *, char *, int),
    184  1.11     lukem 	/* like write, but pass it a tcp_handle, not sock */
    185  1.33      matt 	int (*writeit)(char *, char *, int))
    186   1.1       cgd {
    187  1.18  christos 	RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
    188   1.1       cgd 
    189   1.1       cgd 	if (rstrm == NULL) {
    190  1.12     lukem 		warnx("xdrrec_create: out of memory");
    191   1.1       cgd 		/*
    192   1.1       cgd 		 *  This is bad.  Should rework xdrrec_create to
    193   1.1       cgd 		 *  return a handle, and in this case return NULL
    194   1.1       cgd 		 */
    195   1.1       cgd 		return;
    196   1.1       cgd 	}
    197  1.19      fvdl 
    198   1.1       cgd 	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
    199  1.25      yamt 	rstrm->out_base = malloc(rstrm->sendsize);
    200  1.19      fvdl 	if (rstrm->out_base == NULL) {
    201  1.19      fvdl 		warnx("xdrrec_create: out of memory");
    202  1.19      fvdl 		mem_free(rstrm, sizeof(RECSTREAM));
    203  1.19      fvdl 		return;
    204  1.19      fvdl 	}
    205  1.19      fvdl 
    206   1.1       cgd 	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
    207  1.25      yamt 	rstrm->in_base = malloc(recvsize);
    208  1.19      fvdl 	if (rstrm->in_base == NULL) {
    209  1.12     lukem 		warnx("xdrrec_create: out of memory");
    210  1.19      fvdl 		mem_free(rstrm->out_base, sendsize);
    211  1.19      fvdl 		mem_free(rstrm, sizeof(RECSTREAM));
    212   1.1       cgd 		return;
    213   1.1       cgd 	}
    214   1.1       cgd 	/*
    215   1.1       cgd 	 * now the rest ...
    216   1.1       cgd 	 */
    217   1.1       cgd 	xdrs->x_ops = &xdrrec_ops;
    218  1.15  christos 	xdrs->x_private = rstrm;
    219   1.1       cgd 	rstrm->tcp_handle = tcp_handle;
    220   1.1       cgd 	rstrm->readit = readit;
    221   1.1       cgd 	rstrm->writeit = writeit;
    222   1.1       cgd 	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
    223  1.33      matt 	rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
    224  1.33      matt 	rstrm->out_finger += sizeof(uint32_t);
    225   1.1       cgd 	rstrm->out_boundry += sendsize;
    226   1.1       cgd 	rstrm->frag_sent = FALSE;
    227   1.1       cgd 	rstrm->in_size = recvsize;
    228   1.1       cgd 	rstrm->in_boundry = rstrm->in_base;
    229   1.1       cgd 	rstrm->in_finger = (rstrm->in_boundry += recvsize);
    230   1.1       cgd 	rstrm->fbtbc = 0;
    231   1.1       cgd 	rstrm->last_frag = TRUE;
    232  1.19      fvdl 	rstrm->in_haveheader = FALSE;
    233  1.19      fvdl 	rstrm->in_hdrlen = 0;
    234  1.19      fvdl 	rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
    235  1.19      fvdl 	rstrm->nonblock = FALSE;
    236  1.19      fvdl 	rstrm->in_reclen = 0;
    237  1.19      fvdl 	rstrm->in_received = 0;
    238   1.1       cgd }
    239   1.1       cgd 
    240   1.1       cgd 
    241   1.1       cgd /*
    242  1.11     lukem  * The reoutines defined below are the xdr ops which will go into the
    243   1.1       cgd  * xdr handle filled in by xdrrec_create.
    244   1.1       cgd  */
    245   1.1       cgd 
    246   1.1       cgd static bool_t
    247  1.33      matt xdrrec_getlong(XDR *xdrs, long *lp)
    248   1.1       cgd {
    249  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    250  1.15  christos 	int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
    251   1.4       cgd 	int32_t mylong;
    252   1.1       cgd 
    253   1.1       cgd 	/* first try the inline, fast case */
    254  1.30     lukem 	if ((rstrm->fbtbc >= (long)sizeof(int32_t)) &&
    255  1.30     lukem 		(((uintptr_t)rstrm->in_boundry - (uintptr_t)buflp) >= sizeof(int32_t))) {
    256  1.33      matt 		*lp = (long)ntohl((uint32_t)(*buflp));
    257   1.4       cgd 		rstrm->fbtbc -= sizeof(int32_t);
    258   1.4       cgd 		rstrm->in_finger += sizeof(int32_t);
    259   1.1       cgd 	} else {
    260  1.15  christos 		if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
    261  1.32  christos 		    (u_int)sizeof(int32_t)))
    262   1.1       cgd 			return (FALSE);
    263  1.33      matt 		*lp = (long)ntohl((uint32_t)mylong);
    264   1.1       cgd 	}
    265   1.1       cgd 	return (TRUE);
    266   1.1       cgd }
    267   1.1       cgd 
    268   1.1       cgd static bool_t
    269  1.33      matt xdrrec_putlong(XDR *xdrs, const long *lp)
    270   1.1       cgd {
    271  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    272  1.15  christos 	int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
    273   1.1       cgd 
    274   1.4       cgd 	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
    275   1.1       cgd 		/*
    276   1.1       cgd 		 * this case should almost never happen so the code is
    277   1.1       cgd 		 * inefficient
    278   1.1       cgd 		 */
    279   1.4       cgd 		rstrm->out_finger -= sizeof(int32_t);
    280   1.1       cgd 		rstrm->frag_sent = TRUE;
    281   1.1       cgd 		if (! flush_out(rstrm, FALSE))
    282   1.1       cgd 			return (FALSE);
    283  1.15  christos 		dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
    284   1.4       cgd 		rstrm->out_finger += sizeof(int32_t);
    285   1.1       cgd 	}
    286  1.33      matt 	*dest_lp = (int32_t)htonl((uint32_t)(*lp));
    287   1.1       cgd 	return (TRUE);
    288   1.1       cgd }
    289   1.1       cgd 
    290   1.1       cgd static bool_t  /* must manage buffers, fragments, and records */
    291  1.33      matt xdrrec_getbytes(XDR *xdrs, char *addr, u_int len)
    292   1.1       cgd {
    293  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    294  1.30     lukem 	u_int current;
    295   1.1       cgd 
    296   1.1       cgd 	while (len > 0) {
    297  1.30     lukem 		current = (u_int)rstrm->fbtbc;
    298   1.1       cgd 		if (current == 0) {
    299   1.1       cgd 			if (rstrm->last_frag)
    300   1.1       cgd 				return (FALSE);
    301   1.1       cgd 			if (! set_input_fragment(rstrm))
    302   1.1       cgd 				return (FALSE);
    303   1.1       cgd 			continue;
    304   1.1       cgd 		}
    305   1.1       cgd 		current = (len < current) ? len : current;
    306   1.1       cgd 		if (! get_input_bytes(rstrm, addr, current))
    307   1.1       cgd 			return (FALSE);
    308   1.1       cgd 		addr += current;
    309   1.1       cgd 		rstrm->fbtbc -= current;
    310   1.1       cgd 		len -= current;
    311   1.1       cgd 	}
    312   1.1       cgd 	return (TRUE);
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.1       cgd static bool_t
    316  1.33      matt xdrrec_putbytes(XDR *xdrs, const char *addr, u_int len)
    317   1.1       cgd {
    318  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    319  1.15  christos 	size_t current;
    320   1.1       cgd 
    321   1.1       cgd 	while (len > 0) {
    322  1.15  christos 		current = (size_t)((u_long)rstrm->out_boundry -
    323  1.15  christos 		    (u_long)rstrm->out_finger);
    324   1.1       cgd 		current = (len < current) ? len : current;
    325  1.12     lukem 		memmove(rstrm->out_finger, addr, current);
    326   1.1       cgd 		rstrm->out_finger += current;
    327   1.1       cgd 		addr += current;
    328  1.32  christos 		_DIAGASSERT(__type_fit(u_int, current));
    329  1.32  christos 		len -= (u_int)current;
    330   1.1       cgd 		if (rstrm->out_finger == rstrm->out_boundry) {
    331   1.1       cgd 			rstrm->frag_sent = TRUE;
    332   1.1       cgd 			if (! flush_out(rstrm, FALSE))
    333   1.1       cgd 				return (FALSE);
    334   1.1       cgd 		}
    335   1.1       cgd 	}
    336   1.1       cgd 	return (TRUE);
    337   1.1       cgd }
    338   1.1       cgd 
    339  1.11     lukem static u_int
    340  1.33      matt xdrrec_getpos(XDR *xdrs)
    341   1.1       cgd {
    342  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    343  1.15  christos 	off_t pos;
    344   1.1       cgd 
    345  1.15  christos 	pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
    346   1.1       cgd 	if (pos != -1)
    347   1.1       cgd 		switch (xdrs->x_op) {
    348   1.1       cgd 
    349   1.1       cgd 		case XDR_ENCODE:
    350   1.1       cgd 			pos += rstrm->out_finger - rstrm->out_base;
    351   1.1       cgd 			break;
    352   1.1       cgd 
    353   1.1       cgd 		case XDR_DECODE:
    354   1.1       cgd 			pos -= rstrm->in_boundry - rstrm->in_finger;
    355   1.1       cgd 			break;
    356   1.1       cgd 
    357   1.1       cgd 		default:
    358  1.15  christos 			pos = (off_t) -1;
    359   1.1       cgd 			break;
    360   1.1       cgd 		}
    361  1.11     lukem 	return ((u_int) pos);
    362   1.1       cgd }
    363   1.1       cgd 
    364   1.1       cgd static bool_t
    365  1.33      matt xdrrec_setpos(XDR *xdrs, u_int pos)
    366   1.1       cgd {
    367  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    368  1.11     lukem 	u_int currpos = xdrrec_getpos(xdrs);
    369   1.1       cgd 	int delta = currpos - pos;
    370  1.14   mycroft 	char *newpos;
    371   1.1       cgd 
    372   1.1       cgd 	if ((int)currpos != -1)
    373   1.1       cgd 		switch (xdrs->x_op) {
    374   1.1       cgd 
    375   1.1       cgd 		case XDR_ENCODE:
    376   1.1       cgd 			newpos = rstrm->out_finger - delta;
    377  1.15  christos 			if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
    378   1.1       cgd 				(newpos < rstrm->out_boundry)) {
    379   1.1       cgd 				rstrm->out_finger = newpos;
    380   1.1       cgd 				return (TRUE);
    381   1.1       cgd 			}
    382   1.1       cgd 			break;
    383   1.1       cgd 
    384   1.1       cgd 		case XDR_DECODE:
    385   1.1       cgd 			newpos = rstrm->in_finger - delta;
    386   1.1       cgd 			if ((delta < (int)(rstrm->fbtbc)) &&
    387   1.1       cgd 				(newpos <= rstrm->in_boundry) &&
    388   1.1       cgd 				(newpos >= rstrm->in_base)) {
    389   1.1       cgd 				rstrm->in_finger = newpos;
    390   1.1       cgd 				rstrm->fbtbc -= delta;
    391   1.1       cgd 				return (TRUE);
    392   1.1       cgd 			}
    393   1.1       cgd 			break;
    394   1.7  christos 
    395   1.7  christos 		case XDR_FREE:
    396   1.7  christos 			break;
    397   1.1       cgd 		}
    398   1.1       cgd 	return (FALSE);
    399   1.1       cgd }
    400   1.1       cgd 
    401   1.4       cgd static int32_t *
    402  1.33      matt xdrrec_inline(XDR *xdrs, u_int len)
    403   1.1       cgd {
    404  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    405   1.4       cgd 	int32_t *buf = NULL;
    406   1.1       cgd 
    407   1.1       cgd 	switch (xdrs->x_op) {
    408   1.1       cgd 
    409   1.1       cgd 	case XDR_ENCODE:
    410   1.1       cgd 		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
    411  1.15  christos 			buf = (int32_t *)(void *)rstrm->out_finger;
    412   1.1       cgd 			rstrm->out_finger += len;
    413   1.1       cgd 		}
    414   1.1       cgd 		break;
    415   1.1       cgd 
    416   1.1       cgd 	case XDR_DECODE:
    417  1.30     lukem 		if ((len <= (u_int)rstrm->fbtbc) &&
    418   1.1       cgd 			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
    419  1.15  christos 			buf = (int32_t *)(void *)rstrm->in_finger;
    420   1.1       cgd 			rstrm->fbtbc -= len;
    421   1.1       cgd 			rstrm->in_finger += len;
    422   1.1       cgd 		}
    423   1.7  christos 		break;
    424   1.7  christos 
    425   1.7  christos 	case XDR_FREE:
    426   1.1       cgd 		break;
    427   1.1       cgd 	}
    428   1.1       cgd 	return (buf);
    429   1.1       cgd }
    430   1.1       cgd 
    431   1.1       cgd static void
    432  1.33      matt xdrrec_destroy(XDR *xdrs)
    433   1.1       cgd {
    434  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    435   1.1       cgd 
    436  1.19      fvdl 	mem_free(rstrm->out_base, rstrm->sendsize);
    437  1.19      fvdl 	mem_free(rstrm->in_base, rstrm->recvsize);
    438  1.15  christos 	mem_free(rstrm, sizeof(RECSTREAM));
    439   1.1       cgd }
    440   1.1       cgd 
    441   1.1       cgd 
    442   1.1       cgd /*
    443   1.1       cgd  * Exported routines to manage xdr records
    444   1.1       cgd  */
    445   1.1       cgd 
    446   1.1       cgd /*
    447   1.1       cgd  * Before reading (deserializing from the stream, one should always call
    448   1.1       cgd  * this procedure to guarantee proper record alignment.
    449   1.1       cgd  */
    450   1.1       cgd bool_t
    451  1.33      matt xdrrec_skiprecord(XDR *xdrs)
    452   1.1       cgd {
    453  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    454  1.19      fvdl 	enum xprt_stat xstat;
    455   1.1       cgd 
    456  1.23      fvdl 	if (rstrm->nonblock) {
    457  1.23      fvdl 		if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
    458  1.23      fvdl 			rstrm->fbtbc = 0;
    459  1.23      fvdl 			return TRUE;
    460  1.23      fvdl 		}
    461  1.23      fvdl 		if (rstrm->in_finger == rstrm->in_boundry &&
    462  1.23      fvdl 		    xstat == XPRT_MOREREQS) {
    463  1.23      fvdl 			rstrm->fbtbc = 0;
    464  1.23      fvdl 			return TRUE;
    465  1.23      fvdl 		}
    466  1.23      fvdl 		return FALSE;
    467  1.23      fvdl 	}
    468   1.1       cgd 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    469   1.1       cgd 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
    470   1.1       cgd 			return (FALSE);
    471   1.1       cgd 		rstrm->fbtbc = 0;
    472   1.1       cgd 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
    473   1.1       cgd 			return (FALSE);
    474   1.1       cgd 	}
    475   1.1       cgd 	rstrm->last_frag = FALSE;
    476   1.1       cgd 	return (TRUE);
    477   1.1       cgd }
    478   1.1       cgd 
    479   1.1       cgd /*
    480   1.1       cgd  * Look ahead fuction.
    481  1.29       rtr  * Returns TRUE iff there is no more input in the buffer
    482   1.1       cgd  * after consuming the rest of the current record.
    483   1.1       cgd  */
    484   1.1       cgd bool_t
    485  1.33      matt xdrrec_eof(XDR *xdrs)
    486   1.1       cgd {
    487  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    488   1.1       cgd 
    489   1.1       cgd 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    490  1.19      fvdl 		if (!skip_input_bytes(rstrm, rstrm->fbtbc))
    491   1.1       cgd 			return (TRUE);
    492   1.1       cgd 		rstrm->fbtbc = 0;
    493  1.19      fvdl 		if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
    494   1.1       cgd 			return (TRUE);
    495   1.1       cgd 	}
    496   1.1       cgd 	if (rstrm->in_finger == rstrm->in_boundry)
    497   1.1       cgd 		return (TRUE);
    498   1.1       cgd 	return (FALSE);
    499   1.1       cgd }
    500   1.1       cgd 
    501   1.1       cgd /*
    502   1.1       cgd  * The client must tell the package when an end-of-record has occurred.
    503   1.1       cgd  * The second paraemters tells whether the record should be flushed to the
    504   1.1       cgd  * (output) tcp stream.  (This let's the package support batched or
    505   1.1       cgd  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
    506   1.1       cgd  */
    507   1.1       cgd bool_t
    508  1.33      matt xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
    509   1.1       cgd {
    510  1.12     lukem 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    511  1.12     lukem 	u_long len;  /* fragment length */
    512   1.1       cgd 
    513   1.1       cgd 	if (sendnow || rstrm->frag_sent ||
    514  1.33      matt 		((u_long)rstrm->out_finger + sizeof(uint32_t) >=
    515  1.11     lukem 		(u_long)rstrm->out_boundry)) {
    516   1.1       cgd 		rstrm->frag_sent = FALSE;
    517   1.1       cgd 		return (flush_out(rstrm, TRUE));
    518   1.1       cgd 	}
    519  1.11     lukem 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
    520  1.33      matt 	   sizeof(uint32_t);
    521  1.33      matt 	*(rstrm->frag_header) = htonl((uint32_t)len | LAST_FRAG);
    522  1.33      matt 	rstrm->frag_header = (uint32_t *)(void *)rstrm->out_finger;
    523  1.33      matt 	rstrm->out_finger += sizeof(uint32_t);
    524   1.1       cgd 	return (TRUE);
    525   1.1       cgd }
    526   1.1       cgd 
    527  1.19      fvdl /*
    528  1.19      fvdl  * Fill the stream buffer with a record for a non-blocking connection.
    529  1.19      fvdl  * Return true if a record is available in the buffer, false if not.
    530  1.19      fvdl  */
    531  1.19      fvdl bool_t
    532  1.33      matt __xdrrec_getrec(XDR *xdrs, enum xprt_stat *statp, bool_t expectdata)
    533  1.19      fvdl {
    534  1.19      fvdl 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    535  1.19      fvdl 	ssize_t n;
    536  1.19      fvdl 	int fraglen;
    537  1.19      fvdl 
    538  1.19      fvdl 	if (!rstrm->in_haveheader) {
    539  1.19      fvdl 		n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
    540  1.19      fvdl 		    (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
    541  1.19      fvdl 		if (n == 0) {
    542  1.19      fvdl 			*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
    543  1.19      fvdl 			return FALSE;
    544  1.19      fvdl 		}
    545  1.19      fvdl 		if (n < 0) {
    546  1.19      fvdl 			*statp = XPRT_DIED;
    547  1.19      fvdl 			return FALSE;
    548  1.19      fvdl 		}
    549  1.19      fvdl 		rstrm->in_hdrp += n;
    550  1.32  christos 		_DIAGASSERT(__type_fit(int, n));
    551  1.32  christos 		rstrm->in_hdrlen += (int)n;
    552  1.30     lukem 		if (rstrm->in_hdrlen < (int)sizeof(rstrm->in_header)) {
    553  1.19      fvdl 			*statp = XPRT_MOREREQS;
    554  1.19      fvdl 			return FALSE;
    555  1.19      fvdl 		}
    556  1.19      fvdl 		rstrm->in_header = ntohl(rstrm->in_header);
    557  1.19      fvdl 		fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
    558  1.19      fvdl 		if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
    559  1.19      fvdl 		    (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
    560  1.19      fvdl 			*statp = XPRT_DIED;
    561  1.19      fvdl 			return FALSE;
    562  1.19      fvdl 		}
    563  1.19      fvdl 		rstrm->in_reclen += fraglen;
    564  1.31  christos 		if ((u_int)rstrm->in_reclen > rstrm->recvsize) {
    565  1.31  christos 			if (!realloc_stream(rstrm, rstrm->in_reclen)) {
    566  1.31  christos 				*statp = XPRT_DIED;
    567  1.31  christos 				return FALSE;
    568  1.31  christos 			}
    569  1.31  christos 		}
    570  1.19      fvdl 		if (rstrm->in_header & LAST_FRAG) {
    571  1.19      fvdl 			rstrm->in_header &= ~LAST_FRAG;
    572  1.19      fvdl 			rstrm->last_frag = TRUE;
    573  1.19      fvdl 		}
    574  1.19      fvdl 	}
    575  1.19      fvdl 
    576  1.19      fvdl 	n =  rstrm->readit(rstrm->tcp_handle,
    577  1.19      fvdl 	    rstrm->in_base + rstrm->in_received,
    578  1.19      fvdl 	    (rstrm->in_reclen - rstrm->in_received));
    579  1.19      fvdl 
    580  1.19      fvdl 	if (n < 0) {
    581  1.19      fvdl 		*statp = XPRT_DIED;
    582  1.19      fvdl 		return FALSE;
    583  1.19      fvdl 	}
    584  1.19      fvdl 
    585  1.19      fvdl 	if (n == 0) {
    586  1.19      fvdl 		*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
    587  1.19      fvdl 		return FALSE;
    588  1.19      fvdl 	}
    589  1.19      fvdl 
    590  1.32  christos 	_DIAGASSERT(__type_fit(int, n));
    591  1.32  christos 	rstrm->in_received += (int)n;
    592  1.19      fvdl 
    593  1.19      fvdl 	if (rstrm->in_received == rstrm->in_reclen) {
    594  1.19      fvdl 		rstrm->in_haveheader = FALSE;
    595  1.19      fvdl 		rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
    596  1.19      fvdl 		rstrm->in_hdrlen = 0;
    597  1.19      fvdl 		if (rstrm->last_frag) {
    598  1.19      fvdl 			rstrm->fbtbc = rstrm->in_reclen;
    599  1.19      fvdl 			rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
    600  1.19      fvdl 			rstrm->in_finger = rstrm->in_base;
    601  1.21      fvdl 			rstrm->in_reclen = rstrm->in_received = 0;
    602  1.19      fvdl 			*statp = XPRT_MOREREQS;
    603  1.19      fvdl 			return TRUE;
    604  1.19      fvdl 		}
    605  1.19      fvdl 	}
    606  1.19      fvdl 
    607  1.19      fvdl 	*statp = XPRT_MOREREQS;
    608  1.19      fvdl 	return FALSE;
    609  1.19      fvdl }
    610  1.19      fvdl 
    611  1.19      fvdl bool_t
    612  1.33      matt __xdrrec_setnonblock(XDR *xdrs, int maxrec)
    613  1.19      fvdl {
    614  1.19      fvdl 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    615  1.19      fvdl 
    616  1.19      fvdl 	rstrm->nonblock = TRUE;
    617  1.19      fvdl 	if (maxrec == 0)
    618  1.19      fvdl 		maxrec = rstrm->recvsize;
    619  1.19      fvdl 	rstrm->in_maxrec = maxrec;
    620  1.19      fvdl 	return TRUE;
    621  1.19      fvdl }
    622  1.19      fvdl 
    623   1.1       cgd 
    624   1.1       cgd /*
    625   1.1       cgd  * Internal useful routines
    626   1.1       cgd  */
    627   1.1       cgd static bool_t
    628  1.33      matt flush_out(RECSTREAM *rstrm, bool_t eor)
    629  1.33      matt {
    630  1.33      matt 	uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
    631  1.33      matt 	uint32_t len = (uint32_t)((u_long)(rstrm->out_finger) -
    632  1.33      matt 		(u_long)(rstrm->frag_header) - sizeof(uint32_t));
    633   1.1       cgd 
    634   1.1       cgd 	*(rstrm->frag_header) = htonl(len | eormask);
    635  1.33      matt 	len = (uint32_t)((u_long)(rstrm->out_finger) -
    636  1.15  christos 	    (u_long)(rstrm->out_base));
    637   1.1       cgd 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
    638   1.1       cgd 		!= (int)len)
    639   1.1       cgd 		return (FALSE);
    640  1.33      matt 	rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
    641  1.33      matt 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(uint32_t);
    642   1.1       cgd 	return (TRUE);
    643   1.1       cgd }
    644   1.1       cgd 
    645   1.1       cgd static bool_t  /* knows nothing about records!  Only about input buffers */
    646  1.33      matt fill_input_buf(RECSTREAM *rstrm)
    647   1.1       cgd {
    648  1.14   mycroft 	char *where;
    649  1.33      matt 	uint32_t i;
    650  1.15  christos 	int len;
    651   1.1       cgd 
    652  1.19      fvdl 	if (rstrm->nonblock)
    653  1.19      fvdl 		return FALSE;
    654   1.1       cgd 	where = rstrm->in_base;
    655  1.33      matt 	i = (uint32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
    656   1.1       cgd 	where += i;
    657  1.33      matt 	len = (uint32_t)(rstrm->in_size - i);
    658   1.1       cgd 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
    659   1.1       cgd 		return (FALSE);
    660   1.1       cgd 	rstrm->in_finger = where;
    661   1.1       cgd 	where += len;
    662   1.1       cgd 	rstrm->in_boundry = where;
    663   1.1       cgd 	return (TRUE);
    664   1.1       cgd }
    665   1.1       cgd 
    666   1.1       cgd static bool_t  /* knows nothing about records!  Only about input buffers */
    667  1.33      matt get_input_bytes(RECSTREAM *rstrm, char *addr, u_int len)
    668   1.1       cgd {
    669  1.30     lukem 	u_int current;
    670   1.1       cgd 
    671  1.19      fvdl 	if (rstrm->nonblock) {
    672  1.30     lukem 		if (len > ((uintptr_t)rstrm->in_boundry - (uintptr_t)rstrm->in_finger))
    673  1.19      fvdl 			return FALSE;
    674  1.30     lukem 		memcpy(addr, rstrm->in_finger, len);
    675  1.19      fvdl 		rstrm->in_finger += len;
    676  1.19      fvdl 		return TRUE;
    677  1.19      fvdl 	}
    678  1.19      fvdl 
    679   1.1       cgd 	while (len > 0) {
    680  1.32  christos 		uintptr_t d = ((uintptr_t)rstrm->in_boundry -
    681  1.30     lukem 		    (uintptr_t)rstrm->in_finger);
    682  1.32  christos 		_DIAGASSERT(__type_fit(u_int, d));
    683  1.32  christos 		current = (u_int)d;
    684   1.1       cgd 		if (current == 0) {
    685   1.1       cgd 			if (! fill_input_buf(rstrm))
    686   1.1       cgd 				return (FALSE);
    687   1.1       cgd 			continue;
    688   1.1       cgd 		}
    689   1.1       cgd 		current = (len < current) ? len : current;
    690  1.12     lukem 		memmove(addr, rstrm->in_finger, current);
    691   1.1       cgd 		rstrm->in_finger += current;
    692   1.1       cgd 		addr += current;
    693   1.1       cgd 		len -= current;
    694   1.1       cgd 	}
    695   1.1       cgd 	return (TRUE);
    696   1.1       cgd }
    697   1.1       cgd 
    698   1.1       cgd static bool_t  /* next two bytes of the input stream are treated as a header */
    699  1.33      matt set_input_fragment(RECSTREAM *rstrm)
    700   1.1       cgd {
    701  1.33      matt 	uint32_t header;
    702   1.1       cgd 
    703  1.19      fvdl 	if (rstrm->nonblock)
    704  1.19      fvdl 		return FALSE;
    705  1.32  christos 	if (! get_input_bytes(rstrm, (char *)(void *)&header,
    706  1.32  christos 	    (u_int)sizeof(header)))
    707   1.1       cgd 		return (FALSE);
    708  1.15  christos 	header = ntohl(header);
    709   1.1       cgd 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
    710  1.16     lukem 	/*
    711  1.16     lukem 	 * Sanity check. Try not to accept wildly incorrect
    712  1.16     lukem 	 * record sizes. Unfortunately, the only record size
    713  1.16     lukem 	 * we can positively identify as being 'wildly incorrect'
    714  1.16     lukem 	 * is zero. Ridiculously large record sizes may look wrong,
    715  1.16     lukem 	 * but we don't have any way to be certain that they aren't
    716  1.16     lukem 	 * what the client actually intended to send us.
    717  1.16     lukem 	 */
    718  1.26  christos 	if (header == 0)
    719  1.16     lukem 		return(FALSE);
    720   1.1       cgd 	rstrm->fbtbc = header & (~LAST_FRAG);
    721   1.1       cgd 	return (TRUE);
    722   1.1       cgd }
    723   1.1       cgd 
    724   1.1       cgd static bool_t  /* consumes input bytes; knows nothing about records! */
    725  1.33      matt skip_input_bytes(RECSTREAM *rstrm, long cnt)
    726   1.1       cgd {
    727  1.33      matt 	uint32_t current;
    728   1.1       cgd 
    729   1.1       cgd 	while (cnt > 0) {
    730  1.32  christos 		current = (uint32_t)((long)rstrm->in_boundry -
    731  1.15  christos 		    (long)rstrm->in_finger);
    732   1.1       cgd 		if (current == 0) {
    733   1.1       cgd 			if (! fill_input_buf(rstrm))
    734   1.1       cgd 				return (FALSE);
    735   1.1       cgd 			continue;
    736   1.1       cgd 		}
    737  1.33      matt 		current = ((uint32_t)cnt < current) ? (uint32_t)cnt : current;
    738   1.1       cgd 		rstrm->in_finger += current;
    739   1.1       cgd 		cnt -= current;
    740   1.1       cgd 	}
    741   1.1       cgd 	return (TRUE);
    742   1.1       cgd }
    743   1.1       cgd 
    744  1.11     lukem static u_int
    745  1.33      matt fix_buf_size(u_int s)
    746   1.1       cgd {
    747   1.1       cgd 
    748   1.1       cgd 	if (s < 100)
    749   1.1       cgd 		s = 4000;
    750   1.1       cgd 	return (RNDUP(s));
    751  1.19      fvdl }
    752  1.19      fvdl 
    753  1.19      fvdl /*
    754  1.19      fvdl  * Reallocate the input buffer for a non-block stream.
    755  1.19      fvdl  */
    756  1.19      fvdl static bool_t
    757  1.33      matt realloc_stream(RECSTREAM *rstrm, int size)
    758  1.19      fvdl {
    759  1.19      fvdl 	ptrdiff_t diff;
    760  1.19      fvdl 	char *buf;
    761  1.19      fvdl 
    762  1.30     lukem 	if ((u_int)size > rstrm->recvsize) {
    763  1.19      fvdl 		buf = realloc(rstrm->in_base, (size_t)size);
    764  1.19      fvdl 		if (buf == NULL)
    765  1.19      fvdl 			return FALSE;
    766  1.19      fvdl 		diff = buf - rstrm->in_base;
    767  1.19      fvdl 		rstrm->in_finger += diff;
    768  1.19      fvdl 		rstrm->in_base = buf;
    769  1.19      fvdl 		rstrm->in_boundry = buf + size;
    770  1.19      fvdl 		rstrm->recvsize = size;
    771  1.19      fvdl 		rstrm->in_size = size;
    772  1.19      fvdl 	}
    773  1.19      fvdl 
    774  1.19      fvdl 	return TRUE;
    775   1.1       cgd }
    776