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