Home | History | Annotate | Line # | Download | only in rpc
xdr_rec.c revision 1.22
      1 /*	$NetBSD: xdr_rec.c,v 1.22 2003/05/18 23:59:28 christos 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.22 2003/05/18 23:59:28 christos 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 	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 fuction.
    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(xdrs)
    488 	XDR *xdrs;
    489 {
    490 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    491 	enum xprt_stat xstat;
    492 
    493 	if (rstrm->nonblock) {
    494 		if (__xdrrec_getrec(xdrs, &xstat, FALSE))
    495 			return FALSE;
    496 		if (!rstrm->in_haveheader && xstat == XPRT_IDLE)
    497 			return TRUE;
    498 		return FALSE;
    499 	}
    500 
    501 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    502 		if (!skip_input_bytes(rstrm, rstrm->fbtbc))
    503 			return (TRUE);
    504 		rstrm->fbtbc = 0;
    505 		if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
    506 			return (TRUE);
    507 	}
    508 	if (rstrm->in_finger == rstrm->in_boundry)
    509 		return (TRUE);
    510 	return (FALSE);
    511 }
    512 
    513 /*
    514  * The client must tell the package when an end-of-record has occurred.
    515  * The second paraemters tells whether the record should be flushed to the
    516  * (output) tcp stream.  (This let's the package support batched or
    517  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
    518  */
    519 bool_t
    520 xdrrec_endofrecord(xdrs, sendnow)
    521 	XDR *xdrs;
    522 	bool_t sendnow;
    523 {
    524 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    525 	u_long len;  /* fragment length */
    526 
    527 	if (sendnow || rstrm->frag_sent ||
    528 		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
    529 		(u_long)rstrm->out_boundry)) {
    530 		rstrm->frag_sent = FALSE;
    531 		return (flush_out(rstrm, TRUE));
    532 	}
    533 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
    534 	   sizeof(u_int32_t);
    535 	*(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
    536 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
    537 	rstrm->out_finger += sizeof(u_int32_t);
    538 	return (TRUE);
    539 }
    540 
    541 /*
    542  * Fill the stream buffer with a record for a non-blocking connection.
    543  * Return true if a record is available in the buffer, false if not.
    544  */
    545 bool_t
    546 __xdrrec_getrec(xdrs, statp, expectdata)
    547 	XDR *xdrs;
    548 	enum xprt_stat *statp;
    549 	bool_t expectdata;
    550 {
    551 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    552 	ssize_t n;
    553 	int fraglen;
    554 
    555 	if (!rstrm->in_haveheader) {
    556 		n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
    557 		    (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
    558 		if (n == 0) {
    559 			*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
    560 			return FALSE;
    561 		}
    562 		if (n < 0) {
    563 			*statp = XPRT_DIED;
    564 			return FALSE;
    565 		}
    566 		rstrm->in_hdrp += n;
    567 		rstrm->in_hdrlen += n;
    568 		if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
    569 			*statp = XPRT_MOREREQS;
    570 			return FALSE;
    571 		}
    572 		rstrm->in_header = ntohl(rstrm->in_header);
    573 		fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
    574 		if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
    575 		    (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
    576 			*statp = XPRT_DIED;
    577 			return FALSE;
    578 		}
    579 		rstrm->in_reclen += fraglen;
    580 		if (rstrm->in_reclen > rstrm->recvsize)
    581 			realloc_stream(rstrm, rstrm->in_reclen);
    582 		if (rstrm->in_header & LAST_FRAG) {
    583 			rstrm->in_header &= ~LAST_FRAG;
    584 			rstrm->last_frag = TRUE;
    585 		}
    586 	}
    587 
    588 	n =  rstrm->readit(rstrm->tcp_handle,
    589 	    rstrm->in_base + rstrm->in_received,
    590 	    (rstrm->in_reclen - rstrm->in_received));
    591 
    592 	if (n < 0) {
    593 		*statp = XPRT_DIED;
    594 		return FALSE;
    595 	}
    596 
    597 	if (n == 0) {
    598 		*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
    599 		return FALSE;
    600 	}
    601 
    602 	rstrm->in_received += n;
    603 
    604 	if (rstrm->in_received == rstrm->in_reclen) {
    605 		rstrm->in_haveheader = FALSE;
    606 		rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
    607 		rstrm->in_hdrlen = 0;
    608 		if (rstrm->last_frag) {
    609 			rstrm->fbtbc = rstrm->in_reclen;
    610 			rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
    611 			rstrm->in_finger = rstrm->in_base;
    612 			rstrm->in_reclen = rstrm->in_received = 0;
    613 			*statp = XPRT_MOREREQS;
    614 			return TRUE;
    615 		}
    616 	}
    617 
    618 	rstrm->in_reclen = rstrm->in_received = 0;
    619 	*statp = XPRT_MOREREQS;
    620 	return FALSE;
    621 }
    622 
    623 bool_t
    624 __xdrrec_setnonblock(xdrs, maxrec)
    625 	XDR *xdrs;
    626 	int maxrec;
    627 {
    628 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    629 
    630 	rstrm->nonblock = TRUE;
    631 	if (maxrec == 0)
    632 		maxrec = rstrm->recvsize;
    633 	rstrm->in_maxrec = maxrec;
    634 	return TRUE;
    635 }
    636 
    637 
    638 /*
    639  * Internal useful routines
    640  */
    641 static bool_t
    642 flush_out(rstrm, eor)
    643 	RECSTREAM *rstrm;
    644 	bool_t eor;
    645 {
    646 	u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
    647 	u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
    648 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t));
    649 
    650 	*(rstrm->frag_header) = htonl(len | eormask);
    651 	len = (u_int32_t)((u_long)(rstrm->out_finger) -
    652 	    (u_long)(rstrm->out_base));
    653 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
    654 		!= (int)len)
    655 		return (FALSE);
    656 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
    657 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
    658 	return (TRUE);
    659 }
    660 
    661 static bool_t  /* knows nothing about records!  Only about input buffers */
    662 fill_input_buf(rstrm)
    663 	RECSTREAM *rstrm;
    664 {
    665 	char *where;
    666 	u_int32_t i;
    667 	int len;
    668 
    669 	if (rstrm->nonblock)
    670 		return FALSE;
    671 	where = rstrm->in_base;
    672 	i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
    673 	where += i;
    674 	len = (u_int32_t)(rstrm->in_size - i);
    675 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
    676 		return (FALSE);
    677 	rstrm->in_finger = where;
    678 	where += len;
    679 	rstrm->in_boundry = where;
    680 	return (TRUE);
    681 }
    682 
    683 static bool_t  /* knows nothing about records!  Only about input buffers */
    684 get_input_bytes(rstrm, addr, len)
    685 	RECSTREAM *rstrm;
    686 	char *addr;
    687 	int len;
    688 {
    689 	size_t current;
    690 
    691 	if (rstrm->nonblock) {
    692 		if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
    693 			return FALSE;
    694 		memcpy(addr, rstrm->in_finger, (size_t)len);
    695 		rstrm->in_finger += len;
    696 		return TRUE;
    697 	}
    698 
    699 	while (len > 0) {
    700 		current = (size_t)((long)rstrm->in_boundry -
    701 		    (long)rstrm->in_finger);
    702 		if (current == 0) {
    703 			if (! fill_input_buf(rstrm))
    704 				return (FALSE);
    705 			continue;
    706 		}
    707 		current = (len < current) ? len : current;
    708 		memmove(addr, rstrm->in_finger, current);
    709 		rstrm->in_finger += current;
    710 		addr += current;
    711 		len -= current;
    712 	}
    713 	return (TRUE);
    714 }
    715 
    716 static bool_t  /* next two bytes of the input stream are treated as a header */
    717 set_input_fragment(rstrm)
    718 	RECSTREAM *rstrm;
    719 {
    720 	u_int32_t header;
    721 
    722 	if (rstrm->nonblock)
    723 		return FALSE;
    724 	if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
    725 		return (FALSE);
    726 	header = ntohl(header);
    727 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
    728 	/*
    729 	 * Sanity check. Try not to accept wildly incorrect
    730 	 * record sizes. Unfortunately, the only record size
    731 	 * we can positively identify as being 'wildly incorrect'
    732 	 * is zero. Ridiculously large record sizes may look wrong,
    733 	 * but we don't have any way to be certain that they aren't
    734 	 * what the client actually intended to send us.
    735 	 */
    736 	if ((header & (~LAST_FRAG)) == 0)
    737 		return(FALSE);
    738 	rstrm->fbtbc = header & (~LAST_FRAG);
    739 	return (TRUE);
    740 }
    741 
    742 static bool_t  /* consumes input bytes; knows nothing about records! */
    743 skip_input_bytes(rstrm, cnt)
    744 	RECSTREAM *rstrm;
    745 	long cnt;
    746 {
    747 	u_int32_t current;
    748 
    749 	while (cnt > 0) {
    750 		current = (size_t)((long)rstrm->in_boundry -
    751 		    (long)rstrm->in_finger);
    752 		if (current == 0) {
    753 			if (! fill_input_buf(rstrm))
    754 				return (FALSE);
    755 			continue;
    756 		}
    757 		current = (u_int32_t)((cnt < current) ? cnt : current);
    758 		rstrm->in_finger += current;
    759 		cnt -= current;
    760 	}
    761 	return (TRUE);
    762 }
    763 
    764 static u_int
    765 fix_buf_size(s)
    766 	u_int s;
    767 {
    768 
    769 	if (s < 100)
    770 		s = 4000;
    771 	return (RNDUP(s));
    772 }
    773 
    774 /*
    775  * Reallocate the input buffer for a non-block stream.
    776  */
    777 static bool_t
    778 realloc_stream(rstrm, size)
    779 	RECSTREAM *rstrm;
    780 	int size;
    781 {
    782 	ptrdiff_t diff;
    783 	char *buf;
    784 
    785 	if (size > rstrm->recvsize) {
    786 		buf = realloc(rstrm->in_base, (size_t)size);
    787 		if (buf == NULL)
    788 			return FALSE;
    789 		diff = buf - rstrm->in_base;
    790 		rstrm->in_finger += diff;
    791 		rstrm->in_base = buf;
    792 		rstrm->in_boundry = buf + size;
    793 		rstrm->recvsize = size;
    794 		rstrm->in_size = size;
    795 	}
    796 
    797 	return TRUE;
    798 }
    799