3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 #include <sys/cdefs.h>
34 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
35 * layer above tcp (for rpc's use).
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * These routines interface XDRSTREAMS to a tcp/ip connection.
40 * There is a record marking layer between the xdr stream
41 * and the tcp transport level. A record is composed on one or more
42 * record fragments. A record fragment is a thirty-two bit header followed
43 * by n bytes of data, where n is contained in the header. The header
44 * is represented as a htonl(u_long). Thegh order bit encodes
45 * whether or not the fragment is the last fragment of the record
46 * (1 => fragment is last, 0 => more fragments to follow.
47 * The other 31 bits encode the byte length of the fragment.
50 #include <sys/types.h>
52 #include <netinet/in.h>
59 #include <rpc/types.h>
62 #include <rpc/svc_auth.h>
67 static bool_t xdrrec_getlong(XDR *, long *);
68 static bool_t xdrrec_putlong(XDR *, const long *);
69 static bool_t xdrrec_getbytes(XDR *, char *, u_int);
71 static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
72 static u_int xdrrec_getpos(XDR *);
73 static bool_t xdrrec_setpos(XDR *, u_int);
74 static int32_t *xdrrec_inline(XDR *, u_int);
75 static void xdrrec_destroy(XDR *);
77 static const struct xdr_ops xdrrec_ops = {
89 * A record is composed of one or more record fragments.
90 * A record fragment is a four-byte header followed by zero to
91 * 2**32-1 bytes. The header is treated as a long unsigned and is
92 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
93 * are a byte count of the fragment. The highest order bit is a boolean:
94 * 1 => this fragment is the last fragment of the record,
95 * 0 => this fragment is followed by more fragment(s).
97 * The fragment/record machinery is not general; it is constructed to
98 * meet the needs of xdr and rpc based on tcp.
101 #define LAST_FRAG ((u_int32_t)(1 << 31))
103 typedef struct rec_strm {
108 int (*writeit)(void *, void *, int);
109 char *out_base; /* output buffer (points to frag header) */
110 char *out_finger; /* next output position */
111 char *out_boundry; /* data cannot up to this address */
112 u_int32_t *frag_header; /* beginning of curren fragment */
113 bool_t frag_sent; /* true if buffer sent in middle of record */
117 int (*readit)(void *, void *, int);
118 u_long in_size; /* fixed size of the input buffer */
120 char *in_finger; /* location of next byte to be had */
121 char *in_boundry; /* can read up to this location */
122 long fbtbc; /* fragment bytes to be consumed */
128 bool_t in_haveheader;
137 static u_int fix_buf_size(u_int);
138 static bool_t flush_out(RECSTREAM *, bool_t);
139 static bool_t fill_input_buf(RECSTREAM *);
140 static bool_t get_input_bytes(RECSTREAM *, char *, int);
141 static bool_t set_input_fragment(RECSTREAM *);
142 static bool_t skip_input_bytes(RECSTREAM *, long);
143 static bool_t realloc_stream(RECSTREAM *, int);
147 * Create an xdr handle for xdrrec
148 * xdrrec_create fills in xdrs. Sendsize and recvsize are
149 * send and recv buffer sizes (0 => use default).
150 * tcp_handle is an opaque handle that is passed as the first parameter to
151 * the procedures readit and writeit. Readit and writeit are read and
152 * write respectively. They are like the system
153 * calls expect that they take an opaque handle rather than an fd.
156 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
161 /* like read, but pass it a tcp_handle, not sock */
162 int (*readit)(void *, void *, int);
163 /* like write, but pass it a tcp_handle, not sock */
164 int (*writeit)(void *, void *, int);
166 RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
169 warnx("xdrrec_create: out of memory");
171 * This is bad. Should rework xdrrec_create to
172 * return a handle, and in this case return NULL
176 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
177 rstrm->out_base = mem_alloc(rstrm->sendsize);
178 if (rstrm->out_base == NULL) {
179 warnx("xdrrec_create: out of memory");
180 mem_free(rstrm, sizeof(RECSTREAM));
183 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
184 rstrm->in_base = mem_alloc(recvsize);
185 if (rstrm->in_base == NULL) {
186 warnx("xdrrec_create: out of memory");
187 mem_free(rstrm->out_base, sendsize);
188 mem_free(rstrm, sizeof(RECSTREAM));
194 xdrs->x_ops = &xdrrec_ops;
195 xdrs->x_private = rstrm;
196 rstrm->tcp_handle = tcp_handle;
197 rstrm->readit = readit;
198 rstrm->writeit = writeit;
199 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
200 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
201 rstrm->out_finger += sizeof(u_int32_t);
202 rstrm->out_boundry += sendsize;
203 rstrm->frag_sent = FALSE;
204 rstrm->in_size = recvsize;
205 rstrm->in_boundry = rstrm->in_base;
206 rstrm->in_finger = (rstrm->in_boundry += recvsize);
208 rstrm->last_frag = TRUE;
209 rstrm->in_haveheader = FALSE;
210 rstrm->in_hdrlen = 0;
211 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
212 rstrm->nonblock = FALSE;
213 rstrm->in_reclen = 0;
214 rstrm->in_received = 0;
219 * The reoutines defined below are the xdr ops which will go into the
220 * xdr handle filled in by xdrrec_create.
224 xdrrec_getlong(xdrs, lp)
228 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
229 int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
232 /* first try the inline, fast case */
233 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
234 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
235 *lp = (long)ntohl((u_int32_t)(*buflp));
236 rstrm->fbtbc -= sizeof(int32_t);
237 rstrm->in_finger += sizeof(int32_t);
239 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
242 *lp = (long)ntohl((u_int32_t)mylong);
248 xdrrec_putlong(xdrs, lp)
252 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
253 int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
255 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
257 * this case should almost never happen so the code is
260 rstrm->out_finger -= sizeof(int32_t);
261 rstrm->frag_sent = TRUE;
262 if (! flush_out(rstrm, FALSE))
264 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
265 rstrm->out_finger += sizeof(int32_t);
267 *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
271 static bool_t /* must manage buffers, fragments, and records */
272 xdrrec_getbytes(xdrs, addr, len)
277 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
281 current = (int)rstrm->fbtbc;
283 if (rstrm->last_frag)
285 if (! set_input_fragment(rstrm))
289 current = (len < current) ? len : current;
290 if (! get_input_bytes(rstrm, addr, current))
293 rstrm->fbtbc -= current;
300 xdrrec_putbytes(xdrs, addr, len)
305 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
309 current = (size_t)((u_long)rstrm->out_boundry -
310 (u_long)rstrm->out_finger);
311 current = (len < current) ? len : current;
312 memmove(rstrm->out_finger, addr, current);
313 rstrm->out_finger += current;
316 if (rstrm->out_finger == rstrm->out_boundry) {
317 rstrm->frag_sent = TRUE;
318 if (! flush_out(rstrm, FALSE))
329 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
332 switch (xdrs->x_op) {
335 pos = rstrm->out_finger - rstrm->out_base
336 - BYTES_PER_XDR_UNIT;
340 pos = rstrm->in_boundry - rstrm->in_finger
341 - BYTES_PER_XDR_UNIT;
348 return ((u_int) pos);
352 xdrrec_setpos(xdrs, pos)
356 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
357 u_int currpos = xdrrec_getpos(xdrs);
358 int delta = currpos - pos;
361 if ((int)currpos != -1)
362 switch (xdrs->x_op) {
365 newpos = rstrm->out_finger - delta;
366 if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
367 (newpos < rstrm->out_boundry)) {
368 rstrm->out_finger = newpos;
374 newpos = rstrm->in_finger - delta;
375 if ((delta < (int)(rstrm->fbtbc)) &&
376 (newpos <= rstrm->in_boundry) &&
377 (newpos >= rstrm->in_base)) {
378 rstrm->in_finger = newpos;
379 rstrm->fbtbc -= delta;
391 xdrrec_inline(xdrs, len)
395 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
398 switch (xdrs->x_op) {
401 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
402 buf = (int32_t *)(void *)rstrm->out_finger;
403 rstrm->out_finger += len;
408 if ((len <= rstrm->fbtbc) &&
409 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
410 buf = (int32_t *)(void *)rstrm->in_finger;
412 rstrm->in_finger += len;
426 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
428 mem_free(rstrm->out_base, rstrm->sendsize);
429 mem_free(rstrm->in_base, rstrm->recvsize);
430 mem_free(rstrm, sizeof(RECSTREAM));
435 * Exported routines to manage xdr records
439 * Before reading (deserializing from the stream, one should always call
440 * this procedure to guarantee proper record alignment.
443 xdrrec_skiprecord(xdrs)
446 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
447 enum xprt_stat xstat;
449 if (rstrm->nonblock) {
450 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
454 if (rstrm->in_finger == rstrm->in_boundry &&
455 xstat == XPRT_MOREREQS) {
462 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
463 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
466 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
469 rstrm->last_frag = FALSE;
474 * Look ahead function.
475 * Returns TRUE iff there is no more input in the buffer
476 * after consuming the rest of the current record.
482 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
484 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
485 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
488 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
491 if (rstrm->in_finger == rstrm->in_boundry)
497 * The client must tell the package when an end-of-record has occurred.
498 * The second paraemters tells whether the record should be flushed to the
499 * (output) tcp stream. (This let's the package support batched or
500 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
503 xdrrec_endofrecord(xdrs, sendnow)
507 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
508 u_long len; /* fragment length */
510 if (sendnow || rstrm->frag_sent ||
511 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
512 (u_long)rstrm->out_boundry)) {
513 rstrm->frag_sent = FALSE;
514 return (flush_out(rstrm, TRUE));
516 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
518 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
519 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
520 rstrm->out_finger += sizeof(u_int32_t);
525 * Fill the stream buffer with a record for a non-blocking connection.
526 * Return true if a record is available in the buffer, false if not.
529 __xdrrec_getrec(xdrs, statp, expectdata)
531 enum xprt_stat *statp;
534 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
538 if (!rstrm->in_haveheader) {
539 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
540 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
542 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
550 rstrm->in_hdrlen += n;
551 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
552 *statp = XPRT_MOREREQS;
555 rstrm->in_header = ntohl(rstrm->in_header);
556 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
557 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
558 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
562 rstrm->in_reclen += fraglen;
563 if (rstrm->in_reclen > rstrm->recvsize)
564 realloc_stream(rstrm, rstrm->in_reclen);
565 if (rstrm->in_header & LAST_FRAG) {
566 rstrm->in_header &= ~LAST_FRAG;
567 rstrm->last_frag = TRUE;
571 n = rstrm->readit(rstrm->tcp_handle,
572 rstrm->in_base + rstrm->in_received,
573 (rstrm->in_reclen - rstrm->in_received));
581 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
585 rstrm->in_received += n;
587 if (rstrm->in_received == rstrm->in_reclen) {
588 rstrm->in_haveheader = FALSE;
589 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
590 rstrm->in_hdrlen = 0;
591 if (rstrm->last_frag) {
592 rstrm->fbtbc = rstrm->in_reclen;
593 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
594 rstrm->in_finger = rstrm->in_base;
595 rstrm->in_reclen = rstrm->in_received = 0;
596 *statp = XPRT_MOREREQS;
601 *statp = XPRT_MOREREQS;
606 __xdrrec_setnonblock(xdrs, maxrec)
610 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
612 rstrm->nonblock = TRUE;
614 maxrec = rstrm->recvsize;
615 rstrm->in_maxrec = maxrec;
620 * Internal useful routines
623 flush_out(rstrm, eor)
627 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
628 u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
629 (u_long)(rstrm->frag_header) - sizeof(u_int32_t));
631 *(rstrm->frag_header) = htonl(len | eormask);
632 len = (u_int32_t)((u_long)(rstrm->out_finger) -
633 (u_long)(rstrm->out_base));
634 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
637 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
638 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
642 static bool_t /* knows nothing about records! Only about input buffers */
643 fill_input_buf(rstrm)
653 where = rstrm->in_base;
654 i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
656 len = (u_int32_t)(rstrm->in_size - i);
657 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
659 rstrm->in_finger = where;
661 rstrm->in_boundry = where;
665 static bool_t /* knows nothing about records! Only about input buffers */
666 get_input_bytes(rstrm, addr, len)
673 if (rstrm->nonblock) {
674 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
676 memcpy(addr, rstrm->in_finger, (size_t)len);
677 rstrm->in_finger += len;
682 current = (size_t)((long)rstrm->in_boundry -
683 (long)rstrm->in_finger);
685 if (! fill_input_buf(rstrm))
689 current = (len < current) ? len : current;
690 memmove(addr, rstrm->in_finger, current);
691 rstrm->in_finger += current;
698 static bool_t /* next two bytes of the input stream are treated as a header */
699 set_input_fragment(rstrm)
706 if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
708 header = ntohl(header);
709 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
711 * Sanity check. Try not to accept wildly incorrect
712 * record sizes. Unfortunately, the only record size
713 * we can positively identify as being 'wildly incorrect'
714 * is zero. Ridiculously large record sizes may look wrong,
715 * but we don't have any way to be certain that they aren't
716 * what the client actually intended to send us.
720 rstrm->fbtbc = header & (~LAST_FRAG);
724 static bool_t /* consumes input bytes; knows nothing about records! */
725 skip_input_bytes(rstrm, cnt)
732 current = (size_t)((long)rstrm->in_boundry -
733 (long)rstrm->in_finger);
735 if (! fill_input_buf(rstrm))
739 current = (u_int32_t)((cnt < current) ? cnt : current);
740 rstrm->in_finger += current;
757 * Reallocate the input buffer for a non-block stream.
760 realloc_stream(rstrm, size)
767 if (size > rstrm->recvsize) {
768 buf = realloc(rstrm->in_base, (size_t)size);
771 diff = buf - rstrm->in_base;
772 rstrm->in_finger += diff;
773 rstrm->in_base = buf;
774 rstrm->in_boundry = buf + size;
775 rstrm->recvsize = size;
776 rstrm->in_size = size;