sync header with GNU libc / kernel
[uclibc-ng.git] / libc / inet / rpc / xdr_rec.c
blobc17bfa12b0d171283f590951817ee47f5adac79f
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
31 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
32 * layer above tcp (for rpc's use).
34 * Copyright (C) 1984, Sun Microsystems, Inc.
36 * These routines interface XDRSTREAMS to a tcp/ip connection.
37 * There is a record marking layer between the xdr stream
38 * and the tcp transport level. A record is composed on one or more
39 * record fragments. A record fragment is a thirty-two bit header followed
40 * by n bytes of data, where n is contained in the header. The header
41 * is represented as a htonl(u_long). The high order bit encodes
42 * whether or not the fragment is the last fragment of the record
43 * (1 => fragment is last, 0 => more fragments to follow.
44 * The other 31 bits encode the byte length of the fragment.
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include "rpc_private.h"
52 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
53 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
54 static bool_t xdrrec_getint32 (XDR *, int32_t *);
55 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
56 #if ULONG_MAX != 0xffffffff
57 static bool_t xdrrec_getlong (XDR *, long *);
58 static bool_t xdrrec_putlong (XDR *, const long *);
59 #endif
60 static u_int xdrrec_getpos (const XDR *);
61 static bool_t xdrrec_setpos (XDR *, u_int);
62 static int32_t *xdrrec_inline (XDR *, u_int);
63 static void xdrrec_destroy (XDR *);
65 static const struct xdr_ops xdrrec_ops = {
66 #if ULONG_MAX == 0xffffffff
67 (bool_t (*)(XDR *, long *)) xdrrec_getint32,
68 (bool_t (*)(XDR *, const long *)) xdrrec_putint32,
69 #else
70 xdrrec_getlong,
71 xdrrec_putlong,
72 #endif
73 xdrrec_getbytes,
74 xdrrec_putbytes,
75 xdrrec_getpos,
76 xdrrec_setpos,
77 xdrrec_inline,
78 xdrrec_destroy,
79 xdrrec_getint32,
80 xdrrec_putint32
84 * A record is composed of one or more record fragments.
85 * A record fragment is a two-byte header followed by zero to
86 * 2**32-1 bytes. The header is treated as a long unsigned and is
87 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
88 * are a byte count of the fragment. The highest order bit is a boolean:
89 * 1 => this fragment is the last fragment of the record,
90 * 0 => this fragment is followed by more fragment(s).
92 * The fragment/record machinery is not general; it is constructed to
93 * meet the needs of xdr and rpc based on tcp.
96 #define LAST_FRAG (1UL << 31)
98 typedef struct rec_strm
100 caddr_t tcp_handle;
101 caddr_t the_buffer;
103 * out-going bits
105 int (*writeit) (char *, char *, int);
106 caddr_t out_base; /* output buffer (points to frag header) */
107 caddr_t out_finger; /* next output position */
108 caddr_t out_boundry; /* data cannot up to this address */
109 u_int32_t *frag_header; /* beginning of curren fragment */
110 bool_t frag_sent; /* true if buffer sent in middle of record */
112 * in-coming bits
114 int (*readit) (char *, char *, int);
115 u_long in_size; /* fixed size of the input buffer */
116 caddr_t in_base;
117 caddr_t in_finger; /* location of next byte to be had */
118 caddr_t in_boundry; /* can read up to this location */
119 long fbtbc; /* fragment bytes to be consumed */
120 bool_t last_frag;
121 u_int sendsize;
122 u_int recvsize;
124 RECSTREAM;
126 static u_int fix_buf_size (u_int) internal_function;
127 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
128 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
129 static bool_t set_input_fragment (RECSTREAM *) internal_function;
130 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
133 * Create an xdr handle for xdrrec
134 * xdrrec_create fills in xdrs. Sendsize and recvsize are
135 * send and recv buffer sizes (0 => use default).
136 * tcp_handle is an opaque handle that is passed as the first parameter to
137 * the procedures readit and writeit. Readit and writeit are read and
138 * write respectively. They are like the system
139 * calls expect that they take an opaque handle rather than an fd.
141 void
142 xdrrec_create (XDR *xdrs, u_int sendsize,
143 u_int recvsize, caddr_t tcp_handle,
144 int (*readit) (char *, char *, int),
145 int (*writeit) (char *, char *, int))
147 RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
148 caddr_t tmp;
149 char *buf;
151 sendsize = fix_buf_size (sendsize);
152 recvsize = fix_buf_size (recvsize);
153 buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
155 if (rstrm == NULL || buf == NULL)
157 (void) fputs ("xdrrec_create: out of memory\n", stderr);
158 mem_free (rstrm, sizeof (RECSTREAM));
159 mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
161 * This is bad. Should rework xdrrec_create to
162 * return a handle, and in this case return NULL
164 return;
167 * adjust sizes and allocate buffer quad byte aligned
169 rstrm->sendsize = sendsize;
170 rstrm->recvsize = recvsize;
171 rstrm->the_buffer = buf;
172 tmp = rstrm->the_buffer;
173 if ((size_t)tmp % BYTES_PER_XDR_UNIT)
174 tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
175 rstrm->out_base = tmp;
176 rstrm->in_base = tmp + sendsize;
178 * now the rest ...
180 xdrs->x_ops = &xdrrec_ops;
181 xdrs->x_private = (caddr_t) rstrm;
182 rstrm->tcp_handle = tcp_handle;
183 rstrm->readit = readit;
184 rstrm->writeit = writeit;
185 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
186 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
187 rstrm->out_finger += 4;
188 rstrm->out_boundry += sendsize;
189 rstrm->frag_sent = FALSE;
190 rstrm->in_size = recvsize;
191 rstrm->in_boundry = rstrm->in_base;
192 rstrm->in_finger = (rstrm->in_boundry += recvsize);
193 rstrm->fbtbc = 0;
194 rstrm->last_frag = TRUE;
196 libc_hidden_def(xdrrec_create)
200 * The routines defined below are the xdr ops which will go into the
201 * xdr handle filled in by xdrrec_create.
204 static bool_t
205 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
207 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
208 int32_t *bufip = (int32_t *) rstrm->in_finger;
209 int32_t mylong;
211 /* first try the inline, fast case */
212 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
213 rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
215 *ip = ntohl (*bufip);
216 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
217 rstrm->in_finger += BYTES_PER_XDR_UNIT;
219 else
221 if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
222 BYTES_PER_XDR_UNIT))
223 return FALSE;
224 *ip = ntohl (mylong);
226 return TRUE;
229 #if ULONG_MAX != 0xffffffff
230 static bool_t
231 xdrrec_getlong (XDR *xdrs, long *lp)
233 int32_t v;
234 bool_t r = xdrrec_getint32 (xdrs, &v);
235 *lp = v;
236 return r;
238 #endif
240 static bool_t
241 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
243 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
244 int32_t *dest_ip = (int32_t *) rstrm->out_finger;
246 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
249 * this case should almost never happen so the code is
250 * inefficient
252 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
253 rstrm->frag_sent = TRUE;
254 if (!flush_out (rstrm, FALSE))
255 return FALSE;
256 dest_ip = (int32_t *) rstrm->out_finger;
257 rstrm->out_finger += BYTES_PER_XDR_UNIT;
259 *dest_ip = htonl (*ip);
260 return TRUE;
263 #if ULONG_MAX != 0xffffffff
264 static bool_t
265 xdrrec_putlong (XDR *xdrs, const long *lp)
267 int32_t v = *lp;
268 return xdrrec_putint32 (xdrs, &v);
270 #endif
272 static bool_t /* must manage buffers, fragments, and records */
273 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
275 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
276 u_int current;
278 while (len > 0)
280 current = rstrm->fbtbc;
281 if (current == 0)
283 if (rstrm->last_frag)
284 return FALSE;
285 if (!set_input_fragment (rstrm))
286 return FALSE;
287 continue;
289 current = (len < current) ? len : current;
290 if (!get_input_bytes (rstrm, addr, current))
291 return FALSE;
292 addr += current;
293 rstrm->fbtbc -= current;
294 len -= current;
296 return TRUE;
299 static bool_t
300 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
302 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
303 u_int current;
305 while (len > 0)
307 current = rstrm->out_boundry - rstrm->out_finger;
308 current = (len < current) ? len : current;
309 memcpy (rstrm->out_finger, addr, current);
310 rstrm->out_finger += current;
311 addr += current;
312 len -= current;
313 if (rstrm->out_finger == rstrm->out_boundry && len > 0)
315 rstrm->frag_sent = TRUE;
316 if (!flush_out (rstrm, FALSE))
317 return FALSE;
320 return TRUE;
323 static u_int
324 xdrrec_getpos (const XDR *xdrs)
326 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
327 long pos;
329 pos = lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
330 if (pos != -1)
331 switch (xdrs->x_op)
334 case XDR_ENCODE:
335 pos += rstrm->out_finger - rstrm->out_base;
336 break;
338 case XDR_DECODE:
339 pos -= rstrm->in_boundry - rstrm->in_finger;
340 break;
342 default:
343 pos = (u_int) - 1;
344 break;
346 return (u_int) pos;
349 static bool_t
350 xdrrec_setpos (XDR *xdrs, u_int pos)
352 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
353 u_int currpos = xdrrec_getpos (xdrs);
354 int delta = currpos - pos;
355 caddr_t newpos;
357 if ((int) currpos != -1)
358 switch (xdrs->x_op)
361 case XDR_ENCODE:
362 newpos = rstrm->out_finger - delta;
363 if (newpos > (caddr_t) rstrm->frag_header &&
364 newpos < rstrm->out_boundry)
366 rstrm->out_finger = newpos;
367 return TRUE;
369 break;
371 case XDR_DECODE:
372 newpos = rstrm->in_finger - delta;
373 if ((delta < (int) (rstrm->fbtbc)) &&
374 (newpos <= rstrm->in_boundry) &&
375 (newpos >= rstrm->in_base))
377 rstrm->in_finger = newpos;
378 rstrm->fbtbc -= delta;
379 return TRUE;
381 break;
383 default:
384 break;
386 return FALSE;
389 static int32_t *
390 xdrrec_inline (XDR *xdrs, u_int len)
392 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
393 int32_t *buf = NULL;
395 switch (xdrs->x_op)
398 case XDR_ENCODE:
399 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
401 buf = (int32_t *) rstrm->out_finger;
402 rstrm->out_finger += len;
404 break;
406 case XDR_DECODE:
407 if ((len <= rstrm->fbtbc) &&
408 ((rstrm->in_finger + len) <= rstrm->in_boundry))
410 buf = (int32_t *) rstrm->in_finger;
411 rstrm->fbtbc -= len;
412 rstrm->in_finger += len;
414 break;
416 default:
417 break;
419 return buf;
422 static void
423 xdrrec_destroy (XDR *xdrs)
425 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
427 mem_free (rstrm->the_buffer,
428 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
429 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
433 * Exported routines to manage xdr records
437 * Before reading (deserializing from the stream, one should always call
438 * this procedure to guarantee proper record alignment.
440 bool_t
441 xdrrec_skiprecord (XDR *xdrs)
443 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
445 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
447 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
448 return FALSE;
449 rstrm->fbtbc = 0;
450 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
451 return FALSE;
453 rstrm->last_frag = FALSE;
454 return TRUE;
456 libc_hidden_def(xdrrec_skiprecord)
459 * Lookahead function.
460 * Returns TRUE iff there is no more input in the buffer
461 * after consuming the rest of the current record.
463 bool_t
464 xdrrec_eof (XDR *xdrs)
466 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
468 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
470 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
471 return TRUE;
472 rstrm->fbtbc = 0;
473 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
474 return TRUE;
476 if (rstrm->in_finger == rstrm->in_boundry)
477 return TRUE;
478 return FALSE;
480 libc_hidden_def(xdrrec_eof)
483 * The client must tell the package when an end-of-record has occurred.
484 * The second parameter tells whether the record should be flushed to the
485 * (output) tcp stream. (This lets the package support batched or
486 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
488 bool_t
489 xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
491 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
492 u_long len; /* fragment length */
494 if (sendnow || rstrm->frag_sent
495 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
497 rstrm->frag_sent = FALSE;
498 return flush_out (rstrm, TRUE);
500 len = (rstrm->out_finger - (char *) rstrm->frag_header
501 - BYTES_PER_XDR_UNIT);
502 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
503 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
504 rstrm->out_finger += BYTES_PER_XDR_UNIT;
505 return TRUE;
507 libc_hidden_def(xdrrec_endofrecord)
510 * Internal useful routines
512 static bool_t
513 internal_function
514 flush_out (RECSTREAM *rstrm, bool_t eor)
516 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
517 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
518 - BYTES_PER_XDR_UNIT);
520 *rstrm->frag_header = htonl (len | eormask);
521 len = rstrm->out_finger - rstrm->out_base;
522 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
523 != (int) len)
524 return FALSE;
525 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
526 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
527 return TRUE;
530 static bool_t /* knows nothing about records! Only about input buffers */
531 fill_input_buf (RECSTREAM *rstrm)
533 caddr_t where;
534 size_t i;
535 int len;
537 where = rstrm->in_base;
538 i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
539 where += i;
540 len = rstrm->in_size - i;
541 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
542 return FALSE;
543 rstrm->in_finger = where;
544 where += len;
545 rstrm->in_boundry = where;
546 return TRUE;
549 static bool_t /* knows nothing about records! Only about input buffers */
550 internal_function
551 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
553 int current;
555 while (len > 0)
557 current = rstrm->in_boundry - rstrm->in_finger;
558 if (current == 0)
560 if (!fill_input_buf (rstrm))
561 return FALSE;
562 continue;
564 current = (len < current) ? len : current;
565 memcpy (addr, rstrm->in_finger, current);
566 rstrm->in_finger += current;
567 addr += current;
568 len -= current;
570 return TRUE;
573 static bool_t /* next two bytes of the input stream are treated as a header */
574 internal_function
575 set_input_fragment (RECSTREAM *rstrm)
577 uint32_t header;
579 if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
580 return FALSE;
581 header = ntohl (header);
582 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
584 * Sanity check. Try not to accept wildly incorrect fragment
585 * sizes. Unfortunately, only a size of zero can be identified as
586 * 'wildely incorrect', and this only, if it is not the last
587 * fragment of a message. Ridiculously large fragment sizes may look
588 * wrong, but we don't have any way to be certain that they aren't
589 * what the client actually intended to send us. Many existing RPC
590 * implementations may sent a fragment of size zero as the last
591 * fragment of a message.
593 if (header == 0)
594 return FALSE;
595 rstrm->fbtbc = header & ~LAST_FRAG;
596 return TRUE;
599 static bool_t /* consumes input bytes; knows nothing about records! */
600 internal_function
601 skip_input_bytes (RECSTREAM *rstrm, long cnt)
603 int current;
605 while (cnt > 0)
607 current = rstrm->in_boundry - rstrm->in_finger;
608 if (current == 0)
610 if (!fill_input_buf (rstrm))
611 return FALSE;
612 continue;
614 current = (cnt < current) ? cnt : current;
615 rstrm->in_finger += current;
616 cnt -= current;
618 return TRUE;
621 static u_int
622 internal_function
623 fix_buf_size (u_int s)
625 if (s < 100)
626 s = 4000;
627 return RNDUP (s);