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). The high 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.
52 #include <rpc/types.h>
54 #include <netinet/in.h>
58 static u_int fix_buf_size();
60 static bool_t xdrrec_getlong();
61 static bool_t xdrrec_putlong();
62 static bool_t xdrrec_getbytes();
63 static bool_t xdrrec_putbytes();
64 static u_int xdrrec_getpos();
65 static bool_t xdrrec_setpos();
66 static long * xdrrec_inline();
67 static void xdrrec_destroy();
69 static struct xdr_ops xdrrec_ops = {
81 * A record is composed of one or more record fragments.
82 * A record fragment is a two-byte header followed by zero to
83 * 2**32-1 bytes. The header is treated as a long unsigned and is
84 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
85 * are a byte count of the fragment. The highest order bit is a boolean:
86 * 1 => this fragment is the last fragment of the record,
87 * 0 => this fragment is followed by more fragment(s).
89 * The fragment/record machinery is not general; it is constructed to
90 * meet the needs of xdr and rpc based on tcp.
93 #define LAST_FRAG (1UL << 31)
95 typedef struct rec_strm {
102 caddr_t out_base; /* output buffer (points to frag header) */
103 caddr_t out_finger; /* next output position */
104 caddr_t out_boundry; /* data cannot up to this address */
105 u_int32_t *frag_header; /* beginning of curren fragment */
106 bool_t frag_sent; /* true if buffer sent in middle of record */
111 u_long in_size; /* fixed size of the input buffer */
113 caddr_t in_finger; /* location of next byte to be had */
114 caddr_t in_boundry; /* can read up to this location */
115 long fbtbc; /* fragment bytes to be consumed */
123 * Create an xdr handle for xdrrec
124 * xdrrec_create fills in xdrs. Sendsize and recvsize are
125 * send and recv buffer sizes (0 => use default).
126 * tcp_handle is an opaque handle that is passed as the first parameter to
127 * the procedures readit and writeit. Readit and writeit are read and
128 * write respectively. They are like the system
129 * calls expect that they take an opaque handle rather than an fd.
132 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
134 register u_int sendsize;
135 register u_int recvsize;
137 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
138 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
140 register RECSTREAM *rstrm =
141 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
144 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
146 * This is bad. Should rework xdrrec_create to
147 * return a handle, and in this case return NULL
152 * adjust sizes and allocate buffer quad byte aligned
154 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
155 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
156 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
157 if (rstrm->the_buffer == NULL) {
158 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
161 for (rstrm->out_base = rstrm->the_buffer;
162 (u_int)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
164 rstrm->in_base = rstrm->out_base + sendsize;
168 xdrs->x_ops = &xdrrec_ops;
169 xdrs->x_private = (caddr_t)rstrm;
170 rstrm->tcp_handle = tcp_handle;
171 rstrm->readit = readit;
172 rstrm->writeit = writeit;
173 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
174 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
175 rstrm->out_finger += 4;
176 rstrm->out_boundry += sendsize;
177 rstrm->frag_sent = FALSE;
178 rstrm->in_size = recvsize;
179 rstrm->in_boundry = rstrm->in_base;
180 rstrm->in_finger = (rstrm->in_boundry += recvsize);
182 rstrm->last_frag = TRUE;
187 * The reoutines defined below are the xdr ops which will go into the
188 * xdr handle filled in by xdrrec_create.
192 xdrrec_getlong(xdrs, lp)
196 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
197 register int32_t *buflp = (int32_t *) rstrm->in_finger;
200 /* first try the inline, fast case */
201 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
202 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
204 *lp = (int32_t) ntohl(*buflp);
205 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
206 rstrm->in_finger += BYTES_PER_XDR_UNIT;
208 if (! xdrrec_getbytes(xdrs, (caddr_t) &mylong,
211 *lp = (int32_t) ntohl(mylong);
217 xdrrec_putlong(xdrs, lp)
221 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
222 register int32_t *dest_lp = (int32_t *) rstrm->out_finger;
224 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry) {
226 * this case should almost never happen so the code is
229 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
230 rstrm->frag_sent = TRUE;
231 if (! flush_out(rstrm, FALSE))
233 dest_lp = (int32_t *) rstrm->out_finger;
234 rstrm->out_finger += BYTES_PER_XDR_UNIT;
236 *dest_lp = htonl(*lp);
240 static bool_t /* must manage buffers, fragments, and records */
241 xdrrec_getbytes(xdrs, addr, len)
243 register caddr_t addr;
246 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
247 register int current;
250 current = rstrm->fbtbc;
252 if (rstrm->last_frag)
254 if (! set_input_fragment(rstrm))
258 current = (len < current) ? len : current;
259 if (! get_input_bytes(rstrm, addr, current))
262 rstrm->fbtbc -= current;
269 xdrrec_putbytes(xdrs, addr, len)
271 register caddr_t addr;
274 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
275 register int current;
278 current = rstrm->out_boundry - rstrm->out_finger;
279 current = (len < current) ? len : current;
280 bcopy(addr, rstrm->out_finger, current);
281 rstrm->out_finger += current;
284 if (rstrm->out_finger == rstrm->out_boundry) {
285 rstrm->frag_sent = TRUE;
286 if (! flush_out(rstrm, FALSE))
297 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
300 pos = lseek((int)rstrm->tcp_handle, (long) 0, 1);
302 switch (xdrs->x_op) {
305 pos += rstrm->out_finger - rstrm->out_base;
309 pos -= rstrm->in_boundry - rstrm->in_finger;
316 return ((u_int) pos);
320 xdrrec_setpos(xdrs, pos)
324 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
325 u_int currpos = xdrrec_getpos(xdrs);
326 int delta = currpos - pos;
329 if ((int)currpos != -1)
330 switch (xdrs->x_op) {
333 newpos = rstrm->out_finger - delta;
334 if (newpos > (caddr_t) rstrm->frag_header &&
335 newpos < rstrm->out_boundry)
337 rstrm->out_finger = newpos;
343 newpos = rstrm->in_finger - delta;
344 if ((delta < (int)(rstrm->fbtbc)) &&
345 (newpos <= rstrm->in_boundry) &&
346 (newpos >= rstrm->in_base)) {
347 rstrm->in_finger = newpos;
348 rstrm->fbtbc -= delta;
357 xdrrec_inline(xdrs, len)
361 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
364 switch (xdrs->x_op) {
367 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
368 buf = (long *) rstrm->out_finger;
369 rstrm->out_finger += len;
374 if ((len <= rstrm->fbtbc) &&
375 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
376 buf = (long *) rstrm->in_finger;
378 rstrm->in_finger += len;
389 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
391 mem_free(rstrm->the_buffer,
392 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
393 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
398 * Exported routines to manage xdr records
402 * Before reading (deserializing from the stream, one should always call
403 * this procedure to guarantee proper record alignment.
406 xdrrec_skiprecord(xdrs)
409 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
411 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
412 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
415 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
418 rstrm->last_frag = FALSE;
423 * Look ahead fuction.
424 * Returns TRUE iff there is no more input in the buffer
425 * after consuming the rest of the current record.
431 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
433 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
434 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
437 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
440 if (rstrm->in_finger == rstrm->in_boundry)
446 * The client must tell the package when an end-of-record has occurred.
447 * The second paraemters tells whether the record should be flushed to the
448 * (output) tcp stream. (This let's the package support batched or
449 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
452 xdrrec_endofrecord(xdrs, sendnow)
456 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
457 register u_long len; /* fragment length */
459 if (sendnow || rstrm->frag_sent
460 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
462 rstrm->frag_sent = FALSE;
463 return flush_out(rstrm, TRUE);
465 len = (rstrm->out_finger - (char *) rstrm->frag_header
466 - BYTES_PER_XDR_UNIT);
467 *rstrm->frag_header = htonl((u_long)len | LAST_FRAG);
468 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
469 rstrm->out_finger += BYTES_PER_XDR_UNIT;
475 * Internal useful routines
478 flush_out(rstrm, eor)
479 register RECSTREAM *rstrm;
482 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
483 register u_long len = (rstrm->out_finger
484 - (char *) rstrm->frag_header
485 - BYTES_PER_XDR_UNIT);
487 *rstrm->frag_header = htonl(len | eormask);
488 len = rstrm->out_finger - rstrm->out_base;
489 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
492 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
493 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
497 static bool_t /* knows nothing about records! Only about input buffers */
498 fill_input_buf(rstrm)
499 register RECSTREAM *rstrm;
501 register caddr_t where;
505 where = rstrm->in_base;
506 i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
508 len = rstrm->in_size - i;
509 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
511 rstrm->in_finger = where;
513 rstrm->in_boundry = where;
517 static bool_t /* knows nothing about records! Only about input buffers */
518 get_input_bytes(rstrm, addr, len)
519 register RECSTREAM *rstrm;
520 register caddr_t addr;
523 register int current;
526 current = rstrm->in_boundry - rstrm->in_finger;
528 if (! fill_input_buf(rstrm))
532 current = (len < current) ? len : current;
533 bcopy(rstrm->in_finger, addr, current);
534 rstrm->in_finger += current;
541 static bool_t /* next two bytes of the input stream are treated as a header */
542 set_input_fragment(rstrm)
543 register RECSTREAM *rstrm;
547 if (! get_input_bytes(rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
549 header = ntohl(header);
550 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
551 rstrm->fbtbc = header & ~LAST_FRAG;
555 static bool_t /* consumes input bytes; knows nothing about records! */
556 skip_input_bytes(rstrm, cnt)
557 register RECSTREAM *rstrm;
560 register int current;
563 current = rstrm->in_boundry - rstrm->in_finger;
565 if (! fill_input_buf(rstrm))
569 current = (cnt < current) ? cnt : current;
570 rstrm->in_finger += current;