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>
58 #include <netinet/in.h>
70 /* * A record is composed of one or more record fragments.
71 * A record fragment is a two-byte header followed by zero to
72 * 2**32-1 bytes. The header is treated as an afs_int32 unsigned and is
73 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
74 * are a byte count of the fragment. The highest order bit is a boolean:
75 * 1 => this fragment is the last fragment of the record,
76 * 0 => this fragment is followed by more fragment(s).
78 * The fragment/record machinery is not general; it is constructed to
79 * meet the needs of xdr and rpc based on tcp.
82 #define LAST_FRAG ((afs_uint32)(1 << 31))
84 typedef struct rec_strm {
88 int (*writeit)(caddr_t tcp_handle, caddr_t out_base, int len);
89 caddr_t out_base; /* output buffer (points to frag header) */
90 caddr_t out_finger; /* next output position */
91 caddr_t out_boundry; /* data cannot up to this address */
92 afs_uint32 *frag_header; /* beginning of curren fragment */
93 bool_t frag_sent; /* true if buffer sent in middle of record */
96 int (*readit)(caddr_t tcp_handle, caddr_t out_base, int len);
97 afs_uint32 in_size; /* fixed size of the input buffer */
99 caddr_t in_finger; /* location of next byte to be had */
100 caddr_t in_boundry; /* can read up to this location */
101 afs_int32 fbtbc; /* fragment bytes to be consumed */
107 /* Prototypes for static routines */
108 static bool_t xdrrec_getint32(XDR *xdrs, afs_int32 *lp);
109 static bool_t xdrrec_putint32(XDR *xdrs, afs_int32 *lp);
110 static bool_t xdrrec_getbytes(XDR *xdrs, register caddr_t addr, register u_int len);
111 static bool_t xdrrec_putbytes(XDR *xdrs, register caddr_t addr, register u_int len);
112 static u_int xdrrec_getpos(register XDR *xdrs);
113 static bool_t xdrrec_setpos(register XDR *xdrs, u_int pos);
114 static afs_int32 *xdrrec_inline(register XDR *xdrs, int len);
115 static void xdrrec_destroy(register XDR *xdrs);
116 static bool_t flush_out(register RECSTREAM *rstrm, bool_t eor);
117 static bool_t fill_input_buf(register RECSTREAM *rstrm);
118 static bool_t get_input_bytes(register RECSTREAM *rstrm, register caddr_t addr, register int len);
119 static bool_t set_input_fragment(register RECSTREAM *rstrm);
120 static bool_t skip_input_bytes(register RECSTREAM *rstrm, int cnt);
121 static u_int fix_buf_size(register u_int s);
123 static struct xdr_ops xdrrec_ops = {
134 /* * Create an xdr handle for xdrrec
135 * xdrrec_create fills in xdrs. Sendsize and recvsize are
136 * send and recv buffer sizes (0 => use default).
137 * tcp_handle is an opaque handle that is passed as the first parameter to
138 * the procedures readit and writeit. Readit and writeit are read and
139 * write respectively. They are like the system
140 * calls expect that they take an opaque handle rather than an fd.
143 int (*readit)(); * like read, but pass it a tcp_handle, not sock *
144 int (*writeit)(); * like write, but pass it a tcp_handle, not sock *
146 void xdrrec_create(register XDR *xdrs, u_int sendsize, u_int recvsize, caddr_t tcp_handle,
147 int (*readit)(caddr_t tcp_handle, caddr_t out_base, int len),
148 int (*writeit)(caddr_t tcp_handle, caddr_t out_base, int len))
150 register RECSTREAM *rstrm =
151 (RECSTREAM *)osi_alloc(sizeof(RECSTREAM));
155 * This is bad. Should rework xdrrec_create to
156 * return a handle, and in this case return NULL
160 xdrs->x_ops = &xdrrec_ops;
161 xdrs->x_private = (caddr_t)rstrm;
162 rstrm->tcp_handle = tcp_handle;
163 rstrm->readit = readit;
164 rstrm->writeit = writeit;
165 sendsize = fix_buf_size(sendsize);
166 if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
167 osi_alloc(sendsize)) == NULL) {
170 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
171 rstrm->out_finger += sizeof(afs_uint32);
172 rstrm->out_boundry += sendsize;
173 rstrm->frag_sent = FALSE;
174 rstrm->in_size = recvsize = fix_buf_size(recvsize);
175 if ((rstrm->in_base = rstrm->in_boundry=osi_alloc(recvsize)) == NULL) {
178 rstrm->in_finger = (rstrm->in_boundry += recvsize);
180 rstrm->last_frag = TRUE;
181 rstrm->sendsize = sendsize;
182 rstrm->recvsize = recvsize;
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_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)rstrm->in_boundry - (int)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);
210 static bool_t xdrrec_putint32(XDR *xdrs, afs_int32 *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 xdrrec_getbytes(XDR *xdrs, register caddr_t addr, register u_int len)
233 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
234 register int current;
237 current = rstrm->fbtbc;
239 if (rstrm->last_frag)
241 if (! set_input_fragment(rstrm))
245 current = (len < current) ? len : current;
246 if (! get_input_bytes(rstrm, addr, current))
249 rstrm->fbtbc -= current;
255 static bool_t xdrrec_putbytes(XDR *xdrs, register caddr_t addr, register u_int len)
257 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
258 register int current;
261 current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger;
262 current = (len < current) ? len : current;
263 memcpy(rstrm->out_finger, addr, current);
264 rstrm->out_finger += current;
267 if (rstrm->out_finger == rstrm->out_boundry) {
268 rstrm->frag_sent = TRUE;
269 if (! flush_out(rstrm, FALSE))
276 static u_int xdrrec_getpos(register XDR *xdrs)
278 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
281 pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
283 switch (xdrs->x_op) {
286 pos += rstrm->out_finger - rstrm->out_base;
290 pos -= rstrm->in_boundry - rstrm->in_finger;
300 static bool_t xdrrec_setpos(register XDR *xdrs, u_int pos)
302 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
303 u_int currpos = xdrrec_getpos(xdrs);
304 int delta = currpos - pos;
307 if ((int)currpos != -1)
308 switch (xdrs->x_op) {
311 newpos = rstrm->out_finger - delta;
312 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
313 (newpos < rstrm->out_boundry)) {
314 rstrm->out_finger = newpos;
320 newpos = rstrm->in_finger - delta;
321 if ((delta < (int)(rstrm->fbtbc)) &&
322 (newpos <= rstrm->in_boundry) &&
323 (newpos >= rstrm->in_base)) {
324 rstrm->in_finger = newpos;
325 rstrm->fbtbc -= delta;
333 static afs_int32 *xdrrec_inline(register XDR *xdrs, int len)
335 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
336 afs_int32 * buf = NULL;
338 switch (xdrs->x_op) {
341 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
342 buf = (afs_int32 *) rstrm->out_finger;
343 rstrm->out_finger += len;
348 if ((len <= rstrm->fbtbc) &&
349 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
350 buf = (afs_int32 *) rstrm->in_finger;
352 rstrm->in_finger += len;
359 static void xdrrec_destroy(register XDR *xdrs)
361 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
363 osi_free(rstrm->out_base, rstrm->sendsize);
364 osi_free(rstrm->in_base, rstrm->recvsize);
365 osi_free((caddr_t)rstrm, sizeof(RECSTREAM));
370 * Exported routines to manage xdr records
374 * Before reading (deserializing from the stream, one should always call
375 * this procedure to guarantee proper record alignment.
377 bool_t xdrrec_skiprecord(XDR *xdrs)
379 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
381 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
382 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
385 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
388 rstrm->last_frag = FALSE;
393 * Look ahead fuction.
394 * Returns TRUE iff there is no more input in the buffer
395 * after consuming the rest of the current record.
397 bool_t xdrrec_eof(XDR *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 if (rstrm->in_finger == rstrm->in_boundry)
414 * The client must tell the package when an end-of-record has occurred.
415 * The second paraemters tells whether the record should be flushed to the
416 * (output) tcp stream. (This let's the package support batched or
417 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
419 bool_t xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
421 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
422 register afs_uint32 len; /* fragment length */
424 if (sendnow || rstrm->frag_sent ||
425 ((afs_uint32)rstrm->out_finger + sizeof(afs_uint32) >=
426 (afs_uint32)rstrm->out_boundry)) {
427 rstrm->frag_sent = FALSE;
428 return (flush_out(rstrm, TRUE));
430 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->frag_header) -
432 *(rstrm->frag_header) = htonl(len | LAST_FRAG);
433 rstrm->frag_header = (afs_uint32 *)rstrm->out_finger;
434 rstrm->out_finger += sizeof(afs_uint32);
440 * Internal useful routines
442 static bool_t flush_out(register RECSTREAM *rstrm, bool_t eor)
444 register afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
445 register afs_uint32 len = (afs_uint32)(rstrm->out_finger) -
446 (afs_uint32)(rstrm->frag_header) - sizeof(afs_uint32);
448 *(rstrm->frag_header) = htonl(len | eormask);
449 len = (afs_uint32)(rstrm->out_finger) - (afs_uint32)(rstrm->out_base);
450 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
453 rstrm->frag_header = (afs_uint32 *)rstrm->out_base;
454 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(afs_uint32);
458 static bool_t fill_input_buf(register RECSTREAM *rstrm)
460 register caddr_t where = rstrm->in_base;
461 register int len = rstrm->in_size;
462 u_int adjust = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
464 /* Bump the current position out to the next alignment boundary*/
468 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
470 rstrm->in_finger = where;
472 rstrm->in_boundry = where;
476 static bool_t get_input_bytes(register RECSTREAM *rstrm, register caddr_t addr, register int len)
478 register int current;
481 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
483 if (! fill_input_buf(rstrm))
487 current = (len < current) ? len : current;
488 memcpy(addr, rstrm->in_finger, current);
489 rstrm->in_finger += current;
496 /* next two bytes of the input stream are treated as a header */
497 static bool_t set_input_fragment(register RECSTREAM *rstrm)
501 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
503 header = ntohl(header);
504 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
505 rstrm->fbtbc = header & (~LAST_FRAG);
509 /* consumes input bytes; knows nothing about records! */
510 static bool_t skip_input_bytes(register RECSTREAM *rstrm, int cnt)
512 register int current;
515 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
517 if (! fill_input_buf(rstrm))
521 current = (cnt < current) ? cnt : current;
522 rstrm->in_finger += current;
528 static u_int fix_buf_size(register u_int s)
533 return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
534 * BYTES_PER_XDR_UNIT;