174462Salfred/*	$NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
3259118Shrs/*-
4259118Shrs * Copyright (c) 2010, Oracle America, Inc.
58870Srgrimes *
6259118Shrs * Redistribution and use in source and binary forms, with or without
7259118Shrs * modification, are permitted provided that the following conditions are
8259118Shrs * met:
98870Srgrimes *
10259118Shrs *     * Redistributions of source code must retain the above copyright
11259118Shrs *       notice, this list of conditions and the following disclaimer.
12259118Shrs *     * Redistributions in binary form must reproduce the above
13259118Shrs *       copyright notice, this list of conditions and the following
14259118Shrs *       disclaimer in the documentation and/or other materials
15259118Shrs *       provided with the distribution.
16259118Shrs *     * Neither the name of the "Oracle America, Inc." nor the names of its
17259118Shrs *       contributors may be used to endorse or promote products derived
18259118Shrs *       from this software without specific prior written permission.
198870Srgrimes *
20259118Shrs *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21259118Shrs *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22259118Shrs *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23259118Shrs *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24259118Shrs *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25259118Shrs *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26259118Shrs *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27259118Shrs *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28259118Shrs *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29259118Shrs *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30259118Shrs *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31259118Shrs *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321902Swollman */
3374462Salfred
3474462Salfred#if defined(LIBC_SCCS) && !defined(lint)
35136582Sobrienstatic char *sccsid2 = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
3692986Sobrienstatic char *sccsid = "@(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";
371902Swollman#endif
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
401902Swollman
411902Swollman/*
421902Swollman * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
431902Swollman * layer above tcp (for rpc's use).
441902Swollman *
451902Swollman * These routines interface XDRSTREAMS to a tcp/ip connection.
461902Swollman * There is a record marking layer between the xdr stream
471902Swollman * and the tcp transport level.  A record is composed on one or more
481902Swollman * record fragments.  A record fragment is a thirty-two bit header followed
491902Swollman * by n bytes of data, where n is contained in the header.  The header
501902Swollman * is represented as a htonl(u_long).  Thegh order bit encodes
511902Swollman * whether or not the fragment is the last fragment of the record
5274462Salfred * (1 => fragment is last, 0 => more fragments to follow.
531902Swollman * The other 31 bits encode the byte length of the fragment.
541902Swollman */
551902Swollman
5674462Salfred#include "namespace.h"
5774462Salfred#include <sys/types.h>
5874462Salfred
5974462Salfred#include <netinet/in.h>
6074462Salfred
6174462Salfred#include <err.h>
621902Swollman#include <stdio.h>
631902Swollman#include <stdlib.h>
6411669Sphk#include <string.h>
6574462Salfred
661902Swollman#include <rpc/types.h>
671902Swollman#include <rpc/xdr.h>
68109359Smbr#include <rpc/auth.h>
69109359Smbr#include <rpc/svc.h>
70109359Smbr#include <rpc/clnt.h>
71109359Smbr#include <sys/stddef.h>
7274462Salfred#include "un-namespace.h"
73111618Snectar#include "rpc_com.h"
741902Swollman
7592905Sobrienstatic bool_t	xdrrec_getlong(XDR *, long *);
7692905Sobrienstatic bool_t	xdrrec_putlong(XDR *, const long *);
7792905Sobrienstatic bool_t	xdrrec_getbytes(XDR *, char *, u_int);
781902Swollman
7992905Sobrienstatic bool_t	xdrrec_putbytes(XDR *, const char *, u_int);
8092905Sobrienstatic u_int	xdrrec_getpos(XDR *);
8192905Sobrienstatic bool_t	xdrrec_setpos(XDR *, u_int);
8292905Sobrienstatic int32_t *xdrrec_inline(XDR *, u_int);
8392905Sobrienstatic void	xdrrec_destroy(XDR *);
841902Swollman
8574462Salfredstatic const struct  xdr_ops xdrrec_ops = {
861902Swollman	xdrrec_getlong,
871902Swollman	xdrrec_putlong,
881902Swollman	xdrrec_getbytes,
891902Swollman	xdrrec_putbytes,
901902Swollman	xdrrec_getpos,
911902Swollman	xdrrec_setpos,
921902Swollman	xdrrec_inline,
931902Swollman	xdrrec_destroy
941902Swollman};
951902Swollman
961902Swollman/*
971902Swollman * A record is composed of one or more record fragments.
98109359Smbr * A record fragment is a four-byte header followed by zero to
991902Swollman * 2**32-1 bytes.  The header is treated as a long unsigned and is
1001902Swollman * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
1011902Swollman * are a byte count of the fragment.  The highest order bit is a boolean:
1021902Swollman * 1 => this fragment is the last fragment of the record,
1031902Swollman * 0 => this fragment is followed by more fragment(s).
1041902Swollman *
1051902Swollman * The fragment/record machinery is not general;  it is constructed to
1061902Swollman * meet the needs of xdr and rpc based on tcp.
1071902Swollman */
1081902Swollman
109258780Seadler#define LAST_FRAG ((u_int32_t)(1U << 31))
1101902Swollman
1111902Swollmantypedef struct rec_strm {
11274462Salfred	char *tcp_handle;
1131902Swollman	/*
1141902Swollman	 * out-goung bits
1151902Swollman	 */
11695658Sdes	int (*writeit)(void *, void *, int);
11774462Salfred	char *out_base;	/* output buffer (points to frag header) */
11874462Salfred	char *out_finger;	/* next output position */
11974462Salfred	char *out_boundry;	/* data cannot up to this address */
12074462Salfred	u_int32_t *frag_header;	/* beginning of curren fragment */
1211902Swollman	bool_t frag_sent;	/* true if buffer sent in middle of record */
1221902Swollman	/*
1231902Swollman	 * in-coming bits
1241902Swollman	 */
12595658Sdes	int (*readit)(void *, void *, int);
1261902Swollman	u_long in_size;	/* fixed size of the input buffer */
12774462Salfred	char *in_base;
12874462Salfred	char *in_finger;	/* location of next byte to be had */
12974462Salfred	char *in_boundry;	/* can read up to this location */
1301902Swollman	long fbtbc;		/* fragment bytes to be consumed */
1311902Swollman	bool_t last_frag;
1321902Swollman	u_int sendsize;
1331902Swollman	u_int recvsize;
134109359Smbr
135109359Smbr	bool_t nonblock;
136109359Smbr	bool_t in_haveheader;
137109359Smbr	u_int32_t in_header;
138109359Smbr	char *in_hdrp;
139109359Smbr	int in_hdrlen;
140109359Smbr	int in_reclen;
141109359Smbr	int in_received;
142109359Smbr	int in_maxrec;
1431902Swollman} RECSTREAM;
1441902Swollman
14592905Sobrienstatic u_int	fix_buf_size(u_int);
14692905Sobrienstatic bool_t	flush_out(RECSTREAM *, bool_t);
14792905Sobrienstatic bool_t	fill_input_buf(RECSTREAM *);
14892905Sobrienstatic bool_t	get_input_bytes(RECSTREAM *, char *, int);
14992905Sobrienstatic bool_t	set_input_fragment(RECSTREAM *);
15092905Sobrienstatic bool_t	skip_input_bytes(RECSTREAM *, long);
151109359Smbrstatic bool_t	realloc_stream(RECSTREAM *, int);
1521902Swollman
15374462Salfred
1541902Swollman/*
1551902Swollman * Create an xdr handle for xdrrec
1561902Swollman * xdrrec_create fills in xdrs.  Sendsize and recvsize are
1571902Swollman * send and recv buffer sizes (0 => use default).
1581902Swollman * tcp_handle is an opaque handle that is passed as the first parameter to
1591902Swollman * the procedures readit and writeit.  Readit and writeit are read and
1601902Swollman * write respectively.   They are like the system
1611902Swollman * calls expect that they take an opaque handle rather than an fd.
1621902Swollman */
1631902Swollmanvoid
164283833Srodrigcxdrrec_create(XDR *xdrs, u_int sendsize, u_int recvsize, void *tcp_handle,
165283833Srodrigc    int (*readit)(void *, void *, int), int (*writeit)(void *, void *, int))
166283833Srodrigc/*
167283833Srodrigc *	XDR *xdrs;
168283833Srodrigc *	u_int sendsize;
169283833Srodrigc *	u_int recvsize;
170283833Srodrigc *	void *tcp_handle;
171283833Srodrigc *	// like read, but pass it a tcp_handle, not sock
172283833Srodrigc *	int (*readit)(void *, void *, int);
173283833Srodrigc *	// like write, but pass it a tcp_handle, not sock
174283833Srodrigc *	int (*writeit)(void *, void *, int);
175283833Srodrigc */
1761902Swollman{
17774462Salfred	RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
1781902Swollman
1791902Swollman	if (rstrm == NULL) {
18074462Salfred		warnx("xdrrec_create: out of memory");
18174462Salfred		/*
18274462Salfred		 *  This is bad.  Should rework xdrrec_create to
1831902Swollman		 *  return a handle, and in this case return NULL
1841902Swollman		 */
1851902Swollman		return;
1861902Swollman	}
1871902Swollman	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
188109359Smbr	rstrm->out_base = mem_alloc(rstrm->sendsize);
189109359Smbr	if (rstrm->out_base == NULL) {
190109359Smbr		warnx("xdrrec_create: out of memory");
191109359Smbr		mem_free(rstrm, sizeof(RECSTREAM));
192109359Smbr		return;
193109359Smbr	}
1941902Swollman	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
195109359Smbr	rstrm->in_base = mem_alloc(recvsize);
196109359Smbr	if (rstrm->in_base == NULL) {
19774462Salfred		warnx("xdrrec_create: out of memory");
198109359Smbr		mem_free(rstrm->out_base, sendsize);
199109359Smbr		mem_free(rstrm, sizeof(RECSTREAM));
2001902Swollman		return;
2011902Swollman	}
2021902Swollman	/*
2031902Swollman	 * now the rest ...
2041902Swollman	 */
2051902Swollman	xdrs->x_ops = &xdrrec_ops;
20674462Salfred	xdrs->x_private = rstrm;
2071902Swollman	rstrm->tcp_handle = tcp_handle;
2081902Swollman	rstrm->readit = readit;
2091902Swollman	rstrm->writeit = writeit;
2101902Swollman	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
21174462Salfred	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
21221062Speter	rstrm->out_finger += sizeof(u_int32_t);
2131902Swollman	rstrm->out_boundry += sendsize;
2141902Swollman	rstrm->frag_sent = FALSE;
2151902Swollman	rstrm->in_size = recvsize;
2161902Swollman	rstrm->in_boundry = rstrm->in_base;
2171902Swollman	rstrm->in_finger = (rstrm->in_boundry += recvsize);
2181902Swollman	rstrm->fbtbc = 0;
2191902Swollman	rstrm->last_frag = TRUE;
220109359Smbr	rstrm->in_haveheader = FALSE;
221109359Smbr	rstrm->in_hdrlen = 0;
222109359Smbr	rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
223109359Smbr	rstrm->nonblock = FALSE;
224109359Smbr	rstrm->in_reclen = 0;
225109359Smbr	rstrm->in_received = 0;
2261902Swollman}
2271902Swollman
2281902Swollman
2291902Swollman/*
2301902Swollman * The reoutines defined below are the xdr ops which will go into the
2311902Swollman * xdr handle filled in by xdrrec_create.
2321902Swollman */
2331902Swollman
2341902Swollmanstatic bool_t
235283833Srodrigcxdrrec_getlong(XDR *xdrs, long *lp)
2361902Swollman{
23774462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
23874462Salfred	int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
23921062Speter	int32_t mylong;
2401902Swollman
2411902Swollman	/* first try the inline, fast case */
24221062Speter	if ((rstrm->fbtbc >= sizeof(int32_t)) &&
24321062Speter		(((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
24421062Speter		*lp = (long)ntohl((u_int32_t)(*buflp));
24521062Speter		rstrm->fbtbc -= sizeof(int32_t);
24621062Speter		rstrm->in_finger += sizeof(int32_t);
2471902Swollman	} else {
24874462Salfred		if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
24974462Salfred		    sizeof(int32_t)))
2501902Swollman			return (FALSE);
25121062Speter		*lp = (long)ntohl((u_int32_t)mylong);
2521902Swollman	}
2531902Swollman	return (TRUE);
2541902Swollman}
2551902Swollman
2561902Swollmanstatic bool_t
257283833Srodrigcxdrrec_putlong(XDR *xdrs, const long *lp)
2581902Swollman{
25974462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
26074462Salfred	int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
2611902Swollman
26221062Speter	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
2631902Swollman		/*
2641902Swollman		 * this case should almost never happen so the code is
2651902Swollman		 * inefficient
2661902Swollman		 */
26721062Speter		rstrm->out_finger -= sizeof(int32_t);
2681902Swollman		rstrm->frag_sent = TRUE;
2691902Swollman		if (! flush_out(rstrm, FALSE))
2701902Swollman			return (FALSE);
27174462Salfred		dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
27221062Speter		rstrm->out_finger += sizeof(int32_t);
2731902Swollman	}
27421062Speter	*dest_lp = (int32_t)htonl((u_int32_t)(*lp));
2751902Swollman	return (TRUE);
2761902Swollman}
2771902Swollman
2781902Swollmanstatic bool_t  /* must manage buffers, fragments, and records */
279283833Srodrigcxdrrec_getbytes(XDR *xdrs, char *addr, u_int len)
2801902Swollman{
28174462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
28274462Salfred	int current;
2831902Swollman
2841902Swollman	while (len > 0) {
28574462Salfred		current = (int)rstrm->fbtbc;
2861902Swollman		if (current == 0) {
2871902Swollman			if (rstrm->last_frag)
2881902Swollman				return (FALSE);
2891902Swollman			if (! set_input_fragment(rstrm))
2901902Swollman				return (FALSE);
2911902Swollman			continue;
2921902Swollman		}
2931902Swollman		current = (len < current) ? len : current;
2941902Swollman		if (! get_input_bytes(rstrm, addr, current))
2951902Swollman			return (FALSE);
29674462Salfred		addr += current;
2971902Swollman		rstrm->fbtbc -= current;
2981902Swollman		len -= current;
2991902Swollman	}
3001902Swollman	return (TRUE);
3011902Swollman}
3021902Swollman
3031902Swollmanstatic bool_t
304283833Srodrigcxdrrec_putbytes(XDR *xdrs, const char *addr, u_int len)
3051902Swollman{
30674462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
30774462Salfred	size_t current;
3081902Swollman
3091902Swollman	while (len > 0) {
31074462Salfred		current = (size_t)((u_long)rstrm->out_boundry -
31174462Salfred		    (u_long)rstrm->out_finger);
3121902Swollman		current = (len < current) ? len : current;
31374462Salfred		memmove(rstrm->out_finger, addr, current);
3141902Swollman		rstrm->out_finger += current;
3151902Swollman		addr += current;
3161902Swollman		len -= current;
3171902Swollman		if (rstrm->out_finger == rstrm->out_boundry) {
3181902Swollman			rstrm->frag_sent = TRUE;
3191902Swollman			if (! flush_out(rstrm, FALSE))
3201902Swollman				return (FALSE);
3211902Swollman		}
3221902Swollman	}
3231902Swollman	return (TRUE);
3241902Swollman}
3251902Swollman
3261902Swollmanstatic u_int
327283833Srodrigcxdrrec_getpos(XDR *xdrs)
3281902Swollman{
32974462Salfred	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
33074462Salfred	off_t pos;
3311902Swollman
33274462Salfred	pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
333181344Sdfr	if (pos == -1)
334181344Sdfr		pos = 0;
335181344Sdfr	switch (xdrs->x_op) {
3361902Swollman
337181344Sdfr	case XDR_ENCODE:
338181344Sdfr		pos += rstrm->out_finger - rstrm->out_base;
339181344Sdfr		break;
3401902Swollman
341181344Sdfr	case XDR_DECODE:
342181344Sdfr		pos -= rstrm->in_boundry - rstrm->in_finger;
343181344Sdfr		break;
3441902Swollman
345181344Sdfr	default:
346181344Sdfr		pos = (off_t) -1;
347181344Sdfr		break;
348181344Sdfr	}
3491902Swollman	return ((u_int) pos);
3501902Swollman}
3511902Swollman
3521902Swollmanstatic bool_t
353283833Srodrigcxdrrec_setpos(XDR *xdrs, u_int pos)
3541902Swollman{
35574462Salfred	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
3561902Swollman	u_int currpos = xdrrec_getpos(xdrs);
3571902Swollman	int delta = currpos - pos;
35874462Salfred	char *newpos;
3591902Swollman
3601902Swollman	if ((int)currpos != -1)
3611902Swollman		switch (xdrs->x_op) {
3621902Swollman
3631902Swollman		case XDR_ENCODE:
3641902Swollman			newpos = rstrm->out_finger - delta;
36574462Salfred			if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
3661902Swollman				(newpos < rstrm->out_boundry)) {
3671902Swollman				rstrm->out_finger = newpos;
3681902Swollman				return (TRUE);
3691902Swollman			}
3701902Swollman			break;
3711902Swollman
3721902Swollman		case XDR_DECODE:
3731902Swollman			newpos = rstrm->in_finger - delta;
3741902Swollman			if ((delta < (int)(rstrm->fbtbc)) &&
3751902Swollman				(newpos <= rstrm->in_boundry) &&
3761902Swollman				(newpos >= rstrm->in_base)) {
3771902Swollman				rstrm->in_finger = newpos;
3781902Swollman				rstrm->fbtbc -= delta;
3791902Swollman				return (TRUE);
3801902Swollman			}
3811902Swollman			break;
38274462Salfred
38374462Salfred		case XDR_FREE:
38474462Salfred			break;
3851902Swollman		}
3861902Swollman	return (FALSE);
3871902Swollman}
3881902Swollman
38921062Speterstatic int32_t *
390283833Srodrigcxdrrec_inline(XDR *xdrs, u_int len)
3911902Swollman{
39274462Salfred	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
39374462Salfred	int32_t *buf = NULL;
3941902Swollman
3951902Swollman	switch (xdrs->x_op) {
3961902Swollman
3971902Swollman	case XDR_ENCODE:
3981902Swollman		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
39974462Salfred			buf = (int32_t *)(void *)rstrm->out_finger;
4001902Swollman			rstrm->out_finger += len;
4011902Swollman		}
4021902Swollman		break;
4031902Swollman
4041902Swollman	case XDR_DECODE:
4051902Swollman		if ((len <= rstrm->fbtbc) &&
4061902Swollman			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
40774462Salfred			buf = (int32_t *)(void *)rstrm->in_finger;
4081902Swollman			rstrm->fbtbc -= len;
4091902Swollman			rstrm->in_finger += len;
4101902Swollman		}
4111902Swollman		break;
41274462Salfred
41374462Salfred	case XDR_FREE:
41474462Salfred		break;
4151902Swollman	}
4161902Swollman	return (buf);
4171902Swollman}
4181902Swollman
4191902Swollmanstatic void
420283833Srodrigcxdrrec_destroy(XDR *xdrs)
4211902Swollman{
42274462Salfred	RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
4231902Swollman
424109359Smbr	mem_free(rstrm->out_base, rstrm->sendsize);
425109359Smbr	mem_free(rstrm->in_base, rstrm->recvsize);
42674462Salfred	mem_free(rstrm, sizeof(RECSTREAM));
4271902Swollman}
4281902Swollman
4291902Swollman
4301902Swollman/*
4311902Swollman * Exported routines to manage xdr records
4321902Swollman */
4331902Swollman
4341902Swollman/*
4351902Swollman * Before reading (deserializing from the stream, one should always call
4361902Swollman * this procedure to guarantee proper record alignment.
4371902Swollman */
4381902Swollmanbool_t
439283833Srodrigcxdrrec_skiprecord(XDR *xdrs)
4401902Swollman{
44174462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
442109359Smbr	enum xprt_stat xstat;
4431902Swollman
444109359Smbr	if (rstrm->nonblock) {
445109359Smbr		if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
446109359Smbr			rstrm->fbtbc = 0;
447109359Smbr			return TRUE;
448109359Smbr		}
449109359Smbr		if (rstrm->in_finger == rstrm->in_boundry &&
450109359Smbr		    xstat == XPRT_MOREREQS) {
451109359Smbr			rstrm->fbtbc = 0;
452109359Smbr			return TRUE;
453109359Smbr		}
454109359Smbr		return FALSE;
455109359Smbr	}
456109359Smbr
4571902Swollman	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
4581902Swollman		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
4591902Swollman			return (FALSE);
4601902Swollman		rstrm->fbtbc = 0;
4611902Swollman		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
4621902Swollman			return (FALSE);
4631902Swollman	}
4641902Swollman	rstrm->last_frag = FALSE;
4651902Swollman	return (TRUE);
4661902Swollman}
4671902Swollman
4681902Swollman/*
46974462Salfred * Look ahead function.
4708870Srgrimes * Returns TRUE iff there is no more input in the buffer
4711902Swollman * after consuming the rest of the current record.
4721902Swollman */
4731902Swollmanbool_t
474283833Srodrigcxdrrec_eof(XDR *xdrs)
4751902Swollman{
47674462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
4771902Swollman
4781902Swollman	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
4791902Swollman		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
4801902Swollman			return (TRUE);
4811902Swollman		rstrm->fbtbc = 0;
4821902Swollman		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
4831902Swollman			return (TRUE);
4841902Swollman	}
4851902Swollman	if (rstrm->in_finger == rstrm->in_boundry)
4861902Swollman		return (TRUE);
4871902Swollman	return (FALSE);
4881902Swollman}
4891902Swollman
4901902Swollman/*
4911902Swollman * The client must tell the package when an end-of-record has occurred.
4921902Swollman * The second paraemters tells whether the record should be flushed to the
4931902Swollman * (output) tcp stream.  (This let's the package support batched or
4941902Swollman * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
4951902Swollman */
4961902Swollmanbool_t
497283833Srodrigcxdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
4981902Swollman{
49974462Salfred	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
50074462Salfred	u_long len;  /* fragment length */
5011902Swollman
5021902Swollman	if (sendnow || rstrm->frag_sent ||
50321062Speter		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
5041902Swollman		(u_long)rstrm->out_boundry)) {
5051902Swollman		rstrm->frag_sent = FALSE;
5061902Swollman		return (flush_out(rstrm, TRUE));
5071902Swollman	}
5081902Swollman	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
50921062Speter	   sizeof(u_int32_t);
51074462Salfred	*(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
51174462Salfred	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
51221062Speter	rstrm->out_finger += sizeof(u_int32_t);
5131902Swollman	return (TRUE);
5141902Swollman}
5151902Swollman
516109359Smbr/*
517109359Smbr * Fill the stream buffer with a record for a non-blocking connection.
518109359Smbr * Return true if a record is available in the buffer, false if not.
519109359Smbr */
520109359Smbrbool_t
521283833Srodrigc__xdrrec_getrec(XDR *xdrs, enum xprt_stat *statp, bool_t expectdata)
522109359Smbr{
523109359Smbr	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
524109359Smbr	ssize_t n;
525109359Smbr	int fraglen;
5261902Swollman
527109359Smbr	if (!rstrm->in_haveheader) {
528109359Smbr		n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
529109359Smbr		    (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
530109359Smbr		if (n == 0) {
531109359Smbr			*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
532109359Smbr			return FALSE;
533109359Smbr		}
534109359Smbr		if (n < 0) {
535109359Smbr			*statp = XPRT_DIED;
536109359Smbr			return FALSE;
537109359Smbr		}
538109359Smbr		rstrm->in_hdrp += n;
539109359Smbr		rstrm->in_hdrlen += n;
540109359Smbr		if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
541109359Smbr			*statp = XPRT_MOREREQS;
542109359Smbr			return FALSE;
543109359Smbr		}
544109359Smbr		rstrm->in_header = ntohl(rstrm->in_header);
545109359Smbr		fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
546109359Smbr		if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
547109359Smbr		    (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
548109359Smbr			*statp = XPRT_DIED;
549109359Smbr			return FALSE;
550109359Smbr		}
551109359Smbr		rstrm->in_reclen += fraglen;
552109359Smbr		if (rstrm->in_reclen > rstrm->recvsize)
553109359Smbr			realloc_stream(rstrm, rstrm->in_reclen);
554109359Smbr		if (rstrm->in_header & LAST_FRAG) {
555109359Smbr			rstrm->in_header &= ~LAST_FRAG;
556109359Smbr			rstrm->last_frag = TRUE;
557109359Smbr		}
558177736Sdfr		/*
559177736Sdfr		 * We can only reasonably expect to read once from a
560177736Sdfr		 * non-blocking stream. Reading the fragment header
561177736Sdfr		 * may have drained the stream.
562177736Sdfr		 */
563177736Sdfr		expectdata = FALSE;
564109359Smbr	}
565109359Smbr
566109359Smbr	n =  rstrm->readit(rstrm->tcp_handle,
567109359Smbr	    rstrm->in_base + rstrm->in_received,
568109359Smbr	    (rstrm->in_reclen - rstrm->in_received));
569109359Smbr
570109359Smbr	if (n < 0) {
571109359Smbr		*statp = XPRT_DIED;
572109359Smbr		return FALSE;
573109359Smbr	}
574109359Smbr
575109359Smbr	if (n == 0) {
576109359Smbr		*statp = expectdata ? XPRT_DIED : XPRT_IDLE;
577109359Smbr		return FALSE;
578109359Smbr	}
579109359Smbr
580109359Smbr	rstrm->in_received += n;
581109359Smbr
582109359Smbr	if (rstrm->in_received == rstrm->in_reclen) {
583109359Smbr		rstrm->in_haveheader = FALSE;
584109359Smbr		rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
585109359Smbr		rstrm->in_hdrlen = 0;
586109359Smbr		if (rstrm->last_frag) {
587109359Smbr			rstrm->fbtbc = rstrm->in_reclen;
588109359Smbr			rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
589109359Smbr			rstrm->in_finger = rstrm->in_base;
590109950Smbr			rstrm->in_reclen = rstrm->in_received = 0;
591109359Smbr			*statp = XPRT_MOREREQS;
592109359Smbr			return TRUE;
593109359Smbr		}
594109359Smbr	}
595109359Smbr
596109359Smbr	*statp = XPRT_MOREREQS;
597109359Smbr	return FALSE;
598109359Smbr}
599109359Smbr
600109359Smbrbool_t
601283833Srodrigc__xdrrec_setnonblock(XDR *xdrs, int maxrec)
602109359Smbr{
603109359Smbr	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
604109359Smbr
605109359Smbr	rstrm->nonblock = TRUE;
606109359Smbr	if (maxrec == 0)
607109359Smbr		maxrec = rstrm->recvsize;
608109359Smbr	rstrm->in_maxrec = maxrec;
609109359Smbr	return TRUE;
610109359Smbr}
611109359Smbr
6121902Swollman/*
6131902Swollman * Internal useful routines
6141902Swollman */
6151902Swollmanstatic bool_t
616283833Srodrigcflush_out(RECSTREAM *rstrm, bool_t eor)
6171902Swollman{
61874462Salfred	u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
61974462Salfred	u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
62074462Salfred		(u_long)(rstrm->frag_header) - sizeof(u_int32_t));
6211902Swollman
6221902Swollman	*(rstrm->frag_header) = htonl(len | eormask);
62374462Salfred	len = (u_int32_t)((u_long)(rstrm->out_finger) -
62474462Salfred	    (u_long)(rstrm->out_base));
6251902Swollman	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
6261902Swollman		!= (int)len)
6271902Swollman		return (FALSE);
62874462Salfred	rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
62974462Salfred	rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
6301902Swollman	return (TRUE);
6311902Swollman}
6321902Swollman
6331902Swollmanstatic bool_t  /* knows nothing about records!  Only about input buffers */
634283833Srodrigcfill_input_buf(RECSTREAM *rstrm)
6351902Swollman{
63674462Salfred	char *where;
63774462Salfred	u_int32_t i;
63874462Salfred	int len;
6391902Swollman
640109359Smbr	if (rstrm->nonblock)
641109359Smbr		return FALSE;
642109359Smbr
6431902Swollman	where = rstrm->in_base;
64474462Salfred	i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
6451902Swollman	where += i;
64674462Salfred	len = (u_int32_t)(rstrm->in_size - i);
6471902Swollman	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
6481902Swollman		return (FALSE);
6491902Swollman	rstrm->in_finger = where;
6501902Swollman	where += len;
6511902Swollman	rstrm->in_boundry = where;
6521902Swollman	return (TRUE);
6531902Swollman}
6541902Swollman
6551902Swollmanstatic bool_t  /* knows nothing about records!  Only about input buffers */
656283833Srodrigcget_input_bytes(RECSTREAM *rstrm, char *addr, int len)
6571902Swollman{
65874462Salfred	size_t current;
6591902Swollman
660109950Smbr	if (rstrm->nonblock) {
661109950Smbr		if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
662109950Smbr			return FALSE;
663109950Smbr		memcpy(addr, rstrm->in_finger, (size_t)len);
664109950Smbr		rstrm->in_finger += len;
665109950Smbr		return TRUE;
666109950Smbr	}
667109950Smbr
6681902Swollman	while (len > 0) {
66974462Salfred		current = (size_t)((long)rstrm->in_boundry -
67074462Salfred		    (long)rstrm->in_finger);
6711902Swollman		if (current == 0) {
6721902Swollman			if (! fill_input_buf(rstrm))
6731902Swollman				return (FALSE);
6741902Swollman			continue;
6751902Swollman		}
6761902Swollman		current = (len < current) ? len : current;
67774462Salfred		memmove(addr, rstrm->in_finger, current);
6781902Swollman		rstrm->in_finger += current;
6791902Swollman		addr += current;
6801902Swollman		len -= current;
6811902Swollman	}
6821902Swollman	return (TRUE);
6831902Swollman}
6841902Swollman
6851902Swollmanstatic bool_t  /* next two bytes of the input stream are treated as a header */
686283833Srodrigcset_input_fragment(RECSTREAM *rstrm)
6871902Swollman{
68821062Speter	u_int32_t header;
6891902Swollman
690115363Smbr	if (rstrm->nonblock)
691115363Smbr		return FALSE;
69274462Salfred	if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
6931902Swollman		return (FALSE);
69474462Salfred	header = ntohl(header);
6951902Swollman	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
69636087Swpaul	/*
69736087Swpaul	 * Sanity check. Try not to accept wildly incorrect
69836257Swpaul	 * record sizes. Unfortunately, the only record size
69936257Swpaul	 * we can positively identify as being 'wildly incorrect'
70036257Swpaul	 * is zero. Ridiculously large record sizes may look wrong,
70136257Swpaul	 * but we don't have any way to be certain that they aren't
70236257Swpaul	 * what the client actually intended to send us.
70336087Swpaul	 */
70456273Swpaul	if (header == 0)
70536087Swpaul		return(FALSE);
7061902Swollman	rstrm->fbtbc = header & (~LAST_FRAG);
7071902Swollman	return (TRUE);
7081902Swollman}
7091902Swollman
7101902Swollmanstatic bool_t  /* consumes input bytes; knows nothing about records! */
711283833Srodrigcskip_input_bytes(RECSTREAM *rstrm, long cnt)
7121902Swollman{
71374462Salfred	u_int32_t current;
7141902Swollman
7151902Swollman	while (cnt > 0) {
71674462Salfred		current = (size_t)((long)rstrm->in_boundry -
71774462Salfred		    (long)rstrm->in_finger);
7181902Swollman		if (current == 0) {
7191902Swollman			if (! fill_input_buf(rstrm))
7201902Swollman				return (FALSE);
7211902Swollman			continue;
7221902Swollman		}
72374462Salfred		current = (u_int32_t)((cnt < current) ? cnt : current);
7241902Swollman		rstrm->in_finger += current;
7251902Swollman		cnt -= current;
7261902Swollman	}
7271902Swollman	return (TRUE);
7281902Swollman}
7291902Swollman
7301902Swollmanstatic u_int
731283833Srodrigcfix_buf_size(u_int s)
7321902Swollman{
7331902Swollman
7341902Swollman	if (s < 100)
7351902Swollman		s = 4000;
7361902Swollman	return (RNDUP(s));
7371902Swollman}
738109359Smbr
739109359Smbr/*
740109359Smbr * Reallocate the input buffer for a non-block stream.
741109359Smbr */
742109359Smbrstatic bool_t
743283833Srodrigcrealloc_stream(RECSTREAM *rstrm, int size)
744109359Smbr{
745109359Smbr	ptrdiff_t diff;
746109359Smbr	char *buf;
747109359Smbr
748109359Smbr	if (size > rstrm->recvsize) {
749109359Smbr		buf = realloc(rstrm->in_base, (size_t)size);
750109359Smbr		if (buf == NULL)
751109359Smbr			return FALSE;
752109359Smbr		diff = buf - rstrm->in_base;
753109359Smbr		rstrm->in_finger += diff;
754109359Smbr		rstrm->in_base = buf;
755109359Smbr		rstrm->in_boundry = buf + size;
756109359Smbr		rstrm->recvsize = size;
757109359Smbr		rstrm->in_size = size;
758109359Smbr	}
759109359Smbr
760109359Smbr	return TRUE;
761109359Smbr}
762