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 /* * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
31 * layer above tcp (for rpc's use).
33 * Copyright (C) 1984, Sun Microsystems, Inc.
35 * These routines interface XDRSTREAMS to a tcp/ip connection.
36 * There is a record marking layer between the xdr stream
37 * and the tcp transport level. A record is composed on one or more
38 * record fragments. A record fragment is a thirty-two bit header followed
39 * by n bytes of data, where n is contained in the header. The header
40 * is represented as a htonl(afs_uint32). Thegh order bit encodes
41 * whether or not the fragment is the last fragment of the record
42 * (1 => fragment is last, 0 => more fragments to follow.
43 * The other 31 bits encode the byte length of the fragment.
46 #include <afsconfig.h>
47 #include <afs/param.h>
55 #include <netinet/in.h>
61 extern afs_int32 lseek();
63 static u_int fix_buf_size();
65 static bool_t flush_out();
66 static bool_t get_input_bytes();
67 static bool_t set_input_fragment();
68 static bool_t skip_input_bytes();
70 static bool_t xdrrec_getint32();
71 static bool_t xdrrec_putint32();
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 afs_int32 * xdrrec_inline();
77 static void xdrrec_destroy();
79 static struct xdr_ops xdrrec_ops = {
90 /* * A record is composed of one or more record fragments.
91 * A record fragment is a two-byte header followed by zero to
92 * 2**32-1 bytes. The header is treated as an afs_int32 unsigned and is
93 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
94 * are a byte count of the fragment. The highest order bit is a boolean:
95 * 1 => this fragment is the last fragment of the record,
96 * 0 => this fragment is followed by more fragment(s).
98 * The fragment/record machinery is not general; it is constructed to
99 * meet the needs of xdr and rpc based on tcp.
102 #define LAST_FRAG ((afs_uint32)(1 << 31))
104 typedef struct rec_strm {
109 caddr_t out_base; /* output buffer (points to frag header) */
110 caddr_t out_finger; /* next output position */
111 caddr_t out_boundry; /* data cannot up to this address */
112 afs_uint32 *frag_header; /* beginning of curren fragment */
113 bool_t frag_sent; /* true if buffer sent in middle of record */
117 afs_uint32 in_size; /* fixed size of the input buffer */
119 caddr_t in_finger; /* location of next byte to be had */
120 caddr_t in_boundry; /* can read up to this location */
121 afs_int32 fbtbc; /* fragment bytes to be consumed */
128 /* * Create an xdr handle for xdrrec
129 * xdrrec_create fills in xdrs. Sendsize and recvsize are
130 * send and recv buffer sizes (0 => use default).
131 * tcp_handle is an opaque handle that is passed as the first parameter to
132 * the procedures readit and writeit. Readit and writeit are read and
133 * write respectively. They are like the system
134 * calls expect that they take an opaque handle rather than an fd.
137 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
142 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */
143 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */
145 register RECSTREAM *rstrm =
146 (RECSTREAM *)osi_alloc(sizeof(RECSTREAM));
150 * This is bad. Should rework xdrrec_create to
151 * return a handle, and in this case return NULL
155 xdrs->x_ops = &xdrrec_ops;
156 xdrs->x_private = (caddr_t)rstrm;
157 rstrm->tcp_handle = tcp_handle;
158 rstrm->readit = readit;
159 rstrm->writeit = writeit;
160 sendsize = fix_buf_size(sendsize);
161 if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
162 osi_alloc(sendsize)) == NULL) {
165 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
166 rstrm->out_finger += sizeof(afs_uint32);
167 rstrm->out_boundry += sendsize;
168 rstrm->frag_sent = FALSE;
169 rstrm->in_size = recvsize = fix_buf_size(recvsize);
170 if ((rstrm->in_base = rstrm->in_boundry=osi_alloc(recvsize)) == NULL) {
173 rstrm->in_finger = (rstrm->in_boundry += recvsize);
175 rstrm->last_frag = TRUE;
176 rstrm->sendsize = sendsize;
177 rstrm->recvsize = recvsize;
181 /* * The reoutines defined below are the xdr ops which will go into the
182 * xdr handle filled in by xdrrec_create.
186 xdrrec_getint32(xdrs, lp)
190 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
191 register afs_int32 *buflp = (afs_int32 *)(rstrm->in_finger);
194 /* first try the inline, fast case */
195 if ((rstrm->fbtbc >= sizeof(afs_int32)) &&
196 (((int)rstrm->in_boundry - (int)buflp) >= sizeof(afs_int32))) {
198 rstrm->fbtbc -= sizeof(afs_int32);
199 rstrm->in_finger += sizeof(afs_int32);
201 if (! xdrrec_getbytes(xdrs, (caddr_t)&myint32, sizeof(afs_int32)))
203 *lp = ntohl(myint32);
209 xdrrec_putint32(xdrs, lp)
213 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
214 register afs_int32 *dest_lp = ((afs_int32 *)(rstrm->out_finger));
216 if ((rstrm->out_finger += sizeof(afs_int32)) > rstrm->out_boundry) {
218 * this case should almost never happen so the code is
221 rstrm->out_finger -= sizeof(afs_int32);
222 rstrm->frag_sent = TRUE;
223 if (! flush_out(rstrm, FALSE))
225 dest_lp = ((afs_int32 *)(rstrm->out_finger));
226 rstrm->out_finger += sizeof(afs_int32);
228 *dest_lp = htonl(*lp);
232 static bool_t /* must manage buffers, fragments, and records */
233 xdrrec_getbytes(xdrs, addr, len)
235 register caddr_t addr;
238 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
239 register int current;
242 current = rstrm->fbtbc;
244 if (rstrm->last_frag)
246 if (! set_input_fragment(rstrm))
250 current = (len < current) ? len : current;
251 if (! get_input_bytes(rstrm, addr, current))
254 rstrm->fbtbc -= current;
261 xdrrec_putbytes(xdrs, addr, len)
263 register caddr_t addr;
266 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
267 register int current;
270 current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger;
271 current = (len < current) ? len : current;
272 bcopy(addr, rstrm->out_finger, current);
273 rstrm->out_finger += current;
276 if (rstrm->out_finger == rstrm->out_boundry) {
277 rstrm->frag_sent = TRUE;
278 if (! flush_out(rstrm, FALSE))
289 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
292 pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
294 switch (xdrs->x_op) {
297 pos += rstrm->out_finger - rstrm->out_base;
301 pos -= rstrm->in_boundry - rstrm->in_finger;
312 xdrrec_setpos(xdrs, pos)
316 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
317 u_int currpos = xdrrec_getpos(xdrs);
318 int delta = currpos - pos;
321 if ((int)currpos != -1)
322 switch (xdrs->x_op) {
325 newpos = rstrm->out_finger - delta;
326 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
327 (newpos < rstrm->out_boundry)) {
328 rstrm->out_finger = newpos;
334 newpos = rstrm->in_finger - delta;
335 if ((delta < (int)(rstrm->fbtbc)) &&
336 (newpos <= rstrm->in_boundry) &&
337 (newpos >= rstrm->in_base)) {
338 rstrm->in_finger = newpos;
339 rstrm->fbtbc -= delta;
348 xdrrec_inline(xdrs, len)
352 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
353 afs_int32 * buf = NULL;
355 switch (xdrs->x_op) {
358 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
359 buf = (afs_int32 *) rstrm->out_finger;
360 rstrm->out_finger += len;
365 if ((len <= rstrm->fbtbc) &&
366 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
367 buf = (afs_int32 *) rstrm->in_finger;
369 rstrm->in_finger += len;
380 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
382 osi_free(rstrm->out_base, rstrm->sendsize);
383 osi_free(rstrm->in_base, rstrm->recvsize);
384 osi_free((caddr_t)rstrm, sizeof(RECSTREAM));
389 * Exported routines to manage xdr records
393 * Before reading (deserializing from the stream, one should always call
394 * this procedure to guarantee proper record alignment.
397 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.
422 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
424 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
425 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
428 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
431 if (rstrm->in_finger == rstrm->in_boundry)
437 * The client must tell the package when an end-of-record has occurred.
438 * The second paraemters tells whether the record should be flushed to the
439 * (output) tcp stream. (This let's the package support batched or
440 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
443 xdrrec_endofrecord(xdrs, sendnow)
447 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
448 register afs_uint32 len; /* fragment length */
450 if (sendnow || rstrm->frag_sent ||
451 ((afs_uint32)rstrm->out_finger + sizeof(afs_uint32) >=
452 (afs_uint32)rstrm->out_boundry)) {
453 rstrm->frag_sent = FALSE;
454 return (flush_out(rstrm, TRUE));
456 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->frag_header) -
458 *(rstrm->frag_header) = htonl(len | LAST_FRAG);
459 rstrm->frag_header = (afs_uint32 *)rstrm->out_finger;
460 rstrm->out_finger += sizeof(afs_uint32);
466 * Internal useful routines
469 flush_out(rstrm, eor)
470 register RECSTREAM *rstrm;
473 register afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
474 register afs_uint32 len = (afs_uint32)(rstrm->out_finger) -
475 (afs_uint32)(rstrm->frag_header) - sizeof(afs_uint32);
477 *(rstrm->frag_header) = htonl(len | eormask);
478 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->out_base);
479 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
482 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
483 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(afs_uint32);
487 static bool_t /* knows nothing about records! Only about input buffers */
488 fill_input_buf(rstrm)
489 register RECSTREAM *rstrm;
491 register caddr_t where = rstrm->in_base;
492 register int len = rstrm->in_size;
493 u_int adjust = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
495 /* Bump the current position out to the next alignment boundary*/
499 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
501 rstrm->in_finger = where;
503 rstrm->in_boundry = where;
507 static bool_t /* knows nothing about records! Only about input buffers */
508 get_input_bytes(rstrm, addr, len)
509 register RECSTREAM *rstrm;
510 register caddr_t addr;
513 register int current;
516 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
518 if (! fill_input_buf(rstrm))
522 current = (len < current) ? len : current;
523 bcopy(rstrm->in_finger, addr, current);
524 rstrm->in_finger += current;
531 static bool_t /* 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 = ntohl(header);
540 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
541 rstrm->fbtbc = header & (~LAST_FRAG);
545 static bool_t /* consumes input bytes; knows nothing about records! */
546 skip_input_bytes(rstrm, cnt)
547 register RECSTREAM *rstrm;
550 register int current;
553 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
555 if (! fill_input_buf(rstrm))
559 current = (cnt < current) ? cnt : current;
560 rstrm->in_finger += current;
573 return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
574 * BYTES_PER_XDR_UNIT;