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