(insert_value): Correct order of arguments for lr_error call.
[glibc.git] / sunrpc / xdr_rec.c
blob101f8c85cee46faa5fb24b2b69f3a9e6a7204cda
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/rpc.h>
51 #include <libintl.h>
53 #ifdef USE_IN_LIBIO
54 # include <libio/iolibio.h>
55 # define fputs(s, f) _IO_fputs (s, f)
56 #endif
58 static bool_t xdrrec_getlong (XDR *, long *);
59 static bool_t xdrrec_putlong (XDR *, const long *);
60 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
61 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
62 static u_int xdrrec_getpos (const XDR *);
63 static bool_t xdrrec_setpos (XDR *, u_int);
64 static int32_t *xdrrec_inline (XDR *, int);
65 static void xdrrec_destroy (XDR *);
66 static bool_t xdrrec_getint32 (XDR *, int32_t *);
67 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
69 static const struct xdr_ops xdrrec_ops =
71 xdrrec_getlong,
72 xdrrec_putlong,
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;
150 if (rstrm == NULL)
152 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
154 * This is bad. Should rework xdrrec_create to
155 * return a handle, and in this case return NULL
157 return;
160 * adjust sizes and allocate buffer quad byte aligned
162 rstrm->sendsize = sendsize = fix_buf_size (sendsize);
163 rstrm->recvsize = recvsize = fix_buf_size (recvsize);
164 rstrm->the_buffer = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
165 if (rstrm->the_buffer == NULL)
167 (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
168 return;
170 tmp = rstrm->the_buffer;
171 if ((size_t)tmp % BYTES_PER_XDR_UNIT)
172 tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
173 rstrm->out_base = tmp;
174 rstrm->in_base = tmp + sendsize;
176 * now the rest ...
178 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
179 is not `const'. */
180 xdrs->x_ops = (struct xdr_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;
199 * The routines defined below are the xdr ops which will go into the
200 * xdr handle filled in by xdrrec_create.
203 static bool_t
204 xdrrec_getlong (XDR *xdrs, long *lp)
206 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
207 int32_t *buflp = (int32_t *) rstrm->in_finger;
208 int32_t mylong;
210 /* first try the inline, fast case */
211 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
212 rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
214 *lp = (int32_t) ntohl (*buflp);
215 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
216 rstrm->in_finger += BYTES_PER_XDR_UNIT;
218 else
220 if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
221 BYTES_PER_XDR_UNIT))
222 return FALSE;
223 *lp = (int32_t) ntohl (mylong);
225 return TRUE;
228 static bool_t
229 xdrrec_putlong (XDR *xdrs, const long *lp)
231 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
232 int32_t *dest_lp = (int32_t *) rstrm->out_finger;
234 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
237 * this case should almost never happen so the code is
238 * inefficient
240 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
241 rstrm->frag_sent = TRUE;
242 if (!flush_out (rstrm, FALSE))
243 return FALSE;
244 dest_lp = (int32_t *) rstrm->out_finger;
245 rstrm->out_finger += BYTES_PER_XDR_UNIT;
247 *dest_lp = htonl (*lp);
248 return TRUE;
251 static bool_t /* must manage buffers, fragments, and records */
252 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
254 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
255 u_int current;
257 while (len > 0)
259 current = rstrm->fbtbc;
260 if (current == 0)
262 if (rstrm->last_frag)
263 return FALSE;
264 if (!set_input_fragment (rstrm))
265 return FALSE;
266 continue;
268 current = (len < current) ? len : current;
269 if (!get_input_bytes (rstrm, addr, current))
270 return FALSE;
271 addr += current;
272 rstrm->fbtbc -= current;
273 len -= current;
275 return TRUE;
278 static bool_t
279 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
281 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
282 u_int current;
284 while (len > 0)
286 current = rstrm->out_boundry - rstrm->out_finger;
287 current = (len < current) ? len : current;
288 bcopy (addr, rstrm->out_finger, current);
289 rstrm->out_finger += current;
290 addr += current;
291 len -= current;
292 if (rstrm->out_finger == rstrm->out_boundry)
294 rstrm->frag_sent = TRUE;
295 if (!flush_out (rstrm, FALSE))
296 return FALSE;
299 return TRUE;
302 static u_int
303 xdrrec_getpos (const XDR *xdrs)
305 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
306 long pos;
308 pos = __lseek ((int) rstrm->tcp_handle, (long) 0, 1);
309 if (pos != -1)
310 switch (xdrs->x_op)
313 case XDR_ENCODE:
314 pos += rstrm->out_finger - rstrm->out_base;
315 break;
317 case XDR_DECODE:
318 pos -= rstrm->in_boundry - rstrm->in_finger;
319 break;
321 default:
322 pos = (u_int) - 1;
323 break;
325 return (u_int) pos;
328 static bool_t
329 xdrrec_setpos (XDR *xdrs, u_int pos)
331 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
332 u_int currpos = xdrrec_getpos (xdrs);
333 int delta = currpos - pos;
334 caddr_t newpos;
336 if ((int) currpos != -1)
337 switch (xdrs->x_op)
340 case XDR_ENCODE:
341 newpos = rstrm->out_finger - delta;
342 if (newpos > (caddr_t) rstrm->frag_header &&
343 newpos < rstrm->out_boundry)
345 rstrm->out_finger = newpos;
346 return TRUE;
348 break;
350 case XDR_DECODE:
351 newpos = rstrm->in_finger - delta;
352 if ((delta < (int) (rstrm->fbtbc)) &&
353 (newpos <= rstrm->in_boundry) &&
354 (newpos >= rstrm->in_base))
356 rstrm->in_finger = newpos;
357 rstrm->fbtbc -= delta;
358 return TRUE;
360 break;
362 default:
363 break;
365 return FALSE;
368 static int32_t *
369 xdrrec_inline (XDR *xdrs, int len)
371 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
372 int32_t *buf = NULL;
374 switch (xdrs->x_op)
377 case XDR_ENCODE:
378 if ((rstrm->out_finger + len) <= rstrm->out_boundry)
380 buf = (int32_t *) rstrm->out_finger;
381 rstrm->out_finger += len;
383 break;
385 case XDR_DECODE:
386 if ((len <= rstrm->fbtbc) &&
387 ((rstrm->in_finger + len) <= rstrm->in_boundry))
389 buf = (int32_t *) rstrm->in_finger;
390 rstrm->fbtbc -= len;
391 rstrm->in_finger += len;
393 break;
395 default:
396 break;
398 return buf;
401 static void
402 xdrrec_destroy (XDR *xdrs)
404 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
406 mem_free (rstrm->the_buffer,
407 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
408 mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
411 static bool_t
412 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
414 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
415 int32_t *bufip = (int32_t *) rstrm->in_finger;
416 int32_t mylong;
418 /* first try the inline, fast case */
419 if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
420 rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
422 *ip = ntohl (*bufip);
423 rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
424 rstrm->in_finger += BYTES_PER_XDR_UNIT;
426 else
428 if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
429 BYTES_PER_XDR_UNIT))
430 return FALSE;
431 *ip = ntohl (mylong);
433 return TRUE;
436 static bool_t
437 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
439 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
440 int32_t *dest_ip = (int32_t *) rstrm->out_finger;
442 if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
445 * this case should almost never happen so the code is
446 * inefficient
448 rstrm->out_finger -= BYTES_PER_XDR_UNIT;
449 rstrm->frag_sent = TRUE;
450 if (!flush_out (rstrm, FALSE))
451 return FALSE;
452 dest_ip = (int32_t *) rstrm->out_finger;
453 rstrm->out_finger += BYTES_PER_XDR_UNIT;
455 *dest_ip = htonl (*ip);
456 return TRUE;
460 * Exported routines to manage xdr records
464 * Before reading (deserializing from the stream, one should always call
465 * this procedure to guarantee proper record alignment.
467 bool_t
468 xdrrec_skiprecord (xdrs)
469 XDR *xdrs;
471 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
473 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
475 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
476 return FALSE;
477 rstrm->fbtbc = 0;
478 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
479 return FALSE;
481 rstrm->last_frag = FALSE;
482 return TRUE;
486 * Lookahead function.
487 * Returns TRUE iff there is no more input in the buffer
488 * after consuming the rest of the current record.
490 bool_t
491 xdrrec_eof (xdrs)
492 XDR *xdrs;
494 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
496 while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
498 if (!skip_input_bytes (rstrm, rstrm->fbtbc))
499 return TRUE;
500 rstrm->fbtbc = 0;
501 if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
502 return TRUE;
504 if (rstrm->in_finger == rstrm->in_boundry)
505 return TRUE;
506 return FALSE;
510 * The client must tell the package when an end-of-record has occurred.
511 * The second parameter tells whether the record should be flushed to the
512 * (output) tcp stream. (This lets the package support batched or
513 * pipelined procedure calls.) TRUE => immediate flush to tcp connection.
515 bool_t
516 xdrrec_endofrecord (xdrs, sendnow)
517 XDR *xdrs;
518 bool_t sendnow;
520 RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
521 u_long len; /* fragment length */
523 if (sendnow || rstrm->frag_sent
524 || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
526 rstrm->frag_sent = FALSE;
527 return flush_out (rstrm, TRUE);
529 len = (rstrm->out_finger - (char *) rstrm->frag_header
530 - BYTES_PER_XDR_UNIT);
531 *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
532 rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
533 rstrm->out_finger += BYTES_PER_XDR_UNIT;
534 return TRUE;
539 * Internal useful routines
541 static bool_t
542 internal_function
543 flush_out (RECSTREAM *rstrm, bool_t eor)
545 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
546 u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
547 - BYTES_PER_XDR_UNIT);
549 *rstrm->frag_header = htonl (len | eormask);
550 len = rstrm->out_finger - rstrm->out_base;
551 if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
552 != (int) len)
553 return FALSE;
554 rstrm->frag_header = (u_int32_t *) rstrm->out_base;
555 rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
556 return TRUE;
559 static bool_t /* knows nothing about records! Only about input buffers */
560 fill_input_buf (RECSTREAM *rstrm)
562 caddr_t where;
563 size_t i;
564 int len;
566 where = rstrm->in_base;
567 i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
568 where += i;
569 len = rstrm->in_size - i;
570 if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
571 return FALSE;
572 rstrm->in_finger = where;
573 where += len;
574 rstrm->in_boundry = where;
575 return TRUE;
578 static bool_t /* knows nothing about records! Only about input buffers */
579 internal_function
580 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
582 int current;
584 while (len > 0)
586 current = rstrm->in_boundry - rstrm->in_finger;
587 if (current == 0)
589 if (!fill_input_buf (rstrm))
590 return FALSE;
591 continue;
593 current = (len < current) ? len : current;
594 bcopy (rstrm->in_finger, addr, current);
595 rstrm->in_finger += current;
596 addr += current;
597 len -= current;
599 return TRUE;
602 static bool_t /* next two bytes of the input stream are treated as a header */
603 internal_function
604 set_input_fragment (RECSTREAM *rstrm)
606 u_long header;
608 if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
609 return FALSE;
610 header = ntohl (header);
611 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
613 * Sanity check. Try not to accept wildly incorrect
614 * record sizes. Unfortunately, the only record size
615 * we can positively identify as being 'wildly incorrect'
616 * is zero. Ridiculously large record sizes may look wrong,
617 * but we don't have any way to be certain that they aren't
618 * what the client actually intended to send us.
620 if ((header & (~LAST_FRAG)) == 0)
621 return FALSE;
622 rstrm->fbtbc = header & ~LAST_FRAG;
623 return TRUE;
626 static bool_t /* consumes input bytes; knows nothing about records! */
627 internal_function
628 skip_input_bytes (RECSTREAM *rstrm, long cnt)
630 int current;
632 while (cnt > 0)
634 current = rstrm->in_boundry - rstrm->in_finger;
635 if (current == 0)
637 if (!fill_input_buf (rstrm))
638 return FALSE;
639 continue;
641 current = (cnt < current) ? cnt : current;
642 rstrm->in_finger += current;
643 cnt -= current;
645 return TRUE;
648 static u_int
649 internal_function
650 fix_buf_size (u_int s)
652 if (s < 100)
653 s = 4000;
654 return RNDUP (s);