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_WINSOCK2_H
61 #ifdef HAVE_ARPA_INET_H
62 #include <arpa/inet.h>
65 #include <rpc/types.h>
70 static u_int fix_buf_size();
72 static bool_t xdrrec_getlong();
73 static bool_t xdrrec_putlong();
74 static bool_t xdrrec_getbytes();
75 static bool_t xdrrec_putbytes();
76 static u_int xdrrec_getpos();
77 static bool_t xdrrec_setpos();
78 static long * xdrrec_inline();
79 static void xdrrec_destroy();
81 static struct xdr_ops xdrrec_ops = {
93 * A record is composed of one or more record fragments.
94 * A record fragment is a two-byte header followed by zero to
95 * 2**32-1 bytes. The header is treated as a long unsigned and is
96 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
97 * are a byte count of the fragment. The highest order bit is a boolean:
98 * 1 => this fragment is the last fragment of the record,
99 * 0 => this fragment is followed by more fragment(s).
101 * The fragment/record machinery is not general; it is constructed to
102 * meet the needs of xdr and rpc based on tcp.
105 #define LAST_FRAG ((u_long)(1 << 31))
107 typedef struct rec_strm {
114 caddr_t out_base; /* output buffer (points to frag header) */
115 caddr_t out_finger; /* next output position */
116 caddr_t out_boundry; /* data cannot up to this address */
117 u_long *frag_header; /* beginning of curren fragment */
118 bool_t frag_sent; /* true if buffer sent in middle of record */
123 u_long in_size; /* fixed size of the input buffer */
125 caddr_t in_finger; /* location of next byte to be had */
126 caddr_t in_boundry; /* can read up to this location */
127 long fbtbc; /* fragment bytes to be consumed */
133 static bool_t flush_out(register RECSTREAM *, bool_t );
134 static bool_t get_input_bytes(register RECSTREAM *, register caddr_t, register int );
135 static bool_t set_input_fragment(register RECSTREAM *);
136 static bool_t skip_input_bytes(register RECSTREAM *, long);
139 * Create an xdr handle for xdrrec
140 * xdrrec_create fills in xdrs. Sendsize and recvsize are
141 * send and recv buffer sizes (0 => use default).
142 * tcp_handle is an opaque handle that is passed as the first parameter to
143 * the procedures readit and writeit. Readit and writeit are read and
144 * write respectively. They are like the system
145 * calls expect that they take an opaque handle rather than an fd.
148 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
150 register u_int sendsize;
151 register u_int recvsize;
153 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
154 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
156 register RECSTREAM *rstrm =
157 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
160 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
162 * This is bad. Should rework xdrrec_create to
163 * return a handle, and in this case return NULL
168 * adjust sizes and allocate buffer quad byte aligned
170 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
171 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
172 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
173 if (rstrm->the_buffer == NULL) {
174 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
177 for (rstrm->out_base = rstrm->the_buffer;
178 (long) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
180 rstrm->in_base = rstrm->out_base + sendsize;
184 xdrs->x_ops = &xdrrec_ops;
185 xdrs->x_private = (caddr_t)rstrm;
186 rstrm->tcp_handle = tcp_handle;
187 rstrm->readit = readit;
188 rstrm->writeit = writeit;
189 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
190 rstrm->frag_header = (u_long *)rstrm->out_base;
191 rstrm->out_finger += sizeof(u_long);
192 rstrm->out_boundry += sendsize;
193 rstrm->frag_sent = FALSE;
194 rstrm->in_size = recvsize;
195 rstrm->in_boundry = rstrm->in_base;
196 rstrm->in_finger = (rstrm->in_boundry += recvsize);
198 rstrm->last_frag = TRUE;
203 * The reoutines defined below are the xdr ops which will go into the
204 * xdr handle filled in by xdrrec_create.
208 xdrrec_getlong(xdrs, lp)
212 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
213 register long *buflp = (long *)(rstrm->in_finger);
216 /* first try the inline, fast case */
217 if ((rstrm->fbtbc >= sizeof(long)) &&
218 ((rstrm->in_boundry - (caddr_t) buflp) >= sizeof(long))) {
219 *lp = (long)ntohl((u_long)(*buflp));
220 rstrm->fbtbc -= sizeof(long);
221 rstrm->in_finger += sizeof(long);
223 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long)))
225 *lp = (long)ntohl((u_long)mylong);
231 xdrrec_putlong(xdrs, lp)
235 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
236 register long *dest_lp = ((long *)(rstrm->out_finger));
238 if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
240 * this case should almost never happen so the code is
243 rstrm->out_finger -= sizeof(long);
244 rstrm->frag_sent = TRUE;
245 if (! flush_out(rstrm, FALSE))
247 dest_lp = ((long *)(rstrm->out_finger));
248 rstrm->out_finger += sizeof(long);
250 *dest_lp = (long)htonl((u_long)(*lp));
254 static bool_t /* must manage buffers, fragments, and records */
255 xdrrec_getbytes(xdrs, addr, len)
257 register caddr_t addr;
260 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
261 register int current;
264 current = rstrm->fbtbc;
266 if (rstrm->last_frag)
268 if (! set_input_fragment(rstrm))
272 current = (len < current) ? len : current;
273 if (! get_input_bytes(rstrm, addr, current))
276 rstrm->fbtbc -= current;
283 xdrrec_putbytes(xdrs, addr, len)
285 register caddr_t addr;
288 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
289 register int current;
292 current = rstrm->out_boundry - rstrm->out_finger;
293 current = (len < current) ? len : current;
294 memcpy (rstrm->out_finger, addr, current);
295 rstrm->out_finger += current;
298 if (rstrm->out_finger == rstrm->out_boundry) {
299 rstrm->frag_sent = TRUE;
300 if (! flush_out(rstrm, FALSE))
311 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
314 pos = lseek(rstrm->tcp_handle, (long) 0, 1);
316 switch (xdrs->x_op) {
319 pos += rstrm->out_finger - rstrm->out_base;
323 pos -= rstrm->in_boundry - rstrm->in_finger;
330 return ((u_int) pos);
334 xdrrec_setpos(xdrs, pos)
338 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
339 u_int currpos = xdrrec_getpos(xdrs);
340 int delta = currpos - pos;
343 if ((int)currpos != -1)
344 switch (xdrs->x_op) {
346 case XDR_FREE: break; /* keep gcc happy */
349 newpos = rstrm->out_finger - delta;
350 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
351 (newpos < rstrm->out_boundry)) {
352 rstrm->out_finger = newpos;
358 newpos = rstrm->in_finger - delta;
359 if ((delta < (int)(rstrm->fbtbc)) &&
360 (newpos <= rstrm->in_boundry) &&
361 (newpos >= rstrm->in_base)) {
362 rstrm->in_finger = newpos;
363 rstrm->fbtbc -= delta;
372 xdrrec_inline(xdrs, len)
376 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
379 switch (xdrs->x_op) {
381 case XDR_FREE: break; /* keep gcc happy */
384 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
385 buf = (long *) rstrm->out_finger;
386 rstrm->out_finger += len;
391 if ((len <= rstrm->fbtbc) &&
392 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
393 buf = (long *) rstrm->in_finger;
395 rstrm->in_finger += len;
406 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
408 mem_free(rstrm->the_buffer,
409 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
410 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
415 * Exported routines to manage xdr records
419 * Before reading (deserializing from the stream, one should always call
420 * this procedure to guarantee proper record alignment.
423 xdrrec_skiprecord(xdrs)
426 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
428 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
429 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
432 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
435 rstrm->last_frag = FALSE;
440 * Look ahead fuction.
441 * Returns TRUE iff there is no more input in the buffer
442 * after consuming the rest of the current record.
448 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
450 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
451 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
454 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
457 if (rstrm->in_finger == rstrm->in_boundry)
463 * The client must tell the package when an end-of-record has occurred.
464 * The second paraemters tells whether the record should be flushed to the
465 * (output) tcp stream. (This let's the package support batched or
466 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
469 xdrrec_endofrecord(xdrs, sendnow)
473 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
474 register u_long len; /* fragment length */
476 if (sendnow || rstrm->frag_sent ||
477 ((u_long)rstrm->out_finger + sizeof(u_long) >=
478 (u_long)rstrm->out_boundry)) {
479 rstrm->frag_sent = FALSE;
480 return (flush_out(rstrm, TRUE));
482 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
484 *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
485 rstrm->frag_header = (u_long *)rstrm->out_finger;
486 rstrm->out_finger += sizeof(u_long);
492 * Internal useful routines
495 flush_out(rstrm, eor)
496 register RECSTREAM *rstrm;
499 register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
500 register u_long len = (u_long)(rstrm->out_finger) -
501 (u_long)(rstrm->frag_header) - sizeof(u_long);
503 *(rstrm->frag_header) = htonl(len | eormask);
504 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
505 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
508 rstrm->frag_header = (u_long *)rstrm->out_base;
509 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
513 static bool_t /* knows nothing about records! Only about input buffers */
514 fill_input_buf(rstrm)
515 register RECSTREAM *rstrm;
517 register caddr_t where;
521 where = rstrm->in_base;
522 i = (long) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
524 len = rstrm->in_size - i;
525 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
527 rstrm->in_finger = where;
529 rstrm->in_boundry = where;
533 static bool_t /* knows nothing about records! Only about input buffers */
534 get_input_bytes(rstrm, addr, len)
535 register RECSTREAM *rstrm;
536 register caddr_t addr;
539 register int current;
542 current = rstrm->in_boundry - rstrm->in_finger;
544 if (! fill_input_buf(rstrm))
548 current = (len < current) ? len : current;
549 memcpy (addr, rstrm->in_finger, current);
550 rstrm->in_finger += current;
557 static bool_t /* next two bytes of the input stream are treated as a header */
558 set_input_fragment(rstrm)
559 register RECSTREAM *rstrm;
563 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
565 header = (long)ntohl(header);
566 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
567 rstrm->fbtbc = header & (~LAST_FRAG);
571 static bool_t /* consumes input bytes; knows nothing about records! */
572 skip_input_bytes(rstrm, cnt)
573 register RECSTREAM *rstrm;
576 register int current;
579 current = rstrm->in_boundry - rstrm->in_finger;
581 if (! fill_input_buf(rstrm))
585 current = (cnt < current) ? cnt : current;
586 rstrm->in_finger += current;