Blame sunrpc/xdr_rec.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
Packit 6c4009
 * layer above tcp (for rpc's use).
Packit 6c4009
 *
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 *
Packit 6c4009
 * These routines interface XDRSTREAMS to a tcp/ip connection.
Packit 6c4009
 * There is a record marking layer between the xdr stream
Packit 6c4009
 * and the tcp transport level.  A record is composed on one or more
Packit 6c4009
 * record fragments.  A record fragment is a thirty-two bit header followed
Packit 6c4009
 * by n bytes of data, where n is contained in the header.  The header
Packit 6c4009
 * is represented as a htonl(u_long).  The high order bit encodes
Packit 6c4009
 * whether or not the fragment is the last fragment of the record
Packit 6c4009
 * (1 => fragment is last, 0 => more fragments to follow.
Packit 6c4009
 * The other 31 bits encode the byte length of the fragment.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <libio/iolibio.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
static bool_t xdrrec_getlong (XDR *, long *);
Packit 6c4009
static bool_t xdrrec_putlong (XDR *, const long *);
Packit 6c4009
static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
Packit 6c4009
static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
Packit 6c4009
static u_int xdrrec_getpos (const XDR *);
Packit 6c4009
static bool_t xdrrec_setpos (XDR *, u_int);
Packit 6c4009
static int32_t *xdrrec_inline (XDR *, u_int);
Packit 6c4009
static void xdrrec_destroy (XDR *);
Packit 6c4009
static bool_t xdrrec_getint32 (XDR *, int32_t *);
Packit 6c4009
static bool_t xdrrec_putint32 (XDR *, const int32_t *);
Packit 6c4009
Packit 6c4009
static const struct xdr_ops xdrrec_ops = {
Packit 6c4009
  xdrrec_getlong,
Packit 6c4009
  xdrrec_putlong,
Packit 6c4009
  xdrrec_getbytes,
Packit 6c4009
  xdrrec_putbytes,
Packit 6c4009
  xdrrec_getpos,
Packit 6c4009
  xdrrec_setpos,
Packit 6c4009
  xdrrec_inline,
Packit 6c4009
  xdrrec_destroy,
Packit 6c4009
  xdrrec_getint32,
Packit 6c4009
  xdrrec_putint32
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * A record is composed of one or more record fragments.
Packit 6c4009
 * A record fragment is a two-byte header followed by zero to
Packit 6c4009
 * 2**32-1 bytes.  The header is treated as a long unsigned and is
Packit 6c4009
 * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
Packit 6c4009
 * are a byte count of the fragment.  The highest order bit is a boolean:
Packit 6c4009
 * 1 => this fragment is the last fragment of the record,
Packit 6c4009
 * 0 => this fragment is followed by more fragment(s).
Packit 6c4009
 *
Packit 6c4009
 * The fragment/record machinery is not general;  it is constructed to
Packit 6c4009
 * meet the needs of xdr and rpc based on tcp.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#define LAST_FRAG (1UL << 31)
Packit 6c4009
Packit 6c4009
typedef struct rec_strm
Packit 6c4009
  {
Packit 6c4009
    caddr_t tcp_handle;
Packit 6c4009
    caddr_t the_buffer;
Packit 6c4009
    /*
Packit 6c4009
     * out-going bits
Packit 6c4009
     */
Packit 6c4009
    int (*writeit) (char *, char *, int);
Packit 6c4009
    caddr_t out_base;		/* output buffer (points to frag header) */
Packit 6c4009
    caddr_t out_finger;		/* next output position */
Packit 6c4009
    caddr_t out_boundry;	/* data cannot up to this address */
Packit 6c4009
    uint32_t *frag_header;	/* beginning of curren fragment */
Packit 6c4009
    bool_t frag_sent;		/* true if buffer sent in middle of record */
Packit 6c4009
    /*
Packit 6c4009
     * in-coming bits
Packit 6c4009
     */
Packit 6c4009
    int (*readit) (char *, char *, int);
Packit 6c4009
    u_long in_size;		/* fixed size of the input buffer */
Packit 6c4009
    caddr_t in_base;
Packit 6c4009
    caddr_t in_finger;		/* location of next byte to be had */
Packit 6c4009
    caddr_t in_boundry;		/* can read up to this location */
Packit 6c4009
    long fbtbc;			/* fragment bytes to be consumed */
Packit 6c4009
    bool_t last_frag;
Packit 6c4009
    u_int sendsize;
Packit 6c4009
    u_int recvsize;
Packit 6c4009
  }
Packit 6c4009
RECSTREAM;
Packit 6c4009
Packit 6c4009
static u_int fix_buf_size (u_int);
Packit 6c4009
static bool_t skip_input_bytes (RECSTREAM *, long);
Packit 6c4009
static bool_t flush_out (RECSTREAM *, bool_t);
Packit 6c4009
static bool_t set_input_fragment (RECSTREAM *);
Packit 6c4009
static bool_t get_input_bytes (RECSTREAM *, caddr_t, int);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Create an xdr handle for xdrrec
Packit 6c4009
 * xdrrec_create fills in xdrs.  Sendsize and recvsize are
Packit 6c4009
 * send and recv buffer sizes (0 => use default).
Packit 6c4009
 * tcp_handle is an opaque handle that is passed as the first parameter to
Packit 6c4009
 * the procedures readit and writeit.  Readit and writeit are read and
Packit 6c4009
 * write respectively.   They are like the system
Packit 6c4009
 * calls expect that they take an opaque handle rather than an fd.
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
xdrrec_create (XDR *xdrs, u_int sendsize,
Packit 6c4009
	       u_int recvsize, caddr_t tcp_handle,
Packit 6c4009
	       int (*readit) (char *, char *, int),
Packit 6c4009
	       int (*writeit) (char *, char *, int))
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
Packit 6c4009
  caddr_t tmp;
Packit 6c4009
  char *buf;
Packit 6c4009
Packit 6c4009
  sendsize = fix_buf_size (sendsize);
Packit 6c4009
  recvsize = fix_buf_size (recvsize);
Packit 6c4009
  buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
Packit 6c4009
Packit 6c4009
  if (rstrm == NULL || buf == NULL)
Packit 6c4009
    {
Packit 6c4009
      (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
Packit 6c4009
      mem_free (rstrm, sizeof (RECSTREAM));
Packit 6c4009
      mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
Packit 6c4009
      /*
Packit 6c4009
       *  This is bad.  Should rework xdrrec_create to
Packit 6c4009
       *  return a handle, and in this case return NULL
Packit 6c4009
       */
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  /*
Packit 6c4009
   * adjust sizes and allocate buffer quad byte aligned
Packit 6c4009
   */
Packit 6c4009
  rstrm->sendsize = sendsize;
Packit 6c4009
  rstrm->recvsize = recvsize;
Packit 6c4009
  rstrm->the_buffer = buf;
Packit 6c4009
  tmp = rstrm->the_buffer;
Packit 6c4009
  if ((size_t)tmp % BYTES_PER_XDR_UNIT)
Packit 6c4009
    tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
Packit 6c4009
  rstrm->out_base = tmp;
Packit 6c4009
  rstrm->in_base = tmp + sendsize;
Packit 6c4009
  /*
Packit 6c4009
   * now the rest ...
Packit 6c4009
   */
Packit 6c4009
  /* We have to add the cast since the `struct xdr_ops' in `struct XDR'
Packit 6c4009
     is not `const'.  */
Packit 6c4009
  xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
Packit 6c4009
  xdrs->x_private = (caddr_t) rstrm;
Packit 6c4009
  rstrm->tcp_handle = tcp_handle;
Packit 6c4009
  rstrm->readit = readit;
Packit 6c4009
  rstrm->writeit = writeit;
Packit 6c4009
  rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
Packit 6c4009
  rstrm->frag_header = (uint32_t *) rstrm->out_base;
Packit 6c4009
  rstrm->out_finger += 4;
Packit 6c4009
  rstrm->out_boundry += sendsize;
Packit 6c4009
  rstrm->frag_sent = FALSE;
Packit 6c4009
  rstrm->in_size = recvsize;
Packit 6c4009
  rstrm->in_boundry = rstrm->in_base;
Packit 6c4009
  rstrm->in_finger = (rstrm->in_boundry += recvsize);
Packit 6c4009
  rstrm->fbtbc = 0;
Packit 6c4009
  rstrm->last_frag = TRUE;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrrec_create, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * The routines defined below are the xdr ops which will go into the
Packit 6c4009
 * xdr handle filled in by xdrrec_create.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_getlong (XDR *xdrs, long *lp)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  int32_t *buflp = (int32_t *) rstrm->in_finger;
Packit 6c4009
  int32_t mylong;
Packit 6c4009
Packit 6c4009
  /* first try the inline, fast case */
Packit 6c4009
  if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
Packit 6c4009
      rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
Packit 6c4009
    {
Packit 6c4009
      *lp = (int32_t) ntohl (*buflp);
Packit 6c4009
      rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
Packit 6c4009
      rstrm->in_finger += BYTES_PER_XDR_UNIT;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
Packit 6c4009
			    BYTES_PER_XDR_UNIT))
Packit 6c4009
	return FALSE;
Packit 6c4009
      *lp = (int32_t) ntohl (mylong);
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_putlong (XDR *xdrs, const long *lp)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  int32_t *dest_lp = (int32_t *) rstrm->out_finger;
Packit 6c4009
Packit 6c4009
  if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
Packit 6c4009
    {
Packit 6c4009
      /*
Packit 6c4009
       * this case should almost never happen so the code is
Packit 6c4009
       * inefficient
Packit 6c4009
       */
Packit 6c4009
      rstrm->out_finger -= BYTES_PER_XDR_UNIT;
Packit 6c4009
      rstrm->frag_sent = TRUE;
Packit 6c4009
      if (!flush_out (rstrm, FALSE))
Packit 6c4009
	return FALSE;
Packit 6c4009
      dest_lp = (int32_t *) rstrm->out_finger;
Packit 6c4009
      rstrm->out_finger += BYTES_PER_XDR_UNIT;
Packit 6c4009
    }
Packit 6c4009
  *dest_lp = htonl (*lp);
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t	   /* must manage buffers, fragments, and records */
Packit 6c4009
xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  u_int current;
Packit 6c4009
Packit 6c4009
  while (len > 0)
Packit 6c4009
    {
Packit 6c4009
      current = rstrm->fbtbc;
Packit 6c4009
      if (current == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (rstrm->last_frag)
Packit 6c4009
	    return FALSE;
Packit 6c4009
	  if (!set_input_fragment (rstrm))
Packit 6c4009
	    return FALSE;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      current = (len < current) ? len : current;
Packit 6c4009
      if (!get_input_bytes (rstrm, addr, current))
Packit 6c4009
	return FALSE;
Packit 6c4009
      addr += current;
Packit 6c4009
      rstrm->fbtbc -= current;
Packit 6c4009
      len -= current;
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  u_int current;
Packit 6c4009
Packit 6c4009
  while (len > 0)
Packit 6c4009
    {
Packit 6c4009
      current = rstrm->out_boundry - rstrm->out_finger;
Packit 6c4009
      current = (len < current) ? len : current;
Packit 6c4009
      memcpy (rstrm->out_finger, addr, current);
Packit 6c4009
      rstrm->out_finger += current;
Packit 6c4009
      addr += current;
Packit 6c4009
      len -= current;
Packit 6c4009
      if (rstrm->out_finger == rstrm->out_boundry && len > 0)
Packit 6c4009
	{
Packit 6c4009
	  rstrm->frag_sent = TRUE;
Packit 6c4009
	  if (!flush_out (rstrm, FALSE))
Packit 6c4009
	    return FALSE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static u_int
Packit 6c4009
xdrrec_getpos (const XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  long pos;
Packit 6c4009
Packit 6c4009
  pos = __lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
Packit 6c4009
  if (pos != -1)
Packit 6c4009
    switch (xdrs->x_op)
Packit 6c4009
      {
Packit 6c4009
Packit 6c4009
      case XDR_ENCODE:
Packit 6c4009
	pos += rstrm->out_finger - rstrm->out_base;
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case XDR_DECODE:
Packit 6c4009
	pos -= rstrm->in_boundry - rstrm->in_finger;
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      default:
Packit 6c4009
	pos = (u_int) - 1;
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
  return (u_int) pos;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_setpos (XDR *xdrs, u_int pos)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  u_int currpos = xdrrec_getpos (xdrs);
Packit 6c4009
  int delta = currpos - pos;
Packit 6c4009
  caddr_t newpos;
Packit 6c4009
Packit 6c4009
  if ((int) currpos != -1)
Packit 6c4009
    switch (xdrs->x_op)
Packit 6c4009
      {
Packit 6c4009
Packit 6c4009
      case XDR_ENCODE:
Packit 6c4009
	newpos = rstrm->out_finger - delta;
Packit 6c4009
	if (newpos > (caddr_t) rstrm->frag_header &&
Packit 6c4009
	    newpos < rstrm->out_boundry)
Packit 6c4009
	  {
Packit 6c4009
	    rstrm->out_finger = newpos;
Packit 6c4009
	    return TRUE;
Packit 6c4009
	  }
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case XDR_DECODE:
Packit 6c4009
	newpos = rstrm->in_finger - delta;
Packit 6c4009
	if ((delta < (int) (rstrm->fbtbc)) &&
Packit 6c4009
	    (newpos <= rstrm->in_boundry) &&
Packit 6c4009
	    (newpos >= rstrm->in_base))
Packit 6c4009
	  {
Packit 6c4009
	    rstrm->in_finger = newpos;
Packit 6c4009
	    rstrm->fbtbc -= delta;
Packit 6c4009
	    return TRUE;
Packit 6c4009
	  }
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      default:
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
  return FALSE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int32_t *
Packit 6c4009
xdrrec_inline (XDR *xdrs, u_int len)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  int32_t *buf = NULL;
Packit 6c4009
Packit 6c4009
  switch (xdrs->x_op)
Packit 6c4009
    {
Packit 6c4009
Packit 6c4009
    case XDR_ENCODE:
Packit 6c4009
      if ((rstrm->out_finger + len) <= rstrm->out_boundry)
Packit 6c4009
	{
Packit 6c4009
	  buf = (int32_t *) rstrm->out_finger;
Packit 6c4009
	  rstrm->out_finger += len;
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case XDR_DECODE:
Packit 6c4009
      if ((len <= rstrm->fbtbc) &&
Packit 6c4009
	  ((rstrm->in_finger + len) <= rstrm->in_boundry))
Packit 6c4009
	{
Packit 6c4009
	  buf = (int32_t *) rstrm->in_finger;
Packit 6c4009
	  rstrm->fbtbc -= len;
Packit 6c4009
	  rstrm->in_finger += len;
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
  return buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
xdrrec_destroy (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
Packit 6c4009
  mem_free (rstrm->the_buffer,
Packit 6c4009
	    rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
Packit 6c4009
  mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_getint32 (XDR *xdrs, int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  int32_t *bufip = (int32_t *) rstrm->in_finger;
Packit 6c4009
  int32_t mylong;
Packit 6c4009
Packit 6c4009
  /* first try the inline, fast case */
Packit 6c4009
  if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
Packit 6c4009
      rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
Packit 6c4009
    {
Packit 6c4009
      *ip = ntohl (*bufip);
Packit 6c4009
      rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
Packit 6c4009
      rstrm->in_finger += BYTES_PER_XDR_UNIT;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
Packit 6c4009
			    BYTES_PER_XDR_UNIT))
Packit 6c4009
	return FALSE;
Packit 6c4009
      *ip = ntohl (mylong);
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  int32_t *dest_ip = (int32_t *) rstrm->out_finger;
Packit 6c4009
Packit 6c4009
  if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
Packit 6c4009
    {
Packit 6c4009
      /*
Packit 6c4009
       * this case should almost never happen so the code is
Packit 6c4009
       * inefficient
Packit 6c4009
       */
Packit 6c4009
      rstrm->out_finger -= BYTES_PER_XDR_UNIT;
Packit 6c4009
      rstrm->frag_sent = TRUE;
Packit 6c4009
      if (!flush_out (rstrm, FALSE))
Packit 6c4009
	return FALSE;
Packit 6c4009
      dest_ip = (int32_t *) rstrm->out_finger;
Packit 6c4009
      rstrm->out_finger += BYTES_PER_XDR_UNIT;
Packit 6c4009
    }
Packit 6c4009
  *dest_ip = htonl (*ip);
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Exported routines to manage xdr records
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Before reading (deserializing from the stream, one should always call
Packit 6c4009
 * this procedure to guarantee proper record alignment.
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdrrec_skiprecord (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
Packit 6c4009
  while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
Packit 6c4009
    {
Packit 6c4009
      if (!skip_input_bytes (rstrm, rstrm->fbtbc))
Packit 6c4009
	return FALSE;
Packit 6c4009
      rstrm->fbtbc = 0;
Packit 6c4009
      if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
Packit 6c4009
	return FALSE;
Packit 6c4009
    }
Packit 6c4009
  rstrm->last_frag = FALSE;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrrec_skiprecord, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Lookahead function.
Packit 6c4009
 * Returns TRUE iff there is no more input in the buffer
Packit 6c4009
 * after consuming the rest of the current record.
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdrrec_eof (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
Packit 6c4009
  while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
Packit 6c4009
    {
Packit 6c4009
      if (!skip_input_bytes (rstrm, rstrm->fbtbc))
Packit 6c4009
	return TRUE;
Packit 6c4009
      rstrm->fbtbc = 0;
Packit 6c4009
      if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
Packit 6c4009
	return TRUE;
Packit 6c4009
    }
Packit 6c4009
  if (rstrm->in_finger == rstrm->in_boundry)
Packit 6c4009
    return TRUE;
Packit 6c4009
  return FALSE;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrrec_eof, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * The client must tell the package when an end-of-record has occurred.
Packit 6c4009
 * The second parameter tells whether the record should be flushed to the
Packit 6c4009
 * (output) tcp stream.  (This lets the package support batched or
Packit 6c4009
 * pipelined procedure calls.)  TRUE => immediate flush to tcp connection.
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
Packit 6c4009
{
Packit 6c4009
  RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
Packit 6c4009
  u_long len;		/* fragment length */
Packit 6c4009
Packit 6c4009
  if (sendnow || rstrm->frag_sent
Packit 6c4009
      || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
Packit 6c4009
    {
Packit 6c4009
      rstrm->frag_sent = FALSE;
Packit 6c4009
      return flush_out (rstrm, TRUE);
Packit 6c4009
    }
Packit 6c4009
  len = (rstrm->out_finger - (char *) rstrm->frag_header
Packit 6c4009
	 - BYTES_PER_XDR_UNIT);
Packit 6c4009
  *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
Packit 6c4009
  rstrm->frag_header = (uint32_t *) rstrm->out_finger;
Packit 6c4009
  rstrm->out_finger += BYTES_PER_XDR_UNIT;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrrec_endofrecord, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Internal useful routines
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
flush_out (RECSTREAM *rstrm, bool_t eor)
Packit 6c4009
{
Packit 6c4009
  u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
Packit 6c4009
  u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
Packit 6c4009
		- BYTES_PER_XDR_UNIT);
Packit 6c4009
Packit 6c4009
  *rstrm->frag_header = htonl (len | eormask);
Packit 6c4009
  len = rstrm->out_finger - rstrm->out_base;
Packit 6c4009
  if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
Packit 6c4009
      != (int) len)
Packit 6c4009
    return FALSE;
Packit 6c4009
  rstrm->frag_header = (uint32_t *) rstrm->out_base;
Packit 6c4009
  rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t	/* knows nothing about records!  Only about input buffers */
Packit 6c4009
fill_input_buf (RECSTREAM *rstrm)
Packit 6c4009
{
Packit 6c4009
  caddr_t where;
Packit 6c4009
  size_t i;
Packit 6c4009
  int len;
Packit 6c4009
Packit 6c4009
  where = rstrm->in_base;
Packit 6c4009
  i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
Packit 6c4009
  where += i;
Packit 6c4009
  len = rstrm->in_size - i;
Packit 6c4009
  if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
Packit 6c4009
    return FALSE;
Packit 6c4009
  rstrm->in_finger = where;
Packit 6c4009
  where += len;
Packit 6c4009
  rstrm->in_boundry = where;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t	/* knows nothing about records!  Only about input buffers */
Packit 6c4009
get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
Packit 6c4009
{
Packit 6c4009
  int current;
Packit 6c4009
Packit 6c4009
  while (len > 0)
Packit 6c4009
    {
Packit 6c4009
      current = rstrm->in_boundry - rstrm->in_finger;
Packit 6c4009
      if (current == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (!fill_input_buf (rstrm))
Packit 6c4009
	    return FALSE;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      current = (len < current) ? len : current;
Packit 6c4009
      memcpy (addr, rstrm->in_finger, current);
Packit 6c4009
      rstrm->in_finger += current;
Packit 6c4009
      addr += current;
Packit 6c4009
      len -= current;
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t /* next two bytes of the input stream are treated as a header */
Packit 6c4009
set_input_fragment (RECSTREAM *rstrm)
Packit 6c4009
{
Packit 6c4009
  uint32_t header;
Packit 6c4009
Packit 6c4009
  if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
Packit 6c4009
    return FALSE;
Packit 6c4009
  header = ntohl (header);
Packit 6c4009
  rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
Packit 6c4009
  /*
Packit 6c4009
   * Sanity check. Try not to accept wildly incorrect fragment
Packit 6c4009
   * sizes. Unfortunately, only a size of zero can be identified as
Packit 6c4009
   * 'wildely incorrect', and this only, if it is not the last
Packit 6c4009
   * fragment of a message. Ridiculously large fragment sizes may look
Packit 6c4009
   * wrong, but we don't have any way to be certain that they aren't
Packit 6c4009
   * what the client actually intended to send us. Many existing RPC
Packit 6c4009
   * implementations may sent a fragment of size zero as the last
Packit 6c4009
   * fragment of a message.
Packit 6c4009
   */
Packit 6c4009
  if (header == 0)
Packit 6c4009
    return FALSE;
Packit 6c4009
  rstrm->fbtbc = header & ~LAST_FRAG;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t	/* consumes input bytes; knows nothing about records! */
Packit 6c4009
skip_input_bytes (RECSTREAM *rstrm, long cnt)
Packit 6c4009
{
Packit 6c4009
  int current;
Packit 6c4009
Packit 6c4009
  while (cnt > 0)
Packit 6c4009
    {
Packit 6c4009
      current = rstrm->in_boundry - rstrm->in_finger;
Packit 6c4009
      if (current == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (!fill_input_buf (rstrm))
Packit 6c4009
	    return FALSE;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      current = (cnt < current) ? cnt : current;
Packit 6c4009
      rstrm->in_finger += current;
Packit 6c4009
      cnt -= current;
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static u_int
Packit 6c4009
fix_buf_size (u_int s)
Packit 6c4009
{
Packit 6c4009
  if (s < 100)
Packit 6c4009
    s = 4000;
Packit 6c4009
  return RNDUP (s);
Packit 6c4009
}