2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * 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 AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
38 #include <rpc/types.h>
41 static void xdrmbuf_destroy(XDR *);
42 static bool_t xdrmbuf_getlong(XDR *, long *);
43 static bool_t xdrmbuf_putlong(XDR *, const long *);
44 static bool_t xdrmbuf_getbytes(XDR *, char *, u_int);
45 static bool_t xdrmbuf_putbytes(XDR *, const char *, u_int);
46 /* XXX: w/64-bit pointers, u_int not enough! */
47 static u_int xdrmbuf_getpos(XDR *);
48 static bool_t xdrmbuf_setpos(XDR *, u_int);
49 static int32_t *xdrmbuf_inline(XDR *, u_int);
51 static const struct xdr_ops xdrmbuf_ops = {
63 * The procedure xdrmbuf_create initializes a stream descriptor for a
67 xdrmbuf_create(XDR *xdrs, struct mbuf *m, enum xdr_op op)
70 KASSERT(m != NULL, ("xdrmbuf_create with NULL mbuf chain"));
72 xdrs->x_ops = &xdrmbuf_ops;
73 xdrs->x_base = (char *) m;
74 if (op == XDR_ENCODE) {
77 xdrs->x_handy = m->m_len;
85 xdrmbuf_append(XDR *xdrs, struct mbuf *madd)
89 KASSERT(xdrs->x_ops == &xdrmbuf_ops && xdrs->x_op == XDR_ENCODE,
90 ("xdrmbuf_append: invalid XDR stream"));
92 if (m_length(madd, NULL) == 0) {
97 m = (struct mbuf *) xdrs->x_private;
102 xdrs->x_handy = m->m_len;
106 xdrmbuf_getall(XDR *xdrs)
110 KASSERT(xdrs->x_ops == &xdrmbuf_ops && xdrs->x_op == XDR_DECODE,
111 ("xdrmbuf_append: invalid XDR stream"));
113 m0 = (struct mbuf *) xdrs->x_base;
114 m = (struct mbuf *) xdrs->x_private;
116 while (m0->m_next != m)
119 xdrs->x_private = NULL;
122 xdrs->x_private = NULL;
126 m_adj(m, xdrs->x_handy);
128 m = m_get(M_WAITOK, MT_DATA);
133 xdrmbuf_destroy(XDR *xdrs)
136 if (xdrs->x_op == XDR_DECODE && xdrs->x_base) {
137 m_freem((struct mbuf *) xdrs->x_base);
139 xdrs->x_private = NULL;
144 xdrmbuf_getlong(XDR *xdrs, long *lp)
149 p = xdrmbuf_inline(xdrs, sizeof(int32_t));
153 xdrmbuf_getbytes(xdrs, (char *) &t, sizeof(int32_t));
161 xdrmbuf_putlong(xdrs, lp)
166 int32_t t = htonl(*lp);
168 p = xdrmbuf_inline(xdrs, sizeof(int32_t));
173 return (xdrmbuf_putbytes(xdrs, (char *) &t, sizeof(int32_t)));
178 xdrmbuf_getbytes(XDR *xdrs, char *addr, u_int len)
180 struct mbuf *m = (struct mbuf *) xdrs->x_private;
185 * Make sure we haven't hit the end.
192 * See how much we can get from this mbuf.
194 sz = m->m_len - xdrs->x_handy;
197 bcopy(mtod(m, const char *) + xdrs->x_handy, addr, sz);
203 if (xdrs->x_handy == m->m_len) {
205 xdrs->x_private = (void *) m;
214 xdrmbuf_putbytes(XDR *xdrs, const char *addr, u_int len)
216 struct mbuf *m = (struct mbuf *) xdrs->x_private;
221 sz = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
224 bcopy(addr, mtod(m, char *) + xdrs->x_handy, sz);
227 if (xdrs->x_handy > m->m_len)
228 m->m_len = xdrs->x_handy;
231 if (xdrs->x_handy == m->m_len && M_TRAILINGSPACE(m) == 0) {
233 if (m->m_flags & M_EXT)
234 n = m_getcl(M_WAITOK, m->m_type, 0);
236 n = m_get(M_WAITOK, m->m_type);
240 xdrs->x_private = (void *) m;
249 xdrmbuf_getpos(XDR *xdrs)
251 struct mbuf *m0 = (struct mbuf *) xdrs->x_base;
252 struct mbuf *m = (struct mbuf *) xdrs->x_private;
255 while (m0 && m0 != m) {
259 KASSERT(m0, ("Corrupted mbuf chain"));
261 return (pos + xdrs->x_handy);
265 xdrmbuf_setpos(XDR *xdrs, u_int pos)
267 struct mbuf *m = (struct mbuf *) xdrs->x_base;
269 while (m && pos > m->m_len) {
273 KASSERT(m, ("Corrupted mbuf chain"));
275 xdrs->x_private = (void *) m;
282 xdrmbuf_inline(XDR *xdrs, u_int len)
284 struct mbuf *m = (struct mbuf *) xdrs->x_private;
290 if (xdrs->x_op == XDR_ENCODE) {
291 available = M_TRAILINGSPACE(m) + (m->m_len - xdrs->x_handy);
293 available = m->m_len - xdrs->x_handy;
296 if (available >= len) {
297 p = mtod(m, char *) + xdrs->x_handy;
298 if (((uintptr_t) p) & (sizeof(int32_t) - 1))
300 xdrs->x_handy += len;
301 if (xdrs->x_handy > m->m_len)
302 m->m_len = xdrs->x_handy;
303 return ((int32_t *) p);