1 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
35 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
36 * layer above tcp (for rpc's use).
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * These routines interface XDRSTREAMS to a tcp/ip connection.
41 * There is a record marking layer between the xdr stream
42 * and the tcp transport level. A record is composed on one or more
43 * record fragments. A record fragment is a thirty-two bit header followed
44 * by n bytes of data, where n is contained in the header. The header
45 * is represented as a htonl(u_long). Thegh order bit encodes
46 * whether or not the fragment is the last fragment of the record
47 * (1 => fragment is last, 0 => more fragments to follow.
48 * The other 31 bits encode the byte length of the fragment.
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
61 #include <rpc/types.h>
68 static u_int fix_buf_size();
70 static bool_t xdrrec_getlong();
71 static bool_t xdrrec_putlong();
72 static bool_t xdrrec_getbytes();
73 static bool_t xdrrec_putbytes();
74 static u_int xdrrec_getpos();
75 static bool_t xdrrec_setpos();
76 static long * xdrrec_inline();
77 static void xdrrec_destroy();
79 static struct xdr_ops xdrrec_ops = {
91 * A record is composed of one or more record fragments.
92 * A record fragment is a two-byte header followed by zero to
93 * 2**32-1 bytes. The header is treated as a long unsigned and is
94 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
95 * are a byte count of the fragment. The highest order bit is a boolean:
96 * 1 => this fragment is the last fragment of the record,
97 * 0 => this fragment is followed by more fragment(s).
99 * The fragment/record machinery is not general; it is constructed to
100 * meet the needs of xdr and rpc based on tcp.
103 #define LAST_FRAG ((u_long)(1 << 31))
105 typedef struct rec_strm {
112 caddr_t out_base; /* output buffer (points to frag header) */
113 caddr_t out_finger; /* next output position */
114 caddr_t out_boundry; /* data cannot up to this address */
115 u_long *frag_header; /* beginning of curren fragment */
116 bool_t frag_sent; /* true if buffer sent in middle of record */
121 u_long in_size; /* fixed size of the input buffer */
123 caddr_t in_finger; /* location of next byte to be had */
124 caddr_t in_boundry; /* can read up to this location */
125 long fbtbc; /* fragment bytes to be consumed */
131 static bool_t flush_out(register RECSTREAM *, bool_t );
132 static bool_t get_input_bytes(register RECSTREAM *, register caddr_t, register int );
133 static bool_t set_input_fragment(register RECSTREAM *);
134 static bool_t skip_input_bytes(register RECSTREAM *, long);
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.
146 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
148 register u_int sendsize;
149 register u_int recvsize;
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 */
154 register RECSTREAM *rstrm =
155 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
158 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
160 * This is bad. Should rework xdrrec_create to
161 * return a handle, and in this case return NULL
166 * adjust sizes and allocate buffer quad byte aligned
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");
175 for (rstrm->out_base = rstrm->the_buffer;
176 (long) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
178 rstrm->in_base = rstrm->out_base + sendsize;
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_long *)rstrm->out_base;
189 rstrm->out_finger += sizeof(u_long);
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);
196 rstrm->last_frag = TRUE;
201 * The reoutines defined below are the xdr ops which will go into the
202 * xdr handle filled in by xdrrec_create.
206 xdrrec_getlong(xdrs, lp)
210 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
211 register long *buflp = (long *)(rstrm->in_finger);
214 /* first try the inline, fast case */
215 if ((rstrm->fbtbc >= sizeof(long)) &&
216 ((rstrm->in_boundry - (caddr_t) buflp) >= sizeof(long))) {
217 *lp = (long)ntohl((u_long)(*buflp));
218 rstrm->fbtbc -= sizeof(long);
219 rstrm->in_finger += sizeof(long);
221 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long)))
223 *lp = (long)ntohl((u_long)mylong);
229 xdrrec_putlong(xdrs, lp)
233 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
234 register long *dest_lp = ((long *)(rstrm->out_finger));
236 if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
238 * this case should almost never happen so the code is
241 rstrm->out_finger -= sizeof(long);
242 rstrm->frag_sent = TRUE;
243 if (! flush_out(rstrm, FALSE))
245 dest_lp = ((long *)(rstrm->out_finger));
246 rstrm->out_finger += sizeof(long);
248 *dest_lp = (long)htonl((u_long)(*lp));
252 static bool_t /* must manage buffers, fragments, and records */
253 xdrrec_getbytes(xdrs, addr, len)
255 register caddr_t addr;
258 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
259 register int current;
262 current = rstrm->fbtbc;
264 if (rstrm->last_frag)
266 if (! set_input_fragment(rstrm))
270 current = (len < current) ? len : current;
271 if (! get_input_bytes(rstrm, addr, current))
274 rstrm->fbtbc -= current;
281 xdrrec_putbytes(xdrs, addr, len)
283 register caddr_t addr;
286 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
287 register int current;
290 current = rstrm->out_boundry - rstrm->out_finger;
291 current = (len < current) ? len : current;
292 memcpy (rstrm->out_finger, addr, current);
293 rstrm->out_finger += current;
296 if (rstrm->out_finger == rstrm->out_boundry) {
297 rstrm->frag_sent = TRUE;
298 if (! flush_out(rstrm, FALSE))
309 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
312 pos = lseek(rstrm->tcp_handle, (long) 0, 1);
314 switch (xdrs->x_op) {
317 pos += rstrm->out_finger - rstrm->out_base;
321 pos -= rstrm->in_boundry - rstrm->in_finger;
328 return ((u_int) pos);
332 xdrrec_setpos(xdrs, pos)
336 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
337 u_int currpos = xdrrec_getpos(xdrs);
338 int delta = currpos - pos;
341 if ((int)currpos != -1)
342 switch (xdrs->x_op) {
344 case XDR_FREE: break; /* keep gcc happy */
347 newpos = rstrm->out_finger - delta;
348 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
349 (newpos < rstrm->out_boundry)) {
350 rstrm->out_finger = newpos;
356 newpos = rstrm->in_finger - delta;
357 if ((delta < (int)(rstrm->fbtbc)) &&
358 (newpos <= rstrm->in_boundry) &&
359 (newpos >= rstrm->in_base)) {
360 rstrm->in_finger = newpos;
361 rstrm->fbtbc -= delta;
370 xdrrec_inline(xdrs, len)
374 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
377 switch (xdrs->x_op) {
379 case XDR_FREE: break; /* keep gcc happy */
382 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
383 buf = (long *) rstrm->out_finger;
384 rstrm->out_finger += len;
389 if ((len <= rstrm->fbtbc) &&
390 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
391 buf = (long *) rstrm->in_finger;
393 rstrm->in_finger += len;
404 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
406 mem_free(rstrm->the_buffer,
407 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
408 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
413 * Exported routines to manage xdr records
417 * Before reading (deserializing from the stream, one should always call
418 * this procedure to guarantee proper record alignment.
421 xdrrec_skiprecord(xdrs)
424 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
426 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
427 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
430 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
433 rstrm->last_frag = FALSE;
438 * Look ahead fuction.
439 * Returns TRUE iff there is no more input in the buffer
440 * after consuming the rest of the current record.
446 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
448 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
449 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
452 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
455 if (rstrm->in_finger == rstrm->in_boundry)
461 * The client must tell the package when an end-of-record has occurred.
462 * The second paraemters tells whether the record should be flushed to the
463 * (output) tcp stream. (This let's the package support batched or
464 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
467 xdrrec_endofrecord(xdrs, sendnow)
471 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
472 register u_long len; /* fragment length */
474 if (sendnow || rstrm->frag_sent ||
475 ((u_long)rstrm->out_finger + sizeof(u_long) >=
476 (u_long)rstrm->out_boundry)) {
477 rstrm->frag_sent = FALSE;
478 return (flush_out(rstrm, TRUE));
480 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
482 *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
483 rstrm->frag_header = (u_long *)rstrm->out_finger;
484 rstrm->out_finger += sizeof(u_long);
490 * Internal useful routines
493 flush_out(rstrm, eor)
494 register RECSTREAM *rstrm;
497 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
498 register u_long len = (u_long)(rstrm->out_finger) -
499 (u_long)(rstrm->frag_header) - sizeof(u_long);
501 *(rstrm->frag_header) = htonl(len | eormask);
502 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
503 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
506 rstrm->frag_header = (u_long *)rstrm->out_base;
507 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
511 static bool_t /* knows nothing about records! Only about input buffers */
512 fill_input_buf(rstrm)
513 register RECSTREAM *rstrm;
515 register caddr_t where;
519 where = rstrm->in_base;
520 i = (long) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
522 len = rstrm->in_size - i;
523 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
525 rstrm->in_finger = where;
527 rstrm->in_boundry = where;
531 static bool_t /* knows nothing about records! Only about input buffers */
532 get_input_bytes(rstrm, addr, len)
533 register RECSTREAM *rstrm;
534 register caddr_t addr;
537 register int current;
540 current = rstrm->in_boundry - rstrm->in_finger;
542 if (! fill_input_buf(rstrm))
546 current = (len < current) ? len : current;
547 memcpy (addr, rstrm->in_finger, current);
548 rstrm->in_finger += current;
555 static bool_t /* next two bytes of the input stream are treated as a header */
556 set_input_fragment(rstrm)
557 register RECSTREAM *rstrm;
561 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
563 header = (long)ntohl(header);
564 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
565 rstrm->fbtbc = header & (~LAST_FRAG);
569 static bool_t /* consumes input bytes; knows nothing about records! */
570 skip_input_bytes(rstrm, cnt)
571 register RECSTREAM *rstrm;
574 register int current;
577 current = rstrm->in_boundry - rstrm->in_finger;
579 if (! fill_input_buf(rstrm))
583 current = (cnt < current) ? cnt : current;
584 rstrm->in_finger += current;