Update.
[glibc.git] / sunrpc / xdr_rec.c
blobb2ab2faabc1d3478b45ba4327fbf1ea7ef467be3
1 /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32 #endif
35 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
36 * layer above tcp (for rpc's use).
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * These routines interface XDRSTREAMS to a tcp/ip connection.
41 * There is a record marking layer between the xdr stream
42 * and the tcp transport level. A record is composed on one or more
43 * record fragments. A record fragment is a thirty-two bit header followed
44 * by n bytes of data, where n is contained in the header. The header
45 * is represented as a htonl(u_long). The high order bit encodes
46 * whether or not the fragment is the last fragment of the record
47 * (1 => fragment is last, 0 => more fragments to follow.
48 * The other 31 bits encode the byte length of the fragment.
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <rpc/rpc.h>
56 static bool_t xdrrec_getlong (XDR *, long *);
57 static bool_t xdrrec_putlong (XDR *, const long *);
58 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
59 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
60 static u_int xdrrec_getpos (const XDR *);
61 static bool_t xdrrec_setpos (XDR *, u_int);
62 static long *xdrrec_inline (XDR *, int);
63 static void xdrrec_destroy (XDR *);
65 static const struct xdr_ops xdrrec_ops =
67 xdrrec_getlong,
68 xdrrec_putlong,
69 xdrrec_getbytes,
70 xdrrec_putbytes,
71 xdrrec_getpos,
72 xdrrec_setpos,
73 xdrrec_inline,
74 xdrrec_destroy
78 * A record is composed of one or more record fragments.
79 * A record fragment is a two-byte header followed by zero to
80 * 2**32-1 bytes. The header is treated as a long unsigned and is
81 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
82 * are a byte count of the fragment. The highest order bit is a boolean:
83 * 1 => this fragment is the last fragment of the record,
84 * 0 => this fragment is followed by more fragment(s).
86 * The fragment/record machinery is not general; it is constructed to
87 * meet the needs of xdr and rpc based on tcp.
90 #define LAST_FRAG (1UL << 31)
92 typedef struct rec_strm
94 caddr_t tcp_handle;
95 caddr_t the_buffer;
97 * out-going bits
99 int (*writeit) (char *, char *, int);
100 caddr_t out_base; /* output buffer (points to frag header) */
101 caddr_t out_finger; /* next output position */
102 caddr_t out_boundry; /* data cannot up to this address */
103 u_int32_t *frag_header; /* beginning of curren fragment */
104 bool_t frag_sent; /* true if buffer sent in middle of record */
106 * in-coming bits
108 int (*readit) (char *, char *, int);
109 u_long in_size; /* fixed size of the input buffer */
110 caddr_t in_base;
111 caddr_t in_finger; /* location of next byte to be had */
112 caddr_t in_boundry; /* can read up to this location */
113 long fbtbc; /* fragment bytes to be consumed */
114 bool_t last_frag;
115 u_int sendsize;
116 u_int recvsize;
118 RECSTREAM;
120 static u_int fix_buf_size (u_int) internal_function;
121 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
122 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
123 static bool_t set_input_fragment (RECSTREAM *) internal_function;
124 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
127 * Create an xdr handle for xdrrec
128 * xdrrec_create fills in xdrs. Sendsize and recvsize are
129 * send and recv buffer sizes (0 => use default).
130 * tcp_handle is an opaque handle that is passed as the first parameter to
131 * the procedures readit and writeit. Readit and writeit are read and
132 * write respectively. They are like the system
133 * calls expect that they take an opaque handle rather than an fd.
135 void
136 xdrrec_create (XDR *xdrs, u_int sendsize,
137 u_int recvsize, caddr_t tcp_handle,
138 int (*readit) (char *, char *, int),
139 int (*writeit) (char *, char *, int))
141 RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
142 caddr_t tmp;
144 if (rstrm == NULL)
146 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
148 * This is bad. Should rework xdrrec_create to
149 * return a handle, and in this case return NULL
151 return;
154 * adjust sizes and allocate buffer quad byte aligned
156 rstrm->sendsize = sendsize = fix_buf_size (sendsize);
157 rstrm->recvsize = recvsize = fix_buf_size (recvsize);
158 rstrm->the_buffer = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
159 if (rstrm->the_buffer == NULL)
161 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
162 return;
164 tmp = rstrm->the_buffer;
165 if ((size_t)tmp % BYTES_PER_XDR_UNIT)
166 tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
167 rstrm->out_base = tmp;
168 rstrm->in_base = tmp + sendsize;
170 * now the rest ...
172 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
173 is not `const'. */
174 xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
175 xdrs->x_private = (caddr_t) rstrm;
176 rstrm->tcp_handle = tcp_handle;
177 rstrm->readit = readit;
178 rstrm->writeit = writeit;
179 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
180 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
181 rstrm->out_finger += 4;
182 rstrm->out_boundry += sendsize;
183 rstrm->frag_sent = FALSE;
184 rstrm->in_size = recvsize;
185 rstrm->in_boundry = rstrm->in_base;
186 rstrm->in_finger = (rstrm->in_boundry += recvsize);
187 rstrm->fbtbc = 0;
188 rstrm->last_frag = TRUE;
193 * The routines defined below are the xdr ops which will go into the
194 * xdr handle filled in by xdrrec_create.
197 static bool_t
198 xdrrec_getlong (xdrs, lp)
199 XDR *xdrs;
200 long *lp;
202 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
203 int32_t *buflp = (int32_t *) rstrm->in_finger;
204 int32_t mylong;
206 /* first try the inline, fast case */
207 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
208 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
210 *lp = (int32_t) ntohl (*buflp);
211 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
212 rstrm->in_finger += BYTES_PER_XDR_UNIT;
214 else
216 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
217 BYTES_PER_XDR_UNIT))
218 return FALSE;
219 *lp = (int32_t) ntohl (mylong);
221 return TRUE;
224 static bool_t
225 xdrrec_putlong (xdrs, lp)
226 XDR *xdrs;
227 const long *lp;
229 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
230 int32_t *dest_lp = (int32_t *) rstrm->out_finger;
232 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
235 * this case should almost never happen so the code is
236 * inefficient
238 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
239 rstrm->frag_sent = TRUE;
240 if (!flush_out (rstrm, FALSE))
241 return FALSE;
242 dest_lp = (int32_t *) rstrm->out_finger;
243 rstrm->out_finger += BYTES_PER_XDR_UNIT;
245 *dest_lp = htonl (*lp);
246 return TRUE;
249 static bool_t /* must manage buffers, fragments, and records */
250 xdrrec_getbytes (xdrs, addr, len)
251 XDR *xdrs;
252 caddr_t addr;
253 u_int len;
255 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
256 u_int current;
258 while (len > 0)
260 current = rstrm->fbtbc;
261 if (current == 0)
263 if (rstrm->last_frag)
264 return FALSE;
265 if (!set_input_fragment (rstrm))
266 return FALSE;
267 continue;
269 current = (len < current) ? len : current;
270 if (!get_input_bytes (rstrm, addr, current))
271 return FALSE;
272 addr += current;
273 rstrm->fbtbc -= current;
274 len -= current;
276 return TRUE;
279 static bool_t
280 xdrrec_putbytes (xdrs, addr, len)
281 XDR *xdrs;
282 const char *addr;
283 u_int len;
285 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
286 u_int current;
288 while (len > 0)
290 current = rstrm->out_boundry - rstrm->out_finger;
291 current = (len < current) ? len : current;
292 bcopy (addr, rstrm->out_finger, current);
293 rstrm->out_finger += current;
294 addr += current;
295 len -= current;
296 if (rstrm->out_finger == rstrm->out_boundry)
298 rstrm->frag_sent = TRUE;
299 if (!flush_out (rstrm, FALSE))
300 return FALSE;
303 return TRUE;
306 static u_int
307 xdrrec_getpos (const XDR *xdrs)
309 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
310 long pos;
312 pos = lseek ((int) rstrm->tcp_handle, (long) 0, 1);
313 if (pos != -1)
314 switch (xdrs->x_op)
317 case XDR_ENCODE:
318 pos += rstrm->out_finger - rstrm->out_base;
319 break;
321 case XDR_DECODE:
322 pos -= rstrm->in_boundry - rstrm->in_finger;
323 break;
325 default:
326 pos = (u_int) - 1;
327 break;
329 return (u_int) pos;
332 static bool_t
333 xdrrec_setpos (xdrs, pos)
334 XDR *xdrs;
335 u_int pos;
337 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
338 u_int currpos = xdrrec_getpos (xdrs);
339 int delta = currpos - pos;
340 caddr_t newpos;
342 if ((int) currpos != -1)
343 switch (xdrs->x_op)
346 case XDR_ENCODE:
347 newpos = rstrm->out_finger - delta;
348 if (newpos > (caddr_t) rstrm->frag_header &&
349 newpos < rstrm->out_boundry)
351 rstrm->out_finger = newpos;
352 return TRUE;
354 break;
356 case XDR_DECODE:
357 newpos = rstrm->in_finger - delta;
358 if ((delta < (int) (rstrm->fbtbc)) &&
359 (newpos <= rstrm->in_boundry) &&
360 (newpos >= rstrm->in_base))
362 rstrm->in_finger = newpos;
363 rstrm->fbtbc -= delta;
364 return TRUE;
366 break;
368 default:
369 break;
371 return FALSE;
374 static long *
375 xdrrec_inline (XDR *xdrs, int len)
377 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
378 long *buf = NULL;
380 switch (xdrs->x_op)
383 case XDR_ENCODE:
384 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
386 buf = (long *) rstrm->out_finger;
387 rstrm->out_finger += len;
389 break;
391 case XDR_DECODE:
392 if ((len <= rstrm->fbtbc) &&
393 ((rstrm->in_finger + len) <= rstrm->in_boundry))
395 buf = (long *) rstrm->in_finger;
396 rstrm->fbtbc -= len;
397 rstrm->in_finger += len;
399 break;
401 default:
402 break;
404 return buf;
407 static void
408 xdrrec_destroy (xdrs)
409 XDR *xdrs;
411 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
413 mem_free (rstrm->the_buffer,
414 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
415 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
420 * Exported routines to manage xdr records
424 * Before reading (deserializing from the stream, one should always call
425 * this procedure to guarantee proper record alignment.
427 bool_t
428 xdrrec_skiprecord (xdrs)
429 XDR *xdrs;
431 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
433 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
435 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
436 return FALSE;
437 rstrm->fbtbc = 0;
438 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
439 return FALSE;
441 rstrm->last_frag = FALSE;
442 return TRUE;
446 * Lookahead function.
447 * Returns TRUE iff there is no more input in the buffer
448 * after consuming the rest of the current record.
450 bool_t
451 xdrrec_eof (xdrs)
452 XDR *xdrs;
454 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
456 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
458 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
459 return TRUE;
460 rstrm->fbtbc = 0;
461 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
462 return TRUE;
464 if (rstrm->in_finger == rstrm->in_boundry)
465 return TRUE;
466 return FALSE;
470 * The client must tell the package when an end-of-record has occurred.
471 * The second parameter tells whether the record should be flushed to the
472 * (output) tcp stream. (This lets the package support batched or
473 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
475 bool_t
476 xdrrec_endofrecord (xdrs, sendnow)
477 XDR *xdrs;
478 bool_t sendnow;
480 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
481 u_long len; /* fragment length */
483 if (sendnow || rstrm->frag_sent
484 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
486 rstrm->frag_sent = FALSE;
487 return flush_out (rstrm, TRUE);
489 len = (rstrm->out_finger - (char *) rstrm->frag_header
490 - BYTES_PER_XDR_UNIT);
491 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
492 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
493 rstrm->out_finger += BYTES_PER_XDR_UNIT;
494 return TRUE;
499 * Internal useful routines
501 static bool_t
502 internal_function
503 flush_out (RECSTREAM *rstrm, bool_t eor)
505 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
506 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
507 - BYTES_PER_XDR_UNIT);
509 *rstrm->frag_header = htonl (len | eormask);
510 len = rstrm->out_finger - rstrm->out_base;
511 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
512 != (int) len)
513 return FALSE;
514 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
515 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
516 return TRUE;
519 static bool_t /* knows nothing about records! Only about input buffers */
520 fill_input_buf (RECSTREAM *rstrm)
522 caddr_t where;
523 size_t i;
524 int len;
526 where = rstrm->in_base;
527 i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
528 where += i;
529 len = rstrm->in_size - i;
530 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
531 return FALSE;
532 rstrm->in_finger = where;
533 where += len;
534 rstrm->in_boundry = where;
535 return TRUE;
538 static bool_t /* knows nothing about records! Only about input buffers */
539 internal_function
540 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
542 int current;
544 while (len > 0)
546 current = rstrm->in_boundry - rstrm->in_finger;
547 if (current == 0)
549 if (!fill_input_buf (rstrm))
550 return FALSE;
551 continue;
553 current = (len < current) ? len : current;
554 bcopy (rstrm->in_finger, addr, current);
555 rstrm->in_finger += current;
556 addr += current;
557 len -= current;
559 return TRUE;
562 static bool_t /* next two bytes of the input stream are treated as a header */
563 internal_function
564 set_input_fragment (RECSTREAM *rstrm)
566 u_long header;
568 if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
569 return FALSE;
570 header = ntohl (header);
571 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
573 * Sanity check. Try not to accept wildly incorrect
574 * record sizes. Unfortunately, the only record size
575 * we can positively identify as being 'wildly incorrect'
576 * is zero. Ridiculously large record sizes may look wrong,
577 * but we don't have any way to be certain that they aren't
578 * what the client actually intended to send us.
580 if ((header & (~LAST_FRAG)) == 0)
581 return FALSE;
582 rstrm->fbtbc = header & ~LAST_FRAG;
583 return TRUE;
586 static bool_t /* consumes input bytes; knows nothing about records! */
587 internal_function
588 skip_input_bytes (RECSTREAM *rstrm, long cnt)
590 int current;
592 while (cnt > 0)
594 current = rstrm->in_boundry - rstrm->in_finger;
595 if (current == 0)
597 if (!fill_input_buf (rstrm))
598 return FALSE;
599 continue;
601 current = (cnt < current) ? cnt : current;
602 rstrm->in_finger += current;
603 cnt -= current;
605 return TRUE;
608 static u_int
609 internal_function
610 fix_buf_size (u_int s)
612 if (s < 100)
613 s = 4000;
614 return RNDUP (s);