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.
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 ((u_long)(1 << 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_long *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.
131 void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
133 register u_int sendsize;
134 register u_int recvsize;
136 int (*readit) (); /* like read, but pass it a tcp_handle, not sock */
137 int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
139 register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM));
142 (void) fprintf(stderr, "xdrrec_create: out of memory\n");
144 * This is bad. Should rework xdrrec_create to
145 * return a handle, and in this case return NULL
150 * adjust sizes and allocate buffer quad byte aligned
152 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
153 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
155 mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
156 if (rstrm->the_buffer == NULL) {
157 (void) fprintf(stderr, "xdrrec_create: out of memory\n");
160 for (rstrm->out_base = rstrm->the_buffer;
161 (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
163 rstrm->in_base = rstrm->out_base + sendsize;
167 xdrs->x_ops = &xdrrec_ops;
168 xdrs->x_private = (caddr_t) rstrm;
169 rstrm->tcp_handle = tcp_handle;
170 rstrm->readit = readit;
171 rstrm->writeit = writeit;
172 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
173 rstrm->frag_header = (u_long *) rstrm->out_base;
174 rstrm->out_finger += sizeof(u_long);
175 rstrm->out_boundry += sendsize;
176 rstrm->frag_sent = FALSE;
177 rstrm->in_size = recvsize;
178 rstrm->in_boundry = rstrm->in_base;
179 rstrm->in_finger = (rstrm->in_boundry += recvsize);
181 rstrm->last_frag = TRUE;
186 * The reoutines defined below are the xdr ops which will go into the
187 * xdr handle filled in by xdrrec_create.
190 static bool_t xdrrec_getlong(xdrs, lp)
194 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
195 register long *buflp = (long *) (rstrm->in_finger);
198 /* first try the inline, fast case */
199 if ((rstrm->fbtbc >= sizeof(long)) &&
200 (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) {
201 *lp = (long) ntohl((u_long) (*buflp));
202 rstrm->fbtbc -= sizeof(long);
203 rstrm->in_finger += sizeof(long);
205 if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long)))
208 *lp = (long) ntohl((u_long) mylong);
213 static bool_t xdrrec_putlong(xdrs, lp)
217 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
218 register long *dest_lp = ((long *) (rstrm->out_finger));
220 if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
222 * this case should almost never happen so the code is
225 rstrm->out_finger -= sizeof(long);
227 rstrm->frag_sent = TRUE;
228 if (!flush_out(rstrm, FALSE))
230 dest_lp = ((long *) (rstrm->out_finger));
231 rstrm->out_finger += sizeof(long);
233 *dest_lp = (long) htonl((u_long) (*lp));
238 /* must manage buffers, fragments, and records */
239 xdrrec_getbytes(xdrs, addr, len)
241 register caddr_t addr;
244 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
245 register int current;
248 current = rstrm->fbtbc;
250 if (rstrm->last_frag)
252 if (!set_input_fragment(rstrm))
256 current = (len < current) ? len : current;
257 if (!get_input_bytes(rstrm, addr, current))
260 rstrm->fbtbc -= current;
266 static bool_t xdrrec_putbytes(xdrs, addr, len)
268 register caddr_t addr;
271 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
272 register int current;
275 current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
276 current = (len < current) ? len : current;
277 bcopy(addr, rstrm->out_finger, current);
278 rstrm->out_finger += current;
281 if (rstrm->out_finger == rstrm->out_boundry) {
282 rstrm->frag_sent = TRUE;
283 if (!flush_out(rstrm, FALSE))
290 static u_int xdrrec_getpos(xdrs)
293 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
296 pos = lseek((int) rstrm->tcp_handle, (long) 0, 1);
298 switch (xdrs->x_op) {
301 pos += rstrm->out_finger - rstrm->out_base;
305 pos -= rstrm->in_boundry - rstrm->in_finger;
312 return ((u_int) pos);
315 static bool_t xdrrec_setpos(xdrs, pos)
319 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
320 u_int currpos = xdrrec_getpos(xdrs);
321 int delta = currpos - pos;
324 if ((int) currpos != -1)
325 switch (xdrs->x_op) {
328 newpos = rstrm->out_finger - delta;
329 if ((newpos > (caddr_t) (rstrm->frag_header)) &&
330 (newpos < rstrm->out_boundry)) {
331 rstrm->out_finger = newpos;
337 newpos = rstrm->in_finger - delta;
338 if ((delta < (int) (rstrm->fbtbc)) &&
339 (newpos <= rstrm->in_boundry) &&
340 (newpos >= rstrm->in_base)) {
341 rstrm->in_finger = newpos;
342 rstrm->fbtbc -= delta;
350 static long *xdrrec_inline(xdrs, len)
354 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
357 switch (xdrs->x_op) {
360 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
361 buf = (long *) rstrm->out_finger;
362 rstrm->out_finger += len;
367 if ((len <= rstrm->fbtbc) &&
368 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
369 buf = (long *) rstrm->in_finger;
371 rstrm->in_finger += len;
378 static void xdrrec_destroy(xdrs)
381 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
383 mem_free(rstrm->the_buffer,
384 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
385 mem_free((caddr_t) rstrm, sizeof(RECSTREAM));
390 * Exported routines to manage xdr records
394 * Before reading (deserializing from the stream, one should always call
395 * this procedure to guarantee proper record alignment.
397 bool_t xdrrec_skiprecord(xdrs)
400 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
402 while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
403 if (!skip_input_bytes(rstrm, rstrm->fbtbc))
406 if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
409 rstrm->last_frag = FALSE;
414 * Look ahead fuction.
415 * Returns TRUE iff there is no more input in the buffer
416 * after consuming the rest of the current record.
418 bool_t xdrrec_eof(xdrs)
421 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
423 while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
424 if (!skip_input_bytes(rstrm, rstrm->fbtbc))
427 if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
430 if (rstrm->in_finger == rstrm->in_boundry)
436 * The client must tell the package when an end-of-record has occurred.
437 * The second paraemters tells whether the record should be flushed to the
438 * (output) tcp stream. (This let's the package support batched or
439 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
441 bool_t xdrrec_endofrecord(xdrs, sendnow)
445 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
446 register u_long len; /* fragment length */
448 if (sendnow || rstrm->frag_sent ||
449 ((u_long) rstrm->out_finger + sizeof(u_long) >=
450 (u_long) rstrm->out_boundry)) {
451 rstrm->frag_sent = FALSE;
452 return (flush_out(rstrm, TRUE));
454 len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
456 *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG);
457 rstrm->frag_header = (u_long *) rstrm->out_finger;
458 rstrm->out_finger += sizeof(u_long);
464 * Internal useful routines
466 static bool_t flush_out(rstrm, eor)
467 register RECSTREAM *rstrm;
470 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
471 register u_long len = (u_long) (rstrm->out_finger) -
472 (u_long) (rstrm->frag_header) - sizeof(u_long);
474 *(rstrm->frag_header) = htonl(len | eormask);
475 len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
476 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
479 rstrm->frag_header = (u_long *) rstrm->out_base;
480 rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long);
485 /* knows nothing about records! Only about input buffers */
486 fill_input_buf(rstrm)
487 register RECSTREAM *rstrm;
489 register caddr_t where;
493 where = rstrm->in_base;
494 i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
496 len = rstrm->in_size - i;
497 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
499 rstrm->in_finger = where;
501 rstrm->in_boundry = where;
506 /* knows nothing about records! Only about input buffers */
507 get_input_bytes(rstrm, addr, len)
508 register RECSTREAM *rstrm;
509 register caddr_t addr;
512 register int current;
515 current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
517 if (!fill_input_buf(rstrm))
521 current = (len < current) ? len : current;
522 bcopy(rstrm->in_finger, addr, current);
523 rstrm->in_finger += current;
531 /* next two bytes of the input stream are treated as a header */
532 set_input_fragment(rstrm)
533 register RECSTREAM *rstrm;
537 if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
539 header = (long) ntohl(header);
540 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
541 rstrm->fbtbc = header & (~LAST_FRAG);
546 /* consumes input bytes; knows nothing about records! */
547 skip_input_bytes(rstrm, cnt)
548 register RECSTREAM *rstrm;
551 register int current;
554 current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
556 if (!fill_input_buf(rstrm))
560 current = (cnt < current) ? cnt : current;
561 rstrm->in_finger += current;
567 static u_int fix_buf_size(s)