2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro
30 * @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/xdr/xdr_rec.c,v 1.12 2000/01/19 06:12:32 wpaul Exp $
32 * $DragonFly: src/lib/libcr/xdr/Attic/xdr_rec.c,v 1.4 2004/10/25 19:38:44 drhodus Exp $
36 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
37 * layer above tcp (for rpc's use).
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * These routines interface XDRSTREAMS to a tcp/ip connection.
42 * There is a record marking layer between the xdr stream
43 * and the tcp transport level. A record is composed on one or more
44 * record fragments. A record fragment is a thirty-two bit header followed
45 * by n bytes of data, where n is contained in the header. The header
46 * is represented as a htonl(u_long). Thegh order bit encodes
47 * whether or not the fragment is the last fragment of the record
48 * (1 => fragment is last, 0 => more fragments to follow.
49 * The other 31 bits encode the byte length of the fragment.
55 #include <rpc/types.h>
57 #include <netinet/in.h>
59 static u_int fix_buf_size();
60 static bool_t flush_out();
61 static bool_t get_input_bytes();
62 static bool_t set_input_fragment();
63 static bool_t skip_input_bytes();
65 static bool_t xdrrec_getlong();
66 static bool_t xdrrec_putlong();
67 static bool_t xdrrec_getbytes();
68 static bool_t xdrrec_putbytes();
69 static u_int xdrrec_getpos();
70 static bool_t xdrrec_setpos();
71 static int32_t *xdrrec_inline();
72 static void xdrrec_destroy();
74 static struct xdr_ops xdrrec_ops = {
86 * A record is composed of one or more record fragments.
87 * A record fragment is a two-byte header followed by zero to
88 * 2**32-1 bytes. The header is treated as a long unsigned and is
89 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
90 * are a byte count of the fragment. The highest order bit is a boolean:
91 * 1 => this fragment is the last fragment of the record,
92 * 0 => this fragment is followed by more fragment(s).
94 * The fragment/record machinery is not general; it is constructed to
95 * meet the needs of xdr and rpc based on tcp.
98 #define LAST_FRAG ((u_int32_t)(1 << 31))
100 typedef struct rec_strm {
106 int (*writeit) (caddr_t, caddr_t, int);
107 caddr_t out_base; /* output buffer (points to frag header) */
108 caddr_t out_finger; /* next output position */
109 caddr_t out_boundry; /* data cannot up to this address */
110 u_int32_t *frag_header; /* beginning of current fragment */
111 bool_t frag_sent; /* true if buffer sent in middle of record */
115 int (*readit) (caddr_t, caddr_t, int);
116 u_long 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 long 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 */
146 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
149 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
151 * This is bad. Should rework xdrrec_create to
152 * return a handle, and in this case return NULL
157 * adjust sizes and allocate buffer quad byte aligned
159 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
160 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
161 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
162 if (rstrm->the_buffer == NULL) {
163 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
166 for (rstrm->out_base = rstrm->the_buffer;
167 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
169 rstrm->in_base = rstrm->out_base + sendsize;
173 xdrs->x_ops = &xdrrec_ops;
174 xdrs->x_private = (caddr_t)rstrm;
175 rstrm->tcp_handle = tcp_handle;
176 rstrm->readit = readit;
177 rstrm->writeit = writeit;
178 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
179 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
180 rstrm->out_finger += sizeof(u_int32_t);
181 rstrm->out_boundry += sendsize;
182 rstrm->frag_sent = FALSE;
183 rstrm->in_size = recvsize;
184 rstrm->in_boundry = rstrm->in_base;
185 rstrm->in_finger = (rstrm->in_boundry += recvsize);
187 rstrm->last_frag = TRUE;
192 * The reoutines defined below are the xdr ops which will go into the
193 * xdr handle filled in by xdrrec_create.
197 xdrrec_getlong(xdrs, lp)
201 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
202 int32_t *buflp = (int32_t *)(rstrm->in_finger);
205 /* first try the inline, fast case */
206 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
207 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
208 *lp = (long)ntohl((u_int32_t)(*buflp));
209 rstrm->fbtbc -= sizeof(int32_t);
210 rstrm->in_finger += sizeof(int32_t);
212 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(int32_t)))
214 *lp = (long)ntohl((u_int32_t)mylong);
220 xdrrec_putlong(xdrs, lp)
224 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
225 int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
227 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
229 * this case should almost never happen so the code is
232 rstrm->out_finger -= sizeof(int32_t);
233 rstrm->frag_sent = TRUE;
234 if (! flush_out(rstrm, FALSE))
236 dest_lp = ((int32_t *)(rstrm->out_finger));
237 rstrm->out_finger += sizeof(int32_t);
239 *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
243 static bool_t /* must manage buffers, fragments, and records */
244 xdrrec_getbytes(xdrs, addr, len)
249 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
253 current = rstrm->fbtbc;
255 if (rstrm->last_frag)
257 if (! set_input_fragment(rstrm))
261 current = (len < current) ? len : current;
262 if (! get_input_bytes(rstrm, addr, current))
265 rstrm->fbtbc -= current;
272 xdrrec_putbytes(xdrs, addr, len)
277 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
281 current = (u_long)rstrm->out_boundry -
282 (u_long)rstrm->out_finger;
283 current = (len < current) ? len : current;
284 memcpy(rstrm->out_finger, addr, current);
285 rstrm->out_finger += current;
288 if (rstrm->out_finger == rstrm->out_boundry) {
289 rstrm->frag_sent = TRUE;
290 if (! flush_out(rstrm, FALSE))
301 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
304 pos = lseek((int)(long)rstrm->tcp_handle, (off_t) 0, 1);
306 switch (xdrs->x_op) {
309 pos += rstrm->out_finger - rstrm->out_base;
313 pos -= rstrm->in_boundry - rstrm->in_finger;
320 return ((u_int) pos);
324 xdrrec_setpos(xdrs, pos)
328 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
329 u_int currpos = xdrrec_getpos(xdrs);
330 int delta = currpos - pos;
333 if ((int)currpos != -1)
334 switch (xdrs->x_op) {
337 newpos = rstrm->out_finger - delta;
338 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
339 (newpos < rstrm->out_boundry)) {
340 rstrm->out_finger = newpos;
346 newpos = rstrm->in_finger - delta;
347 if ((delta < (int)(rstrm->fbtbc)) &&
348 (newpos <= rstrm->in_boundry) &&
349 (newpos >= rstrm->in_base)) {
350 rstrm->in_finger = newpos;
351 rstrm->fbtbc -= delta;
360 xdrrec_inline(xdrs, len)
364 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
365 int32_t * buf = NULL;
367 switch (xdrs->x_op) {
370 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
371 buf = (int32_t *) rstrm->out_finger;
372 rstrm->out_finger += len;
377 if ((len <= rstrm->fbtbc) &&
378 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
379 buf = (int32_t *) rstrm->in_finger;
381 rstrm->in_finger += len;
392 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
394 mem_free(rstrm->the_buffer,
395 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
396 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
401 * Exported routines to manage xdr records
405 * Before reading (deserializing from the stream, one should always call
406 * this procedure to guarantee proper record alignment.
409 xdrrec_skiprecord(xdrs)
412 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
414 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
415 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
418 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
421 rstrm->last_frag = FALSE;
426 * Look ahead fuction.
427 * Returns TRUE iff there is no more input in the buffer
428 * after consuming the rest of the current record.
434 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
436 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
437 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
440 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
443 if (rstrm->in_finger == rstrm->in_boundry)
449 * The client must tell the package when an end-of-record has occurred.
450 * The second paraemters tells whether the record should be flushed to the
451 * (output) tcp stream. (This let's the package support batched or
452 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
455 xdrrec_endofrecord(xdrs, sendnow)
459 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
460 u_long len; /* fragment length */
462 if (sendnow || rstrm->frag_sent ||
463 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
464 (u_long)rstrm->out_boundry)) {
465 rstrm->frag_sent = FALSE;
466 return (flush_out(rstrm, TRUE));
468 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
470 *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
471 rstrm->frag_header = (u_int32_t *)rstrm->out_finger;
472 rstrm->out_finger += sizeof(u_int32_t);
478 * Internal useful routines
481 flush_out(rstrm, eor)
485 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
486 u_int32_t len = (u_long)(rstrm->out_finger) -
487 (u_long)(rstrm->frag_header) - sizeof(u_int32_t);
489 *(rstrm->frag_header) = htonl(len | eormask);
490 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
491 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
494 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
495 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t);
499 static bool_t /* knows nothing about records! Only about input buffers */
500 fill_input_buf(rstrm)
507 where = rstrm->in_base;
508 i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
510 len = rstrm->in_size - i;
511 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
513 rstrm->in_finger = where;
515 rstrm->in_boundry = where;
519 static bool_t /* knows nothing about records! Only about input buffers */
520 get_input_bytes(rstrm, addr, len)
528 current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
530 if (! fill_input_buf(rstrm))
534 current = (len < current) ? len : current;
535 memcpy(addr, rstrm->in_finger, current);
536 rstrm->in_finger += current;
543 static bool_t /* next two bytes of the input stream are treated as a header */
544 set_input_fragment(rstrm)
549 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
551 header = (long)ntohl(header);
552 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
554 * Sanity check. Try not to accept wildly incorrect
555 * record sizes. Unfortunately, the only record size
556 * we can positively identify as being 'wildly incorrect'
557 * is zero. Ridiculously large record sizes may look wrong,
558 * but we don't have any way to be certain that they aren't
559 * what the client actually intended to send us.
563 rstrm->fbtbc = header & (~LAST_FRAG);
567 static bool_t /* consumes input bytes; knows nothing about records! */
568 skip_input_bytes(rstrm, cnt)
575 current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
577 if (! fill_input_buf(rstrm))
581 current = (cnt < current) ? cnt : current;
582 rstrm->in_finger += current;