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