1 /* $NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
41 * layer above tcp (for rpc's use).
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * These routines interface XDRSTREAMS to a tcp/ip connection.
46 * There is a record marking layer between the xdr stream
47 * and the tcp transport level. A record is composed on one or more
48 * record fragments. A record fragment is a thirty-two bit header followed
49 * by n bytes of data, where n is contained in the header. The header
50 * is represented as a htonl(u_long). Thegh order bit encodes
51 * whether or not the fragment is the last fragment of the record
52 * (1 => fragment is last, 0 => more fragments to follow.
53 * The other 31 bits encode the byte length of the fragment.
56 #include "namespace.h"
57 #include <sys/types.h>
59 #include <netinet/in.h>
66 #include <rpc/types.h>
71 #include <sys/stddef.h>
72 #include "un-namespace.h"
75 static bool_t xdrrec_getlong(XDR *, long *);
76 static bool_t xdrrec_putlong(XDR *, const long *);
77 static bool_t xdrrec_getbytes(XDR *, char *, u_int);
79 static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
80 static u_int xdrrec_getpos(XDR *);
81 static bool_t xdrrec_setpos(XDR *, u_int);
82 static int32_t *xdrrec_inline(XDR *, u_int);
83 static void xdrrec_destroy(XDR *);
85 static const struct xdr_ops xdrrec_ops = {
97 * A record is composed of one or more record fragments.
98 * A record fragment is a four-byte header followed by zero to
99 * 2**32-1 bytes. The header is treated as a long unsigned and is
100 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
101 * are a byte count of the fragment. The highest order bit is a boolean:
102 * 1 => this fragment is the last fragment of the record,
103 * 0 => this fragment is followed by more fragment(s).
105 * The fragment/record machinery is not general; it is constructed to
106 * meet the needs of xdr and rpc based on tcp.
109 #define LAST_FRAG ((u_int32_t)(1 << 31))
111 typedef struct rec_strm {
116 int (*writeit)(void *, void *, int);
117 char *out_base; /* output buffer (points to frag header) */
118 char *out_finger; /* next output position */
119 char *out_boundry; /* data cannot up to this address */
120 u_int32_t *frag_header; /* beginning of curren fragment */
121 bool_t frag_sent; /* true if buffer sent in middle of record */
125 int (*readit)(void *, void *, int);
126 u_long in_size; /* fixed size of the input buffer */
128 char *in_finger; /* location of next byte to be had */
129 char *in_boundry; /* can read up to this location */
130 long fbtbc; /* fragment bytes to be consumed */
136 bool_t in_haveheader;
145 static u_int fix_buf_size(u_int);
146 static bool_t flush_out(RECSTREAM *, bool_t);
147 static bool_t fill_input_buf(RECSTREAM *);
148 static bool_t get_input_bytes(RECSTREAM *, char *, int);
149 static bool_t set_input_fragment(RECSTREAM *);
150 static bool_t skip_input_bytes(RECSTREAM *, long);
151 static bool_t realloc_stream(RECSTREAM *, int);
155 * Create an xdr handle for xdrrec
156 * xdrrec_create fills in xdrs. Sendsize and recvsize are
157 * send and recv buffer sizes (0 => use default).
158 * tcp_handle is an opaque handle that is passed as the first parameter to
159 * the procedures readit and writeit. Readit and writeit are read and
160 * write respectively. They are like the system
161 * calls expect that they take an opaque handle rather than an fd.
164 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
169 /* like read, but pass it a tcp_handle, not sock */
170 int (*readit)(void *, void *, int);
171 /* like write, but pass it a tcp_handle, not sock */
172 int (*writeit)(void *, void *, int);
174 RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
177 warnx("xdrrec_create: out of memory");
179 * This is bad. Should rework xdrrec_create to
180 * return a handle, and in this case return NULL
184 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
185 rstrm->out_base = mem_alloc(rstrm->sendsize);
186 if (rstrm->out_base == NULL) {
187 warnx("xdrrec_create: out of memory");
188 mem_free(rstrm, sizeof(RECSTREAM));
191 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
192 rstrm->in_base = mem_alloc(recvsize);
193 if (rstrm->in_base == NULL) {
194 warnx("xdrrec_create: out of memory");
195 mem_free(rstrm->out_base, sendsize);
196 mem_free(rstrm, sizeof(RECSTREAM));
202 xdrs->x_ops = &xdrrec_ops;
203 xdrs->x_private = rstrm;
204 rstrm->tcp_handle = tcp_handle;
205 rstrm->readit = readit;
206 rstrm->writeit = writeit;
207 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
208 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
209 rstrm->out_finger += sizeof(u_int32_t);
210 rstrm->out_boundry += sendsize;
211 rstrm->frag_sent = FALSE;
212 rstrm->in_size = recvsize;
213 rstrm->in_boundry = rstrm->in_base;
214 rstrm->in_finger = (rstrm->in_boundry += recvsize);
216 rstrm->last_frag = TRUE;
217 rstrm->in_haveheader = FALSE;
218 rstrm->in_hdrlen = 0;
219 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
220 rstrm->nonblock = FALSE;
221 rstrm->in_reclen = 0;
222 rstrm->in_received = 0;
227 * The reoutines defined below are the xdr ops which will go into the
228 * xdr handle filled in by xdrrec_create.
232 xdrrec_getlong(xdrs, lp)
236 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
237 int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
240 /* first try the inline, fast case */
241 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
242 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
243 *lp = (long)ntohl((u_int32_t)(*buflp));
244 rstrm->fbtbc -= sizeof(int32_t);
245 rstrm->in_finger += sizeof(int32_t);
247 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
250 *lp = (long)ntohl((u_int32_t)mylong);
256 xdrrec_putlong(xdrs, lp)
260 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
261 int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
263 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
265 * this case should almost never happen so the code is
268 rstrm->out_finger -= sizeof(int32_t);
269 rstrm->frag_sent = TRUE;
270 if (! flush_out(rstrm, FALSE))
272 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
273 rstrm->out_finger += sizeof(int32_t);
275 *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
279 static bool_t /* must manage buffers, fragments, and records */
280 xdrrec_getbytes(xdrs, addr, len)
285 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
289 current = (int)rstrm->fbtbc;
291 if (rstrm->last_frag)
293 if (! set_input_fragment(rstrm))
297 current = (len < current) ? len : current;
298 if (! get_input_bytes(rstrm, addr, current))
301 rstrm->fbtbc -= current;
308 xdrrec_putbytes(xdrs, addr, len)
313 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
317 current = (size_t)((u_long)rstrm->out_boundry -
318 (u_long)rstrm->out_finger);
319 current = (len < current) ? len : current;
320 memmove(rstrm->out_finger, addr, current);
321 rstrm->out_finger += current;
324 if (rstrm->out_finger == rstrm->out_boundry) {
325 rstrm->frag_sent = TRUE;
326 if (! flush_out(rstrm, FALSE))
337 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
340 pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
343 switch (xdrs->x_op) {
346 pos += rstrm->out_finger - rstrm->out_base;
350 pos -= rstrm->in_boundry - rstrm->in_finger;
357 return ((u_int) pos);
361 xdrrec_setpos(xdrs, pos)
365 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
366 u_int currpos = xdrrec_getpos(xdrs);
367 int delta = currpos - pos;
370 if ((int)currpos != -1)
371 switch (xdrs->x_op) {
374 newpos = rstrm->out_finger - delta;
375 if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
376 (newpos < rstrm->out_boundry)) {
377 rstrm->out_finger = newpos;
383 newpos = rstrm->in_finger - delta;
384 if ((delta < (int)(rstrm->fbtbc)) &&
385 (newpos <= rstrm->in_boundry) &&
386 (newpos >= rstrm->in_base)) {
387 rstrm->in_finger = newpos;
388 rstrm->fbtbc -= delta;
400 xdrrec_inline(xdrs, len)
404 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
407 switch (xdrs->x_op) {
410 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
411 buf = (int32_t *)(void *)rstrm->out_finger;
412 rstrm->out_finger += len;
417 if ((len <= rstrm->fbtbc) &&
418 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
419 buf = (int32_t *)(void *)rstrm->in_finger;
421 rstrm->in_finger += len;
435 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
437 mem_free(rstrm->out_base, rstrm->sendsize);
438 mem_free(rstrm->in_base, rstrm->recvsize);
439 mem_free(rstrm, sizeof(RECSTREAM));
444 * Exported routines to manage xdr records
448 * Before reading (deserializing from the stream, one should always call
449 * this procedure to guarantee proper record alignment.
452 xdrrec_skiprecord(xdrs)
455 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
456 enum xprt_stat xstat;
458 if (rstrm->nonblock) {
459 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
463 if (rstrm->in_finger == rstrm->in_boundry &&
464 xstat == XPRT_MOREREQS) {
471 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
472 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
475 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
478 rstrm->last_frag = FALSE;
483 * Look ahead function.
484 * Returns TRUE iff there is no more input in the buffer
485 * after consuming the rest of the current record.
491 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
493 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
494 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
497 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
500 if (rstrm->in_finger == rstrm->in_boundry)
506 * The client must tell the package when an end-of-record has occurred.
507 * The second paraemters tells whether the record should be flushed to the
508 * (output) tcp stream. (This let's the package support batched or
509 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
512 xdrrec_endofrecord(xdrs, sendnow)
516 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
517 u_long len; /* fragment length */
519 if (sendnow || rstrm->frag_sent ||
520 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
521 (u_long)rstrm->out_boundry)) {
522 rstrm->frag_sent = FALSE;
523 return (flush_out(rstrm, TRUE));
525 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
527 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
528 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
529 rstrm->out_finger += sizeof(u_int32_t);
534 * Fill the stream buffer with a record for a non-blocking connection.
535 * Return true if a record is available in the buffer, false if not.
538 __xdrrec_getrec(xdrs, statp, expectdata)
540 enum xprt_stat *statp;
543 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
547 if (!rstrm->in_haveheader) {
548 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
549 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
551 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
559 rstrm->in_hdrlen += n;
560 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
561 *statp = XPRT_MOREREQS;
564 rstrm->in_header = ntohl(rstrm->in_header);
565 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
566 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
567 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
571 rstrm->in_reclen += fraglen;
572 if (rstrm->in_reclen > rstrm->recvsize)
573 realloc_stream(rstrm, rstrm->in_reclen);
574 if (rstrm->in_header & LAST_FRAG) {
575 rstrm->in_header &= ~LAST_FRAG;
576 rstrm->last_frag = TRUE;
579 * We can only reasonably expect to read once from a
580 * non-blocking stream. Reading the fragment header
581 * may have drained the stream.
586 n = rstrm->readit(rstrm->tcp_handle,
587 rstrm->in_base + rstrm->in_received,
588 (rstrm->in_reclen - rstrm->in_received));
596 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
600 rstrm->in_received += n;
602 if (rstrm->in_received == rstrm->in_reclen) {
603 rstrm->in_haveheader = FALSE;
604 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
605 rstrm->in_hdrlen = 0;
606 if (rstrm->last_frag) {
607 rstrm->fbtbc = rstrm->in_reclen;
608 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
609 rstrm->in_finger = rstrm->in_base;
610 rstrm->in_reclen = rstrm->in_received = 0;
611 *statp = XPRT_MOREREQS;
616 *statp = XPRT_MOREREQS;
621 __xdrrec_setnonblock(xdrs, maxrec)
625 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
627 rstrm->nonblock = TRUE;
629 maxrec = rstrm->recvsize;
630 rstrm->in_maxrec = maxrec;
635 * Internal useful routines
638 flush_out(rstrm, eor)
642 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
643 u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
644 (u_long)(rstrm->frag_header) - sizeof(u_int32_t));
646 *(rstrm->frag_header) = htonl(len | eormask);
647 len = (u_int32_t)((u_long)(rstrm->out_finger) -
648 (u_long)(rstrm->out_base));
649 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
652 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
653 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
657 static bool_t /* knows nothing about records! Only about input buffers */
658 fill_input_buf(rstrm)
668 where = rstrm->in_base;
669 i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
671 len = (u_int32_t)(rstrm->in_size - i);
672 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
674 rstrm->in_finger = where;
676 rstrm->in_boundry = where;
680 static bool_t /* knows nothing about records! Only about input buffers */
681 get_input_bytes(rstrm, addr, len)
688 if (rstrm->nonblock) {
689 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
691 memcpy(addr, rstrm->in_finger, (size_t)len);
692 rstrm->in_finger += len;
697 current = (size_t)((long)rstrm->in_boundry -
698 (long)rstrm->in_finger);
700 if (! fill_input_buf(rstrm))
704 current = (len < current) ? len : current;
705 memmove(addr, rstrm->in_finger, current);
706 rstrm->in_finger += current;
713 static bool_t /* next two bytes of the input stream are treated as a header */
714 set_input_fragment(rstrm)
721 if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
723 header = ntohl(header);
724 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
726 * Sanity check. Try not to accept wildly incorrect
727 * record sizes. Unfortunately, the only record size
728 * we can positively identify as being 'wildly incorrect'
729 * is zero. Ridiculously large record sizes may look wrong,
730 * but we don't have any way to be certain that they aren't
731 * what the client actually intended to send us.
735 rstrm->fbtbc = header & (~LAST_FRAG);
739 static bool_t /* consumes input bytes; knows nothing about records! */
740 skip_input_bytes(rstrm, cnt)
747 current = (size_t)((long)rstrm->in_boundry -
748 (long)rstrm->in_finger);
750 if (! fill_input_buf(rstrm))
754 current = (u_int32_t)((cnt < current) ? cnt : current);
755 rstrm->in_finger += current;
772 * Reallocate the input buffer for a non-block stream.
775 realloc_stream(rstrm, size)
782 if (size > rstrm->recvsize) {
783 buf = realloc(rstrm->in_base, (size_t)size);
786 diff = buf - rstrm->in_base;
787 rstrm->in_finger += diff;
788 rstrm->in_base = buf;
789 rstrm->in_boundry = buf + size;
790 rstrm->recvsize = size;
791 rstrm->in_size = size;