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