xref: /freebsd/lib/libc/xdr/xdr_rec.c (revision d660d38da5187a02403e342d7290ccedfff05d83)
18360efbdSAlfred Perlstein /*	$NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
3a204967aSHiroki Sato /*-
4a204967aSHiroki Sato  * Copyright (c) 2010, Oracle America, Inc.
5eae561b3SGarrett Wollman  *
6a204967aSHiroki Sato  * Redistribution and use in source and binary forms, with or without
7a204967aSHiroki Sato  * modification, are permitted provided that the following conditions are
8a204967aSHiroki Sato  * met:
9eae561b3SGarrett Wollman  *
10a204967aSHiroki Sato  *     * Redistributions of source code must retain the above copyright
11a204967aSHiroki Sato  *       notice, this list of conditions and the following disclaimer.
12a204967aSHiroki Sato  *     * Redistributions in binary form must reproduce the above
13a204967aSHiroki Sato  *       copyright notice, this list of conditions and the following
14a204967aSHiroki Sato  *       disclaimer in the documentation and/or other materials
15a204967aSHiroki Sato  *       provided with the distribution.
16a204967aSHiroki Sato  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17a204967aSHiroki Sato  *       contributors may be used to endorse or promote products derived
18a204967aSHiroki Sato  *       from this software without specific prior written permission.
19eae561b3SGarrett Wollman  *
20a204967aSHiroki Sato  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21a204967aSHiroki Sato  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a204967aSHiroki Sato  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23a204967aSHiroki Sato  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24a204967aSHiroki Sato  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25a204967aSHiroki Sato  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26a204967aSHiroki Sato  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27a204967aSHiroki Sato  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28a204967aSHiroki Sato  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29a204967aSHiroki Sato  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30a204967aSHiroki Sato  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31a204967aSHiroki Sato  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32eae561b3SGarrett Wollman  */
338360efbdSAlfred Perlstein 
34eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
35a9bdcd37SDavid E. O'Brien static char *sccsid2 = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";
37eae561b3SGarrett Wollman #endif
38333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40eae561b3SGarrett Wollman 
41eae561b3SGarrett Wollman /*
42eae561b3SGarrett Wollman  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
43eae561b3SGarrett Wollman  * layer above tcp (for rpc's use).
44eae561b3SGarrett Wollman  *
45eae561b3SGarrett Wollman  * These routines interface XDRSTREAMS to a tcp/ip connection.
46eae561b3SGarrett Wollman  * There is a record marking layer between the xdr stream
47eae561b3SGarrett Wollman  * and the tcp transport level.  A record is composed on one or more
48eae561b3SGarrett Wollman  * record fragments.  A record fragment is a thirty-two bit header followed
49eae561b3SGarrett Wollman  * by n bytes of data, where n is contained in the header.  The header
50eae561b3SGarrett Wollman  * is represented as a htonl(u_long).  Thegh order bit encodes
51eae561b3SGarrett Wollman  * whether or not the fragment is the last fragment of the record
52eae561b3SGarrett Wollman  * (1 => fragment is last, 0 => more fragments to follow.
53eae561b3SGarrett Wollman  * The other 31 bits encode the byte length of the fragment.
54eae561b3SGarrett Wollman  */
55eae561b3SGarrett Wollman 
568360efbdSAlfred Perlstein #include "namespace.h"
578360efbdSAlfred Perlstein #include <sys/types.h>
588360efbdSAlfred Perlstein 
598360efbdSAlfred Perlstein #include <netinet/in.h>
608360efbdSAlfred Perlstein 
618360efbdSAlfred Perlstein #include <err.h>
62eae561b3SGarrett Wollman #include <stdio.h>
63eae561b3SGarrett Wollman #include <stdlib.h>
6429285d6cSPoul-Henning Kamp #include <string.h>
658360efbdSAlfred Perlstein 
66eae561b3SGarrett Wollman #include <rpc/types.h>
67eae561b3SGarrett Wollman #include <rpc/xdr.h>
6808497c02SMartin Blapp #include <rpc/auth.h>
6908497c02SMartin Blapp #include <rpc/svc.h>
7008497c02SMartin Blapp #include <rpc/clnt.h>
7108497c02SMartin Blapp #include <sys/stddef.h>
728360efbdSAlfred Perlstein #include "un-namespace.h"
732bbd7cf8SJacques Vidrine #include "rpc_com.h"
74eae561b3SGarrett Wollman 
75c05ac53bSDavid E. O'Brien static bool_t	xdrrec_getlong(XDR *, long *);
76c05ac53bSDavid E. O'Brien static bool_t	xdrrec_putlong(XDR *, const long *);
77c05ac53bSDavid E. O'Brien static bool_t	xdrrec_getbytes(XDR *, char *, u_int);
78eae561b3SGarrett Wollman 
79c05ac53bSDavid E. O'Brien static bool_t	xdrrec_putbytes(XDR *, const char *, u_int);
80c05ac53bSDavid E. O'Brien static u_int	xdrrec_getpos(XDR *);
81c05ac53bSDavid E. O'Brien static bool_t	xdrrec_setpos(XDR *, u_int);
82c05ac53bSDavid E. O'Brien static int32_t *xdrrec_inline(XDR *, u_int);
83c05ac53bSDavid E. O'Brien static void	xdrrec_destroy(XDR *);
84eae561b3SGarrett Wollman 
858360efbdSAlfred Perlstein static const struct  xdr_ops xdrrec_ops = {
86eae561b3SGarrett Wollman 	xdrrec_getlong,
87eae561b3SGarrett Wollman 	xdrrec_putlong,
88eae561b3SGarrett Wollman 	xdrrec_getbytes,
89eae561b3SGarrett Wollman 	xdrrec_putbytes,
90eae561b3SGarrett Wollman 	xdrrec_getpos,
91eae561b3SGarrett Wollman 	xdrrec_setpos,
92eae561b3SGarrett Wollman 	xdrrec_inline,
93eae561b3SGarrett Wollman 	xdrrec_destroy
94eae561b3SGarrett Wollman };
95eae561b3SGarrett Wollman 
96eae561b3SGarrett Wollman /*
97eae561b3SGarrett Wollman  * A record is composed of one or more record fragments.
9808497c02SMartin Blapp  * A record fragment is a four-byte header followed by zero to
99eae561b3SGarrett Wollman  * 2**32-1 bytes.  The header is treated as a long unsigned and is
100eae561b3SGarrett Wollman  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
101eae561b3SGarrett Wollman  * are a byte count of the fragment.  The highest order bit is a boolean:
102eae561b3SGarrett Wollman  * 1 => this fragment is the last fragment of the record,
103eae561b3SGarrett Wollman  * 0 => this fragment is followed by more fragment(s).
104eae561b3SGarrett Wollman  *
105eae561b3SGarrett Wollman  * The fragment/record machinery is not general;  it is constructed to
106eae561b3SGarrett Wollman  * meet the needs of xdr and rpc based on tcp.
107eae561b3SGarrett Wollman  */
108eae561b3SGarrett Wollman 
1097a22215cSEitan Adler #define LAST_FRAG ((u_int32_t)(1U << 31))
110eae561b3SGarrett Wollman 
111eae561b3SGarrett Wollman typedef struct rec_strm {
1128360efbdSAlfred Perlstein 	char *tcp_handle;
113eae561b3SGarrett Wollman 	/*
114eae561b3SGarrett Wollman 	 * out-goung bits
115eae561b3SGarrett Wollman 	 */
116f249dbccSDag-Erling Smørgrav 	int (*writeit)(void *, void *, int);
1178360efbdSAlfred Perlstein 	char *out_base;	/* output buffer (points to frag header) */
1188360efbdSAlfred Perlstein 	char *out_finger;	/* next output position */
1198360efbdSAlfred Perlstein 	char *out_boundry;	/* data cannot up to this address */
1208360efbdSAlfred Perlstein 	u_int32_t *frag_header;	/* beginning of curren fragment */
121eae561b3SGarrett Wollman 	bool_t frag_sent;	/* true if buffer sent in middle of record */
122eae561b3SGarrett Wollman 	/*
123eae561b3SGarrett Wollman 	 * in-coming bits
124eae561b3SGarrett Wollman 	 */
125f249dbccSDag-Erling Smørgrav 	int (*readit)(void *, void *, int);
126eae561b3SGarrett Wollman 	u_long in_size;	/* fixed size of the input buffer */
1278360efbdSAlfred Perlstein 	char *in_base;
1288360efbdSAlfred Perlstein 	char *in_finger;	/* location of next byte to be had */
1298360efbdSAlfred Perlstein 	char *in_boundry;	/* can read up to this location */
130eae561b3SGarrett Wollman 	long fbtbc;		/* fragment bytes to be consumed */
131eae561b3SGarrett Wollman 	bool_t last_frag;
132eae561b3SGarrett Wollman 	u_int sendsize;
133eae561b3SGarrett Wollman 	u_int recvsize;
13408497c02SMartin Blapp 
13508497c02SMartin Blapp 	bool_t nonblock;
13608497c02SMartin Blapp 	bool_t in_haveheader;
13708497c02SMartin Blapp 	u_int32_t in_header;
13808497c02SMartin Blapp 	char *in_hdrp;
13908497c02SMartin Blapp 	int in_hdrlen;
14008497c02SMartin Blapp 	int in_reclen;
14108497c02SMartin Blapp 	int in_received;
14208497c02SMartin Blapp 	int in_maxrec;
143eae561b3SGarrett Wollman } RECSTREAM;
144eae561b3SGarrett Wollman 
145c05ac53bSDavid E. O'Brien static u_int	fix_buf_size(u_int);
146c05ac53bSDavid E. O'Brien static bool_t	flush_out(RECSTREAM *, bool_t);
147c05ac53bSDavid E. O'Brien static bool_t	fill_input_buf(RECSTREAM *);
148c05ac53bSDavid E. O'Brien static bool_t	get_input_bytes(RECSTREAM *, char *, int);
149c05ac53bSDavid E. O'Brien static bool_t	set_input_fragment(RECSTREAM *);
150c05ac53bSDavid E. O'Brien static bool_t	skip_input_bytes(RECSTREAM *, long);
15108497c02SMartin Blapp static bool_t	realloc_stream(RECSTREAM *, int);
1528360efbdSAlfred Perlstein 
153eae561b3SGarrett Wollman 
154eae561b3SGarrett Wollman /*
155eae561b3SGarrett Wollman  * Create an xdr handle for xdrrec
156eae561b3SGarrett Wollman  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
157eae561b3SGarrett Wollman  * send and recv buffer sizes (0 => use default).
158eae561b3SGarrett Wollman  * tcp_handle is an opaque handle that is passed as the first parameter to
159eae561b3SGarrett Wollman  * the procedures readit and writeit.  Readit and writeit are read and
160eae561b3SGarrett Wollman  * write respectively.   They are like the system
161eae561b3SGarrett Wollman  * calls expect that they take an opaque handle rather than an fd.
162eae561b3SGarrett Wollman  */
163eae561b3SGarrett Wollman void
164*d660d38dSCraig Rodrigues xdrrec_create(XDR *xdrs, u_int sendsize, u_int recvsize, void *tcp_handle,
165*d660d38dSCraig Rodrigues     int (*readit)(void *, void *, int), int (*writeit)(void *, void *, int))
166*d660d38dSCraig Rodrigues /*
167*d660d38dSCraig Rodrigues  *	XDR *xdrs;
168*d660d38dSCraig Rodrigues  *	u_int sendsize;
169*d660d38dSCraig Rodrigues  *	u_int recvsize;
170*d660d38dSCraig Rodrigues  *	void *tcp_handle;
171*d660d38dSCraig Rodrigues  *	// like read, but pass it a tcp_handle, not sock
172*d660d38dSCraig Rodrigues  *	int (*readit)(void *, void *, int);
173*d660d38dSCraig Rodrigues  *	// like write, but pass it a tcp_handle, not sock
174*d660d38dSCraig Rodrigues  *	int (*writeit)(void *, void *, int);
175*d660d38dSCraig Rodrigues  */
176eae561b3SGarrett Wollman {
1778360efbdSAlfred Perlstein 	RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
178eae561b3SGarrett Wollman 
179eae561b3SGarrett Wollman 	if (rstrm == NULL) {
1808360efbdSAlfred Perlstein 		warnx("xdrrec_create: out of memory");
181eae561b3SGarrett Wollman 		/*
182eae561b3SGarrett Wollman 		 *  This is bad.  Should rework xdrrec_create to
183eae561b3SGarrett Wollman 		 *  return a handle, and in this case return NULL
184eae561b3SGarrett Wollman 		 */
185eae561b3SGarrett Wollman 		return;
186eae561b3SGarrett Wollman 	}
187eae561b3SGarrett Wollman 	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
18808497c02SMartin Blapp 	rstrm->out_base = mem_alloc(rstrm->sendsize);
18908497c02SMartin Blapp 	if (rstrm->out_base == NULL) {
1908360efbdSAlfred Perlstein 		warnx("xdrrec_create: out of memory");
19108497c02SMartin Blapp 		mem_free(rstrm, sizeof(RECSTREAM));
192eae561b3SGarrett Wollman 		return;
193eae561b3SGarrett Wollman 	}
19408497c02SMartin Blapp 	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
19508497c02SMartin Blapp 	rstrm->in_base = mem_alloc(recvsize);
19608497c02SMartin Blapp 	if (rstrm->in_base == NULL) {
19708497c02SMartin Blapp 		warnx("xdrrec_create: out of memory");
19808497c02SMartin Blapp 		mem_free(rstrm->out_base, sendsize);
19908497c02SMartin Blapp 		mem_free(rstrm, sizeof(RECSTREAM));
20008497c02SMartin Blapp 		return;
20108497c02SMartin Blapp 	}
202eae561b3SGarrett Wollman 	/*
203eae561b3SGarrett Wollman 	 * now the rest ...
204eae561b3SGarrett Wollman 	 */
205eae561b3SGarrett Wollman 	xdrs->x_ops = &xdrrec_ops;
2068360efbdSAlfred Perlstein 	xdrs->x_private = rstrm;
207eae561b3SGarrett Wollman 	rstrm->tcp_handle = tcp_handle;
208eae561b3SGarrett Wollman 	rstrm->readit = readit;
209eae561b3SGarrett Wollman 	rstrm->writeit = writeit;
210eae561b3SGarrett Wollman 	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
2118360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
2121ad08a09SPeter Wemm 	rstrm->out_finger += sizeof(u_int32_t);
213eae561b3SGarrett Wollman 	rstrm->out_boundry += sendsize;
214eae561b3SGarrett Wollman 	rstrm->frag_sent = FALSE;
215eae561b3SGarrett Wollman 	rstrm->in_size = recvsize;
216eae561b3SGarrett Wollman 	rstrm->in_boundry = rstrm->in_base;
217eae561b3SGarrett Wollman 	rstrm->in_finger = (rstrm->in_boundry += recvsize);
218eae561b3SGarrett Wollman 	rstrm->fbtbc = 0;
219eae561b3SGarrett Wollman 	rstrm->last_frag = TRUE;
22008497c02SMartin Blapp 	rstrm->in_haveheader = FALSE;
22108497c02SMartin Blapp 	rstrm->in_hdrlen = 0;
22208497c02SMartin Blapp 	rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
22308497c02SMartin Blapp 	rstrm->nonblock = FALSE;
22408497c02SMartin Blapp 	rstrm->in_reclen = 0;
22508497c02SMartin Blapp 	rstrm->in_received = 0;
226eae561b3SGarrett Wollman }
227eae561b3SGarrett Wollman 
228eae561b3SGarrett Wollman 
229eae561b3SGarrett Wollman /*
230eae561b3SGarrett Wollman  * The reoutines defined below are the xdr ops which will go into the
231eae561b3SGarrett Wollman  * xdr handle filled in by xdrrec_create.
232eae561b3SGarrett Wollman  */
233eae561b3SGarrett Wollman 
234eae561b3SGarrett Wollman static bool_t
235*d660d38dSCraig Rodrigues xdrrec_getlong(XDR *xdrs, long *lp)
236eae561b3SGarrett Wollman {
2378360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2388360efbdSAlfred Perlstein 	int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
2391ad08a09SPeter Wemm 	int32_t mylong;
240eae561b3SGarrett Wollman 
241eae561b3SGarrett Wollman 	/* first try the inline, fast case */
2421ad08a09SPeter Wemm 	if ((rstrm->fbtbc >= sizeof(int32_t)) &&
2431ad08a09SPeter Wemm 		(((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
2441ad08a09SPeter Wemm 		*lp = (long)ntohl((u_int32_t)(*buflp));
2451ad08a09SPeter Wemm 		rstrm->fbtbc -= sizeof(int32_t);
2461ad08a09SPeter Wemm 		rstrm->in_finger += sizeof(int32_t);
247eae561b3SGarrett Wollman 	} else {
2488360efbdSAlfred Perlstein 		if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
2498360efbdSAlfred Perlstein 		    sizeof(int32_t)))
250eae561b3SGarrett Wollman 			return (FALSE);
2511ad08a09SPeter Wemm 		*lp = (long)ntohl((u_int32_t)mylong);
252eae561b3SGarrett Wollman 	}
253eae561b3SGarrett Wollman 	return (TRUE);
254eae561b3SGarrett Wollman }
255eae561b3SGarrett Wollman 
256eae561b3SGarrett Wollman static bool_t
257*d660d38dSCraig Rodrigues xdrrec_putlong(XDR *xdrs, const long *lp)
258eae561b3SGarrett Wollman {
2598360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2608360efbdSAlfred Perlstein 	int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
261eae561b3SGarrett Wollman 
2621ad08a09SPeter Wemm 	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
263eae561b3SGarrett Wollman 		/*
264eae561b3SGarrett Wollman 		 * this case should almost never happen so the code is
265eae561b3SGarrett Wollman 		 * inefficient
266eae561b3SGarrett Wollman 		 */
2671ad08a09SPeter Wemm 		rstrm->out_finger -= sizeof(int32_t);
268eae561b3SGarrett Wollman 		rstrm->frag_sent = TRUE;
269eae561b3SGarrett Wollman 		if (! flush_out(rstrm, FALSE))
270eae561b3SGarrett Wollman 			return (FALSE);
2718360efbdSAlfred Perlstein 		dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
2721ad08a09SPeter Wemm 		rstrm->out_finger += sizeof(int32_t);
273eae561b3SGarrett Wollman 	}
2741ad08a09SPeter Wemm 	*dest_lp = (int32_t)htonl((u_int32_t)(*lp));
275eae561b3SGarrett Wollman 	return (TRUE);
276eae561b3SGarrett Wollman }
277eae561b3SGarrett Wollman 
278eae561b3SGarrett Wollman static bool_t  /* must manage buffers, fragments, and records */
279*d660d38dSCraig Rodrigues xdrrec_getbytes(XDR *xdrs, char *addr, u_int len)
280eae561b3SGarrett Wollman {
2818360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
2828360efbdSAlfred Perlstein 	int current;
283eae561b3SGarrett Wollman 
284eae561b3SGarrett Wollman 	while (len > 0) {
2858360efbdSAlfred Perlstein 		current = (int)rstrm->fbtbc;
286eae561b3SGarrett Wollman 		if (current == 0) {
287eae561b3SGarrett Wollman 			if (rstrm->last_frag)
288eae561b3SGarrett Wollman 				return (FALSE);
289eae561b3SGarrett Wollman 			if (! set_input_fragment(rstrm))
290eae561b3SGarrett Wollman 				return (FALSE);
291eae561b3SGarrett Wollman 			continue;
292eae561b3SGarrett Wollman 		}
293eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
294eae561b3SGarrett Wollman 		if (! get_input_bytes(rstrm, addr, current))
295eae561b3SGarrett Wollman 			return (FALSE);
296eae561b3SGarrett Wollman 		addr += current;
297eae561b3SGarrett Wollman 		rstrm->fbtbc -= current;
298eae561b3SGarrett Wollman 		len -= current;
299eae561b3SGarrett Wollman 	}
300eae561b3SGarrett Wollman 	return (TRUE);
301eae561b3SGarrett Wollman }
302eae561b3SGarrett Wollman 
303eae561b3SGarrett Wollman static bool_t
304*d660d38dSCraig Rodrigues xdrrec_putbytes(XDR *xdrs, const char *addr, u_int len)
305eae561b3SGarrett Wollman {
3068360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
3078360efbdSAlfred Perlstein 	size_t current;
308eae561b3SGarrett Wollman 
309eae561b3SGarrett Wollman 	while (len > 0) {
3108360efbdSAlfred Perlstein 		current = (size_t)((u_long)rstrm->out_boundry -
3118360efbdSAlfred Perlstein 		    (u_long)rstrm->out_finger);
312eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
3138360efbdSAlfred Perlstein 		memmove(rstrm->out_finger, addr, current);
314eae561b3SGarrett Wollman 		rstrm->out_finger += current;
315eae561b3SGarrett Wollman 		addr += current;
316eae561b3SGarrett Wollman 		len -= current;
317eae561b3SGarrett Wollman 		if (rstrm->out_finger == rstrm->out_boundry) {
318eae561b3SGarrett Wollman 			rstrm->frag_sent = TRUE;
319eae561b3SGarrett Wollman 			if (! flush_out(rstrm, FALSE))
320eae561b3SGarrett Wollman 				return (FALSE);
321eae561b3SGarrett Wollman 		}
322eae561b3SGarrett Wollman 	}
323eae561b3SGarrett Wollman 	return (TRUE);
324eae561b3SGarrett Wollman }
325eae561b3SGarrett Wollman 
326eae561b3SGarrett Wollman static u_int
327*d660d38dSCraig Rodrigues xdrrec_getpos(XDR *xdrs)
328eae561b3SGarrett Wollman {
3298360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
3308360efbdSAlfred Perlstein 	off_t pos;
331eae561b3SGarrett Wollman 
3328360efbdSAlfred Perlstein 	pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
3338f55a568SDoug Rabson 	if (pos == -1)
3348f55a568SDoug Rabson 		pos = 0;
335eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
336eae561b3SGarrett Wollman 
337eae561b3SGarrett Wollman 	case XDR_ENCODE:
338eae561b3SGarrett Wollman 		pos += rstrm->out_finger - rstrm->out_base;
339eae561b3SGarrett Wollman 		break;
340eae561b3SGarrett Wollman 
341eae561b3SGarrett Wollman 	case XDR_DECODE:
342eae561b3SGarrett Wollman 		pos -= rstrm->in_boundry - rstrm->in_finger;
343eae561b3SGarrett Wollman 		break;
344eae561b3SGarrett Wollman 
345eae561b3SGarrett Wollman 	default:
3468360efbdSAlfred Perlstein 		pos = (off_t) -1;
347eae561b3SGarrett Wollman 		break;
348eae561b3SGarrett Wollman 	}
349eae561b3SGarrett Wollman 	return ((u_int) pos);
350eae561b3SGarrett Wollman }
351eae561b3SGarrett Wollman 
352eae561b3SGarrett Wollman static bool_t
353*d660d38dSCraig Rodrigues xdrrec_setpos(XDR *xdrs, u_int pos)
354eae561b3SGarrett Wollman {
3558360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
356eae561b3SGarrett Wollman 	u_int currpos = xdrrec_getpos(xdrs);
357eae561b3SGarrett Wollman 	int delta = currpos - pos;
3588360efbdSAlfred Perlstein 	char *newpos;
359eae561b3SGarrett Wollman 
360eae561b3SGarrett Wollman 	if ((int)currpos != -1)
361eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
362eae561b3SGarrett Wollman 
363eae561b3SGarrett Wollman 		case XDR_ENCODE:
364eae561b3SGarrett Wollman 			newpos = rstrm->out_finger - delta;
3658360efbdSAlfred Perlstein 			if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
366eae561b3SGarrett Wollman 				(newpos < rstrm->out_boundry)) {
367eae561b3SGarrett Wollman 				rstrm->out_finger = newpos;
368eae561b3SGarrett Wollman 				return (TRUE);
369eae561b3SGarrett Wollman 			}
370eae561b3SGarrett Wollman 			break;
371eae561b3SGarrett Wollman 
372eae561b3SGarrett Wollman 		case XDR_DECODE:
373eae561b3SGarrett Wollman 			newpos = rstrm->in_finger - delta;
374eae561b3SGarrett Wollman 			if ((delta < (int)(rstrm->fbtbc)) &&
375eae561b3SGarrett Wollman 				(newpos <= rstrm->in_boundry) &&
376eae561b3SGarrett Wollman 				(newpos >= rstrm->in_base)) {
377eae561b3SGarrett Wollman 				rstrm->in_finger = newpos;
378eae561b3SGarrett Wollman 				rstrm->fbtbc -= delta;
379eae561b3SGarrett Wollman 				return (TRUE);
380eae561b3SGarrett Wollman 			}
381eae561b3SGarrett Wollman 			break;
3828360efbdSAlfred Perlstein 
3838360efbdSAlfred Perlstein 		case XDR_FREE:
3848360efbdSAlfred Perlstein 			break;
385eae561b3SGarrett Wollman 		}
386eae561b3SGarrett Wollman 	return (FALSE);
387eae561b3SGarrett Wollman }
388eae561b3SGarrett Wollman 
3891ad08a09SPeter Wemm static int32_t *
390*d660d38dSCraig Rodrigues xdrrec_inline(XDR *xdrs, u_int len)
391eae561b3SGarrett Wollman {
3928360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
3931ad08a09SPeter Wemm 	int32_t *buf = NULL;
394eae561b3SGarrett Wollman 
395eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
396eae561b3SGarrett Wollman 
397eae561b3SGarrett Wollman 	case XDR_ENCODE:
398eae561b3SGarrett Wollman 		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
3998360efbdSAlfred Perlstein 			buf = (int32_t *)(void *)rstrm->out_finger;
400eae561b3SGarrett Wollman 			rstrm->out_finger += len;
401eae561b3SGarrett Wollman 		}
402eae561b3SGarrett Wollman 		break;
403eae561b3SGarrett Wollman 
404eae561b3SGarrett Wollman 	case XDR_DECODE:
405eae561b3SGarrett Wollman 		if ((len <= rstrm->fbtbc) &&
406eae561b3SGarrett Wollman 			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
4078360efbdSAlfred Perlstein 			buf = (int32_t *)(void *)rstrm->in_finger;
408eae561b3SGarrett Wollman 			rstrm->fbtbc -= len;
409eae561b3SGarrett Wollman 			rstrm->in_finger += len;
410eae561b3SGarrett Wollman 		}
411eae561b3SGarrett Wollman 		break;
4128360efbdSAlfred Perlstein 
4138360efbdSAlfred Perlstein 	case XDR_FREE:
4148360efbdSAlfred Perlstein 		break;
415eae561b3SGarrett Wollman 	}
416eae561b3SGarrett Wollman 	return (buf);
417eae561b3SGarrett Wollman }
418eae561b3SGarrett Wollman 
419eae561b3SGarrett Wollman static void
420*d660d38dSCraig Rodrigues xdrrec_destroy(XDR *xdrs)
421eae561b3SGarrett Wollman {
4228360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
423eae561b3SGarrett Wollman 
42408497c02SMartin Blapp 	mem_free(rstrm->out_base, rstrm->sendsize);
42508497c02SMartin Blapp 	mem_free(rstrm->in_base, rstrm->recvsize);
4268360efbdSAlfred Perlstein 	mem_free(rstrm, sizeof(RECSTREAM));
427eae561b3SGarrett Wollman }
428eae561b3SGarrett Wollman 
429eae561b3SGarrett Wollman 
430eae561b3SGarrett Wollman /*
431eae561b3SGarrett Wollman  * Exported routines to manage xdr records
432eae561b3SGarrett Wollman  */
433eae561b3SGarrett Wollman 
434eae561b3SGarrett Wollman /*
435eae561b3SGarrett Wollman  * Before reading (deserializing from the stream, one should always call
436eae561b3SGarrett Wollman  * this procedure to guarantee proper record alignment.
437eae561b3SGarrett Wollman  */
438eae561b3SGarrett Wollman bool_t
439*d660d38dSCraig Rodrigues xdrrec_skiprecord(XDR *xdrs)
440eae561b3SGarrett Wollman {
4418360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
44208497c02SMartin Blapp 	enum xprt_stat xstat;
44308497c02SMartin Blapp 
44408497c02SMartin Blapp 	if (rstrm->nonblock) {
44508497c02SMartin Blapp 		if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
44608497c02SMartin Blapp 			rstrm->fbtbc = 0;
44708497c02SMartin Blapp 			return TRUE;
44808497c02SMartin Blapp 		}
44908497c02SMartin Blapp 		if (rstrm->in_finger == rstrm->in_boundry &&
45008497c02SMartin Blapp 		    xstat == XPRT_MOREREQS) {
45108497c02SMartin Blapp 			rstrm->fbtbc = 0;
45208497c02SMartin Blapp 			return TRUE;
45308497c02SMartin Blapp 		}
45408497c02SMartin Blapp 		return FALSE;
45508497c02SMartin Blapp 	}
456eae561b3SGarrett Wollman 
457eae561b3SGarrett Wollman 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
458eae561b3SGarrett Wollman 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
459eae561b3SGarrett Wollman 			return (FALSE);
460eae561b3SGarrett Wollman 		rstrm->fbtbc = 0;
461eae561b3SGarrett Wollman 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
462eae561b3SGarrett Wollman 			return (FALSE);
463eae561b3SGarrett Wollman 	}
464eae561b3SGarrett Wollman 	rstrm->last_frag = FALSE;
465eae561b3SGarrett Wollman 	return (TRUE);
466eae561b3SGarrett Wollman }
467eae561b3SGarrett Wollman 
468eae561b3SGarrett Wollman /*
4698360efbdSAlfred Perlstein  * Look ahead function.
470eae561b3SGarrett Wollman  * Returns TRUE iff there is no more input in the buffer
471eae561b3SGarrett Wollman  * after consuming the rest of the current record.
472eae561b3SGarrett Wollman  */
473eae561b3SGarrett Wollman bool_t
474*d660d38dSCraig Rodrigues xdrrec_eof(XDR *xdrs)
475eae561b3SGarrett Wollman {
4768360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
477eae561b3SGarrett Wollman 
478eae561b3SGarrett Wollman 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
479eae561b3SGarrett Wollman 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
480eae561b3SGarrett Wollman 			return (TRUE);
481eae561b3SGarrett Wollman 		rstrm->fbtbc = 0;
482eae561b3SGarrett Wollman 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
483eae561b3SGarrett Wollman 			return (TRUE);
484eae561b3SGarrett Wollman 	}
485eae561b3SGarrett Wollman 	if (rstrm->in_finger == rstrm->in_boundry)
486eae561b3SGarrett Wollman 		return (TRUE);
487eae561b3SGarrett Wollman 	return (FALSE);
488eae561b3SGarrett Wollman }
489eae561b3SGarrett Wollman 
490eae561b3SGarrett Wollman /*
491eae561b3SGarrett Wollman  * The client must tell the package when an end-of-record has occurred.
492eae561b3SGarrett Wollman  * The second paraemters tells whether the record should be flushed to the
493eae561b3SGarrett Wollman  * (output) tcp stream.  (This let's the package support batched or
494eae561b3SGarrett Wollman  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
495eae561b3SGarrett Wollman  */
496eae561b3SGarrett Wollman bool_t
497*d660d38dSCraig Rodrigues xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
498eae561b3SGarrett Wollman {
4998360efbdSAlfred Perlstein 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
5008360efbdSAlfred Perlstein 	u_long len;  /* fragment length */
501eae561b3SGarrett Wollman 
502eae561b3SGarrett Wollman 	if (sendnow || rstrm->frag_sent ||
5031ad08a09SPeter Wemm 		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
504eae561b3SGarrett Wollman 		(u_long)rstrm->out_boundry)) {
505eae561b3SGarrett Wollman 		rstrm->frag_sent = FALSE;
506eae561b3SGarrett Wollman 		return (flush_out(rstrm, TRUE));
507eae561b3SGarrett Wollman 	}
508eae561b3SGarrett Wollman 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
5091ad08a09SPeter Wemm 	   sizeof(u_int32_t);
5108360efbdSAlfred Perlstein 	*(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
5118360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
5121ad08a09SPeter Wemm 	rstrm->out_finger += sizeof(u_int32_t);
513eae561b3SGarrett Wollman 	return (TRUE);
514eae561b3SGarrett Wollman }
515eae561b3SGarrett Wollman 
51608497c02SMartin Blapp /*
51708497c02SMartin Blapp  * Fill the stream buffer with a record for a non-blocking connection.
51808497c02SMartin Blapp  * Return true if a record is available in the buffer, false if not.
51908497c02SMartin Blapp  */
52008497c02SMartin Blapp bool_t
521*d660d38dSCraig Rodrigues __xdrrec_getrec(XDR *xdrs, enum xprt_stat *statp, bool_t expectdata)
52208497c02SMartin Blapp {
52308497c02SMartin Blapp 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
52408497c02SMartin Blapp 	ssize_t n;
52508497c02SMartin Blapp 	int fraglen;
52608497c02SMartin Blapp 
52708497c02SMartin Blapp 	if (!rstrm->in_haveheader) {
52808497c02SMartin Blapp 		n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
52908497c02SMartin Blapp 		    (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
53008497c02SMartin Blapp 		if (n == 0) {
53108497c02SMartin Blapp 			*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
53208497c02SMartin Blapp 			return FALSE;
53308497c02SMartin Blapp 		}
53408497c02SMartin Blapp 		if (n < 0) {
53508497c02SMartin Blapp 			*statp = XPRT_DIED;
53608497c02SMartin Blapp 			return FALSE;
53708497c02SMartin Blapp 		}
53808497c02SMartin Blapp 		rstrm->in_hdrp += n;
53908497c02SMartin Blapp 		rstrm->in_hdrlen += n;
54008497c02SMartin Blapp 		if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
54108497c02SMartin Blapp 			*statp = XPRT_MOREREQS;
54208497c02SMartin Blapp 			return FALSE;
54308497c02SMartin Blapp 		}
54408497c02SMartin Blapp 		rstrm->in_header = ntohl(rstrm->in_header);
54508497c02SMartin Blapp 		fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
54608497c02SMartin Blapp 		if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
54708497c02SMartin Blapp 		    (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
54808497c02SMartin Blapp 			*statp = XPRT_DIED;
54908497c02SMartin Blapp 			return FALSE;
55008497c02SMartin Blapp 		}
55108497c02SMartin Blapp 		rstrm->in_reclen += fraglen;
55208497c02SMartin Blapp 		if (rstrm->in_reclen > rstrm->recvsize)
55308497c02SMartin Blapp 			realloc_stream(rstrm, rstrm->in_reclen);
55408497c02SMartin Blapp 		if (rstrm->in_header & LAST_FRAG) {
55508497c02SMartin Blapp 			rstrm->in_header &= ~LAST_FRAG;
55608497c02SMartin Blapp 			rstrm->last_frag = TRUE;
55708497c02SMartin Blapp 		}
5587ea7cc4bSDoug Rabson 		/*
5597ea7cc4bSDoug Rabson 		 * We can only reasonably expect to read once from a
5607ea7cc4bSDoug Rabson 		 * non-blocking stream. Reading the fragment header
5617ea7cc4bSDoug Rabson 		 * may have drained the stream.
5627ea7cc4bSDoug Rabson 		 */
5637ea7cc4bSDoug Rabson 		expectdata = FALSE;
56408497c02SMartin Blapp 	}
56508497c02SMartin Blapp 
56608497c02SMartin Blapp 	n =  rstrm->readit(rstrm->tcp_handle,
56708497c02SMartin Blapp 	    rstrm->in_base + rstrm->in_received,
56808497c02SMartin Blapp 	    (rstrm->in_reclen - rstrm->in_received));
56908497c02SMartin Blapp 
57008497c02SMartin Blapp 	if (n < 0) {
57108497c02SMartin Blapp 		*statp = XPRT_DIED;
57208497c02SMartin Blapp 		return FALSE;
57308497c02SMartin Blapp 	}
57408497c02SMartin Blapp 
57508497c02SMartin Blapp 	if (n == 0) {
57608497c02SMartin Blapp 		*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
57708497c02SMartin Blapp 		return FALSE;
57808497c02SMartin Blapp 	}
57908497c02SMartin Blapp 
58008497c02SMartin Blapp 	rstrm->in_received += n;
58108497c02SMartin Blapp 
58208497c02SMartin Blapp 	if (rstrm->in_received == rstrm->in_reclen) {
58308497c02SMartin Blapp 		rstrm->in_haveheader = FALSE;
58408497c02SMartin Blapp 		rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
58508497c02SMartin Blapp 		rstrm->in_hdrlen = 0;
58608497c02SMartin Blapp 		if (rstrm->last_frag) {
58708497c02SMartin Blapp 			rstrm->fbtbc = rstrm->in_reclen;
58808497c02SMartin Blapp 			rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
58908497c02SMartin Blapp 			rstrm->in_finger = rstrm->in_base;
59040525d3dSMartin Blapp 			rstrm->in_reclen = rstrm->in_received = 0;
59108497c02SMartin Blapp 			*statp = XPRT_MOREREQS;
59208497c02SMartin Blapp 			return TRUE;
59308497c02SMartin Blapp 		}
59408497c02SMartin Blapp 	}
59508497c02SMartin Blapp 
59608497c02SMartin Blapp 	*statp = XPRT_MOREREQS;
59708497c02SMartin Blapp 	return FALSE;
59808497c02SMartin Blapp }
59908497c02SMartin Blapp 
60008497c02SMartin Blapp bool_t
601*d660d38dSCraig Rodrigues __xdrrec_setnonblock(XDR *xdrs, int maxrec)
60208497c02SMartin Blapp {
60308497c02SMartin Blapp 	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
60408497c02SMartin Blapp 
60508497c02SMartin Blapp 	rstrm->nonblock = TRUE;
60608497c02SMartin Blapp 	if (maxrec == 0)
60708497c02SMartin Blapp 		maxrec = rstrm->recvsize;
60808497c02SMartin Blapp 	rstrm->in_maxrec = maxrec;
60908497c02SMartin Blapp 	return TRUE;
61008497c02SMartin Blapp }
611eae561b3SGarrett Wollman 
612eae561b3SGarrett Wollman /*
613eae561b3SGarrett Wollman  * Internal useful routines
614eae561b3SGarrett Wollman  */
615eae561b3SGarrett Wollman static bool_t
616*d660d38dSCraig Rodrigues flush_out(RECSTREAM *rstrm, bool_t eor)
617eae561b3SGarrett Wollman {
6188360efbdSAlfred Perlstein 	u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
6198360efbdSAlfred Perlstein 	u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
6208360efbdSAlfred Perlstein 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t));
621eae561b3SGarrett Wollman 
622eae561b3SGarrett Wollman 	*(rstrm->frag_header) = htonl(len | eormask);
6238360efbdSAlfred Perlstein 	len = (u_int32_t)((u_long)(rstrm->out_finger) -
6248360efbdSAlfred Perlstein 	    (u_long)(rstrm->out_base));
625eae561b3SGarrett Wollman 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
626eae561b3SGarrett Wollman 		!= (int)len)
627eae561b3SGarrett Wollman 		return (FALSE);
6288360efbdSAlfred Perlstein 	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
6298360efbdSAlfred Perlstein 	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
630eae561b3SGarrett Wollman 	return (TRUE);
631eae561b3SGarrett Wollman }
632eae561b3SGarrett Wollman 
633eae561b3SGarrett Wollman static bool_t  /* knows nothing about records!  Only about input buffers */
634*d660d38dSCraig Rodrigues fill_input_buf(RECSTREAM *rstrm)
635eae561b3SGarrett Wollman {
6368360efbdSAlfred Perlstein 	char *where;
6378360efbdSAlfred Perlstein 	u_int32_t i;
6388360efbdSAlfred Perlstein 	int len;
639eae561b3SGarrett Wollman 
64008497c02SMartin Blapp 	if (rstrm->nonblock)
64108497c02SMartin Blapp 		return FALSE;
64208497c02SMartin Blapp 
643eae561b3SGarrett Wollman 	where = rstrm->in_base;
6448360efbdSAlfred Perlstein 	i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
645eae561b3SGarrett Wollman 	where += i;
6468360efbdSAlfred Perlstein 	len = (u_int32_t)(rstrm->in_size - i);
647eae561b3SGarrett Wollman 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
648eae561b3SGarrett Wollman 		return (FALSE);
649eae561b3SGarrett Wollman 	rstrm->in_finger = where;
650eae561b3SGarrett Wollman 	where += len;
651eae561b3SGarrett Wollman 	rstrm->in_boundry = where;
652eae561b3SGarrett Wollman 	return (TRUE);
653eae561b3SGarrett Wollman }
654eae561b3SGarrett Wollman 
655eae561b3SGarrett Wollman static bool_t  /* knows nothing about records!  Only about input buffers */
656*d660d38dSCraig Rodrigues get_input_bytes(RECSTREAM *rstrm, char *addr, int len)
657eae561b3SGarrett Wollman {
6588360efbdSAlfred Perlstein 	size_t current;
659eae561b3SGarrett Wollman 
66040525d3dSMartin Blapp 	if (rstrm->nonblock) {
66140525d3dSMartin Blapp 		if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
66240525d3dSMartin Blapp 			return FALSE;
66340525d3dSMartin Blapp 		memcpy(addr, rstrm->in_finger, (size_t)len);
66440525d3dSMartin Blapp 		rstrm->in_finger += len;
66540525d3dSMartin Blapp 		return TRUE;
66640525d3dSMartin Blapp 	}
66740525d3dSMartin Blapp 
668eae561b3SGarrett Wollman 	while (len > 0) {
6698360efbdSAlfred Perlstein 		current = (size_t)((long)rstrm->in_boundry -
6708360efbdSAlfred Perlstein 		    (long)rstrm->in_finger);
671eae561b3SGarrett Wollman 		if (current == 0) {
672eae561b3SGarrett Wollman 			if (! fill_input_buf(rstrm))
673eae561b3SGarrett Wollman 				return (FALSE);
674eae561b3SGarrett Wollman 			continue;
675eae561b3SGarrett Wollman 		}
676eae561b3SGarrett Wollman 		current = (len < current) ? len : current;
6778360efbdSAlfred Perlstein 		memmove(addr, rstrm->in_finger, current);
678eae561b3SGarrett Wollman 		rstrm->in_finger += current;
679eae561b3SGarrett Wollman 		addr += current;
680eae561b3SGarrett Wollman 		len -= current;
681eae561b3SGarrett Wollman 	}
682eae561b3SGarrett Wollman 	return (TRUE);
683eae561b3SGarrett Wollman }
684eae561b3SGarrett Wollman 
685eae561b3SGarrett Wollman static bool_t  /* next two bytes of the input stream are treated as a header */
686*d660d38dSCraig Rodrigues set_input_fragment(RECSTREAM *rstrm)
687eae561b3SGarrett Wollman {
6881ad08a09SPeter Wemm 	u_int32_t header;
689eae561b3SGarrett Wollman 
69022e4d31aSMartin Blapp 	if (rstrm->nonblock)
69122e4d31aSMartin Blapp 		return FALSE;
6928360efbdSAlfred Perlstein 	if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
693eae561b3SGarrett Wollman 		return (FALSE);
6948360efbdSAlfred Perlstein 	header = ntohl(header);
695eae561b3SGarrett Wollman 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
696a9352e90SBill Paul 	/*
697a9352e90SBill Paul 	 * Sanity check. Try not to accept wildly incorrect
6981ce4aec2SBill Paul 	 * record sizes. Unfortunately, the only record size
6991ce4aec2SBill Paul 	 * we can positively identify as being 'wildly incorrect'
7001ce4aec2SBill Paul 	 * is zero. Ridiculously large record sizes may look wrong,
7011ce4aec2SBill Paul 	 * but we don't have any way to be certain that they aren't
7021ce4aec2SBill Paul 	 * what the client actually intended to send us.
703a9352e90SBill Paul 	 */
704c50a9e8fSBill Paul 	if (header == 0)
705a9352e90SBill Paul 		return(FALSE);
706eae561b3SGarrett Wollman 	rstrm->fbtbc = header & (~LAST_FRAG);
707eae561b3SGarrett Wollman 	return (TRUE);
708eae561b3SGarrett Wollman }
709eae561b3SGarrett Wollman 
710eae561b3SGarrett Wollman static bool_t  /* consumes input bytes; knows nothing about records! */
711*d660d38dSCraig Rodrigues skip_input_bytes(RECSTREAM *rstrm, long cnt)
712eae561b3SGarrett Wollman {
7138360efbdSAlfred Perlstein 	u_int32_t current;
714eae561b3SGarrett Wollman 
715eae561b3SGarrett Wollman 	while (cnt > 0) {
7168360efbdSAlfred Perlstein 		current = (size_t)((long)rstrm->in_boundry -
7178360efbdSAlfred Perlstein 		    (long)rstrm->in_finger);
718eae561b3SGarrett Wollman 		if (current == 0) {
719eae561b3SGarrett Wollman 			if (! fill_input_buf(rstrm))
720eae561b3SGarrett Wollman 				return (FALSE);
721eae561b3SGarrett Wollman 			continue;
722eae561b3SGarrett Wollman 		}
7238360efbdSAlfred Perlstein 		current = (u_int32_t)((cnt < current) ? cnt : current);
724eae561b3SGarrett Wollman 		rstrm->in_finger += current;
725eae561b3SGarrett Wollman 		cnt -= current;
726eae561b3SGarrett Wollman 	}
727eae561b3SGarrett Wollman 	return (TRUE);
728eae561b3SGarrett Wollman }
729eae561b3SGarrett Wollman 
730eae561b3SGarrett Wollman static u_int
731*d660d38dSCraig Rodrigues fix_buf_size(u_int s)
732eae561b3SGarrett Wollman {
733eae561b3SGarrett Wollman 
734eae561b3SGarrett Wollman 	if (s < 100)
735eae561b3SGarrett Wollman 		s = 4000;
736eae561b3SGarrett Wollman 	return (RNDUP(s));
737eae561b3SGarrett Wollman }
73808497c02SMartin Blapp 
73908497c02SMartin Blapp /*
74008497c02SMartin Blapp  * Reallocate the input buffer for a non-block stream.
74108497c02SMartin Blapp  */
74208497c02SMartin Blapp static bool_t
743*d660d38dSCraig Rodrigues realloc_stream(RECSTREAM *rstrm, int size)
74408497c02SMartin Blapp {
74508497c02SMartin Blapp 	ptrdiff_t diff;
74608497c02SMartin Blapp 	char *buf;
74708497c02SMartin Blapp 
74808497c02SMartin Blapp 	if (size > rstrm->recvsize) {
74908497c02SMartin Blapp 		buf = realloc(rstrm->in_base, (size_t)size);
75008497c02SMartin Blapp 		if (buf == NULL)
75108497c02SMartin Blapp 			return FALSE;
75208497c02SMartin Blapp 		diff = buf - rstrm->in_base;
75308497c02SMartin Blapp 		rstrm->in_finger += diff;
75408497c02SMartin Blapp 		rstrm->in_base = buf;
75508497c02SMartin Blapp 		rstrm->in_boundry = buf + size;
75608497c02SMartin Blapp 		rstrm->recvsize = size;
75708497c02SMartin Blapp 		rstrm->in_size = size;
75808497c02SMartin Blapp 	}
75908497c02SMartin Blapp 
76008497c02SMartin Blapp 	return TRUE;
76108497c02SMartin Blapp }
762