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
31 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
32 * layer above tcp (for rpc's use).
34 * Copyright (C) 1984, Sun Microsystems, Inc.
36 * These routines interface XDRSTREAMS to a tcp/ip connection.
37 * There is a record marking layer between the xdr stream
38 * and the tcp transport level. A record is composed on one or more
39 * record fragments. A record fragment is a thirty-two bit header followed
40 * by n bytes of data, where n is contained in the header. The header
41 * is represented as a htonl(u_long). The high order bit encodes
42 * whether or not the fragment is the last fragment of the record
43 * (1 => fragment is last, 0 => more fragments to follow.
44 * The other 31 bits encode the byte length of the fragment.
59 # include <libio/iolibio.h>
60 # define fputs(s, f) _IO_fputs (s, f)
63 static bool_t xdrrec_getlong (XDR *, long *);
64 static bool_t xdrrec_putlong (XDR *, const long *);
65 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
66 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
67 static u_int xdrrec_getpos (const XDR *);
68 static bool_t xdrrec_setpos (XDR *, u_int);
69 static int32_t *xdrrec_inline (XDR *, int);
70 static void xdrrec_destroy (XDR *);
71 static bool_t xdrrec_getint32 (XDR *, int32_t *);
72 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
74 static const struct xdr_ops xdrrec_ops = {
88 * A record is composed of one or more record fragments.
89 * A record fragment is a two-byte header followed by zero to
90 * 2**32-1 bytes. The header is treated as a long unsigned and is
91 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
92 * are a byte count of the fragment. The highest order bit is a boolean:
93 * 1 => this fragment is the last fragment of the record,
94 * 0 => this fragment is followed by more fragment(s).
96 * The fragment/record machinery is not general; it is constructed to
97 * meet the needs of xdr and rpc based on tcp.
100 #define LAST_FRAG (1UL << 31)
102 typedef struct rec_strm
109 int (*writeit) (char *, char *, int);
110 caddr_t out_base; /* output buffer (points to frag header) */
111 caddr_t out_finger; /* next output position */
112 caddr_t out_boundry; /* data cannot up to this address */
113 u_int32_t *frag_header; /* beginning of curren fragment */
114 bool_t frag_sent; /* true if buffer sent in middle of record */
118 int (*readit) (char *, char *, int);
119 u_long in_size; /* fixed size of the input buffer */
121 caddr_t in_finger; /* location of next byte to be had */
122 caddr_t in_boundry; /* can read up to this location */
123 long fbtbc; /* fragment bytes to be consumed */
130 static u_int fix_buf_size (u_int) internal_function;
131 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
132 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
133 static bool_t set_input_fragment (RECSTREAM *) internal_function;
134 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
137 * Create an xdr handle for xdrrec
138 * xdrrec_create fills in xdrs. Sendsize and recvsize are
139 * send and recv buffer sizes (0 => use default).
140 * tcp_handle is an opaque handle that is passed as the first parameter to
141 * the procedures readit and writeit. Readit and writeit are read and
142 * write respectively. They are like the system
143 * calls expect that they take an opaque handle rather than an fd.
145 void attribute_hidden
146 __xdrrec_create (XDR *xdrs, u_int sendsize,
147 u_int recvsize, caddr_t tcp_handle,
148 int (*readit) (char *, char *, int),
149 int (*writeit) (char *, char *, int))
151 RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
155 sendsize = fix_buf_size (sendsize);
156 recvsize = fix_buf_size (recvsize);
157 buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
159 if (rstrm == NULL || buf == NULL)
162 if (_IO_fwide (stderr, 0) > 0)
163 (void) __fwprintf (stderr, L"%s", _("xdrrec_create: out of memory\n"));
166 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
167 mem_free (rstrm, sizeof (RECSTREAM));
168 mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
170 * This is bad. Should rework xdrrec_create to
171 * return a handle, and in this case return NULL
176 * adjust sizes and allocate buffer quad byte aligned
178 rstrm->sendsize = sendsize;
179 rstrm->recvsize = recvsize;
180 rstrm->the_buffer = buf;
181 tmp = rstrm->the_buffer;
182 if ((size_t)tmp % BYTES_PER_XDR_UNIT)
183 tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
184 rstrm->out_base = tmp;
185 rstrm->in_base = tmp + sendsize;
189 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
191 xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
192 xdrs->x_private = (caddr_t) rstrm;
193 rstrm->tcp_handle = tcp_handle;
194 rstrm->readit = readit;
195 rstrm->writeit = writeit;
196 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
197 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
198 rstrm->out_finger += 4;
199 rstrm->out_boundry += sendsize;
200 rstrm->frag_sent = FALSE;
201 rstrm->in_size = recvsize;
202 rstrm->in_boundry = rstrm->in_base;
203 rstrm->in_finger = (rstrm->in_boundry += recvsize);
205 rstrm->last_frag = TRUE;
207 strong_alias(__xdrrec_create,xdrrec_create)
211 * The routines defined below are the xdr ops which will go into the
212 * xdr handle filled in by xdrrec_create.
216 xdrrec_getlong (XDR *xdrs, long *lp)
218 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
219 int32_t *buflp = (int32_t *) rstrm->in_finger;
222 /* first try the inline, fast case */
223 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
224 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
226 *lp = (int32_t) ntohl (*buflp);
227 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
228 rstrm->in_finger += BYTES_PER_XDR_UNIT;
232 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
235 *lp = (int32_t) ntohl (mylong);
241 xdrrec_putlong (XDR *xdrs, const long *lp)
243 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
244 int32_t *dest_lp = (int32_t *) rstrm->out_finger;
246 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
249 * this case should almost never happen so the code is
252 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
253 rstrm->frag_sent = TRUE;
254 if (!flush_out (rstrm, FALSE))
256 dest_lp = (int32_t *) rstrm->out_finger;
257 rstrm->out_finger += BYTES_PER_XDR_UNIT;
259 *dest_lp = htonl (*lp);
263 static bool_t /* must manage buffers, fragments, and records */
264 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
266 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
271 current = rstrm->fbtbc;
274 if (rstrm->last_frag)
276 if (!set_input_fragment (rstrm))
280 current = (len < current) ? len : current;
281 if (!get_input_bytes (rstrm, addr, current))
284 rstrm->fbtbc -= current;
291 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
293 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
298 current = rstrm->out_boundry - rstrm->out_finger;
299 current = (len < current) ? len : current;
300 __memcpy (rstrm->out_finger, addr, current);
301 rstrm->out_finger += current;
304 if (rstrm->out_finger == rstrm->out_boundry && len > 0)
306 rstrm->frag_sent = TRUE;
307 if (!flush_out (rstrm, FALSE))
315 xdrrec_getpos (const XDR *xdrs)
317 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
320 pos = lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
326 pos += rstrm->out_finger - rstrm->out_base;
330 pos -= rstrm->in_boundry - rstrm->in_finger;
341 xdrrec_setpos (XDR *xdrs, u_int pos)
343 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
344 u_int currpos = xdrrec_getpos (xdrs);
345 int delta = currpos - pos;
348 if ((int) currpos != -1)
353 newpos = rstrm->out_finger - delta;
354 if (newpos > (caddr_t) rstrm->frag_header &&
355 newpos < rstrm->out_boundry)
357 rstrm->out_finger = newpos;
363 newpos = rstrm->in_finger - delta;
364 if ((delta < (int) (rstrm->fbtbc)) &&
365 (newpos <= rstrm->in_boundry) &&
366 (newpos >= rstrm->in_base))
368 rstrm->in_finger = newpos;
369 rstrm->fbtbc -= delta;
381 xdrrec_inline (XDR *xdrs, int len)
383 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
390 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
392 buf = (int32_t *) rstrm->out_finger;
393 rstrm->out_finger += len;
398 if ((len <= rstrm->fbtbc) &&
399 ((rstrm->in_finger + len) <= rstrm->in_boundry))
401 buf = (int32_t *) rstrm->in_finger;
403 rstrm->in_finger += len;
414 xdrrec_destroy (XDR *xdrs)
416 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
418 mem_free (rstrm->the_buffer,
419 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
420 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
424 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
426 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
427 int32_t *bufip = (int32_t *) rstrm->in_finger;
430 /* first try the inline, fast case */
431 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
432 rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
434 *ip = ntohl (*bufip);
435 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
436 rstrm->in_finger += BYTES_PER_XDR_UNIT;
440 if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
443 *ip = ntohl (mylong);
449 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
451 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
452 int32_t *dest_ip = (int32_t *) rstrm->out_finger;
454 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
457 * this case should almost never happen so the code is
460 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
461 rstrm->frag_sent = TRUE;
462 if (!flush_out (rstrm, FALSE))
464 dest_ip = (int32_t *) rstrm->out_finger;
465 rstrm->out_finger += BYTES_PER_XDR_UNIT;
467 *dest_ip = htonl (*ip);
472 * Exported routines to manage xdr records
476 * Before reading (deserializing from the stream, one should always call
477 * this procedure to guarantee proper record alignment.
479 bool_t attribute_hidden
480 __xdrrec_skiprecord (XDR *xdrs)
482 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
484 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
486 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
489 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
492 rstrm->last_frag = FALSE;
495 strong_alias(__xdrrec_skiprecord,xdrrec_skiprecord)
498 * Lookahead function.
499 * Returns TRUE iff there is no more input in the buffer
500 * after consuming the rest of the current record.
502 bool_t attribute_hidden
503 __xdrrec_eof (XDR *xdrs)
505 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
507 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
509 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
512 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
515 if (rstrm->in_finger == rstrm->in_boundry)
519 strong_alias(__xdrrec_eof,xdrrec_eof)
522 * The client must tell the package when an end-of-record has occurred.
523 * The second parameter tells whether the record should be flushed to the
524 * (output) tcp stream. (This lets the package support batched or
525 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
527 bool_t attribute_hidden
528 __xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
530 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
531 u_long len; /* fragment length */
533 if (sendnow || rstrm->frag_sent
534 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
536 rstrm->frag_sent = FALSE;
537 return flush_out (rstrm, TRUE);
539 len = (rstrm->out_finger - (char *) rstrm->frag_header
540 - BYTES_PER_XDR_UNIT);
541 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
542 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
543 rstrm->out_finger += BYTES_PER_XDR_UNIT;
546 strong_alias(__xdrrec_endofrecord,xdrrec_endofrecord)
549 * Internal useful routines
553 flush_out (RECSTREAM *rstrm, bool_t eor)
555 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
556 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
557 - BYTES_PER_XDR_UNIT);
559 *rstrm->frag_header = htonl (len | eormask);
560 len = rstrm->out_finger - rstrm->out_base;
561 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
564 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
565 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
569 static bool_t /* knows nothing about records! Only about input buffers */
570 fill_input_buf (RECSTREAM *rstrm)
576 where = rstrm->in_base;
577 i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
579 len = rstrm->in_size - i;
580 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
582 rstrm->in_finger = where;
584 rstrm->in_boundry = where;
588 static bool_t /* knows nothing about records! Only about input buffers */
590 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
596 current = rstrm->in_boundry - rstrm->in_finger;
599 if (!fill_input_buf (rstrm))
603 current = (len < current) ? len : current;
604 __memcpy (addr, rstrm->in_finger, current);
605 rstrm->in_finger += current;
612 static bool_t /* next two bytes of the input stream are treated as a header */
614 set_input_fragment (RECSTREAM *rstrm)
618 if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
620 header = ntohl (header);
621 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
623 * Sanity check. Try not to accept wildly incorrect fragment
624 * sizes. Unfortunately, only a size of zero can be identified as
625 * 'wildely incorrect', and this only, if it is not the last
626 * fragment of a message. Ridiculously large fragment sizes may look
627 * wrong, but we don't have any way to be certain that they aren't
628 * what the client actually intended to send us. Many existing RPC
629 * implementations may sent a fragment of size zero as the last
630 * fragment of a message.
634 rstrm->fbtbc = header & ~LAST_FRAG;
638 static bool_t /* consumes input bytes; knows nothing about records! */
640 skip_input_bytes (RECSTREAM *rstrm, long cnt)
646 current = rstrm->in_boundry - rstrm->in_finger;
649 if (!fill_input_buf (rstrm))
653 current = (cnt < current) ? cnt : current;
654 rstrm->in_finger += current;
662 fix_buf_size (u_int s)