Update.
[glibc.git] / sunrpc / xdr_rec.c
blobe76feeb99139bd85cd9b89c94043e8d6eb133c27
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 (const 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);
121 static bool_t skip_input_bytes (RECSTREAM *, long);
122 static bool_t flush_out (RECSTREAM *, bool_t);
123 static bool_t set_input_fragment (RECSTREAM *);
124 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int);
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 =
142 (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
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 for (rstrm->out_base = rstrm->the_buffer;
165 (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
166 rstrm->out_base++);
167 rstrm->in_base = rstrm->out_base + sendsize;
169 * now the rest ...
171 xdrs->x_ops = &xdrrec_ops;
172 xdrs->x_private = (caddr_t) rstrm;
173 rstrm->tcp_handle = tcp_handle;
174 rstrm->readit = readit;
175 rstrm->writeit = writeit;
176 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
177 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
178 rstrm->out_finger += 4;
179 rstrm->out_boundry += sendsize;
180 rstrm->frag_sent = FALSE;
181 rstrm->in_size = recvsize;
182 rstrm->in_boundry = rstrm->in_base;
183 rstrm->in_finger = (rstrm->in_boundry += recvsize);
184 rstrm->fbtbc = 0;
185 rstrm->last_frag = TRUE;
190 * The routines defined below are the xdr ops which will go into the
191 * xdr handle filled in by xdrrec_create.
194 static bool_t
195 xdrrec_getlong (xdrs, lp)
196 XDR *xdrs;
197 long *lp;
199 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
200 int32_t *buflp = (int32_t *) rstrm->in_finger;
201 int32_t mylong;
203 /* first try the inline, fast case */
204 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
205 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
207 *lp = (int32_t) ntohl (*buflp);
208 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
209 rstrm->in_finger += BYTES_PER_XDR_UNIT;
211 else
213 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
214 BYTES_PER_XDR_UNIT))
215 return FALSE;
216 *lp = (int32_t) ntohl (mylong);
218 return TRUE;
221 static bool_t
222 xdrrec_putlong (xdrs, lp)
223 XDR *xdrs;
224 const long *lp;
226 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
227 int32_t *dest_lp = (int32_t *) rstrm->out_finger;
229 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
232 * this case should almost never happen so the code is
233 * inefficient
235 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
236 rstrm->frag_sent = TRUE;
237 if (!flush_out (rstrm, FALSE))
238 return FALSE;
239 dest_lp = (int32_t *) rstrm->out_finger;
240 rstrm->out_finger += BYTES_PER_XDR_UNIT;
242 *dest_lp = htonl (*lp);
243 return TRUE;
246 static bool_t /* must manage buffers, fragments, and records */
247 xdrrec_getbytes (xdrs, addr, len)
248 XDR *xdrs;
249 caddr_t addr;
250 u_int len;
252 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
253 u_int current;
255 while (len > 0)
257 current = rstrm->fbtbc;
258 if (current == 0)
260 if (rstrm->last_frag)
261 return FALSE;
262 if (!set_input_fragment (rstrm))
263 return FALSE;
264 continue;
266 current = (len < current) ? len : current;
267 if (!get_input_bytes (rstrm, addr, current))
268 return FALSE;
269 addr += current;
270 rstrm->fbtbc -= current;
271 len -= current;
273 return TRUE;
276 static bool_t
277 xdrrec_putbytes (xdrs, addr, len)
278 XDR *xdrs;
279 const char *addr;
280 u_int len;
282 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
283 u_int current;
285 while (len > 0)
287 current = rstrm->out_boundry - rstrm->out_finger;
288 current = (len < current) ? len : current;
289 bcopy (addr, rstrm->out_finger, current);
290 rstrm->out_finger += current;
291 addr += current;
292 len -= current;
293 if (rstrm->out_finger == rstrm->out_boundry)
295 rstrm->frag_sent = TRUE;
296 if (!flush_out (rstrm, FALSE))
297 return FALSE;
300 return TRUE;
303 static u_int
304 xdrrec_getpos (const XDR *xdrs)
306 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
307 long pos;
309 pos = lseek ((int) rstrm->tcp_handle, (long) 0, 1);
310 if (pos != -1)
311 switch (xdrs->x_op)
314 case XDR_ENCODE:
315 pos += rstrm->out_finger - rstrm->out_base;
316 break;
318 case XDR_DECODE:
319 pos -= rstrm->in_boundry - rstrm->in_finger;
320 break;
322 default:
323 pos = (u_int) - 1;
324 break;
326 return (u_int) pos;
329 static bool_t
330 xdrrec_setpos (xdrs, pos)
331 XDR *xdrs;
332 u_int pos;
334 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
335 u_int currpos = xdrrec_getpos (xdrs);
336 int delta = currpos - pos;
337 caddr_t newpos;
339 if ((int) currpos != -1)
340 switch (xdrs->x_op)
343 case XDR_ENCODE:
344 newpos = rstrm->out_finger - delta;
345 if (newpos > (caddr_t) rstrm->frag_header &&
346 newpos < rstrm->out_boundry)
348 rstrm->out_finger = newpos;
349 return TRUE;
351 break;
353 case XDR_DECODE:
354 newpos = rstrm->in_finger - delta;
355 if ((delta < (int) (rstrm->fbtbc)) &&
356 (newpos <= rstrm->in_boundry) &&
357 (newpos >= rstrm->in_base))
359 rstrm->in_finger = newpos;
360 rstrm->fbtbc -= delta;
361 return TRUE;
363 break;
365 default:
366 break;
368 return FALSE;
371 static long *
372 xdrrec_inline (XDR *xdrs, int len)
374 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
375 long *buf = NULL;
377 switch (xdrs->x_op)
380 case XDR_ENCODE:
381 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
383 buf = (long *) rstrm->out_finger;
384 rstrm->out_finger += len;
386 break;
388 case XDR_DECODE:
389 if ((len <= rstrm->fbtbc) &&
390 ((rstrm->in_finger + len) <= rstrm->in_boundry))
392 buf = (long *) rstrm->in_finger;
393 rstrm->fbtbc -= len;
394 rstrm->in_finger += len;
396 break;
398 default:
399 break;
401 return buf;
404 static void
405 xdrrec_destroy (xdrs)
406 const XDR *xdrs;
408 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
410 mem_free (rstrm->the_buffer,
411 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
412 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
417 * Exported routines to manage xdr records
421 * Before reading (deserializing from the stream, one should always call
422 * this procedure to guarantee proper record alignment.
424 bool_t
425 xdrrec_skiprecord (xdrs)
426 XDR *xdrs;
428 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
430 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
432 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
433 return FALSE;
434 rstrm->fbtbc = 0;
435 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
436 return FALSE;
438 rstrm->last_frag = FALSE;
439 return TRUE;
443 * Lookahead function.
444 * Returns TRUE iff there is no more input in the buffer
445 * after consuming the rest of the current record.
447 bool_t
448 xdrrec_eof (xdrs)
449 XDR *xdrs;
451 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
453 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
455 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
456 return TRUE;
457 rstrm->fbtbc = 0;
458 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
459 return TRUE;
461 if (rstrm->in_finger == rstrm->in_boundry)
462 return TRUE;
463 return FALSE;
467 * The client must tell the package when an end-of-record has occurred.
468 * The second parameter tells whether the record should be flushed to the
469 * (output) tcp stream. (This lets the package support batched or
470 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
472 bool_t
473 xdrrec_endofrecord (xdrs, sendnow)
474 XDR *xdrs;
475 bool_t sendnow;
477 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
478 u_long len; /* fragment length */
480 if (sendnow || rstrm->frag_sent
481 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
483 rstrm->frag_sent = FALSE;
484 return flush_out (rstrm, TRUE);
486 len = (rstrm->out_finger - (char *) rstrm->frag_header
487 - BYTES_PER_XDR_UNIT);
488 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
489 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
490 rstrm->out_finger += BYTES_PER_XDR_UNIT;
491 return TRUE;
496 * Internal useful routines
498 static bool_t
499 flush_out (RECSTREAM *rstrm, bool_t eor)
501 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
502 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
503 - BYTES_PER_XDR_UNIT);
505 *rstrm->frag_header = htonl (len | eormask);
506 len = rstrm->out_finger - rstrm->out_base;
507 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
508 != (int) len)
509 return FALSE;
510 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
511 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
512 return TRUE;
515 static bool_t /* knows nothing about records! Only about input buffers */
516 fill_input_buf (RECSTREAM *rstrm)
518 caddr_t where;
519 u_int i;
520 int len;
522 where = rstrm->in_base;
523 i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
524 where += i;
525 len = rstrm->in_size - i;
526 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
527 return FALSE;
528 rstrm->in_finger = where;
529 where += len;
530 rstrm->in_boundry = where;
531 return TRUE;
534 static bool_t /* knows nothing about records! Only about input buffers */
535 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
537 int current;
539 while (len > 0)
541 current = rstrm->in_boundry - rstrm->in_finger;
542 if (current == 0)
544 if (!fill_input_buf (rstrm))
545 return FALSE;
546 continue;
548 current = (len < current) ? len : current;
549 bcopy (rstrm->in_finger, addr, current);
550 rstrm->in_finger += current;
551 addr += current;
552 len -= current;
554 return TRUE;
557 static bool_t /* next two bytes of the input stream are treated as a header */
558 set_input_fragment (RECSTREAM *rstrm)
560 u_long header;
562 if (!get_input_bytes (rstrm, (caddr_t) & header, BYTES_PER_XDR_UNIT))
563 return FALSE;
564 header = ntohl (header);
565 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
566 rstrm->fbtbc = header & ~LAST_FRAG;
567 return TRUE;
570 static bool_t /* consumes input bytes; knows nothing about records! */
571 skip_input_bytes (RECSTREAM *rstrm, long cnt)
573 int current;
575 while (cnt > 0)
577 current = rstrm->in_boundry - rstrm->in_finger;
578 if (current == 0)
580 if (!fill_input_buf (rstrm))
581 return FALSE;
582 continue;
584 current = (cnt < current) ? cnt : current;
585 rstrm->in_finger += current;
586 cnt -= current;
588 return TRUE;
591 static u_int
592 fix_buf_size (u_int s)
594 if (s < 100)
595 s = 4000;
596 return RNDUP (s);