1 /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
2 * unrestricted use provided that this legend is included on all tape
3 * media and as a part of the software program in whole or part. Users
4 * may copy or modify Sun RPC without charge, but are not authorized
5 * to license or distribute it to anyone else except as part of a product or
6 * program developed by the user.
8 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
9 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 * Sun RPC is provided with no support and without any obligation on the
13 * part of Sun Microsystems, Inc. to assist in its use, correction,
14 * modification or enhancement.
16 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
17 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
18 * OR ANY PART THEREOF.
20 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
21 * or profits or other special, indirect and consequential damages, even if
22 * Sun has been advised of the possibility of such damages.
24 * Sun Microsystems, Inc.
26 * Mountain View, California 94043
30 static char sccsid[] = "@(#)xdr_rec.c 1.1 86/02/03 Copyr 1984 Sun Micro";
33 /* * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
34 * layer above tcp (for rpc's use).
36 * Copyright (C) 1984, Sun Microsystems, Inc.
38 * These routines interface XDRSTREAMS to a tcp/ip connection.
39 * There is a record marking layer between the xdr stream
40 * and the tcp transport level. A record is composed on one or more
41 * record fragments. A record fragment is a thirty-two bit header followed
42 * by n bytes of data, where n is contained in the header. The header
43 * is represented as a htonl(afs_uint32). Thegh order bit encodes
44 * whether or not the fragment is the last fragment of the record
45 * (1 => fragment is last, 0 => more fragments to follow.
46 * The other 31 bits encode the byte length of the fragment.
49 #include <afs/param.h>
54 #include <netinet/in.h>
60 extern afs_int32 lseek();
62 static u_int fix_buf_size();
64 static bool_t flush_out();
65 static bool_t get_input_bytes();
66 static bool_t set_input_fragment();
67 static bool_t skip_input_bytes();
69 static bool_t xdrrec_getint32();
70 static bool_t xdrrec_putint32();
71 static bool_t xdrrec_getbytes();
72 static bool_t xdrrec_putbytes();
73 static u_int xdrrec_getpos();
74 static bool_t xdrrec_setpos();
75 static afs_int32 * xdrrec_inline();
76 static void xdrrec_destroy();
78 static struct xdr_ops xdrrec_ops = {
89 /* * A record is composed of one or more record fragments.
90 * A record fragment is a two-byte header followed by zero to
91 * 2**32-1 bytes. The header is treated as an afs_int32 unsigned and is
92 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
93 * are a byte count of the fragment. The highest order bit is a boolean:
94 * 1 => this fragment is the last fragment of the record,
95 * 0 => this fragment is followed by more fragment(s).
97 * The fragment/record machinery is not general; it is constructed to
98 * meet the needs of xdr and rpc based on tcp.
101 #define LAST_FRAG ((afs_uint32)(1 << 31))
103 typedef struct rec_strm {
108 caddr_t out_base; /* output buffer (points to frag header) */
109 caddr_t out_finger; /* next output position */
110 caddr_t out_boundry; /* data cannot up to this address */
111 afs_uint32 *frag_header; /* beginning of curren fragment */
112 bool_t frag_sent; /* true if buffer sent in middle of record */
116 afs_uint32 in_size; /* fixed size of the input buffer */
118 caddr_t in_finger; /* location of next byte to be had */
119 caddr_t in_boundry; /* can read up to this location */
120 afs_int32 fbtbc; /* fragment bytes to be consumed */
127 /* * Create an xdr handle for xdrrec
128 * xdrrec_create fills in xdrs. Sendsize and recvsize are
129 * send and recv buffer sizes (0 => use default).
130 * tcp_handle is an opaque handle that is passed as the first parameter to
131 * the procedures readit and writeit. Readit and writeit are read and
132 * write respectively. They are like the system
133 * calls expect that they take an opaque handle rather than an fd.
136 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
141 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
142 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
144 register RECSTREAM *rstrm =
145 (RECSTREAM *)osi_alloc(sizeof(RECSTREAM));
149 * This is bad. Should rework xdrrec_create to
150 * return a handle, and in this case return NULL
154 xdrs->x_ops = &xdrrec_ops;
155 xdrs->x_private = (caddr_t)rstrm;
156 rstrm->tcp_handle = tcp_handle;
157 rstrm->readit = readit;
158 rstrm->writeit = writeit;
159 sendsize = fix_buf_size(sendsize);
160 if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
161 osi_alloc(sendsize)) == NULL) {
164 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
165 rstrm->out_finger += sizeof(afs_uint32);
166 rstrm->out_boundry += sendsize;
167 rstrm->frag_sent = FALSE;
168 rstrm->in_size = recvsize = fix_buf_size(recvsize);
169 if ((rstrm->in_base = rstrm->in_boundry=osi_alloc(recvsize)) == NULL) {
172 rstrm->in_finger = (rstrm->in_boundry += recvsize);
174 rstrm->last_frag = TRUE;
175 rstrm->sendsize = sendsize;
176 rstrm->recvsize = recvsize;
180 /* * The reoutines defined below are the xdr ops which will go into the
181 * xdr handle filled in by xdrrec_create.
185 xdrrec_getint32(xdrs, lp)
189 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
190 register afs_int32 *buflp = (afs_int32 *)(rstrm->in_finger);
193 /* first try the inline, fast case */
194 if ((rstrm->fbtbc >= sizeof(afs_int32)) &&
195 (((int)rstrm->in_boundry - (int)buflp) >= sizeof(afs_int32))) {
197 rstrm->fbtbc -= sizeof(afs_int32);
198 rstrm->in_finger += sizeof(afs_int32);
200 if (! xdrrec_getbytes(xdrs, (caddr_t)&myint32, sizeof(afs_int32)))
202 *lp = ntohl(myint32);
208 xdrrec_putint32(xdrs, lp)
212 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
213 register afs_int32 *dest_lp = ((afs_int32 *)(rstrm->out_finger));
215 if ((rstrm->out_finger += sizeof(afs_int32)) > rstrm->out_boundry) {
217 * this case should almost never happen so the code is
220 rstrm->out_finger -= sizeof(afs_int32);
221 rstrm->frag_sent = TRUE;
222 if (! flush_out(rstrm, FALSE))
224 dest_lp = ((afs_int32 *)(rstrm->out_finger));
225 rstrm->out_finger += sizeof(afs_int32);
227 *dest_lp = htonl(*lp);
231 static bool_t /* must manage buffers, fragments, and records */
232 xdrrec_getbytes(xdrs, addr, len)
234 register caddr_t addr;
237 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
238 register int current;
241 current = rstrm->fbtbc;
243 if (rstrm->last_frag)
245 if (! set_input_fragment(rstrm))
249 current = (len < current) ? len : current;
250 if (! get_input_bytes(rstrm, addr, current))
253 rstrm->fbtbc -= current;
260 xdrrec_putbytes(xdrs, addr, len)
262 register caddr_t addr;
265 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
266 register int current;
269 current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger;
270 current = (len < current) ? len : current;
271 bcopy(addr, rstrm->out_finger, current);
272 rstrm->out_finger += current;
275 if (rstrm->out_finger == rstrm->out_boundry) {
276 rstrm->frag_sent = TRUE;
277 if (! flush_out(rstrm, FALSE))
288 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
291 pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
293 switch (xdrs->x_op) {
296 pos += rstrm->out_finger - rstrm->out_base;
300 pos -= rstrm->in_boundry - rstrm->in_finger;
311 xdrrec_setpos(xdrs, pos)
315 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
316 u_int currpos = xdrrec_getpos(xdrs);
317 int delta = currpos - pos;
320 if ((int)currpos != -1)
321 switch (xdrs->x_op) {
324 newpos = rstrm->out_finger - delta;
325 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
326 (newpos < rstrm->out_boundry)) {
327 rstrm->out_finger = newpos;
333 newpos = rstrm->in_finger - delta;
334 if ((delta < (int)(rstrm->fbtbc)) &&
335 (newpos <= rstrm->in_boundry) &&
336 (newpos >= rstrm->in_base)) {
337 rstrm->in_finger = newpos;
338 rstrm->fbtbc -= delta;
347 xdrrec_inline(xdrs, len)
351 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
352 afs_int32 * buf = NULL;
354 switch (xdrs->x_op) {
357 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
358 buf = (afs_int32 *) rstrm->out_finger;
359 rstrm->out_finger += len;
364 if ((len <= rstrm->fbtbc) &&
365 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
366 buf = (afs_int32 *) rstrm->in_finger;
368 rstrm->in_finger += len;
379 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
381 osi_free(rstrm->out_base, rstrm->sendsize);
382 osi_free(rstrm->in_base, rstrm->recvsize);
383 osi_free((caddr_t)rstrm, sizeof(RECSTREAM));
388 * Exported routines to manage xdr records
392 * Before reading (deserializing from the stream, one should always call
393 * this procedure to guarantee proper record alignment.
396 xdrrec_skiprecord(xdrs)
399 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
401 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
402 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
405 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
408 rstrm->last_frag = FALSE;
413 * Look ahead fuction.
414 * Returns TRUE iff there is no more input in the buffer
415 * after consuming the rest of the current record.
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.
442 xdrrec_endofrecord(xdrs, sendnow)
446 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
447 register afs_uint32 len; /* fragment length */
449 if (sendnow || rstrm->frag_sent ||
450 ((afs_uint32)rstrm->out_finger + sizeof(afs_uint32) >=
451 (afs_uint32)rstrm->out_boundry)) {
452 rstrm->frag_sent = FALSE;
453 return (flush_out(rstrm, TRUE));
455 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->frag_header) -
457 *(rstrm->frag_header) = htonl(len | LAST_FRAG);
458 rstrm->frag_header = (afs_uint32 *)rstrm->out_finger;
459 rstrm->out_finger += sizeof(afs_uint32);
465 * Internal useful routines
468 flush_out(rstrm, eor)
469 register RECSTREAM *rstrm;
472 register afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
473 register afs_uint32 len = (afs_uint32)(rstrm->out_finger) -
474 (afs_uint32)(rstrm->frag_header) - sizeof(afs_uint32);
476 *(rstrm->frag_header) = htonl(len | eormask);
477 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->out_base);
478 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
481 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
482 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(afs_uint32);
486 static bool_t /* knows nothing about records! Only about input buffers */
487 fill_input_buf(rstrm)
488 register RECSTREAM *rstrm;
490 register caddr_t where = rstrm->in_base;
491 register int len = rstrm->in_size;
492 u_int adjust = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
494 /* Bump the current position out to the next alignment boundary*/
498 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
500 rstrm->in_finger = where;
502 rstrm->in_boundry = where;
506 static bool_t /* 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;
530 static bool_t /* next two bytes of the input stream are treated as a header */
531 set_input_fragment(rstrm)
532 register RECSTREAM *rstrm;
536 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
538 header = ntohl(header);
539 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
540 rstrm->fbtbc = header & (~LAST_FRAG);
544 static bool_t /* consumes input bytes; knows nothing about records! */
545 skip_input_bytes(rstrm, cnt)
546 register RECSTREAM *rstrm;
549 register int current;
552 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
554 if (! fill_input_buf(rstrm))
558 current = (cnt < current) ? cnt : current;
559 rstrm->in_finger += current;
572 return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
573 * BYTES_PER_XDR_UNIT;