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>
59 #include <netinet/in.h>
65 /* * A record is composed of one or more record fragments.
66 * A record fragment is a two-byte header followed by zero to
67 * 2**32-1 bytes. The header is treated as an afs_int32 unsigned and is
68 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
69 * are a byte count of the fragment. The highest order bit is a boolean:
70 * 1 => this fragment is the last fragment of the record,
71 * 0 => this fragment is followed by more fragment(s).
73 * The fragment/record machinery is not general; it is constructed to
74 * meet the needs of xdr and rpc based on tcp.
77 #define LAST_FRAG ((afs_uint32)(1 << 31))
79 typedef struct rec_strm {
83 int (*writeit) (caddr_t tcp_handle, caddr_t out_base, int len);
84 caddr_t out_base; /* output buffer (points to frag header) */
85 caddr_t out_finger; /* next output position */
86 caddr_t out_boundry; /* data cannot up to this address */
87 afs_uint32 *frag_header; /* beginning of curren fragment */
88 bool_t frag_sent; /* true if buffer sent in middle of record */
91 int (*readit) (caddr_t tcp_handle, caddr_t out_base, int len);
92 afs_uint32 in_size; /* fixed size of the input buffer */
94 caddr_t in_finger; /* location of next byte to be had */
95 caddr_t in_boundry; /* can read up to this location */
96 afs_int32 fbtbc; /* fragment bytes to be consumed */
102 /* Prototypes for static routines */
103 static bool_t xdrrec_getint32(XDR * xdrs, afs_int32 * lp);
104 static bool_t xdrrec_putint32(XDR * xdrs, afs_int32 * lp);
105 static bool_t xdrrec_getbytes(XDR * xdrs, register caddr_t addr,
107 static bool_t xdrrec_putbytes(XDR * xdrs, register caddr_t addr,
109 static u_int xdrrec_getpos(register XDR * xdrs);
110 static bool_t xdrrec_setpos(register XDR * xdrs, u_int pos);
111 static afs_int32 *xdrrec_inline(register XDR * xdrs, int len);
112 static void xdrrec_destroy(register XDR * xdrs);
113 static bool_t flush_out(register RECSTREAM * rstrm, bool_t eor);
114 static bool_t fill_input_buf(register RECSTREAM * rstrm);
115 static bool_t get_input_bytes(register RECSTREAM * rstrm,
116 register caddr_t addr, register int len);
117 static bool_t set_input_fragment(register RECSTREAM * rstrm);
118 static bool_t skip_input_bytes(register RECSTREAM * rstrm, int cnt);
119 static u_int fix_buf_size(register u_int s);
121 static struct xdr_ops xdrrec_ops = {
132 /* * Create an xdr handle for xdrrec
133 * xdrrec_create fills in xdrs. Sendsize and recvsize are
134 * send and recv buffer sizes (0 => use default).
135 * tcp_handle is an opaque handle that is passed as the first parameter to
136 * the procedures readit and writeit. Readit and writeit are read and
137 * write respectively. They are like the system
138 * calls expect that they take an opaque handle rather than an fd.
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 *
145 xdrrec_create(register XDR * xdrs, u_int sendsize, u_int recvsize,
146 caddr_t tcp_handle, int (*readit) (caddr_t tcp_handle,
147 caddr_t out_base, int len),
148 int (*writeit) (caddr_t tcp_handle, caddr_t out_base, int len))
150 register RECSTREAM *rstrm = (RECSTREAM *) osi_alloc(sizeof(RECSTREAM));
154 * This is bad. Should rework xdrrec_create to
155 * return a handle, and in this case return NULL
159 xdrs->x_ops = &xdrrec_ops;
160 xdrs->x_private = (caddr_t) rstrm;
161 rstrm->tcp_handle = tcp_handle;
162 rstrm->readit = readit;
163 rstrm->writeit = writeit;
164 sendsize = fix_buf_size(sendsize);
165 if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
166 osi_alloc(sendsize)) == NULL) {
169 rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
170 rstrm->out_finger += sizeof(afs_uint32);
171 rstrm->out_boundry += sendsize;
172 rstrm->frag_sent = FALSE;
173 rstrm->in_size = recvsize = fix_buf_size(recvsize);
174 if ((rstrm->in_base = rstrm->in_boundry = osi_alloc(recvsize)) == NULL) {
177 rstrm->in_finger = (rstrm->in_boundry += recvsize);
179 rstrm->last_frag = TRUE;
180 rstrm->sendsize = sendsize;
181 rstrm->recvsize = recvsize;
185 /* * The reoutines defined below are the xdr ops which will go into the
186 * xdr handle filled in by xdrrec_create.
190 xdrrec_getint32(XDR * xdrs, afs_int32 * lp)
192 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
193 register afs_int32 *buflp = (afs_int32 *) (rstrm->in_finger);
196 /* first try the inline, fast case */
197 if ((rstrm->fbtbc >= sizeof(afs_int32))
198 && (((int)((char *)rstrm->in_boundry - (char *)buflp)) >= sizeof(afs_int32))) {
200 rstrm->fbtbc -= sizeof(afs_int32);
201 rstrm->in_finger += sizeof(afs_int32);
203 if (!xdrrec_getbytes(xdrs, (caddr_t) & myint32, sizeof(afs_int32)))
205 *lp = ntohl(myint32);
211 xdrrec_putint32(XDR * xdrs, afs_int32 * 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);
233 xdrrec_getbytes(XDR * xdrs, register caddr_t addr, register u_int len)
235 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
236 register int current;
239 current = rstrm->fbtbc;
241 if (rstrm->last_frag)
243 if (!set_input_fragment(rstrm))
247 current = (len < current) ? len : current;
248 if (!get_input_bytes(rstrm, addr, current))
251 rstrm->fbtbc -= current;
258 xdrrec_putbytes(XDR * xdrs, register caddr_t addr, register u_int len)
260 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
261 register int current;
264 current = (u_int) (rstrm->out_boundry - rstrm->out_finger);
265 current = (len < current) ? len : current;
266 memcpy(rstrm->out_finger, addr, current);
267 rstrm->out_finger += current;
270 if (rstrm->out_finger == rstrm->out_boundry) {
271 rstrm->frag_sent = TRUE;
272 if (!flush_out(rstrm, FALSE))
280 xdrrec_getpos(register XDR * xdrs)
282 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
285 pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
287 switch (xdrs->x_op) {
290 pos += (u_int)(rstrm->out_finger - rstrm->out_base);
294 pos -= (u_int)(rstrm->in_boundry - rstrm->in_finger);
305 xdrrec_setpos(register XDR * xdrs, u_int pos)
307 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
308 u_int currpos = xdrrec_getpos(xdrs);
309 int delta = currpos - pos;
312 if ((int)currpos != -1)
313 switch (xdrs->x_op) {
316 newpos = rstrm->out_finger - delta;
317 if ((newpos > (caddr_t) (rstrm->frag_header))
318 && (newpos < rstrm->out_boundry)) {
319 rstrm->out_finger = newpos;
325 newpos = rstrm->in_finger - delta;
326 if ((delta < (int)(rstrm->fbtbc)) && (newpos <= rstrm->in_boundry)
327 && (newpos >= rstrm->in_base)) {
328 rstrm->in_finger = newpos;
329 rstrm->fbtbc -= delta;
338 xdrrec_inline(register XDR * xdrs, int len)
340 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
341 afs_int32 *buf = NULL;
343 switch (xdrs->x_op) {
346 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
347 buf = (afs_int32 *) rstrm->out_finger;
348 rstrm->out_finger += len;
353 if ((len <= rstrm->fbtbc)
354 && ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
355 buf = (afs_int32 *) rstrm->in_finger;
357 rstrm->in_finger += len;
365 xdrrec_destroy(register XDR * xdrs)
367 register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
369 osi_free(rstrm->out_base, rstrm->sendsize);
370 osi_free(rstrm->in_base, rstrm->recvsize);
371 osi_free((caddr_t) rstrm, sizeof(RECSTREAM));
376 * Exported routines to manage xdr records
380 * Before reading (deserializing from the stream, one should always call
381 * this procedure to guarantee proper record alignment.
384 xdrrec_skiprecord(XDR * xdrs)
386 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
388 while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
389 if (!skip_input_bytes(rstrm, rstrm->fbtbc))
392 if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
395 rstrm->last_frag = FALSE;
400 * Look ahead fuction.
401 * Returns TRUE iff there is no more input in the buffer
402 * after consuming the rest of the current record.
405 xdrrec_eof(XDR * xdrs)
407 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
409 while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
410 if (!skip_input_bytes(rstrm, rstrm->fbtbc))
413 if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
416 if (rstrm->in_finger == rstrm->in_boundry)
422 * The client must tell the package when an end-of-record has occurred.
423 * The second paraemters tells whether the record should be flushed to the
424 * (output) tcp stream. (This let's the package support batched or
425 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
428 xdrrec_endofrecord(XDR * xdrs, bool_t sendnow)
430 register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
431 register afs_uint32 len; /* fragment length */
433 if (sendnow || rstrm->frag_sent
434 || ((afs_uint32) (rstrm->out_finger + sizeof(afs_uint32)) >=
435 (afs_uint32) rstrm->out_boundry)) {
436 rstrm->frag_sent = FALSE;
437 return (flush_out(rstrm, TRUE));
440 (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
442 *(rstrm->frag_header) = htonl(len | LAST_FRAG);
443 rstrm->frag_header = (afs_uint32 *) rstrm->out_finger;
444 rstrm->out_finger += sizeof(afs_uint32);
450 * Internal useful routines
453 flush_out(register RECSTREAM * rstrm, bool_t eor)
455 register afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
456 register afs_uint32 len =
457 (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
460 *(rstrm->frag_header) = htonl(len | eormask);
461 len = (afs_uint32) (rstrm->out_finger) - (afs_uint32) (rstrm->out_base);
462 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int)len)
465 rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
466 rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(afs_uint32);
471 fill_input_buf(register RECSTREAM * rstrm)
473 register caddr_t where = rstrm->in_base;
474 register int len = rstrm->in_size;
475 u_int adjust = (u_int) ((size_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
477 /* Bump the current position out to the next alignment boundary */
481 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
483 rstrm->in_finger = where;
485 rstrm->in_boundry = where;
490 get_input_bytes(register RECSTREAM * rstrm, register caddr_t addr,
493 register int current;
496 current = (int)(rstrm->in_boundry - rstrm->in_finger);
498 if (!fill_input_buf(rstrm))
502 current = (len < current) ? len : current;
503 memcpy(addr, rstrm->in_finger, current);
504 rstrm->in_finger += current;
511 /* next two bytes of the input stream are treated as a header */
513 set_input_fragment(register RECSTREAM * rstrm)
517 if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
519 header = ntohl(header);
520 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
521 rstrm->fbtbc = header & (~LAST_FRAG);
525 /* consumes input bytes; knows nothing about records! */
527 skip_input_bytes(register RECSTREAM * rstrm, int cnt)
529 register int current;
532 current = (int)(rstrm->in_boundry - rstrm->in_finger);
534 if (!fill_input_buf(rstrm))
538 current = (cnt < current) ? cnt : current;
539 rstrm->in_finger += current;
546 fix_buf_size(register u_int s)
551 return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
552 * BYTES_PER_XDR_UNIT;