2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
36 #include <rpc/types.h>
39 static void xdrmbuf_destroy(XDR *);
40 static bool_t xdrmbuf_getlong(XDR *, long *);
41 static bool_t xdrmbuf_putlong(XDR *, const long *);
42 static bool_t xdrmbuf_getbytes(XDR *, char *, u_int);
43 static bool_t xdrmbuf_putbytes(XDR *, const char *, u_int);
44 /* XXX: w/64-bit pointers, u_int not enough! */
45 static u_int xdrmbuf_getpos(XDR *);
46 static bool_t xdrmbuf_setpos(XDR *, u_int);
47 static int32_t *xdrmbuf_inline(XDR *, u_int);
49 static const struct xdr_ops xdrmbuf_ops = {
61 * The procedure xdrmbuf_create initializes a stream descriptor for a
65 xdrmbuf_create(XDR *xdrs, struct mbuf *m, enum xdr_op op)
69 xdrs->x_ops = &xdrmbuf_ops;
70 xdrs->x_base = (char *) m;
71 if (op == XDR_ENCODE) {
74 xdrs->x_handy = m->m_len;
82 xdrmbuf_destroy(XDR *xdrs)
85 if (xdrs->x_op == XDR_DECODE && xdrs->x_base) {
86 m_freem((struct mbuf *) xdrs->x_base);
88 xdrs->x_private = NULL;
93 xdrmbuf_getlong(XDR *xdrs, long *lp)
97 xdrmbuf_getbytes(xdrs, (char *) &t, sizeof(int32_t));
103 xdrmbuf_putlong(xdrs, lp)
107 int32_t t = htonl(*lp);
109 xdrmbuf_putbytes(xdrs, (char *) &t, sizeof(int32_t));
114 xdrmbuf_getbytes(XDR *xdrs, char *addr, u_int len)
116 struct mbuf *m = (struct mbuf *) xdrs->x_private;
121 * Make sure we haven't hit the end.
128 * See how much we can get from this mbuf.
130 sz = m->m_len - xdrs->x_handy;
133 memcpy(addr, mtod(m, const char *) + xdrs->x_handy, sz);
139 if (xdrs->x_handy == m->m_len) {
141 xdrs->x_private = (void *) m;
150 xdrmbuf_putbytes(XDR *xdrs, const char *addr, u_int len)
152 struct mbuf *m = (struct mbuf *) xdrs->x_private;
157 sz = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
160 memcpy(mtod(m, char *) + xdrs->x_handy, addr, sz);
163 if (xdrs->x_handy > m->m_len)
164 m->m_len = xdrs->x_handy;
167 if (xdrs->x_handy == m->m_len && M_TRAILINGSPACE(m) == 0) {
169 MGET(n, M_TRYWAIT, m->m_type);
173 xdrs->x_private = (void *) m;
182 xdrmbuf_getpos(XDR *xdrs)
184 struct mbuf *m0 = (struct mbuf *) xdrs->x_base;
185 struct mbuf *m = (struct mbuf *) xdrs->x_private;
188 while (m0 && m0 != m) {
192 KASSERT(m0, ("Corrupted mbuf chain"));
194 return (pos + xdrs->x_handy);
198 xdrmbuf_setpos(XDR *xdrs, u_int pos)
200 struct mbuf *m = (struct mbuf *) xdrs->x_base;
202 while (m && pos > m->m_len) {
206 KASSERT(m, ("Corrupted mbuf chain"));
208 xdrs->x_private = (void *) m;
215 xdrmbuf_inline(XDR *xdrs, u_int len)
217 struct mbuf *m = (struct mbuf *) xdrs->x_private;
221 if (xdrs->x_op == XDR_ENCODE) {
222 available = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
224 available = m->m_len - xdrs->x_handy;
227 if (available >= len) {
228 p = mtod(m, char *) + xdrs->x_handy;
229 if (((uintptr_t) p) & (sizeof(int32_t) - 1))
231 xdrs->x_handy += len;
232 if (xdrs->x_handy > m->m_len)
233 m->m_len = xdrs->x_handy;
234 return ((int32_t *) p);