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