Home | History | Annotate | Line # | Download | only in rpc
xdr_rec.c revision 1.5.4.1
      1 /*	$NetBSD: xdr_rec.c,v 1.5.4.1 1996/09/16 23:44:47 jtc Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 #if defined(LIBC_SCCS) && !defined(lint)
     32 /*static char *sccsid = "from: @(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";*/
     33 /*static char *sccsid = "from: @(#)xdr_rec.c	2.2 88/08/01 4.0 RPCSRC";*/
     34 static char *rcsid = "$NetBSD: xdr_rec.c,v 1.5.4.1 1996/09/16 23:44:47 jtc Exp $";
     35 #endif
     36 
     37 /*
     38  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
     39  * layer above tcp (for rpc's use).
     40  *
     41  * Copyright (C) 1984, Sun Microsystems, Inc.
     42  *
     43  * These routines interface XDRSTREAMS to a tcp/ip connection.
     44  * There is a record marking layer between the xdr stream
     45  * and the tcp transport level.  A record is composed on one or more
     46  * record fragments.  A record fragment is a thirty-two bit header followed
     47  * by n bytes of data, where n is contained in the header.  The header
     48  * is represented as a htonl(u_long).  Thegh order bit encodes
     49  * whether or not the fragment is the last fragment of the record
     50  * (1 => fragment is last, 0 => more fragments to follow.
     51  * The other 31 bits encode the byte length of the fragment.
     52  */
     53 
     54 #include "namespace.h"
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <rpc/types.h>
     58 #include <rpc/xdr.h>
     59 #include <netinet/in.h>
     60 
     61 #ifdef __weak_alias
     62 __weak_alias(xdrrec_create,_xdrrec_create);
     63 __weak_alias(xdrrec_endofrecord,_xdrrec_endofrecord);
     64 __weak_alias(xdrrec_eof,_xdrrec_eof);
     65 __weak_alias(xdrrec_skiprecord,_xdrrec_skiprecord);
     66 #endif
     67 
     68 static u_int	fix_buf_size();
     69 static bool_t	flush_out();
     70 static bool_t	get_input_bytes();
     71 static bool_t	set_input_fragment();
     72 static bool_t	skip_input_bytes();
     73 
     74 static bool_t	xdrrec_getlong();
     75 static bool_t	xdrrec_putlong();
     76 static bool_t	xdrrec_getbytes();
     77 static bool_t	xdrrec_putbytes();
     78 static u_int	xdrrec_getpos();
     79 static bool_t	xdrrec_setpos();
     80 static int32_t *xdrrec_inline();
     81 static void	xdrrec_destroy();
     82 
     83 static struct  xdr_ops xdrrec_ops = {
     84 	xdrrec_getlong,
     85 	xdrrec_putlong,
     86 	xdrrec_getbytes,
     87 	xdrrec_putbytes,
     88 	xdrrec_getpos,
     89 	xdrrec_setpos,
     90 	xdrrec_inline,
     91 	xdrrec_destroy
     92 };
     93 
     94 /*
     95  * A record is composed of one or more record fragments.
     96  * A record fragment is a two-byte header followed by zero to
     97  * 2**32-1 bytes.  The header is treated as a long unsigned and is
     98  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
     99  * are a byte count of the fragment.  The highest order bit is a boolean:
    100  * 1 => this fragment is the last fragment of the record,
    101  * 0 => this fragment is followed by more fragment(s).
    102  *
    103  * The fragment/record machinery is not general;  it is constructed to
    104  * meet the needs of xdr and rpc based on tcp.
    105  */
    106 
    107 #define LAST_FRAG ((u_int32_t)(1 << 31))
    108 
    109 typedef struct rec_strm {
    110 	caddr_t tcp_handle;
    111 	caddr_t the_buffer;
    112 	/*
    113 	 * out-goung bits
    114 	 */
    115 	int (*writeit) __P((caddr_t, caddr_t, int));
    116 	caddr_t out_base;	/* output buffer (points to frag header) */
    117 	caddr_t out_finger;	/* next output position */
    118 	caddr_t out_boundry;	/* data cannot up to this address */
    119 	u_int32_t *frag_header;	/* beginning of curren fragment */
    120 	bool_t frag_sent;	/* true if buffer sent in middle of record */
    121 	/*
    122 	 * in-coming bits
    123 	 */
    124 	int (*readit) __P((caddr_t, caddr_t, int));
    125 	u_long in_size;	/* fixed size of the input buffer */
    126 	caddr_t in_base;
    127 	caddr_t in_finger;	/* location of next byte to be had */
    128 	caddr_t in_boundry;	/* can read up to this location */
    129 	long fbtbc;		/* fragment bytes to be consumed */
    130 	bool_t last_frag;
    131 	u_int sendsize;
    132 	u_int recvsize;
    133 } RECSTREAM;
    134 
    135 
    136 /*
    137  * Create an xdr handle for xdrrec
    138  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
    139  * send and recv buffer sizes (0 => use default).
    140  * tcp_handle is an opaque handle that is passed as the first parameter to
    141  * the procedures readit and writeit.  Readit and writeit are read and
    142  * write respectively.   They are like the system
    143  * calls expect that they take an opaque handle rather than an fd.
    144  */
    145 void
    146 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
    147 	register XDR *xdrs;
    148 	register u_int sendsize;
    149 	register u_int recvsize;
    150 	caddr_t tcp_handle;
    151 	int (*readit)();  /* like read, but pass it a tcp_handle, not sock */
    152 	int (*writeit)();  /* like write, but pass it a tcp_handle, not sock */
    153 {
    154 	register RECSTREAM *rstrm =
    155 		(RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
    156 
    157 	if (rstrm == NULL) {
    158 		(void)fprintf(stderr, "xdrrec_create: out of memory\n");
    159 		/*
    160 		 *  This is bad.  Should rework xdrrec_create to
    161 		 *  return a handle, and in this case return NULL
    162 		 */
    163 		return;
    164 	}
    165 	/*
    166 	 * adjust sizes and allocate buffer quad byte aligned
    167 	 */
    168 	rstrm->sendsize = sendsize = fix_buf_size(sendsize);
    169 	rstrm->recvsize = recvsize = fix_buf_size(recvsize);
    170 	rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
    171 	if (rstrm->the_buffer == NULL) {
    172 		(void)fprintf(stderr, "xdrrec_create: out of memory\n");
    173 		return;
    174 	}
    175 	for (rstrm->out_base = rstrm->the_buffer;
    176 		(u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
    177 		rstrm->out_base++);
    178 	rstrm->in_base = rstrm->out_base + sendsize;
    179 	/*
    180 	 * now the rest ...
    181 	 */
    182 	xdrs->x_ops = &xdrrec_ops;
    183 	xdrs->x_private = (caddr_t)rstrm;
    184 	rstrm->tcp_handle = tcp_handle;
    185 	rstrm->readit = readit;
    186 	rstrm->writeit = writeit;
    187 	rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
    188 	rstrm->frag_header = (u_int32_t *)rstrm->out_base;
    189 	rstrm->out_finger += sizeof(u_int32_t);
    190 	rstrm->out_boundry += sendsize;
    191 	rstrm->frag_sent = FALSE;
    192 	rstrm->in_size = recvsize;
    193 	rstrm->in_boundry = rstrm->in_base;
    194 	rstrm->in_finger = (rstrm->in_boundry += recvsize);
    195 	rstrm->fbtbc = 0;
    196 	rstrm->last_frag = TRUE;
    197 }
    198 
    199 
    200 /*
    201  * The reoutines defined below are the xdr ops which will go into the
    202  * xdr handle filled in by xdrrec_create.
    203  */
    204 
    205 static bool_t
    206 xdrrec_getlong(xdrs, lp)
    207 	XDR *xdrs;
    208 	long *lp;
    209 {
    210 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    211 	register int32_t *buflp = (int32_t *)(rstrm->in_finger);
    212 	int32_t mylong;
    213 
    214 	/* first try the inline, fast case */
    215 	if ((rstrm->fbtbc >= sizeof(int32_t)) &&
    216 		(((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
    217 		*lp = (long)ntohl((u_int32_t)(*buflp));
    218 		rstrm->fbtbc -= sizeof(int32_t);
    219 		rstrm->in_finger += sizeof(int32_t);
    220 	} else {
    221 		if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(int32_t)))
    222 			return (FALSE);
    223 		*lp = (long)ntohl((u_int32_t)mylong);
    224 	}
    225 	return (TRUE);
    226 }
    227 
    228 static bool_t
    229 xdrrec_putlong(xdrs, lp)
    230 	XDR *xdrs;
    231 	long *lp;
    232 {
    233 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    234 	register int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
    235 
    236 	if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
    237 		/*
    238 		 * this case should almost never happen so the code is
    239 		 * inefficient
    240 		 */
    241 		rstrm->out_finger -= sizeof(int32_t);
    242 		rstrm->frag_sent = TRUE;
    243 		if (! flush_out(rstrm, FALSE))
    244 			return (FALSE);
    245 		dest_lp = ((int32_t *)(rstrm->out_finger));
    246 		rstrm->out_finger += sizeof(int32_t);
    247 	}
    248 	*dest_lp = (int32_t)htonl((u_int32_t)(*lp));
    249 	return (TRUE);
    250 }
    251 
    252 static bool_t  /* must manage buffers, fragments, and records */
    253 xdrrec_getbytes(xdrs, addr, len)
    254 	XDR *xdrs;
    255 	register caddr_t addr;
    256 	register u_int len;
    257 {
    258 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    259 	register int current;
    260 
    261 	while (len > 0) {
    262 		current = rstrm->fbtbc;
    263 		if (current == 0) {
    264 			if (rstrm->last_frag)
    265 				return (FALSE);
    266 			if (! set_input_fragment(rstrm))
    267 				return (FALSE);
    268 			continue;
    269 		}
    270 		current = (len < current) ? len : current;
    271 		if (! get_input_bytes(rstrm, addr, current))
    272 			return (FALSE);
    273 		addr += current;
    274 		rstrm->fbtbc -= current;
    275 		len -= current;
    276 	}
    277 	return (TRUE);
    278 }
    279 
    280 static bool_t
    281 xdrrec_putbytes(xdrs, addr, len)
    282 	XDR *xdrs;
    283 	register caddr_t addr;
    284 	register u_int len;
    285 {
    286 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    287 	register long current;
    288 
    289 	while (len > 0) {
    290 		current = (u_long)rstrm->out_boundry -
    291 		    (u_long)rstrm->out_finger;
    292 		current = (len < current) ? len : current;
    293 		bcopy(addr, rstrm->out_finger, current);
    294 		rstrm->out_finger += current;
    295 		addr += current;
    296 		len -= current;
    297 		if (rstrm->out_finger == rstrm->out_boundry) {
    298 			rstrm->frag_sent = TRUE;
    299 			if (! flush_out(rstrm, FALSE))
    300 				return (FALSE);
    301 		}
    302 	}
    303 	return (TRUE);
    304 }
    305 
    306 static u_int
    307 xdrrec_getpos(xdrs)
    308 	register XDR *xdrs;
    309 {
    310 	register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    311 	register long pos;
    312 
    313 	pos = lseek((off_t)(long)rstrm->tcp_handle, 0, 1);
    314 	if (pos != -1)
    315 		switch (xdrs->x_op) {
    316 
    317 		case XDR_ENCODE:
    318 			pos += rstrm->out_finger - rstrm->out_base;
    319 			break;
    320 
    321 		case XDR_DECODE:
    322 			pos -= rstrm->in_boundry - rstrm->in_finger;
    323 			break;
    324 
    325 		default:
    326 			pos = (u_int) -1;
    327 			break;
    328 		}
    329 	return ((u_int) pos);
    330 }
    331 
    332 static bool_t
    333 xdrrec_setpos(xdrs, pos)
    334 	register XDR *xdrs;
    335 	u_int pos;
    336 {
    337 	register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    338 	u_int currpos = xdrrec_getpos(xdrs);
    339 	int delta = currpos - pos;
    340 	caddr_t newpos;
    341 
    342 	if ((int)currpos != -1)
    343 		switch (xdrs->x_op) {
    344 
    345 		case XDR_ENCODE:
    346 			newpos = rstrm->out_finger - delta;
    347 			if ((newpos > (caddr_t)(rstrm->frag_header)) &&
    348 				(newpos < rstrm->out_boundry)) {
    349 				rstrm->out_finger = newpos;
    350 				return (TRUE);
    351 			}
    352 			break;
    353 
    354 		case XDR_DECODE:
    355 			newpos = rstrm->in_finger - delta;
    356 			if ((delta < (int)(rstrm->fbtbc)) &&
    357 				(newpos <= rstrm->in_boundry) &&
    358 				(newpos >= rstrm->in_base)) {
    359 				rstrm->in_finger = newpos;
    360 				rstrm->fbtbc -= delta;
    361 				return (TRUE);
    362 			}
    363 			break;
    364 		}
    365 	return (FALSE);
    366 }
    367 
    368 static int32_t *
    369 xdrrec_inline(xdrs, len)
    370 	register XDR *xdrs;
    371 	int len;
    372 {
    373 	register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    374 	int32_t *buf = NULL;
    375 
    376 	switch (xdrs->x_op) {
    377 
    378 	case XDR_ENCODE:
    379 		if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
    380 			buf = (int32_t *) rstrm->out_finger;
    381 			rstrm->out_finger += len;
    382 		}
    383 		break;
    384 
    385 	case XDR_DECODE:
    386 		if ((len <= rstrm->fbtbc) &&
    387 			((rstrm->in_finger + len) <= rstrm->in_boundry)) {
    388 			buf = (int32_t *) rstrm->in_finger;
    389 			rstrm->fbtbc -= len;
    390 			rstrm->in_finger += len;
    391 		}
    392 		break;
    393 	}
    394 	return (buf);
    395 }
    396 
    397 static void
    398 xdrrec_destroy(xdrs)
    399 	register XDR *xdrs;
    400 {
    401 	register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
    402 
    403 	mem_free(rstrm->the_buffer,
    404 		rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
    405 	mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
    406 }
    407 
    408 
    409 /*
    410  * Exported routines to manage xdr records
    411  */
    412 
    413 /*
    414  * Before reading (deserializing from the stream, one should always call
    415  * this procedure to guarantee proper record alignment.
    416  */
    417 bool_t
    418 xdrrec_skiprecord(xdrs)
    419 	XDR *xdrs;
    420 {
    421 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    422 
    423 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    424 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
    425 			return (FALSE);
    426 		rstrm->fbtbc = 0;
    427 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
    428 			return (FALSE);
    429 	}
    430 	rstrm->last_frag = FALSE;
    431 	return (TRUE);
    432 }
    433 
    434 /*
    435  * Look ahead fuction.
    436  * Returns TRUE iff there is no more input in the buffer
    437  * after consuming the rest of the current record.
    438  */
    439 bool_t
    440 xdrrec_eof(xdrs)
    441 	XDR *xdrs;
    442 {
    443 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    444 
    445 	while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
    446 		if (! skip_input_bytes(rstrm, rstrm->fbtbc))
    447 			return (TRUE);
    448 		rstrm->fbtbc = 0;
    449 		if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
    450 			return (TRUE);
    451 	}
    452 	if (rstrm->in_finger == rstrm->in_boundry)
    453 		return (TRUE);
    454 	return (FALSE);
    455 }
    456 
    457 /*
    458  * The client must tell the package when an end-of-record has occurred.
    459  * The second paraemters tells whether the record should be flushed to the
    460  * (output) tcp stream.  (This let's the package support batched or
    461  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
    462  */
    463 bool_t
    464 xdrrec_endofrecord(xdrs, sendnow)
    465 	XDR *xdrs;
    466 	bool_t sendnow;
    467 {
    468 	register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
    469 	register u_long len;  /* fragment length */
    470 
    471 	if (sendnow || rstrm->frag_sent ||
    472 		((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
    473 		(u_long)rstrm->out_boundry)) {
    474 		rstrm->frag_sent = FALSE;
    475 		return (flush_out(rstrm, TRUE));
    476 	}
    477 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
    478 	   sizeof(u_int32_t);
    479 	*(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
    480 	rstrm->frag_header = (u_int32_t *)rstrm->out_finger;
    481 	rstrm->out_finger += sizeof(u_int32_t);
    482 	return (TRUE);
    483 }
    484 
    485 
    486 /*
    487  * Internal useful routines
    488  */
    489 static bool_t
    490 flush_out(rstrm, eor)
    491 	register RECSTREAM *rstrm;
    492 	bool_t eor;
    493 {
    494 	register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
    495 	register u_int32_t len = (u_long)(rstrm->out_finger) -
    496 		(u_long)(rstrm->frag_header) - sizeof(u_int32_t);
    497 
    498 	*(rstrm->frag_header) = htonl(len | eormask);
    499 	len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
    500 	if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
    501 		!= (int)len)
    502 		return (FALSE);
    503 	rstrm->frag_header = (u_int32_t *)rstrm->out_base;
    504 	rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t);
    505 	return (TRUE);
    506 }
    507 
    508 static bool_t  /* knows nothing about records!  Only about input buffers */
    509 fill_input_buf(rstrm)
    510 	register RECSTREAM *rstrm;
    511 {
    512 	register caddr_t where;
    513 	u_long i;
    514 	register long len;
    515 
    516 	where = rstrm->in_base;
    517 	i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
    518 	where += i;
    519 	len = rstrm->in_size - i;
    520 	if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
    521 		return (FALSE);
    522 	rstrm->in_finger = where;
    523 	where += len;
    524 	rstrm->in_boundry = where;
    525 	return (TRUE);
    526 }
    527 
    528 static bool_t  /* knows nothing about records!  Only about input buffers */
    529 get_input_bytes(rstrm, addr, len)
    530 	register RECSTREAM *rstrm;
    531 	register caddr_t addr;
    532 	register int len;
    533 {
    534 	register long current;
    535 
    536 	while (len > 0) {
    537 		current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
    538 		if (current == 0) {
    539 			if (! fill_input_buf(rstrm))
    540 				return (FALSE);
    541 			continue;
    542 		}
    543 		current = (len < current) ? len : current;
    544 		bcopy(rstrm->in_finger, addr, current);
    545 		rstrm->in_finger += current;
    546 		addr += current;
    547 		len -= current;
    548 	}
    549 	return (TRUE);
    550 }
    551 
    552 static bool_t  /* next two bytes of the input stream are treated as a header */
    553 set_input_fragment(rstrm)
    554 	register RECSTREAM *rstrm;
    555 {
    556 	u_int32_t header;
    557 
    558 	if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
    559 		return (FALSE);
    560 	header = (long)ntohl(header);
    561 	rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
    562 	rstrm->fbtbc = header & (~LAST_FRAG);
    563 	return (TRUE);
    564 }
    565 
    566 static bool_t  /* consumes input bytes; knows nothing about records! */
    567 skip_input_bytes(rstrm, cnt)
    568 	register RECSTREAM *rstrm;
    569 	long cnt;
    570 {
    571 	register long current;
    572 
    573 	while (cnt > 0) {
    574 		current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
    575 		if (current == 0) {
    576 			if (! fill_input_buf(rstrm))
    577 				return (FALSE);
    578 			continue;
    579 		}
    580 		current = (cnt < current) ? cnt : current;
    581 		rstrm->in_finger += current;
    582 		cnt -= current;
    583 	}
    584 	return (TRUE);
    585 }
    586 
    587 static u_int
    588 fix_buf_size(s)
    589 	register u_int s;
    590 {
    591 
    592 	if (s < 100)
    593 		s = 4000;
    594 	return (RNDUP(s));
    595 }
    596