2 static char sfs_xdr_rec_id[] = "@(#)xdr_rec.c 2.1 97/10/23";
4 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
12 * This product contains benchmarks acquired from several sources who
13 * understand and agree with SPEC's goal of creating fair and objective
14 * benchmarks to measure computer performance.
16 * This copyright notice is placed here only to protect SPEC in the
17 * event the source is misused in any manner that is contrary to the
18 * spirit, the goals and the intent of SPEC.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
24 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25 * unrestricted use provided that this legend is included on all tape
26 * media and as a part of the software program in whole or part. Users
27 * may copy or modify Sun RPC without charge, but are not authorized
28 * to license or distribute it to anyone else except as part of a product or
29 * program developed by the user.
31 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 * Sun RPC is provided with no support and without any obligation on the
36 * part of Sun Microsystems, Inc. to assist in its use, correction,
37 * modification or enhancement.
39 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41 * OR ANY PART THEREOF.
43 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44 * or profits or other special, indirect and consequential damages, even if
45 * Sun has been advised of the possibility of such damages.
47 * Sun Microsystems, Inc.
49 * Mountain View, California 94043
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
56 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
57 * layer above tcp (for rpc's use).
59 * Copyright (C) 1984, Sun Microsystems, Inc.
61 * These routines interface XDRSTREAMS to a tcp/ip connection.
62 * There is a record marking layer between the xdr stream
63 * and the tcp transport level. A record is composed on one or more
64 * record fragments. A record fragment is a thirty-two bit header followed
65 * by n bytes of data, where n is contained in the header. The header
66 * is represented as a htonl(uint32_t). Thegh order bit encodes
67 * whether or not the fragment is the last fragment of the record
68 * (1 => fragment is last, 0 => more fragments to follow.
69 * The other 31 bits encode the byte length of the fragment.
76 #include "rpc/types.h"
78 #include "rpc/osdep.h"
80 static bool_t xdrrec_getlong(XDR *, int32_t *);
81 static bool_t xdrrec_putlong(XDR *, int32_t *);
82 static bool_t xdrrec_getbytes(XDR *, void *, uint_t);
83 static bool_t xdrrec_putbytes(XDR *, void *, uint_t);
84 static uint_t xdrrec_getpos(XDR *);
85 static bool_t xdrrec_setpos(XDR *, uint_t);
86 static int32_t *xdrrec_inline(XDR *, uint_t);
87 static void xdrrec_destroy(XDR *);
89 static struct xdr_ops xdrrec_ops = {
101 * A record is composed of one or more record fragments.
102 * A record fragment is a two-byte header followed by zero to
103 * 2**32-1 bytes. The header is treated as a long unsigned and is
104 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
105 * are a byte count of the fragment. The highest order bit is a boolean:
106 * 1 => this fragment is the last fragment of the record,
107 * 0 => this fragment is followed by more fragment(s).
109 * The fragment/record machinery is not general; it is constructed to
110 * meet the needs of xdr and rpc based on tcp.
113 #define LAST_FRAG (0x80000000)
115 typedef struct rec_strm {
122 char *out_base; /* output buffer (points to frag header) */
123 char *out_finger; /* next output position */
124 char *out_boundry; /* data cannot up to this address */
125 uint32_t *frag_header; /* beginning of curren fragment */
126 bool_t frag_sent; /* true if buffer sent in middle of record */
131 uint32_t in_size; /* fixed size of the input buffer */
133 char *in_finger; /* location of next byte to be had */
134 char *in_boundry; /* can read up to this location */
135 int32_t fbtbc; /* fragment bytes to be consumed */
142 static uint_t fix_buf_size(uint_t);
143 static bool_t flush_out(RECSTREAM *, bool_t);
144 static bool_t set_input_fragment(RECSTREAM *);
145 static bool_t skip_input_bytes(RECSTREAM *, int32_t);
146 static bool_t get_input_bytes(RECSTREAM *, char *, int);
149 * Create an xdr handle for xdrrec
150 * xdrrec_create fills in xdrs. Sendsize and recvsize are
151 * send and recv buffer sizes (0 => use default).
152 * tcp_handle is an opaque handle that is passed as the first parameter to
153 * the procedures readit and writeit. Readit and writeit are read and
154 * write respectively. They are like the system
155 * calls expect that they take an opaque handle rather than an fd.
163 int (*readit)(), /* like read, but pass it a tcp_handle, not sock */
164 int (*writeit)()) /* like write, but pass it a tcp_handle, not sock */
167 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
170 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
172 * This is bad. Should rework xdrrec_create to
173 * return a handle, and in this case return NULL
178 * adjust sizes and allocate buffer quad byte aligned
180 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
181 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
182 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
183 if (rstrm->the_buffer == NULL) {
184 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
187 for (rstrm->out_base = rstrm->the_buffer;
188 (uint_t)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
190 rstrm->in_base = rstrm->out_base + sendsize;
194 xdrs->x_ops = &xdrrec_ops;
195 xdrs->x_private = (void *)rstrm;
196 rstrm->tcp_handle = tcp_handle;
197 rstrm->readit = readit;
198 rstrm->writeit = writeit;
199 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
200 rstrm->frag_header = (uint32_t *)rstrm->out_base;
201 rstrm->out_finger += sizeof(uint32_t);
202 rstrm->out_boundry += sendsize;
203 rstrm->frag_sent = FALSE;
204 rstrm->in_size = recvsize;
205 rstrm->in_boundry = rstrm->in_base;
206 rstrm->in_finger = (rstrm->in_boundry += recvsize);
208 rstrm->last_frag = TRUE;
213 * The reoutines defined below are the xdr ops which will go into the
214 * xdr handle filled in by xdrrec_create.
222 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
223 int32_t *buflp = (int32_t *)(rstrm->in_finger);
226 /* first try the inline, fast case */
227 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
228 (((int)rstrm->in_boundry - (int)buflp) >= sizeof(int32_t))) {
229 *lp = (int32_t)ntohl((uint32_t)(*buflp));
230 rstrm->fbtbc -= sizeof(int32_t);
231 rstrm->in_finger += sizeof(int32_t);
233 if (! xdrrec_getbytes(xdrs, (void *)&mylong, sizeof(int32_t)))
235 *lp = (int32_t)ntohl((uint32_t)mylong);
245 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
246 int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
248 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
250 * this case should almost never happen so the code is
253 rstrm->out_finger -= sizeof(int32_t);
254 rstrm->frag_sent = TRUE;
255 if (! flush_out(rstrm, FALSE))
257 dest_lp = ((int32_t *)(rstrm->out_finger));
258 rstrm->out_finger += sizeof(int32_t);
260 *dest_lp = (int32_t)htonl((uint32_t)(*lp));
264 static bool_t /* must manage buffers, fragments, and records */
271 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
275 current = rstrm->fbtbc;
277 if (rstrm->last_frag)
279 if (! set_input_fragment(rstrm))
283 current = (len < current) ? len : current;
284 if (! get_input_bytes(rstrm, addr, current))
287 rstrm->fbtbc -= current;
300 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
304 current = (uint_t)rstrm->out_boundry - (uint_t)rstrm->out_finger;
305 current = (len < current) ? len : current;
306 memmove(rstrm->out_finger, addr, current);
307 rstrm->out_finger += current;
310 if (rstrm->out_finger == rstrm->out_boundry) {
311 rstrm->frag_sent = TRUE;
312 if (! flush_out(rstrm, FALSE))
323 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
326 pos = lseek((int)rstrm->tcp_handle, (int32_t) 0, 1);
328 switch (xdrs->x_op) {
331 pos += rstrm->out_finger - rstrm->out_base;
335 pos -= rstrm->in_boundry - rstrm->in_finger;
342 return ((uint_t) pos);
350 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
351 uint_t currpos = xdrrec_getpos(xdrs);
352 int delta = currpos - pos;
355 if ((int)currpos != -1)
356 switch (xdrs->x_op) {
359 newpos = rstrm->out_finger - delta;
360 if ((newpos > (char *)(rstrm->frag_header)) &&
361 (newpos < rstrm->out_boundry)) {
362 rstrm->out_finger = newpos;
368 newpos = rstrm->in_finger - delta;
369 if ((delta < (int)(rstrm->fbtbc)) &&
370 (newpos <= rstrm->in_boundry) &&
371 (newpos >= rstrm->in_base)) {
372 rstrm->in_finger = newpos;
373 rstrm->fbtbc -= delta;
386 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
387 int32_t * buf = NULL;
389 switch (xdrs->x_op) {
392 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
393 buf = (int32_t *) rstrm->out_finger;
394 rstrm->out_finger += len;
399 if ((len <= rstrm->fbtbc) &&
400 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
401 buf = (int32_t *) rstrm->in_finger;
403 rstrm->in_finger += len;
414 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
416 mem_free(rstrm->the_buffer,
417 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
418 mem_free((void *)rstrm, sizeof(RECSTREAM));
423 * Exported routines to manage xdr records
427 * Before reading (deserializing from the stream, one should always call
428 * this procedure to guarantee proper record alignment.
434 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
436 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
437 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
440 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
443 rstrm->last_frag = FALSE;
448 * Look ahead fuction.
449 * Returns TRUE iff there is no more input in the buffer
450 * after consuming the rest of the current record.
456 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
458 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
459 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
462 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
465 if (rstrm->in_finger == rstrm->in_boundry)
471 * The client must tell the package when an end-of-record has occurred.
472 * The second paraemters tells whether the record should be flushed to the
473 * (output) tcp stream. (This let's the package support batched or
474 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
481 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
482 uint32_t len; /* fragment length */
484 if (sendnow || rstrm->frag_sent ||
485 ((uint32_t)rstrm->out_finger + sizeof(uint32_t) >=
486 (uint32_t)rstrm->out_boundry)) {
487 rstrm->frag_sent = FALSE;
488 return (flush_out(rstrm, TRUE));
490 len = (uint32_t)(rstrm->out_finger) - (uint32_t)(rstrm->frag_header) -
492 *(rstrm->frag_header) = htonl((uint32_t)len | (uint32_t)LAST_FRAG);
493 rstrm->frag_header = (uint32_t *)rstrm->out_finger;
494 rstrm->out_finger += sizeof(uint32_t);
500 * Internal useful routines
507 uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
508 uint32_t len = (uint32_t)(rstrm->out_finger) -
509 (uint32_t)(rstrm->frag_header) - sizeof(uint32_t);
511 *(rstrm->frag_header) = htonl(len | eormask);
512 len = (uint32_t)(rstrm->out_finger) - (uint32_t)(rstrm->out_base);
513 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
516 rstrm->frag_header = (uint32_t *)rstrm->out_base;
517 rstrm->out_finger = (char *)rstrm->out_base + sizeof(uint32_t);
521 static bool_t /* knows nothing about records! Only about input buffers */
529 where = rstrm->in_base;
530 i = (uint_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
532 len = rstrm->in_size - i;
533 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
535 rstrm->in_finger = where;
537 rstrm->in_boundry = where;
541 static bool_t /* knows nothing about records! Only about input buffers */
550 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
552 if (! fill_input_buf(rstrm))
556 current = (len < current) ? len : current;
557 memmove(addr, rstrm->in_finger, current);
558 rstrm->in_finger += current;
565 static bool_t /* next two bytes of the input stream are treated as a header */
571 if (! get_input_bytes(rstrm, (void *)&header, sizeof(header)))
573 header = (int32_t)ntohl(header);
574 rstrm->last_frag = ((header & (uint32_t)LAST_FRAG) == 0) ? FALSE : TRUE;
575 rstrm->fbtbc = header & (~LAST_FRAG);
579 static bool_t /* consumes input bytes; knows nothing about records! */
587 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
589 if (! fill_input_buf(rstrm))
593 current = (cnt < current) ? cnt : current;
594 rstrm->in_finger += current;