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