1 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
3 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * * Neither the name of the "Oracle America, Inc." nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
39 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
40 * layer above tcp (for rpc's use).
42 * These routines interface XDRSTREAMS to a tcp/ip connection.
43 * There is a record marking layer between the xdr stream
44 * and the tcp transport level. A record is composed on one or more
45 * record fragments. A record fragment is a thirty-two bit header followed
46 * by n bytes of data, where n is contained in the header. The header
47 * is represented as a htonl(uint32_t). Thegh order bit encodes
48 * whether or not the fragment is the last fragment of the record
49 * (1 => fragment is last, 0 => more fragments to follow.
50 * The other 31 bits encode the byte length of the fragment.
54 #include <gssrpc/types.h>
55 #include <gssrpc/xdr.h>
56 #include <netinet/in.h>
61 static bool_t xdrrec_getlong(XDR *, long *);
62 static bool_t xdrrec_putlong(XDR *, long *);
63 static bool_t xdrrec_getbytes(XDR *, caddr_t, u_int);
64 static bool_t xdrrec_putbytes(XDR *, caddr_t, u_int);
65 static u_int xdrrec_getpos(XDR *);
66 static bool_t xdrrec_setpos(XDR *, u_int);
67 static rpc_inline_t * xdrrec_inline(XDR *, int);
68 static void xdrrec_destroy(XDR *);
70 static struct xdr_ops xdrrec_ops = {
82 * A record is composed of one or more record fragments.
83 * A record fragment is a four-byte header followed by zero to
84 * 2**31-1 bytes. The header is treated as an unsigned 32 bit integer and is
85 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
86 * are a byte count of the fragment. The highest order bit is a boolean:
87 * 1 => this fragment is the last fragment of the record,
88 * 0 => this fragment is followed by more fragment(s).
90 * The fragment/record machinery is not general; it is constructed to
91 * meet the needs of xdr and rpc based on tcp.
94 #define LAST_FRAG ((uint32_t)(1UL << 31))
96 typedef struct rec_strm {
103 caddr_t out_base; /* output buffer (points to frag header) */
104 caddr_t out_finger; /* next output position */
105 caddr_t out_boundry; /* data cannot up to this address */
106 uint32_t *frag_header; /* beginning of curren fragment */
107 bool_t frag_sent; /* true if buffer sent in middle of record */
112 uint32_t in_size; /* fixed size of the input buffer */
114 caddr_t in_finger; /* location of next byte to be had */
115 caddr_t in_boundry; /* can read up to this location */
116 int32_t fbtbc; /* fragment bytes to be consumed */
122 static u_int fix_buf_size(u_int);
123 static bool_t flush_out(RECSTREAM *, bool_t);
124 static bool_t get_input_bytes(RECSTREAM *, caddr_t, int);
125 static bool_t set_input_fragment(RECSTREAM *);
126 static bool_t skip_input_bytes(RECSTREAM *, int32_t);
129 * Create an xdr handle for xdrrec
130 * xdrrec_create fills in xdrs. Sendsize and recvsize are
131 * send and recv buffer sizes (0 => use default).
132 * tcp_handle is an opaque handle that is passed as the first parameter to
133 * the procedures readit and writeit. Readit and writeit are read and
134 * write respectively. They are like the system
135 * calls expect that they take an opaque handle rather than an fd.
143 int (*readit)(), /* like read, but pass it a tcp_handle, not sock */
144 int (*writeit)() /* like write, but pass it a tcp_handle, not sock */
147 register RECSTREAM *rstrm =
148 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
151 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
153 * This is bad. Should rework xdrrec_create to
154 * return a handle, and in this case return NULL
159 * adjust sizes and allocate buffer quad byte aligned
161 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
162 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
163 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
164 if (rstrm->the_buffer == NULL) {
165 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
168 for (rstrm->out_base = rstrm->the_buffer;
169 /* Pointer arithmetic - long cast allowed... */
170 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
172 rstrm->in_base = rstrm->out_base + sendsize;
176 xdrs->x_ops = &xdrrec_ops;
177 xdrs->x_private = (caddr_t)rstrm;
178 rstrm->tcp_handle = tcp_handle;
179 rstrm->readit = readit;
180 rstrm->writeit = writeit;
181 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
182 rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
183 rstrm->out_finger += BYTES_PER_XDR_UNIT;
184 rstrm->out_boundry += sendsize;
185 rstrm->frag_sent = FALSE;
186 rstrm->in_size = recvsize;
187 rstrm->in_boundry = rstrm->in_base;
188 rstrm->in_finger = (rstrm->in_boundry += recvsize);
190 rstrm->last_frag = TRUE;
195 * The reoutines defined below are the xdr ops which will go into the
196 * xdr handle filled in by xdrrec_create.
200 xdrrec_getlong(XDR *xdrs, long *lp)
202 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
203 register int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
206 /* first try the inline, fast case */
207 if ((rstrm->fbtbc >= BYTES_PER_XDR_UNIT) &&
208 (((long)rstrm->in_boundry - (long)buflp) >=
209 BYTES_PER_XDR_UNIT)) {
210 *lp = (long)ntohl((uint32_t)(*buflp));
211 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
212 rstrm->in_finger += BYTES_PER_XDR_UNIT;
214 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong,
217 *lp = (long)(int32_t)ntohl(mylong);
223 xdrrec_putlong(XDR *xdrs, long *lp)
225 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
226 register int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
228 if (rstrm->out_boundry - rstrm->out_finger < BYTES_PER_XDR_UNIT) {
230 * this case should almost never happen so the code is
233 rstrm->frag_sent = TRUE;
234 if (! flush_out(rstrm, FALSE))
236 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
238 rstrm->out_finger += BYTES_PER_XDR_UNIT;
239 *dest_lp = (int32_t)htonl((uint32_t)(*lp));
243 static bool_t /* must manage buffers, fragments, and records */
244 xdrrec_getbytes(XDR *xdrs, caddr_t addr, u_int len)
246 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
247 register u_int current;
250 current = rstrm->fbtbc;
252 if (rstrm->last_frag)
254 if (! set_input_fragment(rstrm))
258 current = (len < current) ? len : current;
259 if (! get_input_bytes(rstrm, addr, current))
262 rstrm->fbtbc -= current;
269 xdrrec_putbytes(XDR *xdrs, caddr_t addr, u_int len)
271 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
272 register size_t current;
275 current = (size_t) ((long)rstrm->out_boundry -
276 (long)rstrm->out_finger);
277 current = (len < current) ? len : current;
278 memmove(rstrm->out_finger, addr, current);
279 rstrm->out_finger += current;
282 if (rstrm->out_finger == rstrm->out_boundry) {
283 rstrm->frag_sent = TRUE;
284 if (! flush_out(rstrm, FALSE))
292 xdrrec_getpos(XDR *xdrs)
294 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
297 switch (xdrs->x_op) {
300 pos = rstrm->out_finger - rstrm->out_base
301 - BYTES_PER_XDR_UNIT;
305 pos = rstrm->in_boundry - rstrm->in_finger
306 - BYTES_PER_XDR_UNIT;
313 return ((u_int) pos);
317 xdrrec_setpos(XDR *xdrs, u_int pos)
319 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
320 u_int currpos = xdrrec_getpos(xdrs);
321 int delta = currpos - pos;
324 if ((int)currpos != -1)
325 switch (xdrs->x_op) {
328 newpos = rstrm->out_finger - delta;
329 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
330 (newpos < rstrm->out_boundry)) {
331 rstrm->out_finger = newpos;
337 newpos = rstrm->in_finger - delta;
338 if ((delta < (int)(rstrm->fbtbc)) &&
339 (newpos <= rstrm->in_boundry) &&
340 (newpos >= rstrm->in_base)) {
341 rstrm->in_finger = newpos;
342 rstrm->fbtbc -= delta;
353 static rpc_inline_t *
354 xdrrec_inline(XDR *xdrs, int len)
356 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
357 rpc_inline_t * buf = NULL;
362 switch (xdrs->x_op) {
365 if (len <= (rstrm->out_boundry - rstrm->out_finger)) {
366 buf = (rpc_inline_t *)(void *) rstrm->out_finger;
367 rstrm->out_finger += len;
372 if ((len <= rstrm->fbtbc) &&
373 (len <= (rstrm->in_boundry - rstrm->in_finger))) {
374 buf = (rpc_inline_t *)(void *) rstrm->in_finger;
376 rstrm->in_finger += len;
387 xdrrec_destroy(XDR *xdrs)
389 register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
391 mem_free(rstrm->the_buffer,
392 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
393 mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
398 * Exported routines to manage xdr records
402 * Before reading (deserializing from the stream, one should always call
403 * this procedure to guarantee proper record alignment.
406 xdrrec_skiprecord(XDR *xdrs)
408 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
410 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
411 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
414 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
417 rstrm->last_frag = FALSE;
422 * Look ahead fuction.
423 * Returns TRUE iff there is no more input in the buffer
424 * after consuming the rest of the current record.
427 xdrrec_eof(XDR *xdrs)
429 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
431 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
432 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
435 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
438 if (rstrm->in_finger == rstrm->in_boundry)
444 * The client must tell the package when an end-of-record has occurred.
445 * The second paraemters tells whether the record should be flushed to the
446 * (output) tcp stream. (This let's the package support batched or
447 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
450 xdrrec_endofrecord(XDR *xdrs, bool_t sendnow)
452 register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
453 register uint32_t len; /* fragment length */
455 if (sendnow || rstrm->frag_sent ||
456 ((long)rstrm->out_finger + BYTES_PER_XDR_UNIT >=
457 (long)rstrm->out_boundry)) {
458 rstrm->frag_sent = FALSE;
459 return (flush_out(rstrm, TRUE));
461 len = (long)(rstrm->out_finger) - (long)(rstrm->frag_header) -
463 *(rstrm->frag_header) = htonl((uint32_t)len | LAST_FRAG);
464 rstrm->frag_header = (uint32_t *)(void *)rstrm->out_finger;
465 rstrm->out_finger += BYTES_PER_XDR_UNIT;
471 * Internal useful routines
474 flush_out(RECSTREAM *rstrm, bool_t eor)
476 register uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
477 register uint32_t len = (u_long)(rstrm->out_finger) -
478 (u_long)(rstrm->frag_header) - BYTES_PER_XDR_UNIT;
480 *(rstrm->frag_header) = htonl(len | eormask);
481 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
482 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
485 rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base;
486 rstrm->out_finger = (caddr_t)rstrm->out_base + BYTES_PER_XDR_UNIT;
490 static bool_t /* knows nothing about records! Only about input buffers */
491 fill_input_buf(RECSTREAM *rstrm)
493 register caddr_t where;
497 where = rstrm->in_base;
498 i = (u_int)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
500 len = rstrm->in_size - i;
501 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
503 rstrm->in_finger = where;
505 rstrm->in_boundry = where;
509 static bool_t /* knows nothing about records! Only about input buffers */
510 get_input_bytes(RECSTREAM *rstrm, caddr_t addr, int len)
512 register size_t current;
515 current = (size_t)((long)rstrm->in_boundry -
516 (long)rstrm->in_finger);
518 if (! fill_input_buf(rstrm))
522 current = ((size_t)len < current) ? (size_t)len : current;
523 memmove(addr, rstrm->in_finger, current);
524 rstrm->in_finger += current;
531 static bool_t /* next four bytes of input stream are treated as a header */
532 set_input_fragment(rstrm)
533 register RECSTREAM *rstrm;
537 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
539 header = ntohl(header);
540 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
541 rstrm->fbtbc = header & (~LAST_FRAG);
545 static bool_t /* consumes input bytes; knows nothing about records! */
546 skip_input_bytes(RECSTREAM *rstrm, int32_t cnt)
548 register int current;
551 current = (int)((long)rstrm->in_boundry -
552 (long)rstrm->in_finger);
554 if (! fill_input_buf(rstrm))
558 current = (cnt < current) ? cnt : current;
559 rstrm->in_finger += current;
566 fix_buf_size(u_int s)