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