xref: /netbsd/lib/libc/rpc/xdr_rec.c (revision c4a72b64)
1 /*	$NetBSD: xdr_rec.c,v 1.19 2002/11/08 00:13:08 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.19 2002/11/08 00:13:08 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 			*statp = XPRT_MOREREQS;
625 			return TRUE;
626 		}
627 	}
628 
629 	*statp = XPRT_MOREREQS;
630 	return FALSE;
631 }
632 
633 bool_t
634 __xdrrec_setnonblock(xdrs, maxrec)
635 	XDR *xdrs;
636 	int maxrec;
637 {
638 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
639 
640 	rstrm->nonblock = TRUE;
641 	if (maxrec == 0)
642 		maxrec = rstrm->recvsize;
643 	rstrm->in_maxrec = maxrec;
644 	return TRUE;
645 }
646 
647 
648 /*
649  * Internal useful routines
650  */
651 static bool_t
652 flush_out(rstrm, eor)
653 	RECSTREAM *rstrm;
654 	bool_t eor;
655 {
656 	u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
657 	u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
658 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t));
659 
660 	*(rstrm->frag_header) = htonl(len | eormask);
661 	len = (u_int32_t)((u_long)(rstrm->out_finger) -
662 	    (u_long)(rstrm->out_base));
663 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
664 		!= (int)len)
665 		return (FALSE);
666 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
667 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
668 	return (TRUE);
669 }
670 
671 static bool_t  /* knows nothing about records!  Only about input buffers */
672 fill_input_buf(rstrm)
673 	RECSTREAM *rstrm;
674 {
675 	char *where;
676 	u_int32_t i;
677 	int len;
678 
679 	if (rstrm->nonblock)
680 		return FALSE;
681 	where = rstrm->in_base;
682 	i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
683 	where += i;
684 	len = (u_int32_t)(rstrm->in_size - i);
685 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
686 		return (FALSE);
687 	rstrm->in_finger = where;
688 	where += len;
689 	rstrm->in_boundry = where;
690 	return (TRUE);
691 }
692 
693 static bool_t  /* knows nothing about records!  Only about input buffers */
694 get_input_bytes(rstrm, addr, len)
695 	RECSTREAM *rstrm;
696 	char *addr;
697 	int len;
698 {
699 	size_t current;
700 
701 	if (rstrm->nonblock) {
702 		if (len > rstrm->in_reclen)
703 			return FALSE;
704 		memcpy(addr, rstrm->in_finger, (size_t)len);
705 		rstrm->in_finger += len;
706 		return TRUE;
707 	}
708 
709 	while (len > 0) {
710 		current = (size_t)((long)rstrm->in_boundry -
711 		    (long)rstrm->in_finger);
712 		if (current == 0) {
713 			if (! fill_input_buf(rstrm))
714 				return (FALSE);
715 			continue;
716 		}
717 		current = (len < current) ? len : current;
718 		memmove(addr, rstrm->in_finger, current);
719 		rstrm->in_finger += current;
720 		addr += current;
721 		len -= current;
722 	}
723 	return (TRUE);
724 }
725 
726 static bool_t  /* next two bytes of the input stream are treated as a header */
727 set_input_fragment(rstrm)
728 	RECSTREAM *rstrm;
729 {
730 	u_int32_t header;
731 
732 	if (rstrm->nonblock)
733 		return FALSE;
734 	if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
735 		return (FALSE);
736 	header = ntohl(header);
737 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
738 	/*
739 	 * Sanity check. Try not to accept wildly incorrect
740 	 * record sizes. Unfortunately, the only record size
741 	 * we can positively identify as being 'wildly incorrect'
742 	 * is zero. Ridiculously large record sizes may look wrong,
743 	 * but we don't have any way to be certain that they aren't
744 	 * what the client actually intended to send us.
745 	 */
746 	if ((header & (~LAST_FRAG)) == 0)
747 		return(FALSE);
748 	rstrm->fbtbc = header & (~LAST_FRAG);
749 	return (TRUE);
750 }
751 
752 static bool_t  /* consumes input bytes; knows nothing about records! */
753 skip_input_bytes(rstrm, cnt)
754 	RECSTREAM *rstrm;
755 	long cnt;
756 {
757 	u_int32_t current;
758 
759 	while (cnt > 0) {
760 		current = (size_t)((long)rstrm->in_boundry -
761 		    (long)rstrm->in_finger);
762 		if (current == 0) {
763 			if (! fill_input_buf(rstrm))
764 				return (FALSE);
765 			continue;
766 		}
767 		current = (u_int32_t)((cnt < current) ? cnt : current);
768 		rstrm->in_finger += current;
769 		cnt -= current;
770 	}
771 	return (TRUE);
772 }
773 
774 static u_int
775 fix_buf_size(s)
776 	u_int s;
777 {
778 
779 	if (s < 100)
780 		s = 4000;
781 	return (RNDUP(s));
782 }
783 
784 /*
785  * Reallocate the input buffer for a non-block stream.
786  */
787 static bool_t
788 realloc_stream(rstrm, size)
789 	RECSTREAM *rstrm;
790 	int size;
791 {
792 	ptrdiff_t diff;
793 	char *buf;
794 
795 	if (size > rstrm->recvsize) {
796 		buf = realloc(rstrm->in_base, (size_t)size);
797 		if (buf == NULL)
798 			return FALSE;
799 		diff = buf - rstrm->in_base;
800 		rstrm->in_finger += diff;
801 		rstrm->in_base = buf;
802 		rstrm->in_boundry = buf + size;
803 		rstrm->recvsize = size;
804 		rstrm->in_size = size;
805 	}
806 
807 	return TRUE;
808 }
809