1 /* $NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
43 * layer above tcp (for rpc's use).
45 * These routines interface XDRSTREAMS to a tcp/ip connection.
46 * There is a record marking layer between the xdr stream
47 * and the tcp transport level. A record is composed on one or more
48 * record fragments. A record fragment is a thirty-two bit header followed
49 * by n bytes of data, where n is contained in the header. The header
50 * is represented as a htonl(u_long). Thegh order bit encodes
51 * whether or not the fragment is the last fragment of the record
52 * (1 => fragment is last, 0 => more fragments to follow.
53 * The other 31 bits encode the byte length of the fragment.
56 #include "namespace.h"
57 #include <sys/types.h>
59 #include <netinet/in.h>
66 #include <rpc/types.h>
71 #include <sys/stddef.h>
72 #include "un-namespace.h"
75 static bool_t xdrrec_getlong(XDR *, long *);
76 static bool_t xdrrec_putlong(XDR *, const long *);
77 static bool_t xdrrec_getbytes(XDR *, char *, u_int);
79 static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
80 static u_int xdrrec_getpos(XDR *);
81 static bool_t xdrrec_setpos(XDR *, u_int);
82 static int32_t *xdrrec_inline(XDR *, u_int);
83 static void xdrrec_destroy(XDR *);
85 static const struct xdr_ops xdrrec_ops = {
97 * A record is composed of one or more record fragments.
98 * A record fragment is a four-byte header followed by zero to
99 * 2**32-1 bytes. The header is treated as a long unsigned and is
100 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
101 * are a byte count of the fragment. The highest order bit is a boolean:
102 * 1 => this fragment is the last fragment of the record,
103 * 0 => this fragment is followed by more fragment(s).
105 * The fragment/record machinery is not general; it is constructed to
106 * meet the needs of xdr and rpc based on tcp.
109 #define LAST_FRAG ((u_int32_t)(1U << 31))
111 typedef struct rec_strm {
116 int (*writeit)(void *, void *, int);
117 char *out_base; /* output buffer (points to frag header) */
118 char *out_finger; /* next output position */
119 char *out_boundry; /* data cannot up to this address */
120 u_int32_t *frag_header; /* beginning of curren fragment */
121 bool_t frag_sent; /* true if buffer sent in middle of record */
125 int (*readit)(void *, void *, int);
126 u_long in_size; /* fixed size of the input buffer */
128 char *in_finger; /* location of next byte to be had */
129 char *in_boundry; /* can read up to this location */
130 long fbtbc; /* fragment bytes to be consumed */
136 bool_t in_haveheader;
145 static u_int fix_buf_size(u_int);
146 static bool_t flush_out(RECSTREAM *, bool_t);
147 static bool_t fill_input_buf(RECSTREAM *);
148 static bool_t get_input_bytes(RECSTREAM *, char *, int);
149 static bool_t set_input_fragment(RECSTREAM *);
150 static bool_t skip_input_bytes(RECSTREAM *, long);
151 static bool_t realloc_stream(RECSTREAM *, int);
155 * Create an xdr handle for xdrrec
156 * xdrrec_create fills in xdrs. Sendsize and recvsize are
157 * send and recv buffer sizes (0 => use default).
158 * tcp_handle is an opaque handle that is passed as the first parameter to
159 * the procedures readit and writeit. Readit and writeit are read and
160 * write respectively. They are like the system
161 * calls expect that they take an opaque handle rather than an fd.
164 xdrrec_create(XDR *xdrs, u_int sendsize, u_int recvsize, void *tcp_handle,
165 int (*readit)(void *, void *, int), int (*writeit)(void *, void *, int))
171 * // like read, but pass it a tcp_handle, not sock
172 * int (*readit)(void *, void *, int);
173 * // like write, but pass it a tcp_handle, not sock
174 * int (*writeit)(void *, void *, int);
177 RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
180 warnx("xdrrec_create: out of memory");
182 * This is bad. Should rework xdrrec_create to
183 * return a handle, and in this case return NULL
187 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
188 rstrm->out_base = mem_alloc(rstrm->sendsize);
189 if (rstrm->out_base == NULL) {
190 warnx("xdrrec_create: out of memory");
191 mem_free(rstrm, sizeof(RECSTREAM));
194 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
195 rstrm->in_base = mem_alloc(recvsize);
196 if (rstrm->in_base == NULL) {
197 warnx("xdrrec_create: out of memory");
198 mem_free(rstrm->out_base, sendsize);
199 mem_free(rstrm, sizeof(RECSTREAM));
205 xdrs->x_ops = &xdrrec_ops;
206 xdrs->x_private = rstrm;
207 rstrm->tcp_handle = tcp_handle;
208 rstrm->readit = readit;
209 rstrm->writeit = writeit;
210 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
211 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
212 rstrm->out_finger += sizeof(u_int32_t);
213 rstrm->out_boundry += sendsize;
214 rstrm->frag_sent = FALSE;
215 rstrm->in_size = recvsize;
216 rstrm->in_boundry = rstrm->in_base;
217 rstrm->in_finger = (rstrm->in_boundry += recvsize);
219 rstrm->last_frag = TRUE;
220 rstrm->in_haveheader = FALSE;
221 rstrm->in_hdrlen = 0;
222 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
223 rstrm->nonblock = FALSE;
224 rstrm->in_reclen = 0;
225 rstrm->in_received = 0;
230 * The reoutines defined below are the xdr ops which will go into the
231 * xdr handle filled in by xdrrec_create.
235 xdrrec_getlong(XDR *xdrs, long *lp)
237 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
238 int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
241 /* first try the inline, fast case */
242 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
243 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
244 *lp = (long)ntohl((u_int32_t)(*buflp));
245 rstrm->fbtbc -= sizeof(int32_t);
246 rstrm->in_finger += sizeof(int32_t);
248 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
251 *lp = (long)ntohl((u_int32_t)mylong);
257 xdrrec_putlong(XDR *xdrs, const long *lp)
259 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
260 int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
262 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
264 * this case should almost never happen so the code is
267 rstrm->out_finger -= sizeof(int32_t);
268 rstrm->frag_sent = TRUE;
269 if (! flush_out(rstrm, FALSE))
271 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
272 rstrm->out_finger += sizeof(int32_t);
274 *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
278 static bool_t /* must manage buffers, fragments, and records */
279 xdrrec_getbytes(XDR *xdrs, char *addr, u_int len)
281 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
285 current = (int)rstrm->fbtbc;
287 if (rstrm->last_frag)
289 if (! set_input_fragment(rstrm))
293 current = (len < current) ? len : current;
294 if (! get_input_bytes(rstrm, addr, current))
297 rstrm->fbtbc -= current;
304 xdrrec_putbytes(XDR *xdrs, const char *addr, u_int len)
306 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
310 current = (size_t)((u_long)rstrm->out_boundry -
311 (u_long)rstrm->out_finger);
312 current = (len < current) ? len : current;
313 memmove(rstrm->out_finger, addr, current);
314 rstrm->out_finger += current;
317 if (rstrm->out_finger == rstrm->out_boundry) {
318 rstrm->frag_sent = TRUE;
319 if (! flush_out(rstrm, FALSE))
327 xdrrec_getpos(XDR *xdrs)
329 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
332 pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1);
335 switch (xdrs->x_op) {
338 pos += rstrm->out_finger - rstrm->out_base;
342 pos -= rstrm->in_boundry - rstrm->in_finger;
349 return ((u_int) pos);
353 xdrrec_setpos(XDR *xdrs, u_int pos)
355 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
356 u_int currpos = xdrrec_getpos(xdrs);
357 int delta = currpos - pos;
360 if ((int)currpos != -1)
361 switch (xdrs->x_op) {
364 newpos = rstrm->out_finger - delta;
365 if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
366 (newpos < rstrm->out_boundry)) {
367 rstrm->out_finger = newpos;
373 newpos = rstrm->in_finger - delta;
374 if ((delta < (int)(rstrm->fbtbc)) &&
375 (newpos <= rstrm->in_boundry) &&
376 (newpos >= rstrm->in_base)) {
377 rstrm->in_finger = newpos;
378 rstrm->fbtbc -= delta;
390 xdrrec_inline(XDR *xdrs, u_int len)
392 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
395 switch (xdrs->x_op) {
398 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
399 buf = (int32_t *)(void *)rstrm->out_finger;
400 rstrm->out_finger += len;
405 if ((len <= rstrm->fbtbc) &&
406 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
407 buf = (int32_t *)(void *)rstrm->in_finger;
409 rstrm->in_finger += len;
420 xdrrec_destroy(XDR *xdrs)
422 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
424 mem_free(rstrm->out_base, rstrm->sendsize);
425 mem_free(rstrm->in_base, rstrm->recvsize);
426 mem_free(rstrm, sizeof(RECSTREAM));
431 * Exported routines to manage xdr records
435 * Before reading (deserializing from the stream, one should always call
436 * this procedure to guarantee proper record alignment.
439 xdrrec_skiprecord(XDR *xdrs)
441 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
442 enum xprt_stat xstat;
444 if (rstrm->nonblock) {
445 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
449 if (rstrm->in_finger == rstrm->in_boundry &&
450 xstat == XPRT_MOREREQS) {
457 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
458 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
461 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
464 rstrm->last_frag = FALSE;
469 * Look ahead function.
470 * Returns TRUE iff there is no more input in the buffer
471 * after consuming the rest of the current record.
474 xdrrec_eof(XDR *xdrs)
476 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
478 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
479 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
482 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
485 if (rstrm->in_finger == rstrm->in_boundry)
491 * The client must tell the package when an end-of-record has occurred.
492 * The second paraemters tells whether the record should be flushed to the
493 * (output) tcp stream. (This let's the package support batched or
494 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
497 xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
499 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
500 u_long len; /* fragment length */
502 if (sendnow || rstrm->frag_sent ||
503 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
504 (u_long)rstrm->out_boundry)) {
505 rstrm->frag_sent = FALSE;
506 return (flush_out(rstrm, TRUE));
508 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
510 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
511 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
512 rstrm->out_finger += sizeof(u_int32_t);
517 * Fill the stream buffer with a record for a non-blocking connection.
518 * Return true if a record is available in the buffer, false if not.
521 __xdrrec_getrec(XDR *xdrs, enum xprt_stat *statp, bool_t expectdata)
523 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
527 if (!rstrm->in_haveheader) {
528 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
529 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
531 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
539 rstrm->in_hdrlen += n;
540 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
541 *statp = XPRT_MOREREQS;
544 rstrm->in_header = ntohl(rstrm->in_header);
545 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
546 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
547 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
551 rstrm->in_reclen += fraglen;
552 if (rstrm->in_reclen > rstrm->recvsize)
553 realloc_stream(rstrm, rstrm->in_reclen);
554 if (rstrm->in_header & LAST_FRAG) {
555 rstrm->in_header &= ~LAST_FRAG;
556 rstrm->last_frag = TRUE;
559 * We can only reasonably expect to read once from a
560 * non-blocking stream. Reading the fragment header
561 * may have drained the stream.
566 n = rstrm->readit(rstrm->tcp_handle,
567 rstrm->in_base + rstrm->in_received,
568 (rstrm->in_reclen - rstrm->in_received));
576 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
580 rstrm->in_received += n;
582 if (rstrm->in_received == rstrm->in_reclen) {
583 rstrm->in_haveheader = FALSE;
584 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
585 rstrm->in_hdrlen = 0;
586 if (rstrm->last_frag) {
587 rstrm->fbtbc = rstrm->in_reclen;
588 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
589 rstrm->in_finger = rstrm->in_base;
590 rstrm->in_reclen = rstrm->in_received = 0;
591 *statp = XPRT_MOREREQS;
596 *statp = XPRT_MOREREQS;
601 __xdrrec_setnonblock(XDR *xdrs, int maxrec)
603 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
605 rstrm->nonblock = TRUE;
607 maxrec = rstrm->recvsize;
608 rstrm->in_maxrec = maxrec;
613 * Internal useful routines
616 flush_out(RECSTREAM *rstrm, bool_t eor)
618 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
619 u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
620 (u_long)(rstrm->frag_header) - sizeof(u_int32_t));
622 *(rstrm->frag_header) = htonl(len | eormask);
623 len = (u_int32_t)((u_long)(rstrm->out_finger) -
624 (u_long)(rstrm->out_base));
625 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
628 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
629 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
633 static bool_t /* knows nothing about records! Only about input buffers */
634 fill_input_buf(RECSTREAM *rstrm)
643 where = rstrm->in_base;
644 i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
646 len = (u_int32_t)(rstrm->in_size - i);
647 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
649 rstrm->in_finger = where;
651 rstrm->in_boundry = where;
655 static bool_t /* knows nothing about records! Only about input buffers */
656 get_input_bytes(RECSTREAM *rstrm, char *addr, int len)
660 if (rstrm->nonblock) {
661 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
663 memcpy(addr, rstrm->in_finger, (size_t)len);
664 rstrm->in_finger += len;
669 current = (size_t)((long)rstrm->in_boundry -
670 (long)rstrm->in_finger);
672 if (! fill_input_buf(rstrm))
676 current = (len < current) ? len : current;
677 memmove(addr, rstrm->in_finger, current);
678 rstrm->in_finger += current;
685 static bool_t /* next two bytes of the input stream are treated as a header */
686 set_input_fragment(RECSTREAM *rstrm)
692 if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
694 header = ntohl(header);
695 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
697 * Sanity check. Try not to accept wildly incorrect
698 * record sizes. Unfortunately, the only record size
699 * we can positively identify as being 'wildly incorrect'
700 * is zero. Ridiculously large record sizes may look wrong,
701 * but we don't have any way to be certain that they aren't
702 * what the client actually intended to send us.
706 rstrm->fbtbc = header & (~LAST_FRAG);
710 static bool_t /* consumes input bytes; knows nothing about records! */
711 skip_input_bytes(RECSTREAM *rstrm, long cnt)
716 current = (size_t)((long)rstrm->in_boundry -
717 (long)rstrm->in_finger);
719 if (! fill_input_buf(rstrm))
723 current = (u_int32_t)((cnt < current) ? cnt : current);
724 rstrm->in_finger += current;
731 fix_buf_size(u_int s)
740 * Reallocate the input buffer for a non-block stream.
743 realloc_stream(RECSTREAM *rstrm, int size)
748 if (size > rstrm->recvsize) {
749 buf = realloc(rstrm->in_base, (size_t)size);
752 diff = buf - rstrm->in_base;
753 rstrm->in_finger += diff;
754 rstrm->in_base = buf;
755 rstrm->in_boundry = buf + size;
756 rstrm->recvsize = size;
757 rstrm->in_size = size;